-
测试环境
操作系统:CentOS 7.2 最小化安装
主服务器IP地址:192.168.197.21 master-node
从服务器IP地址:192.168.197.22 slave-node
关闭selinux,关闭防火墙。
Mongodb版本:mongodb-linux-x86_64-3.4.10.tgz
-
上传mongodb包到/software目录下,然后解压缩该包
[root@localhost software]# tar -zxvf mongodb-linux-x86_64-3.4.10.tgz
-
移动解压缩之后的文件至/usr/local/mongodb目录
[root@localhost software]# mv mongodb-linux-x86_64-3.4.10 /usr/local/mongodb
-
在mongodb下面创建data目录和logs目录,用于存放data和logs
[root@localhost local]# mkdir -p /usr/local/mongodb/data
[root@localhost local]# mkdir -p /usr/local/mongodb/logs
-
增加环境变量,并使环境变量生效
[root@localhost data]# vi /etc/profile
export PATH=$PATH:/usr/local/mongodb/bin
[root@localhost data]# source /etc/profile
-
创建mongodb.conf文件并写入一下内容
[root@localhost data]# touch /usr/local/mongodb/bin/mongodb.conf
[root@localhost data]# vi /usr/local/mongodb/bin/mongodb.conf
-
主服务器配置
[root@master-node software]# more /usr/local/mongodb/bin/mongodb.conf
port=27017
dbpath= /usr/local/mongodb/data
logpath= /usr/local/mongodb/logs/log.log
logappend=true
fork=true
maxConns=100
noauth=true
journal=true
storageEngine=wiredTiger
bind_ip = 192.168.197.21
master=true
-
从服务器配置
[root@slave-node software]# more /usr/local/mongodb/bin/mongodb.conf
port=27017
dbpath= /usr/local/mongodb/data
logpath= /usr/local/mongodb/logs/log.log
logappend=true
fork=true
maxConns=100
noauth=true
slave=true
journal=true
storageEngine=wiredTiger
bind_ip = 192.168.197.22
source=192.168.197.21:27017
-
使用配置文件启动两台服务器上的数据库
mongod --config /usr/local/mongodb/bin/mongodb.conf
-
在主服务器上登陆数据库创建testa数据库,并写入20条数据
[root@master-node software]# mongo 192.168.197.21:27017
MongoDB shell version v3.4.10
connecting to: 192.168.197.21:27017
MongoDB server version: 3.4.10
Server has startup warnings:
2019-02-10T21:38:04.484-0500 I CONTROL [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
2019-02-10T21:38:04.484-0500 I CONTROL [initandlisten]
2019-02-10T21:38:04.485-0500 I CONTROL [initandlisten]
2019-02-10T21:38:04.485-0500 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2019-02-10T21:38:04.485-0500 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2019-02-10T21:38:04.485-0500 I CONTROL [initandlisten]
2019-02-10T21:38:04.485-0500 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2019-02-10T21:38:04.485-0500 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2019-02-10T21:38:04.485-0500 I CONTROL [initandlisten]
> use testa
switched to db testa
> function add(){var i = 0;for(;i<20;i++){db.persons.insert({"nameA":"li"+i})}}
> add()
> db.persons.find()
{ "_id" : ObjectId("5c61272206144917c514f1a3"), "nameA" : "li0" }
{ "_id" : ObjectId("5c61272206144917c514f1a4"), "nameA" : "li1" }
{ "_id" : ObjectId("5c61272206144917c514f1a5"), "nameA" : "li2" }
{ "_id" : ObjectId("5c61272206144917c514f1a6"), "nameA" : "li3" }
{ "_id" : ObjectId("5c61272206144917c514f1a7"), "nameA" : "li4" }
{ "_id" : ObjectId("5c61272206144917c514f1a8"), "nameA" : "li5" }
{ "_id" : ObjectId("5c61272206144917c514f1a9"), "nameA" : "li6" }
{ "_id" : ObjectId("5c61272206144917c514f1aa"), "nameA" : "li7" }
{ "_id" : ObjectId("5c61272206144917c514f1ab"), "nameA" : "li8" }
{ "_id" : ObjectId("5c61272206144917c514f1ac"), "nameA" : "li9" }
{ "_id" : ObjectId("5c61272206144917c514f1ad"), "nameA" : "li10" }
{ "_id" : ObjectId("5c61272206144917c514f1ae"), "nameA" : "li11" }
{ "_id" : ObjectId("5c61272206144917c514f1af"), "nameA" : "li12" }
{ "_id" : ObjectId("5c61272206144917c514f1b0"), "nameA" : "li13" }
{ "_id" : ObjectId("5c61272206144917c514f1b1"), "nameA" : "li14" }
{ "_id" : ObjectId("5c61272206144917c514f1b2"), "nameA" : "li15" }
{ "_id" : ObjectId("5c61272206144917c514f1b3"), "nameA" : "li16" }
{ "_id" : ObjectId("5c61272206144917c514f1b4"), "nameA" : "li17" }
{ "_id" : ObjectId("5c61272206144917c514f1b5"), "nameA" : "li18" }
{ "_id" : ObjectId("5c61272206144917c514f1b6"), "nameA" : "li19" }
-
登陆从服务器的数据库查看数据是否同步过来
[root@slave-node software]# mongo 192.168.197.22:27017
MongoDB shell version v3.4.10
connecting to: 192.168.197.22:27017
MongoDB server version: 3.4.10
Server has startup warnings:
2019-02-10T21:43:16.796-0500 I CONTROL [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
2019-02-10T21:43:16.796-0500 I CONTROL [initandlisten]
2019-02-10T21:43:16.796-0500 I CONTROL [initandlisten]
2019-02-10T21:43:16.796-0500 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2019-02-10T21:43:16.796-0500 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2019-02-10T21:43:16.796-0500 I CONTROL [initandlisten]
2019-02-10T21:43:16.796-0500 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2019-02-10T21:43:16.796-0500 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2019-02-10T21:43:16.796-0500 I CONTROL [initandlisten]
> show dbs
2019-02-11T02:43:26.999-0500 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:781:19
shellHelper@src/mongo/shell/utils.js:671:15
@(shellhelp2):1:1
> rs.slaveOk()
> show dbs
admin 0.000GB
local 0.000GB
master_slave 0.000GB
testa 0.000GB
> use testa
switched to db testa
> db.persons.find()
{ "_id" : ObjectId("5c61272206144917c514f1a3"), "nameA" : "li0" }
{ "_id" : ObjectId("5c61272206144917c514f1a4"), "nameA" : "li1" }
{ "_id" : ObjectId("5c61272206144917c514f1a5"), "nameA" : "li2" }
{ "_id" : ObjectId("5c61272206144917c514f1a6"), "nameA" : "li3" }
{ "_id" : ObjectId("5c61272206144917c514f1a7"), "nameA" : "li4" }
{ "_id" : ObjectId("5c61272206144917c514f1a8"), "nameA" : "li5" }
{ "_id" : ObjectId("5c61272206144917c514f1a9"), "nameA" : "li6" }
{ "_id" : ObjectId("5c61272206144917c514f1aa"), "nameA" : "li7" }
{ "_id" : ObjectId("5c61272206144917c514f1ab"), "nameA" : "li8" }
{ "_id" : ObjectId("5c61272206144917c514f1ac"), "nameA" : "li9" }
{ "_id" : ObjectId("5c61272206144917c514f1ad"), "nameA" : "li10" }
{ "_id" : ObjectId("5c61272206144917c514f1ae"), "nameA" : "li11" }
{ "_id" : ObjectId("5c61272206144917c514f1af"), "nameA" : "li12" }
{ "_id" : ObjectId("5c61272206144917c514f1b0"), "nameA" : "li13" }
{ "_id" : ObjectId("5c61272206144917c514f1b1"), "nameA" : "li14" }
{ "_id" : ObjectId("5c61272206144917c514f1b2"), "nameA" : "li15" }
{ "_id" : ObjectId("5c61272206144917c514f1b3"), "nameA" : "li16" }
{ "_id" : ObjectId("5c61272206144917c514f1b4"), "nameA" : "li17" }
{ "_id" : ObjectId("5c61272206144917c514f1b5"), "nameA" : "li18" }
{ "_id" : ObjectId("5c61272206144917c514f1b6"), "nameA" : "li19" }
-
关于报错
如果在slave-node节点上的数据库中查看,有报错:"errmsg" : "not master and slaveOk=false"!!!
首先这是正常的,因为SECONDARY是不允许读写的, 在写多读少的应用中,使用Replica Sets来实现读写分离。通过在连接时指定或者在主库指定slaveOk,由Secondary来分担读的压力,Primary只承担写操作。对于replica set 中的secondary 节点默认是不可读的。
解决办法:在slave-node节点数据库中执行"rs.slaveOk();"命令即可
在slave-node节点数据库中发现已经同步过来了testa库的20条数据,说明mongodb的主从复制环境已经成功了!当配置完主从服务器后,一但主服务器上的数据发生变化,从服务器也会发生变化。
-
设置mongodb的开机启动。
首先要在系统服务目录下创建启动文件mongodb.servicer然后写入一下内容
[root@localhost system]# cd /lib/systemd/system
[root@localhost system]# vi mongodb.service
[Unit]
Description=mongodb
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
ExecStart=/usr/local/mongodb/bin/mongod -f /usr/local/mongodb/bin/mongodb.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/usr/local/mongodb/bin/mongod --shutdown
PrivateTmp=true
[Install]
WantedBy=multi-user.target
注册到开机启动
[root@localhost system]# systemctl enable mongodb.service
Created symlink from /etc/systemd/system/multi-user.target.wants/mongodb.service to /usr/lib/systemd/system/mongodb.service.
启动
systemctl start mongodb.service
关闭
systemctl stop mongodb.service