mongodb单机升级为副本集

服务器信息

ip 系统 配置 目录
172.24.32.201 centos7.7 2c4g /var/lib/mongo (the data directory) /var/log/mongodb (the log directory)
172.24.32.201 centos7.7 2c4g /var/lib/mongo27018 (the data directory) /var/log/mongodb27018 (the log directory)
升级成单节点副本集 1、修改原服务(27017)的启动文件
cat >>/etc/mongod27017.conf<

修改配置文件,重启服务
cat >>/usr/lib/systemd/system/mongod27017.service<

systemctl daemon-reload systemctl restart mongod27017

验证
[root@test02 lib]# ps axf|grep mongod|grep -v 'grep' 7758 ?Sl1:50 /usr/bin/mongod -f /etc/mongod.conf

可以看到,修改成功
启动第一个副本集,进入mongo27017
mongo 172.24.32.201:27017

副本集初始化
rs.initiate()

结果
> rs.initiate() { "info2" : "no configuration specified. Using a default configuration for the set", "me" : "172.24.32.201:27017", "ok" : 1 }

验证副本集信息:
rs.conf()

结果
lugotestrepl:PRIMARY> rs.conf() { "_id" : "lugotestrepl", "version" : 1, "protocolVersion" : NumberLong(1), "members" : [ { "_id" : 0, "host" : "172.24.32.201:27017", "arbiterOnly" : false, "buildIndexes" : true, "hidden" : false, "priority" : 1, "tags" : {}, "slaveDelay" : NumberLong(0), "votes" : 1 } ], "settings" : { "chainingAllowed" : true, "heartbeatIntervalMillis" : 2000, "heartbeatTimeoutSecs" : 10, "electionTimeoutMillis" : 10000, "catchUpTimeoutMillis" : 60000, "getLastErrorModes" : {}, "getLastErrorDefaults" : { "w" : 1, "wtimeout" : 0 }, "replicaSetId" : ObjectId("5ebcb9e6ae7887a3e356b850") } }

2、增加安全认证机制KeyFile
集群之间的复制增加keyFile认证
#生成keymkdir -p /etc/mongokey openssl rand -base64 745 > /etc/mongokey/mongodb-keyfile # 该key的权限必须是600 chown -R mongod:mongod /etc/mongokey/ chmod 600 /etc/mongokey/mongodb-keyfile

3、加入一个副本集
在172.24.32.201服务器上再启动一个27018实例,并加入副本集
创建服务所需目录
mkdir -p /var/log/mongodb27018/ mkdir -p /var/lib/mongo27018 chown mongod:mongod /var/log/mongodb27018/ &&chmod 0755 /var/log/mongodb27018/ chown mongod:mongod /var/lib/mongo27018/ &&chmod 0755 /var/lib/mongo27018/

创建新的配置文件
cat >>/etc/mongod27018.conf<

创建新的启动文件
cat >>/usr/lib/systemd/system/mongod27018.service<

启动新的mongo实例
systemctl daemon-reload systemctl start mongod27018

进入27017副本集
mongo 172.24.32.201:27017/admin

将刚创建的实例添加进副本集
rs.add("172.24.32.201:27018");

结果
lugotestrepl:PRIMARY> rs.add("172.24.32.201:27018"); { "ok" : 1 }

查看副本集状态
rs.status();

结果
lugotestrepl:PRIMARY> rs.status(); { "set" : "lugotestrepl", "date" : ISODate("2020-05-14T06:33:21.372Z"), "myState" : 1, "term" : NumberLong(1), "syncingTo" : "", "syncSourceHost" : "", "syncSourceId" : -1, "heartbeatIntervalMillis" : NumberLong(2000), "optimes" : { "lastCommittedOpTime" : { "ts" : Timestamp(1589437995, 1), "t" : NumberLong(1) }, "appliedOpTime" : { "ts" : Timestamp(1589437995, 1), "t" : NumberLong(1) }, "durableOpTime" : { "ts" : Timestamp(1589437995, 1), "t" : NumberLong(1) } }, "members" : [ { "_id" : 0, "name" : "172.24.32.201:27017", "health" : 1, "state" : 1, "stateStr" : "PRIMARY", "uptime" : 11430, "optime" : { "ts" : Timestamp(1589437995, 1), "t" : NumberLong(1) }, "optimeDate" : ISODate("2020-05-14T06:33:15Z"), "syncingTo" : "", "syncSourceHost" : "", "syncSourceId" : -1, "infoMessage" : "", "electionTime" : Timestamp(1589426662, 2), "electionDate" : ISODate("2020-05-14T03:24:22Z"), "configVersion" : 2, "self" : true, "lastHeartbeatMessage" : "" }, { "_id" : 1, "name" : "172.24.32.201:27018", "health" : 1, "state" : 2, "stateStr" : "SECONDARY", "uptime" : 9728, "optime" : { "ts" : Timestamp(1589437995, 1), "t" : NumberLong(1) }, "optimeDurable" : { "ts" : Timestamp(1589437995, 1), "t" : NumberLong(1) }, "optimeDate" : ISODate("2020-05-14T06:33:15Z"), "optimeDurableDate" : ISODate("2020-05-14T06:33:15Z"), "lastHeartbeat" : ISODate("2020-05-14T06:33:19.465Z"), "lastHeartbeatRecv" : ISODate("2020-05-14T06:33:19.815Z"), "pingMs" : NumberLong(0), "lastHeartbeatMessage" : "", "syncingTo" : "172.24.32.201:27017", "syncSourceHost" : "172.24.32.201:27017", "syncSourceId" : 0, "infoMessage" : "", "configVersion" : 2 } ], "ok" : 1 }

验证原有数据的可用性
我们登陆进刚加入副本集的实例,27018
mongo 172.24.32.201:27018

查看db,报错
lugotestrepl:SECONDARY> show dbs; 2020-05-14T14:37:24.227+0800 E QUERY[thread1] Error: listDatabases failed:{ "ok" : 0, "errmsg" : "not master and slaveOk=false", "code" : 13435, "codeName" : "NotMasterNoSlaveOk" } : _getErrorWithCode@src/mongo/shell/utils.js:25:13 Mongo.prototype.getDBs@src/mongo/shell/mongo.js:62:1 shellHelper.show@src/mongo/shell/utils.js:814:19 shellHelper@src/mongo/shell/utils.js:704:15 @(shellhelp2):1:1

是因为secondary副本不允许读写
临时允许命令
rs.slaveOk();

发现数据一致
lugotestrepl:SECONDARY> rs.slaveOk(); lugotestrepl:SECONDARY> show dbs; admin0.000GB local0.000GB lugotest0.000GB lugotestrepl:SECONDARY> rs.slaveOk(); lugotestrepl:SECONDARY> db.movie.find(); lugotestrepl:SECONDARY> use lugotest; switched to db lugotest lugotestrepl:SECONDARY> rs.slaveOk(); lugotestrepl:SECONDARY> db.movie.find(); { "_id" : ObjectId("5ebbbca5692adbface98b2af"), "moviename" : "大侦探福尔摩斯", "points" : "9.5" } { "_id" : ObjectId("5ebbbca5692adbface98b2b0"), "moviename" : "掠夺", "points" : "9.2" } { "_id" : ObjectId("5ebbbca5692adbface98b2b1"), "moviename" : "摇滚黑帮", "points" : "9.9" } { "_id" : ObjectId("5ebbbca5692adbface98b2b2"), "moviename" : "两杆大烟枪", "points" : "9.1" }

创建用户 开启keyfile认证就默认开启了auth认证了,为了保证后面可以登录,提前创建了用户
登陆primary节点27017,创建管理员账号
mongo 172.24.32.201:27017

db.createUser( { user:"admin", pwd:"test123", roles:[{role:"userAdminAnyDatabase",db:"admin"}] } ); db.createUser( { user: "root", pwd: "test123", roles: [ { role: "root", db: "admin" } ] });

验证用户
lugotestrepl:PRIMARY> db.auth('root','test123'); 1 lugotestrepl:PRIMARY> db.auth('admin','test123') 1

【mongodb单机升级为副本集】重启后进入,顺序,先关从,再关主。先启动主,再启动从
systemctl stop mongod27018 systemctl stop mongod systemctl start mongod systemctl start mongod27018

进入主,验证账号密码
[root@test02 lib]# mongo 172.24.32.201:27017 MongoDB shell version v3.4.24 connecting to: mongodb://172.24.32.201:27017/test MongoDB server version: 3.4.24 lugotestrepl:PRIMARY> db.auth('admin','test123'); 1

创建读写用户
lugotestrepl:PRIMARY> db.createUser({ ...user: "lugo", ...pwd: "test123", ...roles: [ { role: "readWrite", db: "lugotest" } ] ...} ... ); Successfully added user: { "user" : "lugo", "roles" : [ { "role" : "readWrite", "db" : "lugotest" } ] }

使用新创建的用户读取数据
lugotestrepl:PRIMARY> db.auth('lugo','test123'); 1 lugotestrepl:PRIMARY> db.movie.find(); { "_id" : ObjectId("5ebbbca5692adbface98b2af"), "moviename" : "大侦探福尔摩斯", "points" : "9.5" } { "_id" : ObjectId("5ebbbca5692adbface98b2b0"), "moviename" : "掠夺", "points" : "9.2" } { "_id" : ObjectId("5ebbbca5692adbface98b2b1"), "moviename" : "摇滚黑帮", "points" : "9.9" } { "_id" : ObjectId("5ebbbca5692adbface98b2b2"), "moviename" : "两杆大烟枪", "points" : "9.1" }

至此,mongo单点已经成功升级成单节点副本集

    推荐阅读