Mongodb 安装,配置主、副、仲裁节点集群
1.下载MongoDB
下载网址: https://www.mongodb.com/download-center#community
[root@izm5eehstyavisa401mxslz ~]# wget -c https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-4.0.9.tgz
--2019-05-09 22:13:49-- https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-4.0.9.tgz
Resolving fastdl.mongodb.org (fastdl.mongodb.org)... 13.224.51.23, 13.224.51.108, 13.224.51.55, ...
Connecting to fastdl.mongodb.org (fastdl.mongodb.org)|13.224.51.23|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 84969698 (81M) [application/x-gzip]
Saving to: ‘mongodb-linux-x86_64-4.0.9.tgz’
100%[====================================================================================================================================
2019-05-09 22:14:04 (6.53 MB/s) - ‘mongodb-linux-x86_64-4.0.9.tgz’ saved [84969698/84969698]
[root@izm5eehstyavisa401mxslz ~]# tar zxvf mongodb-linux-x86_64-4.0.9.tgz
mongodb-linux-x86_64-4.0.9/README
mongodb-linux-x86_64-4.0.9/THIRD-PARTY-NOTICES
mongodb-linux-x86_64-4.0.9/MPL-2
mongodb-linux-x86_64-4.0.9/LICENSE-Community.txt
mongodb-linux-x86_64-4.0.9/bin/mongodump
mongodb-linux-x86_64-4.0.9/bin/mongorestore
mongodb-linux-x86_64-4.0.9/bin/mongoexport
mongodb-linux-x86_64-4.0.9/bin/mongoimport
mongodb-linux-x86_64-4.0.9/bin/mongostat
mongodb-linux-x86_64-4.0.9/bin/mongotop
mongodb-linux-x86_64-4.0.9/bin/bsondump
mongodb-linux-x86_64-4.0.9/bin/mongofiles
mongodb-linux-x86_64-4.0.9/bin/mongoreplay
mongodb-linux-x86_64-4.0.9/bin/mongod
mongodb-linux-x86_64-4.0.9/bin/mongos
mongodb-linux-x86_64-4.0.9/bin/mongo
mongodb-linux-x86_64-4.0.9/bin/install_compass
[root@izm5eehstyavisa401mxslz ~]# mv mongodb-linux-x86_64-4.0.9 /www/server/
2.添加到系统变量。
export PATH=/www/server/mongodb/bin:$PATH
3,创建日志目录。
[root@izm5eehstyavisa401mxslz ~]# mkdir -p /www/server/mongodb/log/mongod1
[root@izm5eehstyavisa401mxslz ~]# mkdir -p /www/server/mongodb/log/mongod2
[root@izm5eehstyavisa401mxslz ~]# mkdir -p /www/server/mongodb/log/mongod3
4,创建数据库目录
[root@izm5eehstyavisa401mxslz ~]# mkdir /www/server/mongodb/db1
[root@izm5eehstyavisa401mxslz ~]# mkdir /www/server/mongodb/db2
[root@izm5eehstyavisa401mxslz ~]# mkdir /www/server/mongodb/db3
5, 准备配置文件mongod1.conf如下。
systemLog:
destination: file
path: "/www/server/mongodb/log/mongod1/mongod.log"
logAppend: true
storage:
dbPath: /www/server/mongodb/db1
journal:
enabled: true
processManagement:
fork: true
security:
authorization: disabled
net:
bindIp: 127.0.0.1,172.31.0.54
port: 27017
setParameter:
enableLocalhostAuthBypass: false
replication:
replSetName: test
6,复制mongod1.conf为mongod2.conf和mongod3.conf,并修改内容
mongod2.conf的内容
systemLog:
destination: file
path: "/www/server/mongodb/log/mongod2/mongod.log"
logAppend: true
storage:
dbPath: /www/server/mongodb/db2
journal:
enabled: true
processManagement:
fork: true
security:
authorization: disabled
net:
bindIp: 127.0.0.1,172.31.0.54
port: 27018
setParameter:
enableLocalhostAuthBypass: false
replication:
replSetName: test
mongod3.conf的内容
systemLog:
destination: file
path: "/www/server/mongodb/log/mongod3/mongod.log"
logAppend: true
storage:
dbPath: /www/server/mongodb/db3
journal:
enabled: true
processManagement:
fork: true
security:
authorization: disabled
net:
bindIp: 127.0.0.1,172.31.0.54
port: 27019
setParameter:
enableLocalhostAuthBypass: false
replication:
replSetName: test
7, 然后根据配置文件分别启动三个实例.
[root@izm5eehstyavisa401mxslz mongodb]# vim conf/mongod1.conf
[root@izm5eehstyavisa401mxslz mongodb]# mongod --config ./conf/mongod1.conf
about to fork child process, waiting until server is ready for connections.
forked process: 28159
child process started successfully, parent exiting
[root@izm5eehstyavisa401mxslz mongodb]# mongod --config ./conf/mongod2.conf
about to fork child process, waiting until server is ready for connections.
forked process: 28191
child process started successfully, parent exiting
[root@izm5eehstyavisa401mxslz mongodb]# mongod --config ./conf/mongod3.conf
about to fork child process, waiting until server is ready for connections.
forked process: 28221
child process started successfully, parent exiting
8,查看监听情况ls
[root@izm5eehstyavisa401mxslz mongodb]# netstat -lnpt
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 172.31.0.54:27019 0.0.0.0:* LISTEN 28221/mongod
tcp 0 0 127.0.0.1:27019 0.0.0.0:* LISTEN 28221/mongod
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 14972/nginx: master
tcp 0 0 0.0.0.0:21 0.0.0.0:* LISTEN 5705/pure-ftpd (SER
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1172/sshd
tcp 0 0 0.0.0.0:888 0.0.0.0:* LISTEN 14972/nginx: master
tcp 0 0 0.0.0.0:8888 0.0.0.0:* LISTEN 2477/python2
tcp 0 0 172.31.0.54:27017 0.0.0.0:* LISTEN 28159/mongod
tcp 0 0 127.0.0.1:27017 0.0.0.0:* LISTEN 28159/mongod
tcp 0 0 172.31.0.54:27018 0.0.0.0:* LISTEN 28191/mongod
tcp 0 0 127.0.0.1:27018 0.0.0.0:* LISTEN 28191/mongod
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 31064/mysqld
tcp6 0 0 :::21 :::* LISTEN 5705/pure-ftpd (SER
9,连接mondb1
[root@izm5eehstyavisa401mxslz mongodb]# mongo 172.31.0.54:27017
MongoDB shell version v4.0.9
connecting to: mongodb://172.31.0.54:27017/test?gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("08f0d5a6-e482-432d-a5a8-c897ebf7cdb2") }
MongoDB server version: 4.0.9
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
http://docs.mongodb.org/
Questions? Try the support group
http://groups.google.com/group/mongodb-user
Server has startup warnings:
2019-05-09T22:38:24.734+0800 I STORAGE [initandlisten]
2019-05-09T22:38:24.734+0800 I STORAGE [initandlisten] ** WARNING: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine
2019-05-09T22:38:24.734+0800 I STORAGE [initandlisten] ** See http://dochub.mongodb.org/core/prodnotes-filesystem
2019-05-09T22:38:25.380+0800 I CONTROL [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
2019-05-09T22:38:25.380+0800 I CONTROL [initandlisten]
2019-05-09T22:38:25.380+0800 I CONTROL [initandlisten]
2019-05-09T22:38:25.380+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2019-05-09T22:38:25.380+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2019-05-09T22:38:25.380+0800 I CONTROL [initandlisten]
2019-05-09T22:38:25.380+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2019-05-09T22:38:25.380+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2019-05-09T22:38:25.380+0800 I CONTROL [initandlisten]
2019-05-09T22:38:25.380+0800 I CONTROL [initandlisten] ** WARNING: soft rlimits too low. rlimits set to 15088 processes, 65535 files. Number of processes should be at least 32767.5 : 0.5 times number of files.
2019-05-09T22:38:25.380+0800 I CONTROL [initandlisten]
10,输入以下命令
cfg={
_id:'test',
members:[
{_id:0,host:'192.168.70.128:27017'},
{_id:1,host:'192.168.70.128:27018'},
{_id:2,host:'192.168.70.128:27019',arbiterOnly:true}
]
}
rs.initiate(cfg)
返回表示成功。
rs.initiate(cfg)
{"ok" : 1, "operationTime" : Timestamp(1557413522, 1), "$clusterTime" : { "clusterTime" : Timestamp(1557413522, 1), "signature" : { "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="), "keyId" : NumberLong(0) } }
}
11, 执行rs.status()查看集群信息:
test:SECONDARY> rs.status()
{
"set" : "test",
"date" : ISODate("2019-05-09T14:52:29.907Z"),
"myState" : 1,
"term" : NumberLong(1),
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"heartbeatIntervalMillis" : NumberLong(2000),
"optimes" : {
"lastCommittedOpTime" : {
"ts" : Timestamp(1557413534, 5),
"t" : NumberLong(1)
},
"readConcernMajorityOpTime" : {
"ts" : Timestamp(1557413534, 5),
"t" : NumberLong(1)
},
"appliedOpTime" : {
"ts" : Timestamp(1557413534, 5),
"t" : NumberLong(1)
},
"durableOpTime" : {
"ts" : Timestamp(1557413534, 5),
"t" : NumberLong(1)
}
},
"lastStableCheckpointTimestamp" : Timestamp(1557413534, 1),
"members" : [
{
"_id" : 0,
"name" : "172.31.0.54:27017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 845,
"optime" : {
"ts" : Timestamp(1557413534, 5),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2019-05-09T14:52:14Z"),
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "could not find member to sync from",
"electionTime" : Timestamp(1557413533, 1),
"electionDate" : ISODate("2019-05-09T14:52:13Z"),
"configVersion" : 1,
"self" : true,
"lastHeartbeatMessage" : ""
},
{
"_id" : 1,
"name" : "172.31.0.54:27018",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 27,
"optime" : {
"ts" : Timestamp(1557413534, 5),
"t" : NumberLong(1)
},
"optimeDurable" : {
"ts" : Timestamp(1557413534, 5),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2019-05-09T14:52:14Z"),
"optimeDurableDate" : ISODate("2019-05-09T14:52:14Z"),
"lastHeartbeat" : ISODate("2019-05-09T14:52:29.282Z"),
"lastHeartbeatRecv" : ISODate("2019-05-09T14:52:29.433Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncingTo" : "172.31.0.54:27017",
"syncSourceHost" : "172.31.0.54:27017",
"syncSourceId" : 0,
"infoMessage" : "",
"configVersion" : 1
},
{
"_id" : 2,
"name" : "172.31.0.54:27019",
"health" : 1,
"state" : 7,
"stateStr" : "ARBITER",
"uptime" : 27,
"lastHeartbeat" : ISODate("2019-05-09T14:52:29.282Z"),
"lastHeartbeatRecv" : ISODate("2019-05-09T14:52:28.856Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "",
"configVersion" : 1
}
],
"ok" : 1,
"operationTime" : Timestamp(1557413534, 5),
"$clusterTime" : {
"clusterTime" : Timestamp(1557413534, 5),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
可以看出1是节点,2是辅助,3是仲裁节点。
12,插入测试数据。
test:PRIMARY> use test; #创建test数据库
switched to db test
test:PRIMARY> db.testdb.insert({"test1":"testval1"}) #插入数据
WriteResult({ "nInserted" : 1 })
test:PRIMARY> db.testdb.find(); #查看数据
{ "_id" : ObjectId("5cd4420ae9c4c6bdba8bbfe9"), "test1" : "testval1" }
13,查看mongodb1的pid,然后使用kill命令杀掉。
14,杀掉之后,登录mongo2,执行rs.status()查看集群信息,并且查看是否有之前插入mongo1的信息:
test:SECONDARY> rs.status()
{
"set" : "test",
"date" : ISODate("2019-05-09T15:02:26.276Z"),
"myState" : 1,
"term" : NumberLong(3),
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"heartbeatIntervalMillis" : NumberLong(2000),
"optimes" : {
"lastCommittedOpTime" : {
"ts" : Timestamp(1557414124, 1),
"t" : NumberLong(1)
},
"readConcernMajorityOpTime" : {
"ts" : Timestamp(1557414124, 1),
"t" : NumberLong(1)
},
"appliedOpTime" : {
"ts" : Timestamp(1557414141, 1),
"t" : NumberLong(3)
},
"durableOpTime" : {
"ts" : Timestamp(1557414141, 1),
"t" : NumberLong(3)
}
},
"lastStableCheckpointTimestamp" : Timestamp(1557414124, 1),
"members" : [
{
"_id" : 0,
"name" : "172.31.0.54:27017",
"health" : 0,
"state" : 8,
"stateStr" : "(not reachable/healthy)",
"uptime" : 0,
"optime" : {
"ts" : Timestamp(0, 0),
"t" : NumberLong(-1)
},
"optimeDurable" : {
"ts" : Timestamp(0, 0),
"t" : NumberLong(-1)
},
"optimeDate" : ISODate("1970-01-01T00:00:00Z"),
"optimeDurableDate" : ISODate("1970-01-01T00:00:00Z"),
"lastHeartbeat" : ISODate("2019-05-09T15:02:26.003Z"),
"lastHeartbeatRecv" : ISODate("2019-05-09T15:02:07.282Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "Error connecting to 172.31.0.54:27017 :: caused by :: Connection refused",
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "",
"configVersion" : -1
},
{
"_id" : 1,
"name" : "172.31.0.54:27018",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 1410,
"optime" : {
"ts" : Timestamp(1557414141, 1),
"t" : NumberLong(3)
},
"optimeDate" : ISODate("2019-05-09T15:02:21Z"),
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "could not find member to sync from",
"electionTime" : Timestamp(1557414139, 1),
"electionDate" : ISODate("2019-05-09T15:02:19Z"),
"configVersion" : 1,
"self" : true,
"lastHeartbeatMessage" : ""
},
{
"_id" : 2,
"name" : "172.31.0.54:27019",
"health" : 1,
"state" : 7,
"stateStr" : "ARBITER",
"uptime" : 74,
"lastHeartbeat" : ISODate("2019-05-09T15:02:26.002Z"),
"lastHeartbeatRecv" : ISODate("2019-05-09T15:02:24.579Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "",
"configVersion" : 1
}
],
"ok" : 1,
"operationTime" : Timestamp(1557414141, 1),
"$clusterTime" : {
"clusterTime" : Timestamp(1557414141, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
查看mongo2已经成为主节点。
test:PRIMARY> show dbs; #列出所有数据库。
admin 0.000GB
config 0.000GB
local 0.000GB
test 0.000GB
test:PRIMARY> db.testdb.find(); #查询数据库信息。
{ "_id" : ObjectId("5cd4420ae9c4c6bdba8bbfe9"), "test1" : "testval1" }
15重新启动mongo1,然后查询集群状态。
[root@izm5eehstyavisa401mxslz mongodb]# mongod --config ./conf/mongod1.conf
about to fork child process, waiting until server is ready for connections.
forked process: 28501
child process started successfully, parent exiting
[root@izm5eehstyavisa401mxslz mongodb]# mongo 172.31.0.54:27017
MongoDB shell version v4.0.9
connecting to: mongodb://172.31.0.54:27017/test?gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("97ce70e4-e3e5-41a0-97a2-1d54986eca0a") }
MongoDB server version: 4.0.9
Server has startup warnings:
2019-05-09T23:02:40.266+0800 I STORAGE [initandlisten]
2019-05-09T23:02:40.266+0800 I STORAGE [initandlisten] ** WARNING: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine
2019-05-09T23:02:40.266+0800 I STORAGE [initandlisten] ** See http://dochub.mongodb.org/core/prodnotes-filesystem
2019-05-09T23:02:41.219+0800 I CONTROL [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
2019-05-09T23:02:41.219+0800 I CONTROL [initandlisten]
2019-05-09T23:02:41.219+0800 I CONTROL [initandlisten]
2019-05-09T23:02:41.219+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2019-05-09T23:02:41.219+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2019-05-09T23:02:41.219+0800 I CONTROL [initandlisten]
2019-05-09T23:02:41.219+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2019-05-09T23:02:41.219+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2019-05-09T23:02:41.219+0800 I CONTROL [initandlisten]
2019-05-09T23:02:41.219+0800 I CONTROL [initandlisten] ** WARNING: soft rlimits too low. rlimits set to 15088 processes, 65535 files. Number of processes should be at least 32767.5 : 0.5 times number of files.
2019-05-09T23:02:41.219+0800 I CONTROL [initandlisten]
2019-05-09T23:02:41.245+0800 I REPL [replexec-0]
2019-05-09T23:02:41.245+0800 I REPL [replexec-0] ** WARNING: This replica set uses arbiters, but readConcern:majority is enabled
2019-05-09T23:02:41.245+0800 I REPL [replexec-0] ** for this node. This is not a recommended configuration. Please see
2019-05-09T23:02:41.245+0800 I REPL [replexec-0] ** https://dochub.mongodb.org/core/psa-disable-rc-majority-4.0
2019-05-09T23:02:41.245+0800 I REPL [replexec-0]
test:PRIMARY> rs.status()
{
"set" : "test",
"date" : ISODate("2019-05-09T15:03:18.430Z"),
"myState" : 1,
"term" : NumberLong(3),
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"heartbeatIntervalMillis" : NumberLong(2000),
"optimes" : {
"lastCommittedOpTime" : {
"ts" : Timestamp(1557414191, 1),
"t" : NumberLong(3)
},
"readConcernMajorityOpTime" : {
"ts" : Timestamp(1557414191, 1),
"t" : NumberLong(3)
},
"appliedOpTime" : {
"ts" : Timestamp(1557414191, 1),
"t" : NumberLong(3)
},
"durableOpTime" : {
"ts" : Timestamp(1557414191, 1),
"t" : NumberLong(3)
}
},
"lastStableCheckpointTimestamp" : Timestamp(1557414191, 1),
"members" : [
{
"_id" : 0,
"name" : "172.31.0.54:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 36,
"optime" : {
"ts" : Timestamp(1557414191, 1),
"t" : NumberLong(3)
},
"optimeDurable" : {
"ts" : Timestamp(1557414191, 1),
"t" : NumberLong(3)
},
"optimeDate" : ISODate("2019-05-09T15:03:11Z"),
"optimeDurableDate" : ISODate("2019-05-09T15:03:11Z"),
"lastHeartbeat" : ISODate("2019-05-09T15:03:18.005Z"),
"lastHeartbeatRecv" : ISODate("2019-05-09T15:03:18.247Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncingTo" : "172.31.0.54:27018",
"syncSourceHost" : "172.31.0.54:27018",
"syncSourceId" : 1,
"infoMessage" : "",
"configVersion" : 1
},
{
"_id" : 1,
"name" : "172.31.0.54:27018",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 1462,
"optime" : {
"ts" : Timestamp(1557414191, 1),
"t" : NumberLong(3)
},
"optimeDate" : ISODate("2019-05-09T15:03:11Z"),
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "could not find member to sync from",
"electionTime" : Timestamp(1557414139, 1),
"electionDate" : ISODate("2019-05-09T15:02:19Z"),
"configVersion" : 1,
"self" : true,
"lastHeartbeatMessage" : ""
},
{
"_id" : 2,
"name" : "172.31.0.54:27019",
"health" : 1,
"state" : 7,
"stateStr" : "ARBITER",
"uptime" : 126,
"lastHeartbeat" : ISODate("2019-05-09T15:03:18.005Z"),
"lastHeartbeatRecv" : ISODate("2019-05-09T15:03:16.579Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "",
"configVersion" : 1
}
],
"ok" : 1,
"operationTime" : Timestamp(1557414191, 1),
"$clusterTime" : {
"clusterTime" : Timestamp(1557414191, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}