由于业务迅速扩展,当前的分片已经不能满足要求,需要在mongodb线上在增加一个分片。
先设立好ip和域名的映射关系:
vim /etc/hosts
192.168.3.71 mongodb_shard3
先准备好3个mongodb进程,这里先做个模拟例子,所以在一台服务器上使用3个端口来模拟3个mongodb实例(生产环境一般都是3台linux服务器,一台linux服务器一个mongodb实例)。
# 建立mongodb进程数据目录 mkdir -p /data/mongodb/shard37017 mkdir -p /data/mongodb/shard37027 mkdir -p /data/mongodb/shard37037
# 建立mongodb日志目录 mkdir -p /data/mongodb/logs
# 开始启动3个mongodb实例,启动的时候,指定replset名称为shard3,启动方式如下: /usr/local/mongodb-linux-x86_64-3.0.3/bin/mongod --shardsvr --replSet shard3 --port 37017 --dbpath /data/mongodb/shard37017 --oplogSize 2048 --logpath /data/mongodb/logs/shard_m2_37017.log --logappend --fork /usr/local/mongodb-linux-x86_64-3.0.3/bin/mongod --shardsvr --replSet shard3 --port 37027 --dbpath /data/mongodb/shard37027 --oplogSize 2048 --logpath /data/mongodb/logs/shard_m2_37027.log --logappend --fork /usr/local/mongodb-linux-x86_64-3.0.3/bin/mongod --shardsvr --replSet shard3 --port 37037 --dbpath /data/mongodb/shard37037 --oplogSize 2048 --logpath /data/mongodb/logs/shard_m2_37037.log --logappend --fork
|
mongodb启动完,进程如下所示:
# 查看后台启动的mongodb进程 [mongodb@master2 ~]$ ps -eaf|grep mongo root 32652 752 0 17:38 pts/0 00:00:00 su - mongodb mongodb 32653 32652 0 17:38 pts/0 00:00:00 -bash mongodb 33495 1 0 17:39 ? 00:00:00 /usr/local/mongodb-linux-x86_64-3.0.3/bin/mongod --shardsvr --replSet shard3 --port 37017 --dbpath /data/mongodb/shard37017 --oplogSize 2048 --logpath /data/mongodb/logs/shard_m2_37017.log --logappend --fork mongodb 33628 1 0 17:40 ? 00:00:01 /usr/local/mongodb-linux-x86_64-3.0.3/bin/mongod --shardsvr --replSet shard3 --port 37027 --dbpath /data/mongodb/shard37027 --oplogSize 2048 --logpath /data/mongodb/logs/shard_m2_37027.log --logappend --fork mongodb 33780 1 0 17:40 ? 00:00:00 /usr/local/mongodb-linux-x86_64-3.0.3/bin/mongod --shardsvr --replSet shard3 --port 37037 --dbpath /data/mongodb/shard37037 --oplogSize 2048 --logpath /data/mongodb/logs/shard_m2_37037.log --logappend --fork mongodb 33889 32653 0 17:41 pts/0 00:00:00 ps -eaf mongodb 33890 32653 0 17:41 pts/0 00:00:00 grep mongo [mongodb@master2 ~]$
|
# 设置第3个分片
>config = { _id:"shard3", members:[
{_id:0,host:"mongodb_shard3:37017",priority:1},
{_id:1,host:"mongodb_shard3:37027",priority:2},
{_id:2,host:"mongodb_shard3:37037",arbiterOnly:true}
]
};
# 初始化副本集
>rs.initiate(config);
# 执行过程如下:
[mongodb@master2 ~]$ /usr/local/mongodb-linux-x86_64-3.0.3/bin/mongo mongodb_shard3:37017/admin MongoDB shell version: 3.0.3 connecting to: mongodb_shard3:37017/admin Server has startup warnings: 2016-07-08T17:39:51.888+0800 I CONTROL [initandlisten] 2016-07-08T17:39:51.888+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'. 2016-07-08T17:39:51.888+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never' 2016-07-08T17:39:51.888+0800 I CONTROL [initandlisten] 2016-07-08T17:39:51.888+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'. 2016-07-08T17:39:51.888+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never' 2016-07-08T17:39:51.888+0800 I CONTROL [initandlisten] > > config = { _id:"shard3", members:[ ... {_id:0,host:"mongodb_shard3:37017",priority:1}, ... {_id:1,host:"mongodb_shard3:37027",priority:2}, ... {_id:2,host:"mongodb_shard3:37037",arbiterOnly:true} ... ] ... }; { "_id" : "shard3", "members" : [ { "_id" : 0, "host" : "mongodb_shard3:37017", "priority" : 1 }, { "_id" : 1, "host" : "mongodb_shard3:37027", "priority" : 2 }, { "_id" : 2, "host" : "mongodb_shard3:37037", "arbiterOnly" : true } ] } > rs.initiate(config); { "ok" : 1 } shard3:OTHER>
|
在线添加命令:db.runCommand({ addshard :"shard3/mongodb_shard3:37017,mongodb_shard3:37027,mongodb_shard3:37037"});
执行过程如下:
[mongodb@master2 ~]$ /usr/local/mongodb-linux-x86_64-3.0.3/bin/mongo mongodbs1:30000/admin MongoDB shell version: 3.0.3 connecting to: mongodbs1:30000/admin mongos> db.runCommand( { addshard : "shard3/mongodb_shard3:37017,mongodb_shard3:37027,mongodb_shard3:37037"}); { "shardAdded" : "shard3", "ok" : 1 } mongos>
|
问题记录:
mongos> db.runCommand( { addshard :"shard3/mongodb_shard3:37017,mongodb_shard3:37027,mongodb_shard3:37037"});
{
"ok": 0,
"errmsg": "couldn't connect to new shard socket exception [CONNECT_ERROR] for shard3/mongodb_shard3:37017,mongodb_shard3:37027,mongodb_shard3:37037"
}
mongos>
mongos> rs.initiate(config);
{"ok" : 0, "errmsg" : "no such cmd:replSetInitiate" }
mongos>
PS:这个是在mongod进程中执行,不是在mongos里面执行。
查看命令:db.printShardingStatus();
[mongodb@slave1 logs]$ /usr/local/mongodb-linux-x86_64-3.0.3/bin/mongo mongodbs1:30000/admin MongoDB shell version: 3.0.3 connecting to: localhost:30000/admin mongos> mongos> db.printShardingStatus(); 2016-07-08T21:24:22.317+0800 E QUERY Error: error: { "$err" : "could not initialize sharding on connection shard3/mongodb_shard3:37017,mongodb_shard3:37027 :: caused by :: v3.0 mongod is incompatible with v2.6 mongos, a v2.6 mongos may be running in the v3.0 cluster at 192.168.3.62", "code" : 15907 } at Error (<anonymous>) at DBQuery.next (src/mongo/shell/query.js:259:15) at DBCollection.findOne (src/mongo/shell/collection.js:189:22) at printShardingStatus (src/mongo/shell/shardingtest.js:659:55) at DB.printShardingStatus (src/mongo/shell/db.js:988:5) at (shell):1:4 at src/mongo/shell/query.js:259 mongos> |
PS:看到报错信息,因为新的分片是3.0+版本的,但是mongodb分片集群确实2.4.4的低版本,所以虽然addShard成功了,但是没有整合到一起去。解决办法是,启动3个mongodb进程使用低版本2.4.4就ok了。
参考文章地址:https://docs.mongodb.com/manual/reference/command/addShard/