我本地的环境
MongoDB 4.0
jdk 1.7.x
安装参考主要博客
https://blog.csdn.net/wanght89/article/details/77677271#commentBox(记得安装完对解压包做个备份)
按照该网址安装过程中,遇到一些问题的解决方法:
1.
多了一个"g",应为
config_set={"_id":"mySet",members:[{_id:,host:"127.0.0.1:27111"},{_id:,host:"127.0.0.1:27112"},{_id:,host:"127.0.0.1:27113"}]}
2.启动MongoDB的时候,要进入相应的bin目录
mongod -port -dbpath D:\MongoDB\matserdb -logpath D:\MongoDB\matserlog\mongod.log -replSet mySet -logappend mongod -port -dbpath D:\MongoDB\slavedb -logpath D:\MongoDB\slavelog\mongod.log -replSet mySet -logappend mongod -port -dbpath D:\MongoDB\arbiterdb -logpath D:\MongoDB\arbiterlog\mongod.log -replSet mySet -logappend
3.设置仲裁节点
rs.addArb("192.168.32.138:27017")
或者
rs.add("host:port",{arbiterOnly:true})
4.默认情况下SECONDARY节点不能读写,要设置slaveOK为true才行
db.getMongo().setSlaveOk()
或
rs.slaveOk()
5.停止MongoDB服务
db.shutdownServer()
7.如果某一个节点不能启动了,删除db下的所有文件以及文件夹,将备份中的data中的所有复制到刚删除的db中,重启即可成功
8.插入测试数据
db.say.insert({"text":"hello world"});
9.创建用户名、密码
mongo --port use admin db.createUser(
{
user: "adminUser",
pwd: "adminPass",
roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
}
)
10.Java代码
package com.alibaba.fescar.dubbo; import java.util.ArrayList;
import java.util.List; import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBObject;
import com.mongodb.MongoClient;
import com.mongodb.ServerAddress; public class ReplSetTest { public static void main(String[] args) {
try {
List<ServerAddress> addresses = new ArrayList<ServerAddress>();
ServerAddress address1 = new ServerAddress("127.0.0.1", );
ServerAddress address2 = new ServerAddress("127.0.0.1", );
ServerAddress address3 = new ServerAddress("127.0.0.1", );
addresses.add(address1);
addresses.add(address2);
addresses.add(address3);
MongoClient client = new MongoClient(addresses);
DB db = client.getDB( "test" );
DBCollection coll = db.getCollection("say");
BasicDBObject object = new BasicDBObject();
object.append( "name" , "user28");
DBObject dbObject = coll.findOne(object); BasicDBObject obj = new BasicDBObject(); obj.put("name", ""); obj.put("age", ); obj.put("sex", ); coll.insert(obj); System.out .println(dbObject);
} catch (Exception e) {
e.printStackTrace();
}
} }
pom.xml
<dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-mongodb</artifactId> <version>1.10..RELEASE</version> </dependency> <dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>3.2.</version>
</dependency>
数据库显示
参考的网址:
http://www.cnblogs.com/ivictor/p/6795418.html
https://blog.csdn.net/a463620704/article/details/83790566
https://blog.csdn.net/wanght89/article/details/77677271#commentBox
https://blog.csdn.net/fuck487/article/details/78287362
https://blog.csdn.net/com_it/article/details/84702046
https://blog.csdn.net/com_it/article/details/84702046
https://blog.csdn.net/yisun123456/article/details/79161279
https://blog.csdn.net/sunbocong/article/details/78642947
https://blog.csdn.net/dickysun1987/article/details/81077421
https://blog.csdn.net/com_it/article/details/84702046
https://blog.csdn.net/q36835109/article/details/78831598/
https://blog.csdn.net/caofeiliju/article/details/80194815