1.
touch /etc/yum.repos.d/mongodb-org-3.2.repo
vim /etc/yum.repos.d/mongodb-org-3.2.repo
2. 增加内容
[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
3.安装
sudo yum install -y mongodb-org
4.将其他的节点按照上面,进行安装mongodb
安装完成后,都关闭mongodb:
sudo service mongod stop
5. 在每一个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
6. 在每一个mongodb的节点,修改hostname
节点:192.168.220.105 mongo0.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
7.修改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
保存文件后,重启linxu
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()
log如下:
> rs.initiate()
{
"info2" : "no configuration specified. Using a default configuration for the set",
"me" : "mongo0.example.com:27017",
"ok" : 1
}
rs0:OTHER>
增加节点:
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()
日志:
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
}
在shell 下面链接:
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
其他:
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")
3.在副本集的副节点读数据:
db.setSlaveOk()
参考:
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/