mongodb集群部署

环境准备

hostname IP 角色
mongodb-cluster-1 10.200.112.104 primary
mongodb-cluster-2 10.200.112.64 secondary
mongodb-cluster-3 10.200.112.96 secondary

单机环境部署

安装

1
2
3
4
# wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-3.4.18.tgz
# tar xf mongodb-linux-x86_64-3.4.18.tgz -C /usr/local/
# mv /usr/local/mongodb-linux-x86_64-3.4.18/ /usr/local/mongodb
# echo "export PATH=$PATH:/usr/local/mongodb/bin" >> /etc/profile

配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# mkdir /usr/local/mongodb/{conf,data}
# vi /usr/local/mongodb/conf/mongo.conf
systemLog:
destination: file
path: /var/log/mongodb.log
logAppend: true
logRotate: rename
storage:
journal:
enabled: true
dbPath: /usr/local/mongodb/data/
directoryPerDB: true
engine: wiredTiger
wiredTiger:
engineConfig:
cacheSizeGB: 1
directoryForIndexes: true
collectionConfig:
blockCompressor: zlib
indexConfig:
prefixCompression: true
processManagement:
fork: true
pidFilePath: /usr/local/mongodb/mongodb.pid
net:
port: 27017
security:
authorization: enabled
keyFile: /usr/local/mongodb/conf/mongodb-keyfile
# openssl rand -base64 123 > /usr/local/mongodb/conf/mongodb-keyfile
# chmod 600 /usr/local/mongodb/conf/mongodb-keyfile

启动服务

1
2
3
4
5
6
# mongod -f /usr/local/mongodb/conf/mongo.conf
about to fork child process, waiting until server is ready for connections.
forked process: 2830
child process started successfully, parent exiting
# netstat -lntp|grep 27017
tcp 0 0 0.0.0.0:27017 0.0.0.0:* LISTEN 2830/mongod

管理mongodb

创建管理员用户

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# mongo
> use admin
switched to db admin
> db.createUser({user: "admin",pwd: "Boliv1a",roles: [ { role: "root", db: "admin" }]});
Successfully added user: {
"user" : "admin",
"roles" : [
{
"role" : "root",
"db" : "admin"
}
]
}
> db.auth('admin','Boliv1a')
1
> db.stats()
{
"db" : "admin",
"collections" : 2,
"views" : 0,
"objects" : 3,
"avgObjSize" : 129.33333333333334,
"dataSize" : 388,
"storageSize" : 49152,
"numExtents" : 0,
"indexes" : 4,
"indexSize" : 98304,
"ok" : 1
}

创建数据库

创建数据库使用use DATABASE_NAME(需要插入数据才会显示出来)。如果数据库不存在,则创建数据库,否则切换到指定数据库。

1
2
3
4
5
6
7
8
9
10
11
12
13
> use vnimos
switched to db vnimos
vnimos:PRIMARY> show dbs
admin 0.000GB
local 0.000GB
vnimos:PRIMARY> db.vnimos.insert({"id":"1"})
WriteResult({ "nInserted" : 1 })
vnimos:PRIMARY> show dbs
admin 0.000GB
local 0.000GB
vnimos 0.000GB
vnimos:PRIMARY> db.vnimos.find().pretty()
{ "_id" : ObjectId("5c24744dada1f2f00348795a"), "id" : "1" }

集群环境部署

其他两台需要先部署完成单机环境

配置[所有节点执行]

1
2
3
4
5
# vi /usr/local/mongodb/conf/mongo.conf
replication:
oplogSizeMB: 512
replSetName: vnimos
# restart

初始化副本集[primary节点执行]

初始化副本集之前必须确保其他节点上的数据是空的,否则会报CannotInitializeNodeWithData的错误

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# mongo --port 27017 -u admin -p Boliv1a --authenticationDatabase admin
> config = {
_id: "vnimos",
members: [{
_id: 0,
host: "10.200.112.104:27017"
}, {
_id: 1,
host: "10.200.112.64:27017"
}, {
_id: 2,
host: "10.200.112.96:27017"
}]
}
> rs.initiate(config);
{ "ok" : 1 }

连接MongoDB副本集

MongoDB副本集里Primary节点是不固定的,当遇到副本集轮转升级、Primary宕机、网络分区等场景时,副本集可能会选举出一个新的Primary,而原来的Primary则会降级为Secondary,即发生主备切换。

通过以下方式将自动连接到Primary

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# /usr/local/mongodb/bin/mongo mongodb://"10.200.112.104:27017,10.200.112.64:27017,10.200.112.96:27017"/?replicaSet=vnimos -u admin -p Boliv1a --authenticationDatabase admin
vnimos:PRIMARY> rs.isMaster()
{
"hosts" : [
"10.200.112.104:27017",
"10.200.112.64:27017",
"10.200.112.96:27017"
],
"setName" : "vnimos",
"setVersion" : 1,
"ismaster" : true,
"secondary" : false,
"primary" : "10.200.112.104:27017",
"me" : "10.200.112.104:27017",
"electionId" : ObjectId("7fffffff0000000000000004"),
"lastWrite" : {
"opTime" : {
"ts" : Timestamp(1545900033, 1),
"t" : NumberLong(4)
},
"lastWriteDate" : ISODate("2018-12-27T08:40:33Z")
},
"maxBsonObjectSize" : 16777216,
"maxMessageSizeBytes" : 48000000,
"maxWriteBatchSize" : 1000,
"localTime" : ISODate("2018-12-27T08:40:34.252Z"),
"maxWireVersion" : 5,
"minWireVersion" : 0,
"readOnly" : false,
"ok" : 1
}

副本集相关参数

  • arbiterOnly
    Arbiter节点只参与投票(仲裁),不能被选为Primary,并且不从Primary同步数据
  • hidden
    Hidden节点不能被选为主(Priority为0),并且对Driver不可见。
  • priority
    Primary选择的优先级。Priority为0节点不会被选举为Primary
  • votes
    Mongodb 3.0之后,复本集成员最多50个,参与Primary选举投票的成员最多7个,其他成员的vote属性必须设置为0,即不参与投票。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
vnimos:PRIMARY> rs.config()
{
"_id" : "vnimos",
"version" : 1,
"protocolVersion" : NumberLong(1),
"members" : [
{
"_id" : 0,
"host" : "10.200.112.104:27017",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 1,
"tags" : {

},
"slaveDelay" : NumberLong(0),
"votes" : 1
},
{
"_id" : 1,
"host" : "10.200.112.64:27017",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 1,
"tags" : {

},
"slaveDelay" : NumberLong(0),
"votes" : 1
},
{
"_id" : 2,
"host" : "10.200.112.96:27017",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 1,
"tags" : {

},
"slaveDelay" : NumberLong(0),
"votes" : 1
}
],
"settings" : {
"chainingAllowed" : true,
"heartbeatIntervalMillis" : 2000,
"heartbeatTimeoutSecs" : 10,
"electionTimeoutMillis" : 10000,
"catchUpTimeoutMillis" : 60000,
"getLastErrorModes" : {

},
"getLastErrorDefaults" : {
"w" : 1,
"wtimeout" : 0
},
"replicaSetId" : ObjectId("5c246af1c2c0a20795d077cb")
}
}
vnimos:PRIMARY> db.printSlaveReplicationInfo()
source: 10.200.112.64:27017
syncedTo: Thu Dec 27 2018 14:31:09 GMT+0800 (CST)
0 secs (0 hrs) behind the primary
source: 10.200.112.96:27017
syncedTo: Thu Dec 27 2018 14:31:09 GMT+0800 (CST)
0 secs (0 hrs) behind the primary

默认情况下,Secondary是不提供服务的,即不能读写,会提示:NotMasterNoSlaveOk
如果需要读的话则要执行rs.slaveOk() ,只对当前连接有效。

1
2
3
4
5
6
7
8
9
10
11
12
vnimos:SECONDARY> show dbs
2018-12-27T14:33:17.525+0800 E QUERY [thread1] Error: listDatabases failed:{
"ok" : 0,
"errmsg" : "not master and slaveOk=false",
"code" : 13435,
"codeName" : "NotMasterNoSlaveOk"
} :
vnimos:SECONDARY> rs.slaveOk()
vnimos:SECONDARY> show dbs
admin 0.000GB
local 0.000GB
vnimos 0.000GB

备份

备份所有数据库

1
# mongodump -h "vnimos/10.200.112.104:27017,10.200.112.64:27017,10.200.112.96:2701" -u admin -p Boliv1a --authenticationDatabase admin -o /root/backup

备份指定数据库

1
# mongodump -h "vnimos/10.200.112.104:27017,10.200.112.64:27017,10.200.112.96:2701" -u admin -p Boliv1a --authenticationDatabase admin -d vnimos -o /root/backup

恢复

恢复所有数据库

1
# mongorestore -h "vnimos/10.200.112.104:27017,10.200.112.64:27017,10.200.112.96:2701" -u admin -p Boliv1a --authenticationDatabase admin  /root/backup

恢复指定数据库

1
# mongorestore -h "vnimos/10.200.112.104:27017,10.200.112.64:27017,10.200.112.96:2701" -u admin -p Boliv1a --authenticationDatabase admin -d vnimos /root/backup/vnimos/
坚持原创技术分享,您的支持将鼓励我继续创作!
0%