1 安装前准备
1.1 主机列表
本次实验选择5台主机
节点ip | OS版本 | jdk版本 | hostname | 安装软件 |
---|---|---|---|---|
192.168.0.1 | RHEL7.4 | openjdk-1.8.0.131 | mongodb01 | config1/config2/config3 |
192.168.0.2 | RHEL7.4 | openjdk-1.8.0.131 | mongodb02 | mongos1/mongos2/mongos3 |
192.168.0.3 | RHEL7.4 | openjdk-1.8.0.131 | mongodb03 | shard01主/arbiter02/shard03从 |
192.168.0.4 | RHEL7.4 | openjdk-1.8.0.131 | mongodb04 | shard01从/shard02主/arbiter03 |
192.168.0.5 | RHEL7.4 | openjdk-1.8.0.131 | mongodb05 | arbiter01/shard02从/shard03主 |
1.2 安装包下载
官方下载地址:
https://www.mongodb.com/download-center/community
软件包说明
Package Name | Description |
---|---|
mongodb-org | A metapackage that will automatically install the four component packages listed below. |
mongodb-org-server | Contains the mongod daemon, associated init script, and a configuration file(/etc/mongod.conf ). You can use the initialization script to start mongod with the configuration file. For details, see Run MongoDB Community Edition. |
mongodb-org-mongos | Contains the mongos daemon. |
mongodb-org-shell | Contains the mongo shell. |
mongodb-org-tools | Contains the following MongoDB tools: mongoimport bsondump , mongodump , mongoexport , mongofiles , mongorestore , mongostat , and mongotop . |
本文选择3.4.20 for redhat7版本下载:
https://repo.mongodb.org/yum/redhat/7/mongodb-org/3.4/x86_64/RPMS/mongodb-org-server-3.4.20-1.el7.x86_64.rpm
https://repo.mongodb.org/yum/redhat/7/mongodb-org/3.4/x86_64/RPMS/mongodb-org-mongos-3.4.20-1.el7.x86_64.rpm
https://repo.mongodb.org/yum/redhat/7/mongodb-org/3.4/x86_64/RPMS/mongodb-org-tools-3.4.20-1.el7.x86_64.rpm
https://repo.mongodb.org/yum/redhat/7/mongodb-org/3.4/x86_64/RPMS/mongodb-org-shell-3.4.20-1.el7.x86_64.rpm
1.3 配置yum源
cat << EOF > /etc/yum.repos.d/mongodb3.4.20.repo
[mongodb3.4.20]
name=mongodb3.4.20
baseurl=ftp://192.168.0.1/pub/mongodb3.4.20/
enabled=1
gpgcheck=0
gpgkey=
EOF
1.4 安装mongodb
yum install -y mongodb-org-server
1.5 目录创建
mkdir -p /var/run/mongodb
mkdir -p /data/mongod{1..3}
mkdir -p /data/mongoc{1..3}
mkdir -p /etc/mongo
chown -R mongod.mongod /data
chown -R mongod.mongod /var/run/mongodb
1.5 创建集群秘钥
openssl rand -base64 756 > /etc/mongo/mongo.key
chown -R mongod.mongod /etc/mongo
chmod -R 600 /etc/mongo
scp -r /etc/mongo 192.168.0.2:/etc/
scp -r /etc/mongo 192.168.0.3:/etc/
scp -r /etc/mongo 192.168.0.4:/etc/
scp -r /etc/mongo 192.168.0.5:/etc/
副本集总体思路是用户名、密码和keyfile文件,keyfile需要各个副本集服务启动时加载而且要是同一文件,然后在操作库是需要用户名、密码
KeyFile文件必须满足条件:
(1)至少6个字符,小于1024字节,采base64编码,756生成出来刚好1024个字符的秘钥文件
(2)认证时候不考虑文件中空白字符
(3)连接到副本集的成员和mongos进成的keyfile文件内容必须一样
(4)必须是base64编码,但是不能有等号
(5)文件权限必须是x00,也就是说,不能分配任何权限给group成员和other成员
2 Config server部署
2.1 config server配置
生成三个config server的配置文件
cat << EOF > /etc/mongo/mongoc1.conf
systemLog:
destination: file
logAppend: true
logRotate: reopen
timeStampFormat: iso8601-local
path: /var/log/mongodb/mongoc1.log
storage:
dbPath: /data/mongoc1
journal:
enabled: true
mmapv1:
smallFiles: true
wiredTiger:
engineConfig:
cacheSizeGB: 1
directoryForIndexes: true
collectionConfig:
blockCompressor: zlib
indexConfig:
prefixCompression: true
directoryPerDB: true
processManagement:
fork: true
pidFilePath: /var/run/mongodb/mongoc1.pid
net:
port: 27019
bindIp: 0.0.0.0
setParameter:
enableLocalhostAuthBypass: true
security:
keyFile: /etc/mongo/mongo.key
authorization: enabled
replication:
replSetName: mongoc
oplogSizeMB: 1024
sharding:
clusterRole: configsvr
EOF
cat << EOF > /etc/mongo/mongoc2.conf
systemLog:
destination: file
logAppend: true
logRotate: reopen
timeStampFormat: iso8601-local
path: /var/log/mongodb/mongoc2.log
storage:
dbPath: /data/mongoc2
journal:
enabled: true
mmapv1:
smallFiles: true
wiredTiger:
engineConfig:
cacheSizeGB: 1
directoryForIndexes: true
collectionConfig:
blockCompressor: zlib
indexConfig:
prefixCompression: true
directoryPerDB: true
processManagement:
fork: true
pidFilePath: /var/run/mongodb/mongoc2.pid
net:
port: 27029
bindIp: 0.0.0.0
setParameter:
enableLocalhostAuthBypass: true
security:
keyFile: /etc/mongo/mongo.key
authorization: enabled
replication:
replSetName: mongoc
oplogSizeMB: 1024
sharding:
clusterRole: configsvr
EOF
cat << EOF > /etc/mongo/mongoc3.conf
systemLog:
destination: file
logAppend: true
logRotate: reopen
timeStampFormat: iso8601-local
path: /var/log/mongodb/mongoc3.log
storage:
dbPath: /data/mongoc3
journal:
enabled: true
mmapv1:
smallFiles: true
wiredTiger:
engineConfig:
cacheSizeGB: 1
directoryForIndexes: true
collectionConfig:
blockCompressor: zlib
indexConfig:
prefixCompression: true
directoryPerDB: true
processManagement:
fork: true
pidFilePath: /var/run/mongodb/mongoc3.pid
net:
port: 27039
bindIp: 0.0.0.0
setParameter:
enableLocalhostAuthBypass: true
security:
keyFile: /etc/mongo/mongo.key
authorization: enabled
replication:
replSetName: mongoc
oplogSizeMB: 1024
sharding:
clusterRole: configsvr
EOF
注意:
enableLocalhostAuthBypass: true //以便从本机可以进行初始化脚本执行,否则无法通过认证,报错
{
"ok" : 0,
"errmsg" : "not authorized on admin to execute command { replSetGetStatus: 1.0 }",
"code" : 13,
"codeName" : "Unauthorized"
}
2.2 修改配置文件权限
chown -R mongod.mongod /etc/mongo
2.3 启动服务
mongod -f /etc/mongo/mongoc1.conf
mongod -f /etc/mongo/mongoc2.conf
mongod -f /etc/mongo/mongoc3.conf
[root@localhost mongodb]# mongod -f /etc/mongo/mongoc1.conf
about to fork child process, waiting until server is ready for connections.
forked process: 10942
child process started successfully, parent exiting
[root@localhost mongodb]# mongod -f /etc/mongo/mongoc2.conf
mongod -f /etc/mongo/mongoc3.confabout to fork child process, waiting until server is ready for connections.
forked process: 10981
child process started successfully, parent exiting
[root@localhost mongodb]# mongod -f /etc/mongo/mongoc3.conf
about to fork child process, waiting until server is ready for connections.
forked process: 11016
child process started successfully, parent exiting
2.4 检查服务状态
[root@localhost mongodb]# netstat -anp|grep :270
tcp 0 0 0.0.0.0:27039 0.0.0.0:* LISTEN 11016/mongod
tcp 0 0 0.0.0.0:27019 0.0.0.0:* LISTEN 10942/mongod
tcp 0 0 0.0.0.0:27029 0.0.0.0:* LISTEN 10981/mongod
2.5 初始化
# mongo --port 27019
MongoDB shell version v3.4.20
connecting to: mongodb://127.0.0.1:27019/
MongoDB server version: 3.4.20
> use adminuse admin
switched to db admin
> rs.initiate(
{
_id: "mongoc",
version: 1,
protocolVersion: 1,
writeConcernMajorityJournalDefault: true,
configsvr: true,
members: [
{
_id: 0,
host: "192.168.0.1:27019",
arbiterOnly: false,
buildIndexes: true,
hidden: false,
priority: 66,
tags: {
mongoc: "YES"
},
slaveDelay: 0,
votes: 1
},
{
_id: 1,
host: "192.168.0.1:27029",
arbiterOnly: false,
buildIndexes: true,
hidden: false,
priority: 55,
tags: {
mongoc: "NO"
},
slaveDelay: 0,
votes: 1
},
{
_id: 2,
host: "192.168.0.1:27039",
arbiterOnly: false,
buildIndexes: true,
hidden: false,
priority: 33,
tags: {
mongoc: "NO"
},
slaveDelay: 0,
votes: 1
}
],
settings: {
chainingAllowed : true,
}
}
)
{ "ok" : 1 }
2.6 查看集群状态
mongoc:SECONDARY> rs.status()
{
"set" : "mongoc",
"date" : ISODate("2019-05-06T10:15:44.833Z"),
"myState" : 2,
"term" : NumberLong(0),
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"configsvr" : true,
"heartbeatIntervalMillis" : NumberLong(2000),
"optimes" : {
"lastCommittedOpTime" : {
"ts" : Timestamp(0, 0),
"t" : NumberLong(-1)
},
"appliedOpTime" : {
"ts" : Timestamp(1557137738, 1),
"t" : NumberLong(-1)
},
"durableOpTime" : {
"ts" : Timestamp(1557137738, 1),
"t" : NumberLong(-1)
}
},
"members" : [
{
"_id" : 0,
"name" : "192.168.0.1:27019",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 37,
"optime" : {
"ts" : Timestamp(1557137738, 1),
"t" : NumberLong(-1)
},
"optimeDate" : ISODate("2019-05-06T10:15:38Z"),
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "could not find member to sync from",
"configVersion" : 1,
"self" : true,
"lastHeartbeatMessage" : ""
},
{
"_id" : 1,
"name" : "192.168.0.1:27029",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 5,
"optime" : {
"ts" : Timestamp(1557137738, 1),
"t" : NumberLong(-1)
},
"optimeDurable" : {
"ts" : Timestamp(1557137738, 1),
"t" : NumberLong(-1)
},
"optimeDate" : ISODate("2019-05-06T10:15:38Z"),
"optimeDurableDate" : ISODate("2019-05-06T10:15:38Z"),
"lastHeartbeat" : ISODate("2019-05-06T10:15:44.029Z"),
"lastHeartbeatRecv" : ISODate("2019-05-06T10:15:41.100Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "",
"configVersion" : 1
},
{
"_id" : 2,
"name" : "192.168.0.1:27039",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 5,
"optime" : {
"ts" : Timestamp(1557137738, 1),
"t" : NumberLong(-1)
},
"optimeDurable" : {
"ts" : Timestamp(1557137738, 1),
"t" : NumberLong(-1)
},
"optimeDate" : ISODate("2019-05-06T10:15:38Z"),
"optimeDurableDate" : ISODate("2019-05-06T10:15:38Z"),
"lastHeartbeat" : ISODate("2019-05-06T10:15:44.030Z"),
"lastHeartbeatRecv" : ISODate("2019-05-06T10:15:41.097Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "",
"configVersion" : 1
}
],
"ok" : 1
}
3 Shard server部署
3.1 Shard server配置
生成三个shard server的配置文件
cat << EOF > /etc/mongo/mongod1.conf
systemLog:
destination: file
logAppend: true
logRotate: reopen
timeStampFormat: iso8601-local
path: /var/log/mongodb/mongod1.log
storage:
dbPath: /data/mongod1
journal:
enabled: true
mmapv1:
smallFiles: true
wiredTiger:
engineConfig:
cacheSizeGB: 1
directoryForIndexes: true
collectionConfig:
blockCompressor: zlib
indexConfig:
prefixCompression: true
directoryPerDB: true
processManagement:
fork: true
pidFilePath: /var/run/mongodb/mongod1.pid
net:
port: 27017
bindIp: 0.0.0.0
security:
keyFile: /etc/mongo/mongo.key
authorization: enabled
replication:
replSetName: shard1
sharding:
clusterRole: shardsvr
EOF
cat << EOF > /etc/mongo/mongod2.conf
systemLog:
destination: file
logAppend: true
logRotate: reopen
timeStampFormat: iso8601-local
path: /var/log/mongodb/mongod2.log
storage:
dbPath: /data/mongod2
journal:
enabled: true
mmapv1:
smallFiles: true
wiredTiger:
engineConfig:
cacheSizeGB: 1
directoryForIndexes: true
collectionConfig:
blockCompressor: zlib
indexConfig:
prefixCompression: true
directoryPerDB: true
processManagement:
fork: true
pidFilePath: /var/run/mongodb/mongod2.pid
net:
port: 27027
bindIp: 0.0.0.0
security:
keyFile: /etc/mongo/mongo.key
authorization: enabled
replication:
replSetName: shard2
sharding:
clusterRole: shardsvr
EOF
cat << EOF > /etc/mongo/mongod3.conf
systemLog:
destination: file
logAppend: true
logRotate: reopen
timeStampFormat: iso8601-local
path: /var/log/mongodb/mongod3.log
storage:
dbPath: /data/mongod3
journal:
enabled: true
mmapv1:
smallFiles: true
wiredTiger:
engineConfig:
cacheSizeGB: 1
directoryForIndexes: true
collectionConfig:
blockCompressor: zlib
indexConfig:
prefixCompression: true
directoryPerDB: true
processManagement:
fork: true
pidFilePath: /var/run/mongodb/mongod3.pid
net:
port: 27037
bindIp: 0.0.0.0
security:
keyFile: /etc/mongo/mongo.key
authorization: enabled
replication:
replSetName: shard3
sharding:
clusterRole: shardsvr
EOF
3.2 修改配置文件权限
chown -R mongod.mongod /etc/mongo
3.3 启动服务
mongod -f /etc/mongo/mongod1.conf
mongod -f /etc/mongo/mongod2.conf
mongod -f /etc/mongo/mongod3.conf
[root@localhost ~]# mongod -f /etc/mongo/mongod1.conf
mongod -f /etc/mongo/mongod2.conf
mongod -f /etc/mongo/mongod3.confabout to fork child process, waiting until server is ready for connections.
forked process: 9836
child process started successfully, parent exiting
[root@localhost ~]# mongod -f /etc/mongo/mongod2.conf
about to fork child process, waiting until server is ready for connections.
forked process: 9863
child process started successfully, parent exiting
[root@localhost ~]# mongod -f /etc/mongo/mongod3.conf
about to fork child process, waiting until server is ready for connections.
forked process: 9890
child process started successfully, parent exiting
3.4 检查服务状态
[root@localhost mongodb]# netstat -anp|grep :27
tcp 0 0 0.0.0.0:27037 0.0.0.0:* LISTEN 10660/mongod
tcp 0 0 0.0.0.0:27017 0.0.0.0:* LISTEN 10606/mongod
tcp 0 0 0.0.0.0:27027 0.0.0.0:* LISTEN 10633/mongod
3.5 初始化shard1集群
# mongo --port 27017
MongoDB shell version v3.4.20
connecting to: mongodb://127.0.0.1:27019/
MongoDB server version: 3.4.20
> use admin
switched to db admin
> rs.initiate(
{
_id: "shard1",
version: 1,
protocolVersion: 1,
writeConcernMajorityJournalDefault: true,
members: [
{
_id: 0,
host: "192.168.0.3:27017",
arbiterOnly: false,
buildIndexes: true,
hidden: false,
priority: 66,
tags: {
mongoc: "YES"
},
slaveDelay: 0,
votes: 1
},
{
_id: 1,
host: "192.168.0.4:27017",
arbiterOnly: false,
buildIndexes: true,
hidden: false,
priority: 55,
tags: {
mongoc: "NO"
},
slaveDelay: 0,
votes: 1
},
{
_id: 2,
host: "192.168.0.5:27017",
arbiterOnly: true,
buildIndexes: true,
hidden: false,
priority: 33,
slaveDelay: 0,
votes: 1
}
],
settings: {
chainingAllowed : true,
}
}
)
{ "ok" : 1 }
注意:
arbiters节点不能设置tags,如果设置会初始化报错:
{
"ok" : 0,
"errmsg" : "Cannot set tags on arbiters.",
"code" : 93,
"codeName" : "InvalidReplicaSetConfig"
}
3.6 查看shard1集群状态
shard1:PRIMARY> rs.status()
{
"set" : "shard1",
"date" : ISODate("2019-05-06T11:14:18.379Z"),
"myState" : 1,
"term" : NumberLong(1),
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"heartbeatIntervalMillis" : NumberLong(2000),
"optimes" : {
"lastCommittedOpTime" : {
"ts" : Timestamp(1557141251, 1),
"t" : NumberLong(1)
},
"appliedOpTime" : {
"ts" : Timestamp(1557141251, 1),
"t" : NumberLong(1)
},
"durableOpTime" : {
"ts" : Timestamp(1557141251, 1),
"t" : NumberLong(1)
}
},
"members" : [
{
"_id" : 0,
"name" : "192.168.0.3:27017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 2024,
"optime" : {
"ts" : Timestamp(1557141251, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2019-05-06T11:14:11Z"),
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "could not find member to sync from",
"electionTime" : Timestamp(1557141160, 1),
"electionDate" : ISODate("2019-05-06T11:12:40Z"),
"configVersion" : 1,
"self" : true,
"lastHeartbeatMessage" : ""
},
{
"_id" : 1,
"name" : "192.168.0.4:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 108,
"optime" : {
"ts" : Timestamp(1557141251, 1),
"t" : NumberLong(1)
},
"optimeDurable" : {
"ts" : Timestamp(1557141251, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2019-05-06T11:14:11Z"),
"optimeDurableDate" : ISODate("2019-05-06T11:14:11Z"),
"lastHeartbeat" : ISODate("2019-05-06T11:14:18.178Z"),
"lastHeartbeatRecv" : ISODate("2019-05-06T11:14:16.815Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncingTo" : "192.168.0.3:27017",
"syncSourceHost" : "192.168.0.3:27017",
"syncSourceId" : 0,
"infoMessage" : "",
"configVersion" : 1
},
{
"_id" : 2,
"name" : "192.168.0.5:27017",
"health" : 1,
"state" : 7,
"stateStr" : "ARBITER",
"uptime" : 108,
"lastHeartbeat" : ISODate("2019-05-06T11:14:18.180Z"),
"lastHeartbeatRecv" : ISODate("2019-05-06T11:14:16.729Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "",
"configVersion" : 1
}
],
"ok" : 1
}
3.7 初始化shard2集群
# mongo --port 27027
MongoDB shell version v3.4.20
connecting to: mongodb://127.0.0.1:27019/
MongoDB server version: 3.4.20
> use admin
switched to db admin
> rs.initiate(
{
_id: "shard2",
version: 1,
protocolVersion: 1,
writeConcernMajorityJournalDefault: true,
members: [
{
_id: 0,
host: "192.168.0.4:27027",
arbiterOnly: false,
buildIndexes: true,
hidden: false,
priority: 66,
tags: {
mongoc: "YES"
},
slaveDelay: 0,
votes: 1
},
{
_id: 1,
host: "192.168.0.5:27027",
arbiterOnly: false,
buildIndexes: true,
hidden: false,
priority: 55,
tags: {
mongoc: "NO"
},
slaveDelay: 0,
votes: 1
},
{
_id: 2,
host: "192.168.0.3:27027",
arbiterOnly: true,
buildIndexes: true,
hidden: false,
priority: 33,
slaveDelay: 0,
votes: 1
}
],
settings: {
chainingAllowed : true,
}
}
)
{ "ok" : 1 }
注意:
如果设置初始化报错,因为arbiters节点无法执行初始化,可以换一个非arbiters节点重新连接mongo进行初始化即可,建议在主节点上进行初始化:
{
"ok" : 0,
"errmsg" : "This node, 192.168.0.3:27027, with _id 2 is not electable under the new configuration version 1 for replica set shard2",
"code" : 93,
"codeName" : "InvalidReplicaSetConfig"
3.8 查看shard2集群状态
shard2:PRIMARY> rs.status()
{
"set" : "shard2",
"date" : ISODate("2019-05-06T12:24:18.775Z"),
"myState" : 1,
"term" : NumberLong(1),
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"heartbeatIntervalMillis" : NumberLong(2000),
"optimes" : {
"lastCommittedOpTime" : {
"ts" : Timestamp(1557145450, 1),
"t" : NumberLong(1)
},
"appliedOpTime" : {
"ts" : Timestamp(1557145450, 1),
"t" : NumberLong(1)
},
"durableOpTime" : {
"ts" : Timestamp(1557145450, 1),
"t" : NumberLong(1)
}
},
"members" : [
{
"_id" : 0,
"name" : "192.168.0.4:27027",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 81,
"optime" : {
"ts" : Timestamp(1557145450, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2019-05-06T12:24:10Z"),
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "could not find member to sync from",
"electionTime" : Timestamp(1557145448, 1),
"electionDate" : ISODate("2019-05-06T12:24:08Z"),
"configVersion" : 1,
"self" : true,
"lastHeartbeatMessage" : ""
},
{
"_id" : 1,
"name" : "192.168.0.5:27027",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 20,
"optime" : {
"ts" : Timestamp(1557145450, 1),
"t" : NumberLong(1)
},
"optimeDurable" : {
"ts" : Timestamp(1557145450, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2019-05-06T12:24:10Z"),
"optimeDurableDate" : ISODate("2019-05-06T12:24:10Z"),
"lastHeartbeat" : ISODate("2019-05-06T12:24:16.918Z"),
"lastHeartbeatRecv" : ISODate("2019-05-06T12:24:14.932Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncingTo" : "192.168.0.4:27027",
"syncSourceHost" : "192.168.0.4:27027",
"syncSourceId" : 0,
"infoMessage" : "",
"configVersion" : 1
},
{
"_id" : 2,
"name" : "192.168.0.3:27027",
"health" : 1,
"state" : 7,
"stateStr" : "ARBITER",
"uptime" : 20,
"lastHeartbeat" : ISODate("2019-05-06T12:24:16.921Z"),
"lastHeartbeatRecv" : ISODate("2019-05-06T12:24:14.879Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "",
"configVersion" : 1
}
],
"ok" : 1
}
3.9 初始化shard3集群
# mongo --port 27037
MongoDB shell version v3.4.20
connecting to: mongodb://127.0.0.1:27019/
MongoDB server version: 3.4.20
> use admin
switched to db admin
> rs.initiate(
{
_id: "shard3",
version: 1,
protocolVersion: 1,
writeConcernMajorityJournalDefault: true,
members: [
{
_id: 0,
host: "192.168.0.5:27037",
arbiterOnly: false,
buildIndexes: true,
hidden: false,
priority: 66,
tags: {
mongoc: "YES"
},
slaveDelay: 0,
votes: 1
},
{
_id: 1,
host: "192.168.0.3:27037",
arbiterOnly: false,
buildIndexes: true,
hidden: false,
priority: 55,
tags: {
mongoc: "NO"
},
slaveDelay: 0,
votes: 1
},
{
_id: 2,
host: "192.168.0.4:27037",
arbiterOnly: true,
buildIndexes: true,
hidden: false,
priority: 33,
slaveDelay: 0,
votes: 1
}
],
settings: {
chainingAllowed : true,
}
}
)
{ "ok" : 1 }
3.10 查看shard3集群状态
shard3:PRIMARY> rs.status()
{
"set" : "shard3",
"date" : ISODate("2019-05-06T12:05:16.024Z"),
"myState" : 2,
"term" : NumberLong(2),
"syncingTo" : "192.168.0.5:27037",
"syncSourceHost" : "192.168.0.5:27037",
"syncSourceId" : 0,
"heartbeatIntervalMillis" : NumberLong(2000),
"optimes" : {
"lastCommittedOpTime" : {
"ts" : Timestamp(1557144307, 1),
"t" : NumberLong(2)
},
"appliedOpTime" : {
"ts" : Timestamp(1557144307, 1),
"t" : NumberLong(2)
},
"durableOpTime" : {
"ts" : Timestamp(1557144307, 1),
"t" : NumberLong(2)
}
},
"members" : [
{
"_id" : 0,
"name" : "192.168.0.5:27037",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 82,
"optime" : {
"ts" : Timestamp(1557144307, 1),
"t" : NumberLong(2)
},
"optimeDurable" : {
"ts" : Timestamp(1557144307, 1),
"t" : NumberLong(2)
},
"optimeDate" : ISODate("2019-05-06T12:05:07Z"),
"optimeDurableDate" : ISODate("2019-05-06T12:05:07Z"),
"lastHeartbeat" : ISODate("2019-05-06T12:05:14.898Z"),
"lastHeartbeatRecv" : ISODate("2019-05-06T12:05:14.289Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "",
"electionTime" : Timestamp(1557144256, 1),
"electionDate" : ISODate("2019-05-06T12:04:16Z"),
"configVersion" : 1
},
{
"_id" : 1,
"name" : "192.168.0.3:27037",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 5081,
"optime" : {
"ts" : Timestamp(1557144307, 1),
"t" : NumberLong(2)
},
"optimeDate" : ISODate("2019-05-06T12:05:07Z"),
"syncingTo" : "192.168.0.5:27037",
"syncSourceHost" : "192.168.0.5:27037",
"syncSourceId" : 0,
"infoMessage" : "",
"configVersion" : 1,
"self" : true,
"lastHeartbeatMessage" : ""
},
{
"_id" : 2,
"name" : "192.168.0.4:27037",
"health" : 1,
"state" : 7,
"stateStr" : "ARBITER",
"uptime" : 82,
"lastHeartbeat" : ISODate("2019-05-06T12:05:14.898Z"),
"lastHeartbeatRecv" : ISODate("2019-05-06T12:05:15.690Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "",
"configVersion" : 1
}
],
"ok" : 1
}
4 Route部署
4.1 Route配置
cat << EOF > /etc/mongo/mongos1.conf
systemLog:
destination: file
logAppend: true
path: /var/log/mongodb/mongos1.log
processManagement:
fork: true
pidFilePath: /var/run/mongodb/mongos1.pid
net:
bindIp: 0.0.0.0
port: 27015
security:
keyFile: /etc/mongo/mongo.key
sharding:
configDB: mongoc/192.168.0.1:27019,192.168.0.1:27029,192.168.0.1:27039
EOF
cat << EOF > /etc/mongo/mongos2.conf
systemLog:
destination: file
logAppend: true
path: /var/log/mongodb/mongos2.log
processManagement:
fork: true
pidFilePath: /var/run/mongodb/mongos2.pid
net:
bindIp: 0.0.0.0
port: 27025
security:
keyFile: /etc/mongo/mongo.key
sharding:
configDB: mongoc/192.168.0.1:27019,192.168.0.1:27029,192.168.0.1:27039
EOF
cat << EOF > /etc/mongo/mongos3.conf
systemLog:
destination: file
logAppend: true
path: /var/log/mongodb/mongos3.log
processManagement:
fork: true
pidFilePath: /var/run/mongodb/mongos3.pid
net:
bindIp: 0.0.0.0
port: 27035
security:
keyFile: /etc/mongo/mongo.key
sharding:
configDB: mongoc/192.168.0.1:27019,192.168.0.1:27029,192.168.0.1:27039
EOF
4.2 修改配置文件权限
chown -R mongod.mongod /etc/mongo
4.3 启动服务
mongos -f /etc/mongo/mongos1.conf
mongos -f /etc/mongo/mongos2.conf
mongos -f /etc/mongo/mongos3.conf
[root@localhost mongo]# mongos -f /etc/mongo/mongos1.conf
about to fork child process, waiting until server is ready for connections.
forked process: 10720
child process started successfully, parent exiting
[root@localhost mongo]# mongos -f /etc/mongo/mongos2.conf
about to fork child process, waiting until server is ready for connections.
forked process: 10741
child process started successfully, parent exiting
[root@localhost mongo]# mongos -f /etc/mongo/mongos3.conf
about to fork child process, waiting until server is ready for connections.
forked process: 10761
child process started successfully, parent exiting注意:
route启动的命令是mongos,不是config或者shard server所用的mongod
4.4 检查服务状态
[root@localhost mongo]# netstat -anp|grep 27
tcp 0 0 0.0.0.0:27035 0.0.0.0:* LISTEN 10761/mongos
tcp 0 0 0.0.0.0:27015 0.0.0.0:* LISTEN 10720/mongos
tcp 0 0 0.0.0.0:27025 0.0.0.0:* LISTEN 10741/mongos
4.5 创建管理员用户
# mongo --port 27015
MongoDB shell version v3.4.20
connecting to: mongodb://127.0.0.1:27015/
MongoDB server version: 3.4.20
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
mongos> use admin
mongos> db.createUser(
{
user: "root",
pwd: "root12345",
roles: [ { role: "__system", db: "admin" } ]
}
)
Successfully added user: {
"user" : "root",
"roles" : [
{
"role" : "__system",
"db" : "admin"
}
]
}
4.6 使用新管理员用户登录
mongo -uroot -proot12345 --port 27015 --authenticationDatabase admin
4.7 添加shard
sh.addShard("shard1/192.168.0.3:27017,192.168.0.4:27017,192.168.0.5:27017")
sh.addShard("shard2/192.168.0.3:27027,192.168.0.4:27027,192.168.0.5:27027")
sh.addShard("shard3/192.168.0.3:27037,192.168.0.4:27037,192.168.0.5:27037")
mongos> sh.addShard("shard1/192.168.0.3:27017,192.168.0.4:27017,192.168.0.527017")sh.addShard("shard1/192.168.0.3:27017,192.168.0.4:27017,192.168.0.5:27017")
{ "shardAdded" : "shard1", "ok" : 1 }
mongos> sh.addShard("shard2/192.168.0.3:27027,192.168.0.4:27027,192.168.0.5:27027")sh.addShard("shard2/192.168.0.3:27027,192.168.0.4:27027,192.168.0.5:27027")
{ "shardAdded" : "shard2", "ok" : 1 }
mongos> sh.addShard("shard3/192.168.0.5:27037,10.124.3.119:27037,10.124.3.120:27037")sh.addShard("shard3/10.124.3.118:27037,192.168.0.4:27037,192.168.0.5:27037")
{ "shardAdded" : "shard3", "ok" : 1 }
4.8 查看状态
mongos> sh.status()
--- Sharding Status ---
sharding version: {
"_id" : 1,
"minCompatibleVersion" : 5,
"currentVersion" : 6,
"clusterId" : ObjectId("5cd00957fa232d6987f3c1de")
}
shards:
{ "_id" : "shard1", "host" : "shard1/192.168.0.3:27017,192.168.0.4:27017", "state" : 1 }
{ "_id" : "shard2", "host" : "shard2/192.168.0.4:27027,192.168.0.5:27027", "state" : 1 }
{ "_id" : "shard3", "host" : "shard3/192.168.0.3:27037,192.168.0.5:27037", "state" : 1 }
active mongoses:
"3.4.20" : 3
autosplit:
Currently enabled: yes
balancer:
Currently enabled: yes
Currently running: no
NaN
Failed balancer rounds in last 5 attempts: 0
Migration Results for the last 24 hours:
No recent migrations
databases: