开始配置 config server,MongoDB 从3.2版本之后开始规定 config server 也必须要开启副本集功能。
config server 的配置文件如下:config server 一般情况下是监听在 27019 端口
# mongod.conf
# for documentation of all options, see:
#
# 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: 27019
bindIp: 0.0.0.0 # Listen to local interface only, comment to listen on all interfaces.
#security:
#operationProfiling:
replication:
replSetName: cfgReplSet
sharding:
clusterRole: configsvr
## Enterprise-Only Options
#auditLog:
#snmp:
启动三台config server 的mongod 服务
[root@test4 ~]# service mongod start
Starting mongod: [ OK ]
[root@test5 ~]# service mongod start
Starting mongod: [ OK ]
[root@test6 ~]# service mongod start
Starting mongod: [ OK ]
同样,config server 的副本集配置如上文所示,这里的代码就省略了。
配置启动 mongos 路由主机
[root@test7 ~]# mongos --configdb cfgReplSet/test4.lan,test5.lan,test6.lan:27019 --logpath /var/log/mongodb/mongos.log --fork --port 30000
about to fork child process, waiting until server is ready for connections.
forked process: 3338
child process started successfully, parent exiting
MongoDB 版本 >3.2 启动mongos 的时候,需要跟上 config server 的副本集名称 (这里是 cfgReplSet)
连接 mongos 测试
[root@test7 ~]# mongo test7.lan:30000
MongoDB shell version v3.4.4
connecting to: test7.lan:30000
MongoDB server version: 3.4.4
Server has startup warnings:
2017-04-24T23:30:47.285+0800 I CONTROL [main]
2017-04-24T23:30:47.285+0800 I CONTROL [main] ** WARNING: Access control is not enabled for the database.
2017-04-24T23:30:47.285+0800 I CONTROL [main] ** Read and write access to data and configuration is unrestricted.
2017-04-24T23:30:47.285+0800 I CONTROL [main] ** WARNING: You are running this process as the root user, which is not recommended.
2017-04-24T23:30:47.285+0800 I CONTROL [main]
mongos> show dbs
admin 0.000GB
config 0.000GB
mongos> use config
switched to db config
mongos> show collections
chunks
lockpings
locks
migrations
mongos
shards
tags
version
mongos> db.shards.find()
# 这里没有返回文档,也说明分片集群中并没有添加可用分片集群。
配置分片集群:shard