单实例mongodb转换成主从复制(副本集)

环境:
OS:CentOS 7
DB:4.2.9
机器角色:
192.168.1.118:28001 主
192.168.1.85:28002 从
192.168.1.85:28003 仲裁节点

说明:4.0之后的版本只支持副本集的主从复制,不再支持单独的主从复制

 

-----------------------主库---------------------------------
1.产生秘钥验证
[root@test key]# cd /home/middle/mongodb/key
[root@test key]# openssl rand -base64 741 >>keyfile
[root@test key]# chmod 700 keyfile

这个文件需要拷贝到另外2个节点,等另外2个节点都安装好了再拷贝

 

2.修改主节点的配置文件
单节点的mongodb目前已经正在运行,配置如下:

[root@localhost conf]# cd /home/middle/mongodb/conf
[root@localhost conf]# more mongo.cnf
port=28001
fork=true
dbpath=/home/middle/mongodb/data
logpath=/home/middle/mongodb/log/mongodb.log
pidfilepath=/home/middle/mongodb/run/28001.pid
logappend=true
bind_ip=192.168.1.118,127.0.0.1
oplogSize=10000
logRotate=reopen
auth=true

 

修改后的配置如下:

[root@localhost conf]# more mongo.cnf 
port=28001
fork=true
dbpath=/home/middle/mongodb/data
logpath=/home/middle/mongodb/log/mongodb.log
pidfilepath=/home/middle/mongodb/run/28001.pid
logappend=true
bind_ip=192.168.1.118,127.0.0.1
oplogSize=10000
logRotate=reopen
##auth=true
##keyFile=/home/middle/mongodb/key/keyfile
shardsvr=true
replSet=replzhibo

 

##keyFile=/home/middle/mongodb/key/keyfile ##先注释掉,创建好副本集后再启动
##auth=true ##先注释掉,创建好副本集后再启动

 

-------------------------另外2个从库-----------------------

1.创建安装目录
192.168.1.85:28002 对应的安装目录
[root@test services]# mkdir -p /usr/local/services
[root@test services]# mkdir -p /home/middle/mongodb/data
[root@test services]# mkdir -p /home/middle/mongodb/log
[root@test services]# mkdir -p /home/middle/mongodb/key
[root@test services]# mkdir -p /home/middle/mongodb/conf
[root@test services]# mkdir -p /home/middle/mongodb/run

192.168.1.85:28003 仲裁节点对应的安装目录
[root@test services]# mkdir -p /usr/local/services
[root@test services]# mkdir -p /home/middle/mongodb_arbiter/data
[root@test services]# mkdir -p /home/middle/mongodb_arbiter/log
[root@test services]# mkdir -p /home/middle/mongodb_arbiter/key
[root@test services]# mkdir -p /home/middle/mongodb_arbiter/conf
[root@test services]# mkdir -p /home/middle/mongodb_arbiter/run

2.安装数据库
192.168.1.85:28002 安装
[root@test soft]# tar -xvf mongodb-linux-x86_64-rhel70-4.2.9.tgz
[root@test soft]# mv mongodb-linux-x86_64-rhel70-4.2.9 /usr/local/services/mongodb


192.168.1.85:28003 安装
[root@test soft]# tar -xvf mongodb-linux-x86_64-rhel70-4.2.9.tgz
[root@test soft]# mv mongodb-linux-x86_64-rhel70-4.2.9 /usr/local/services/mongodb_arbiter

 

3.将主库产生秘钥拷贝到备库和仲裁节点
在192.168.1.118主节点上操作
[root@localhost key]# cd /home/middle/mongodb/key
[root@localhost key]#scp keyfile root@192.168.1.85:/home/middle/mongodb/key/
[root@localhost key]#scp keyfile root@192.168.1.85:/home/middle/mongodb_arbiter/key/

 

4.生成日志文件
192.168.1.85:28002
[root@test key]#echo>/home/middle/mongodb/log/mongodb.log

192.168.1.85:28003
[root@test key]#echo>/home/middle/mongodb_arbiter/log/mongodb.log

 

5.创建配置文件mongo.cnf
##从节点的配置参数

port=28002
fork=true
dbpath=/home/middle/mongodb/data
logpath=/home/middle/mongodb/log/mongodb.log
pidfilepath=/home/middle/mongodb/run/29002.pid
logappend=true
shardsvr=true
replSet=replzhibo
bind_ip=192.168.1.85,127.0.0.1
oplogSize=10000
logRotate=reopen
##keyFile=/home/middle/mongodb/key/keyfile
##auth=true

 

##仲裁节点的配置参数

port=28003
fork=true
dbpath=/home/middle/mongodb_arbiter/data
logpath=/home/middle/mongodb/log/mongodb.log
pidfilepath=/home/middle/mongodb_arbiter/run/29003.pid
logappend=true
shardsvr=true
replSet=replzhibo
bind_ip=192.168.1.85,127.0.0.1
oplogSize=10000
logRotate=reopen
##keyFile=/home/middle/mongodb_arbiter/key/keyfile
##auth=true
这里keyFile和auth先注释,因为等部署完初始化完集群后再启用

 

5.启动
主节点重启,可以先启动两外两个从节点,看是否可以正常启动,主节点已经在运行了的,可以后面再重启
192.168.1.118:28001
先停掉:
停掉mongodb
/usr/local/services/mongodb/bin/mongo localhost:28001
use admin
db.auth("root","123456");
db.shutdownServer()

然后启动
[root@test key]# /usr/local/services/mongodb/bin/mongod -f /home/middle/mongodb/conf/mongo.cnf

 

从节点启动:

192.168.1.85:28002
[root@test key]# /usr/local/services/mongodb/bin/mongod -f /home/middle/mongodb/conf/mongo.cnf

仲裁节点启动:
192.168.1.85:28003
[root@test key]# /usr/local/services/mongodb_arbiter/bin/mongod -f /home/middle/mongodb_arbiter/conf/mongo.cnf

 

6.初始化副本集
[root@localhost bin]# /usr/local/services/mongodb/bin/mongo 192.168.1.118:28001
use admin
config={_id:'replzhibo',members:[{_id:0,host:'192.168.1.118:28001'},{_id:1,host:'192.168.1.85:28002'},{_id:2,host:'192.168.1.85:28003', arbiterOnly:true}]}
rs.initiate(config)

 

到这里要是不需要带认证的副本集初始化,就配置完成了,下面的部署我们继续配置带认证的

这个时候我们登陆从库查看是否同步过来(之前主库已经在跑的了,已经存在数据)
主节点:

主节点:
[root@localhost conf]# /usr/local/services/mongodb/bin/mongo 192.168.1.118:28001
MongoDB shell version v4.2.9
connecting to: mongodb://192.168.1.118:28001/test?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("7f93ba2e-17b3-4eb5-91d5-92e5a67bc13a") }
MongoDB server version: 4.2.9
Server has startup warnings: 
2021-09-07T11:32:01.184+0800 I  CONTROL  [initandlisten] 
2021-09-07T11:32:01.184+0800 I  CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2021-09-07T11:32:01.184+0800 I  CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2021-09-07T11:32:01.184+0800 I  CONTROL  [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
2021-09-07T11:32:01.184+0800 I  CONTROL  [initandlisten] 
2021-09-07T11:32:01.184+0800 I  CONTROL  [initandlisten] 
2021-09-07T11:32:01.184+0800 I  CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2021-09-07T11:32:01.184+0800 I  CONTROL  [initandlisten] **        We suggest setting it to 'never'
2021-09-07T11:32:01.185+0800 I  CONTROL  [initandlisten] 
2021-09-07T11:32:01.185+0800 I  CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2021-09-07T11:32:01.185+0800 I  CONTROL  [initandlisten] **        We suggest setting it to 'never'
2021-09-07T11:32:01.185+0800 I  CONTROL  [initandlisten] 
---
Enable MongoDB's free cloud-based monitoring service, which will then receive and display
metrics about your deployment (disk utilization, CPU, operation statistics, etc).

The monitoring data will be available on a MongoDB website with a unique URL accessible to you
and anyone you share the URL with. MongoDB may use this information to make product
improvements and to suggest MongoDB products and deployment options to you.

To enable free monitoring, run the following command: db.enableFreeMonitoring()
To permanently disable this reminder, run the following command: db.disableFreeMonitoring()
---

replzhibo:PRIMARY> show dbs
admin       0.000GB
config      0.000GB
local       0.000GB
mgdb_zhibo  0.000GB
replzhibo:PRIMARY> use mgdb_zhibo
switched to db mgdb_zhibo
replzhibo:PRIMARY> show tables
tb_test01
replzhibo:PRIMARY> db.tb_test01.find()
{ "_id" : ObjectId("6136baaaa1a3083a14385d2f"), "name" : "yiibai tutorials" }

从节点:
/usr/local/services/mongodb/bin/mongo 192.168.1.85:28002
[root@localhost conf]# /usr/local/services/mongodb/bin/mongo 192.168.1.85:28002
MongoDB shell version v4.2.9
connecting to: mongodb://192.168.1.85:28002/test?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("c239105b-ac85-431c-a5c2-78c00e02a177") }
MongoDB server version: 4.2.9
Server has startup warnings: 
2021-09-08T11:31:13.449+0800 I  CONTROL  [initandlisten] 
2021-09-08T11:31:13.449+0800 I  CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2021-09-08T11:31:13.449+0800 I  CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2021-09-08T11:31:13.449+0800 I  CONTROL  [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
2021-09-08T11:31:13.449+0800 I  CONTROL  [initandlisten] 
2021-09-08T11:31:13.449+0800 I  CONTROL  [initandlisten] 
2021-09-08T11:31:13.449+0800 I  CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2021-09-08T11:31:13.449+0800 I  CONTROL  [initandlisten] **        We suggest setting it to 'never'
2021-09-08T11:31:13.449+0800 I  CONTROL  [initandlisten] 
2021-09-08T11:31:13.449+0800 I  CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2021-09-08T11:31:13.449+0800 I  CONTROL  [initandlisten] **        We suggest setting it to 'never'
2021-09-08T11:31:13.449+0800 I  CONTROL  [initandlisten] 
2021-09-08T11:31:13.449+0800 I  CONTROL  [initandlisten] ** WARNING: soft rlimits too low. rlimits set to 4096 processes, 65536 files. Number of processes should be at least 32768 : 0.5 times number of files.
2021-09-08T11:31:13.449+0800 I  CONTROL  [initandlisten] 
---
Enable MongoDB's free cloud-based monitoring service, which will then receive and display
metrics about your deployment (disk utilization, CPU, operation statistics, etc).

The monitoring data will be available on a MongoDB website with a unique URL accessible to you
and anyone you share the URL with. MongoDB may use this information to make product
improvements and to suggest MongoDB products and deployment options to you.

To enable free monitoring, run the following command: db.enableFreeMonitoring()
To permanently disable this reminder, run the following command: db.disableFreeMonitoring()
---

replzhibo:SECONDARY> show dbs
2021-09-08T11:37:12.468+0800 E  QUERY    [js] uncaught exception: Error: listDatabases failed:{
        "operationTime" : Timestamp(1630985751, 1),
        "ok" : 0,
        "errmsg" : "not master and slaveOk=false",
        "code" : 13435,
        "codeName" : "NotMasterNoSlaveOk",
        "$clusterTime" : {
                "clusterTime" : Timestamp(1630985751, 1),
                "signature" : {
                        "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
                        "keyId" : NumberLong(0)
                }
        }
} :
_getErrorWithCode@src/mongo/shell/utils.js:25:13
Mongo.prototype.getDBs/<@src/mongo/shell/mongo.js:135:19
Mongo.prototype.getDBs@src/mongo/shell/mongo.js:87:12
shellHelper.show@src/mongo/shell/utils.js:906:13
shellHelper@src/mongo/shell/utils.js:790:15
@(shellhelp2):1:1

解决办法:
SECONDARY> rs.slaveOk();

继续查看
replzhibo:SECONDARY> show dbs
admin       0.000GB
config      0.000GB
local       0.000GB
mgdb_zhibo  0.000GB
replzhibo:SECONDARY> use mgdb_zhibo
switched to db mgdb_zhibo
replzhibo:SECONDARY> show tables;
tb_test01
replzhibo:SECONDARY> db.tb_test01.find()
{ "_id" : ObjectId("6136baaaa1a3083a14385d2f"), "name" : "yiibai tutorials" }

 

7.关闭集群启用认证参数
采用localhost登录进行关闭数据库,每个节点操作一致,可以先停掉从库和仲裁节点再停主库
注意关闭顺序:
仲裁节点-->从节点-->主节点

192.168.1.85上操作
[root@localhost bin]# /usr/local/services/mongodb/bin/mongo localhost:28003
repltest:ARBITER> use admin
switched to db admin
repltest:ARBITER> db.shutdownServer()

[root@localhost bin]# /usr/local/services/mongodb/bin/mongo localhost:28002
repltest:SECONDARY> use admin
repltest:SECONDARY> db.shutdownServer()


192.168.1.118上操作
[root@pxc03 bin]# /usr/local/services/mongodb/bin/mongo localhost:28001
repltest:SECONDARY> use admin
repltest:SECONDARY> db.shutdownServer()

可以查看各进程是否存在
[root@localhost log]# ps -ef|grep mongo

 

8.修改配置文件启用认证
分别修改3个节点的配置文件,将之前注释的两行,启用
keyFile=/opt/mongodb3015/key/keyfile
auth = true

9.再次启动数据库
主节点启动:
192.168.1.118:28001
[root@test key]# /usr/local/services/mongodb/bin/mongod -f /home/middle/mongodb/conf/mongo.cnf

从节点启动:
192.168.1.85:28002
[root@test key]# /usr/local/services/mongodb/bin/mongod -f /home/middle/mongodb/conf/mongo.cnf

仲裁节点启动:
192.168.1.85:28003
[root@test key]# /usr/local/services/mongodb_arbiter/bin/mongod -f /home/middle/mongodb_arbiter/conf/mongo.cnf

 

10.再次登陆查看主从数据情况,这个时候需要密码验证了
/usr/local/services/mongodb/bin/mongo 192.168.1.118:28001

> use admin
switched to db admin
replzhibo:PRIMARY> db.auth("root","123456");
1
replzhibo:PRIMARY> show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
mgdb_zhibo 0.000GB


[root@localhost conf]# /usr/local/services/mongodb/bin/mongo 192.168.1.85:28002
MongoDB shell version v4.2.9
connecting to: mongodb://192.168.1.85:28002/test?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("003578bf-4b01-45be-9187-fa4febf0819f") }
MongoDB server version: 4.2.9
replzhibo:SECONDARY> use admin
switched to db admin
replzhibo:SECONDARY> db.auth("yeemiao","yeemiao123");
1
replzhibo:SECONDARY> show dbs
2021-09-08T11:44:37.809+0800 E QUERY [js] uncaught exception: 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:135:19
Mongo.prototype.getDBs@src/mongo/shell/mongo.js:87:12
shellHelper.show@src/mongo/shell/utils.js:906:13
shellHelper@src/mongo/shell/utils.js:790:15
@(shellhelp2):1:1
replzhibo:SECONDARY>
replzhibo:SECONDARY> rs.slaveOk();
replzhibo:SECONDARY> show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
mgdb_zhibo 0.000GB


11.验证数据同步
使用开发账号登陆到主库
/usr/local/services/mongodb/bin/mongo 192.168.1.118:28001

>use mgdb_zhibo
>db.auth("dev","dev123")
>db.tb_test01.insert( {"name01":"yiibai tutorials"})

然后登陆从库查看
/usr/local/services/mongodb/bin/mongo 192.168.1.85:28002

replzhibo:SECONDARY> show tables;
tb_test01
replzhibo:SECONDARY> db.tb_test01.find();
{ "_id" : ObjectId("6136baaaa1a3083a14385d2f"), "name" : "yiibai tutorials" }
{ "_id" : ObjectId("6136e0abdbe64e26fd84c9f2"), "name01" : "yiibai tutorials" }
replzhibo:SECONDARY>

 

 

-- The End --

 

posted @ 2021-09-07 14:56  slnngk  阅读(481)  评论(0编辑  收藏  举报