我们这里讲解的是如何配置mongodb复制集,首先在每一个机器上面安装mongodb,我们是通过yum的方式安装mongodb3.2
1.在文章中添加yum安装的配置文件:
touch /etc/yum.repos.d/mongodb-org-3.2.repo vim /etc/yum.repos.d/mongodb-org-3.2.repo
打开文件后里面添加内容:
[mongodb-org-3.2] name=MongoDB Repository baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/3.2/x86_64/ gpgcheck=1 enabled=1 gpgkey=https://www.mongodb.org/static/pgp/server-3.2.asc
2.yum安装
sudo yum install -y mongodb-org
其他节点也进行进行上面的步骤,将其他的节点按照上面,进行安装mongodb,安装完成后,都关闭mongodb:
sudo service mongod stop
3. 在每一个mongodb的节点,修改hosts
节点:192.168.220.105 mongo0.example.com
节点:192.168.220.106 mongo1.example.com
vim /etc/hosts
192.168.220.105:在上面打开的文件中添加或修改:
127.0.0.1 localhost mongo0.example.com 192.168.220.105 mongo0.example.com 192.168.220.106 mongo1.example.com
192.168.220.106:在上面文件中添加或者修改:
127.0.0.1 localhost mongo1.example.com 192.168.220.105 mongo0.example.com 192.168.220.106 mongo1.example.com
4. 在每一个mongodb的节点,修改hostname
节点:192.168.220.105 mongo0.example.com
节点:192.168.220.106 mongo1.example.com
192.168.220.105:
sudo hostname mongo0.example.com vim /etc/hostname #在打开的文件中添加: mongo0.example.com
192.168.220.106:
sudo hostname mongo1.example.com vim /etc/hostname mongo1.example.com
5.修改mongodb的配置 vim /etc/mongod.conf
进行的修改为: 把bingIp注释掉,因为这一行只让本地访问:
net: port: 27017 # bindIp: 127.0.0.1 # Listen to local interface only, comment to listen on all interfaces.
添加复制集配置:
replication: replSetName: rs0
编辑完的内容为:
# mongod.conf # for documentation of all options, see: # http://docs.mongodb.org/manual/reference/configuration-options/ # where to write logging data. systemLog: destination: file logAppend: true path: /var/log/mongodb/mongod.log # Where and how to store data. storage: dbPath: /var/lib/mongo journal: enabled: true # engine: # mmapv1: # wiredTiger: # how the process runs processManagement: fork: true # fork and run in background pidFilePath: /var/run/mongodb/mongod.pid # location of pidfile # network interfaces net: port: 27017 # bindIp: 127.0.0.1 # Listen to local interface only, comment to listen on all interfaces. #security: #operationProfiling: replication: replSetName: rs0 #sharding: ## Enterprise-Only Options #auditLog: #snmp:
保存文件后,重启linxu。
添加开机启动
chkconfig mongod on
启动mongodb:
/etc/init.d/mongod restart
8.现在有两台linux,现在开始配置,注意,只能进入一台mongodb,其他的mongdb不要配置,启动就好。
在mongo0中:(这个作为主节点)
mongo
执行log如下:
[root@mongo0 ~]# mongo MongoDB shell version: 3.2.7 connecting to: test Server has startup warnings: 2016-06-20T14:10:48.876+0800 I CONTROL [initandlisten] 2016-06-20T14:10:48.876+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'. 2016-06-20T14:10:48.876+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never' 2016-06-20T14:10:48.876+0800 I CONTROL [initandlisten] 2016-06-20T14:10:48.876+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'. 2016-06-20T14:10:48.876+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never' 2016-06-20T14:10:48.876+0800 I CONTROL [initandlisten] 2016-06-20T14:10:48.876+0800 I CONTROL [initandlisten] ** WARNING: soft rlimits too low. rlimits set to 1024 processes, 64000 files. Number of processes should be at least 32000 : 0.5 times number of files. 2016-06-20T14:10:48.876+0800 I CONTROL [initandlisten]
然后初始化:(注意,从节点不要执行rs.initiate() , 只有主节点执行这个)
rs.initiate()
log如下:
rs.initiate() { "info2" : "no configuration specified. Using a default configuration for the set", "me" : "mongo0.example.com:27017", "ok" : 1 } rs0:OTHER>
增加节点:(在主节点中执行,添加从节点,主要从节点的iptables要添加信任,如果是本地,则可以直接关掉iptables,最好是添加ip信任,添加ip信任的方式
/sbin/iptables -I INPUT -p tcp --dport 27017 -j ACCEPT /etc/rc.d/init.d/iptables save
)
rs0:OTHER> rs.add("mongo1.example.com:27017"); { "ok" : 1 }
rs.config() 查看配置,log如下:
rs0:PRIMARY> rs.config(); { "_id" : "rs0", "version" : 2, "protocolVersion" : NumberLong(1), "members" : [ { "_id" : 0, "host" : "mongo0.example.com:27017", "arbiterOnly" : false, "buildIndexes" : true, "hidden" : false, "priority" : 1, "tags" : { }, "slaveDelay" : NumberLong(0), "votes" : 1 }, { "_id" : 1, "host" : "mongo1.example.com:27017", "arbiterOnly" : false, "buildIndexes" : true, "hidden" : false, "priority" : 1, "tags" : { }, "slaveDelay" : NumberLong(0), "votes" : 1 } ], "settings" : { "chainingAllowed" : true, "heartbeatIntervalMillis" : 2000, "heartbeatTimeoutSecs" : 10, "electionTimeoutMillis" : 10000, "getLastErrorModes" : { }, "getLastErrorDefaults" : { "w" : 1, "wtimeout" : 0 }, "replicaSetId" : ObjectId("5767896cd1595b8f30b7cd4a") } }
查看状态:
rs0:PRIMARY> rs.status()
如果后续添加节点,那么,安装完mongodb,设置完成mongodb后,启动mongodb后,设置完成hosts后,直接在主节点中执行添加:
执行log如下:
rs0:PRIMARY> rs.add("mongo2.example.com:27017"); { "ok" : 1 } rs0:PRIMARY> rs.status function () { return db._adminCommand("replSetGetStatus"); } rs0:PRIMARY> rs.status(); { "set" : "rs0", "date" : ISODate("2016-10-08T02:45:54.068Z"), "myState" : 1, "term" : NumberLong(19), "heartbeatIntervalMillis" : NumberLong(2000), "members" : [ { "_id" : 0, "name" : "mongo0.example.com:27017", "health" : 1, "state" : 1, "stateStr" : "PRIMARY", "uptime" : 1126, "optime" : { "ts" : Timestamp(1475894744, 1), "t" : NumberLong(19) }, "optimeDate" : ISODate("2016-10-08T02:45:44Z"), "electionTime" : Timestamp(1475894303, 1), "electionDate" : ISODate("2016-10-08T02:38:23Z"), "configVersion" : 3, "self" : true }, { "_id" : 1, "name" : "mongo1.example.com:27017", "health" : 1, "state" : 2, "stateStr" : "SECONDARY", "uptime" : 617, "optime" : { "ts" : Timestamp(1475894744, 1), "t" : NumberLong(19) }, "optimeDate" : ISODate("2016-10-08T02:45:44Z"), "lastHeartbeat" : ISODate("2016-10-08T02:45:52.842Z"), "lastHeartbeatRecv" : ISODate("2016-10-08T02:45:52.847Z"), "pingMs" : NumberLong(0), "syncingTo" : "mongo0.example.com:27017", "configVersion" : 3 }, { "_id" : 2, "name" : "mongo2.example.com:27017", "health" : 0, "state" : 8, "stateStr" : "(not reachable/healthy)", "uptime" : 0, "optime" : { "ts" : Timestamp(0, 0), "t" : NumberLong(-1) }, "optimeDate" : ISODate("1970-01-01T00:00:00Z"), "lastHeartbeat" : ISODate("2016-10-08T02:45:52.842Z"), "lastHeartbeatRecv" : ISODate("1970-01-01T00:00:00Z"), "pingMs" : NumberLong(0), "lastHeartbeatMessage" : "No route to host", "configVersion" : -1 } ], "ok" : 1 } rs0:PRIMARY>
日志:
rs0:PRIMARY> rs.status() { "set" : "rs0", "date" : ISODate("2016-06-20T06:14:17.377Z"), "myState" : 1, "term" : NumberLong(1), "heartbeatIntervalMillis" : NumberLong(2000), "members" : [ { "_id" : 0, "name" : "mongo0.example.com:27017", "health" : 1, "state" : 1, "stateStr" : "PRIMARY", "uptime" : 92, "optime" : { "ts" : Timestamp(1466403201, 1), "t" : NumberLong(1) }, "optimeDate" : ISODate("2016-06-20T06:13:21Z"), "infoMessage" : "could not find member to sync from", "electionTime" : Timestamp(1466403181, 1), "electionDate" : ISODate("2016-06-20T06:13:01Z"), "configVersion" : 2, "self" : true }, { "_id" : 1, "name" : "mongo1.example.com:27017", "health" : 1, "state" : 2, "stateStr" : "SECONDARY", "uptime" : 55, "optime" : { "ts" : Timestamp(1466403201, 1), "t" : NumberLong(1) }, "optimeDate" : ISODate("2016-06-20T06:13:21Z"), "lastHeartbeat" : ISODate("2016-06-20T06:14:15.784Z"), "lastHeartbeatRecv" : ISODate("2016-06-20T06:14:14.010Z"), "pingMs" : NumberLong(0), "configVersion" : 2 } ], "ok" : 1 }
如果连接的机器是另外一个机器,也就说不是mongodb复制集中的机器,需要添加host(此处需要说明的是php,如果是和mongodb分开的那么需要在php机器的hosts中添加hosts)
vim /etc/hosts
192.168.220.105 mongo0.example.com 192.168.220.106 mongo1.example.com
在shell 下面链接:(test是数据库的名字,rs0是复制集的名字)
mongo --host rs0/mongo0.example.com:27017,mongo1.example.com:27017 test
在php中连接:
'dsn' => 'mongodb://192.168.220.105:27017/erp,192.168.220.106:27017/erp?replicaSet=rs0&readPreference=primaryPreferred',
备份:
mongodump -h 192.168.220.60:27500 -d erp -o /terry/erp
恢复:
mongorestore -h 192.168.220.105:27017 -d erp /terry/erp
如果在配置复制集过程中出现了问题,无法进行,一般是想重新配置,但有一种比较好的办法,就是删除掉dbPath,在配置文件中dbPath的配置如下:
dbPath: /var/lib/mongo
rm -rf /var/lib/mongo/*
其他:
1. 删除mongodb的库,重新启动
mv /var/lib/mongo /var/lib/mongo1 mkdir /var/lib/mongo chown mongod:mongod /var/lib/mongo
2.删除复制集节点:
rs.remove("mongo0.example.com:27017")
3.在副本集的副节点读数据:
db.setSlaveOk()
4.主从切换
进入主mongo 执行:
rs.stepDown(15);
log如下:
rs0:PRIMARY> rs.stepDown(15); 2016-10-08T10:37:25.511+0800 E QUERY [thread1] Error: error doing query: failed: network error while attempting to run command 'replSetStepDown' on host '127.0.0.1:27017' : DB.prototype.runCommand@src/mongo/shell/db.js:135:1 DB.prototype.adminCommand@src/mongo/shell/db.js:153:16 rs.stepDown@src/mongo/shell/utils.js:1181:12 @(shell):1:1 2016-10-08T10:37:25.513+0800 I NETWORK [thread1] trying reconnect to 127.0.0.1:27017 (127.0.0.1) failed 2016-10-08T10:37:25.525+0800 I NETWORK [thread1] reconnect 127.0.0.1:27017 (127.0.0.1) ok rs0:SECONDARY> rs0:SECONDARY> rs0:SECONDARY> rs0:SECONDARY> rs0:SECONDARY>
参考:
https://gist.github.com/leommoore/309de7c0042ed697ee84
https://docs.mongodb.com/manual/tutorial/install-mongodb-on-red-hat/
https://docs.mongodb.com/manual/tutorial/deploy-replica-set/
https://docs.mongodb.com/manual/reference/configuration-options/