参考以下系列文章:
https://blog.csdn.net/newbie_907486852/article/details/80894413
下载地址:
https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-3.4.7.tgz
创建/opt/module/mongo文件夹:
mkdir /opt/module/mongo
创建/opt/module/mongo/logs文件夹:
mkdir /opt/module/mongo/logs
创建/opt/module/mongo/data文件夹:
mkdir /opt/module/mongo/data
上传安装包到/opt/module/mongo目录下:
解压安装包到当前目录:
tar -zxvf /opt/module/mongo/mongodb-linux-x86_64-3.4.7.tgz -C /opt/module/mongo/
touch /opt/module/mongo/mongodb-linux-x86_64-3.4.7/bin/mongodb.conf
进入bin目录,一下所有操作都在这个目录下进行:
cd /opt/module/mongo/mongodb-linux-x86_64-3.4.7/bin/
为mongodb.conf文件添加内容:
dbpath=/opt/module/mongo/data/
logpath=/opt/module/mongo/logs/mongodb.log
port=27017
fork=true
nohttpinterface=true
保存退出
分别在三台虚拟机上启动MongoDB:
/opt/module/mongo/mongodb-linux-x86_64-3.4.7/bin/mongod --replSet repset -f /opt/module/mongo/mongodb-linux-x86_64-3.4.7/bin/mongodb.conf
-f 指定配置文件
- -port 指定端口,默认是27017
- -dbpath 数据目录路径
- -logpath 日志文件路径
- -logappend 日志append而不是overwrite
- -fork 以创建子进程的方式运行
- -journal 日志提交间隔,默认100ms
- -nojournal 关闭日志功能,2.0版本以上是默认开启的
./mongod --repair --dbpath=/opt/module/mongo/data/
ps -ef|grep mongodb
4.1、进入MongoDB命令模式:
/opt/module/mongo/mongodb-linux-x86_64-3.4.7/bin/mongo
4.2、切换到admin:
use admin
4.3、定义副本集配置变量,这里的_id:”repset”和上面命令参数–replSet repset保持一致:
config = { _id:"repset",members:[{_id:0,host:"192.168.1.105:27017"},{_id:1,host:"192.168.1.106:27017"},{_id:2,host:"192.168.1.107:27017"}]}
4.4、初始化副本集群:
rs.initiate(config);
4.6、查看集群节点的状态:
repset:OTHER> rs.status();
{
"set" : "repset",
"date" : ISODate("2018-07-12T06:16:26.828Z"),
"myState" : 1,
"term" : NumberLong(1),
"heartbeatIntervalMillis" : NumberLong(2000),
"optimes" : {
"lastCommittedOpTime" : {
"ts" : Timestamp(1531376178, 1),
"t" : NumberLong(1)
},
"appliedOpTime" : {
"ts" : Timestamp(1531376178, 1),
"t" : NumberLong(1)
},
"durableOpTime" : {
"ts" : Timestamp(1531376178, 1),
"t" : NumberLong(1)
}
},
"members" : [
{
"_id" : 0,
"name" : "192.168.1.105:27017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 10253,
"optime" : {
"ts" : Timestamp(1531376178, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2018-07-12T06:16:18Z"),
"electionTime" : Timestamp(1531375986, 1),
"electionDate" : ISODate("2018-07-12T06:13:06Z"),
"configVersion" : 1,
"self" : true
},
{
"_id" : 1,
"name" : "192.168.1.106:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 211,
"optime" : {
"ts" : Timestamp(1531376178, 1),
"t" : NumberLong(1)
},
"optimeDurable" : {
"ts" : Timestamp(1531376178, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2018-07-12T06:16:18Z"),
"optimeDurableDate" : ISODate("2018-07-12T06:16:18Z"),
"lastHeartbeat" : ISODate("2018-07-12T06:16:24.948Z"),
"lastHeartbeatRecv" : ISODate("2018-07-12T06:16:26.533Z"),
"pingMs" : NumberLong(0),
"syncingTo" : "192.168.1.107:27017",
"configVersion" : 1
},
{
"_id" : 2,
"name" : "192.168.1.107:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 211,
"optime" : {
"ts" : Timestamp(1531376178, 1),
"t" : NumberLong(1)
},
"optimeDurable" : {
"ts" : Timestamp(1531376178, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2018-07-12T06:16:18Z"),
"optimeDurableDate" : ISODate("2018-07-12T06:16:18Z"),
"lastHeartbeat" : ISODate("2018-07-12T06:16:24.884Z"),
"lastHeartbeatRecv" : ISODate("2018-07-12T06:16:25.662Z"),
"pingMs" : NumberLong(0),
"syncingTo" : "192.168.1.105:27017",
"configVersion" : 1
}
],
"ok" : 1
}
进入MongoDB命令模式:
/opt/module/mongo/mongodb-linux-x86_64-3.4.7/bin/mongo
连接test数据库:
use test;
往testdb表里插入数据
db.testdb.insert({"test1":"testval1"})
在副节点进入MongoDB命令模式:
/opt/module/mongo/mongodb-linux-x86_64-3.4.7/bin/mongo
连接test数据库:
use test;
查询表格:
show tables;
报错信息如下:
2018-07-12T14:45:11.471+0800 E QUERY [thread1] Error: listCollections failed: {
"ok" : 0,
"errmsg" : "not master and slaveOk=false",
"code" : 13435,
"codeName" : "NotMasterNoSlaveOk"
} :
_getErrorWithCode@src/mongo/shell/utils.js:25:13
DB.prototype._getCollectionInfosCommand@src/mongo/shell/db.js:807:1
DB.prototype.getCollectionInfos@src/mongo/shell/db.js:819:19
DB.prototype.getCollectionNames@src/mongo/shell/db.js:830:16
shellHelper.show@src/mongo/shell/utils.js:762:9
shellHelper@src/mongo/shell/utils.js:659:15
@(shellhelp2):1:1
mongodb默认是从主节点读写数据,副本节点上不允许读,设置副本节点可读。
db.getMongo().setSlaveOk();
然后就可以查询复制过来的数据了
db.testdb.find();
结果如下:
{ "_id" : ObjectId("5b46f36cb6a053fd628ec296"), "test1" : "testval1" }
{ "_id" : ObjectId("5b46f8483fc6ccf398f5f475"), "test2" : "testval2" }
关闭主节点(在mongo命令行模式下:./mongo):
use admin
db.shutdownServer()
带基本参数的启动方式:
/opt/module/mongo/mongodb-linux-x86_64-3.4.7/bin/mongod --replSet repset -f /opt/module/mongo/mongodb-linux-x86_64-3.4.7/bin/mongodb.conf
缺省的启动方式(集群方式不建议):
./mongod