分片搭建–分片配置
添加配置文件shard1.conf(三臺機器都操做)web
vim /etc/mongod/shard1.conf //加入以下內容 pidfilepath = /var/run/mongodb/shard1.pid dbpath = /data/mongodb/shard1/data logpath = /data/mongodb/shard1/log/shard1.log logappend = true bind_ip = 0.0.0.0 ##生產環境,最好設爲綁定ip本機;由於咱們沒有設密碼 port = 27001 fork = true httpinterface=true #打開web監控 rest=true replSet=shard1 #副本集名稱 shardsvr = true #declare this is a shard db of a cluster; maxConns=20000 #設置最大鏈接數
添加配置文件shard2.conf(三臺機器都操做)mongodb
vim /etc/mongod/shard2.conf //加入以下內容 pidfilepath = /var/run/mongodb/shard2.pid dbpath = /data/mongodb/shard2/data logpath = /data/mongodb/shard2/log/shard2.log logappend = true bind_ip = 0.0.0.0 port = 27002 fork = true httpinterface=true #打開web監控 rest=true replSet=shard2 #副本集名稱 shardsvr = true #declare this is a shard db of a cluster; maxConns=20000 #設置最大鏈接數
添加配置文件shard3.conf(三臺機器都操做)shell
vim /etc/mongod/shard3.conf //加入以下內容 pidfilepath = /var/run/mongodb/shard3.pid dbpath = /data/mongodb/shard3/data logpath = /data/mongodb/shard3/log/shard3.log logappend = true bind_ip = 0.0.0.0 port = 27003 fork = true httpinterface=true #打開web監控 rest=true replSet=shard3 #副本集名稱 shardsvr = true #declare this is a shard db of a cluster; maxConns=20000 #設置最大鏈接數
啓動shard1vim
mongod -f /etc/mongod/shard1.conf //三臺機器都要操做 登陸130或者132任何一臺機器的27001端口初始化副本集,133之因此不行,是由於shard1咱們把133這臺機器的27001端口做爲了仲裁節點 mongo --port 27001 use admin config = { _id: "shard1", members: [ {_id : 0, host : "192.168.133.130:27001"}, {_id: 1,host : "192.168.133.132:27001"},{_id : 2, host : "192.168.133.133:27001",arbiterOnly:true}] } rs.initiate(config)
啓動shard2服務器
mongod -f /etc/mongod/shard2.conf //三臺機器都要操做 登陸132或者133任何一臺機器的27002端口初始化副本集,130之因此不行,是由於shard2咱們把130這臺機器的27002端口做爲了仲裁節點 mongo --port 27002 use admin config = { _id: "shard2", members: [ {_id : 0, host : "192.168.133.130:27002" ,arbiterOnly:true},{_id : 1, host : "192.168.133.132:27002"},{_id : 2, host : "192.168.133.133:27002"}] } rs.initiate(config)
啓動shard3app
mongod -f /etc/mongod/shard3.conf //三臺機器都要操做 登陸130或者133任何一臺機器的27003端口初始化副本集,132之因此不行,是由於shard3咱們把132這臺機器的27003端口做爲了仲裁節點 mongo --port 27003 use admin config = { _id: "shard3", members: [ {_id : 0, host : "192.168.133.130:27003"}, {_id : 1, host : "192.168.133.132:27003", arbiterOnly:true}, {_id : 2, host : "192.168.133.133:27003"}] } rs.initiate(config)
完成了副本集shard一、二、3的操做以後,接下來就是配置路由服務器;this
分片搭建–配置路由服務器rest
分片搭建–啓用分片code
[root@Dasoncheng ~]# cat /etc/mongod/mongos.conf pidfilepath = /var/run/mongodb/mongos.pid logpath = /data/mongodb/mongos/log/mongos.log logappend = true bind_ip = 0.0.0.0 port = 20000 fork = true configdb = configs/192.168.60.11:21000,192.168.60.12:21000,192.168.60.13:21000 #監聽的配置服務器,只能有1個或者3個,configs爲配置服務器的副本集名字 maxConns=20000 #設置最大鏈接數 [root@Dasoncheng ~]# mongos -f /etc/mongod/mongos.conf about to fork child process, waiting until server is ready for connections. forked process: 3379 child process started successfully, parent exiting [root@Dasoncheng ~]# mongo --port 20000 MongoDB shell version v3.4.9 connecting to: mongodb://127.0.0.1:20000/ MongoDB server version: 3.4.9 mongos> sh.addShard("shard1/192.168.60.11:27001,192.168.60.12:27001,192.168.60.13:27001") { "shardAdded" : "shard1", "ok" : 1 } mongos> sh.addShard("shard2/192.168.60.11:27002,192.168.60.12:27002,192.168.60.13:27002") { "shardAdded" : "shard2", "ok" : 1 } mongos> sh.addShard("shard3/192.168.60.11:27003,192.168.60.12:27003,192.168.60.13:27003") { "shardAdded" : "shard3", "ok" : 1 } mongos> sh.status() --- Sharding Status --- sharding version: { "_id" : 1, "minCompatibleVersion" : 5, "currentVersion" : 6, "clusterId" : ObjectId("59e957b4b1833892fcc37ec7") } shards: { "_id" : "shard1", "host" : "shard1/192.168.60.11:27001,192.168.60.12:27001", "state" : 1 } { "_id" : "shard2", "host" : "shard2/192.168.60.12:27002,192.168.60.13:27002", "state" : 1 } { "_id" : "shard3", "host" : "shard3/192.168.60.11:27003,192.168.60.13:27003", "state" : 1 } active mongoses: "3.4.9" : 1 autosplit: Currently enabled: yes balancer: Currently enabled: yes Currently running: no Balancer lock taken at Fri Oct 20 2017 14:44:12 GMT+0800 (CST) by ConfigServer:Balancer Failed balancer rounds in last 5 attempts: 0 Migration Results for the last 24 hours: No recent migrations databases:
有一個小錯誤:server
[root@Dasoncheng ~]# mongos -f /etc/mongod/mongos.conf ##啓動mongos報錯,只有60.11啓動了;根據報錯提示 查看了mongos.conf發現逗號","後面多了一個空格;我刪掉從新啓動 又報錯 ,看下面 2017-10-20T15:38:02.214+0800 I NETWORK [main] getaddrinfo(" 192.168.60.12") failed: Name or service not known 2017-10-20T15:38:02.330+0800 I NETWORK [main] getaddrinfo(" 192.168.60.13") failed: Name or service not known about to fork child process, waiting until server is ready for connections. forked process: 3316 child process started successfully, parent exiting [root@Dasoncheng ~]# cat /etc/mongod/mongos.conf pidfilepath = /var/run/mongodb/mongos.pid logpath = /data/mongodb/mongos/log/mongos.log logappend = true bind_ip = 0.0.0.0 port = 20000 fork = true configdb = configs/192.168.60.11:21000, 192.168.60.12:21000, 192.168.60.13:21000 #監聽的配置服務器,只能有1個或者3個,configs爲配置服務器的副本集名字 maxConns=20000 #設置最大鏈接數 [root@Dasoncheng ~]# !v vim /etc/mongod/mongos.conf [root@Dasoncheng ~]# mongos -f /etc/mongod/mongos.conf ##在這裏啓動報錯,我就把進程mongos殺死了,pkill mongos=killall monogs about to fork child process, waiting until server is ready for connections. forked process: 3354 ERROR: child process failed, exited with error number 48 [root@Dasoncheng ~]# pkill mongos [root@Dasoncheng ~]# mongos -f /etc/mongod/mongos.conf about to fork child process, waiting until server is ready for connections. forked process: 3379 child process started successfully, parent exiting ##啓動成功;