(案例1)副本集+分片部署
机器角色分配和拓扑环境例如以下:
-------------------配置副本集s1-------------------------------
1.创建文件夹
在s1h1上创建例如以下文件夹
[root@node3 db]# mkdir -p /db/s1/conf
[root@node3 db]# mkdir -p /db/s1/data
[root@node3 db]# mkdir -p /db/s1/log
在在s1h2上创建例如以下文件夹
[root@node4 db]# mkdir -p /db/s1/conf
[root@node4 db]# mkdir -p /db/s1/data
[root@node4 db]# mkdir -p /db/s1/log
2.创建配置文件
在s1h1和s1h2上创建s1.cnf,文件保存在conf文件夹下
port = 27017
fork = true
dbpath = /db/s1/data
logpath = /db/s1/log/logs
logappend = true
shardsvr = true
replSet = s1
3.安装mongodb
在在s1h1和s1h2上做相同的操作
[root@node3 soft]# tar -xvf mongodb-linux-x86_64-2.6.5.tgz
[root@node3 soft]# mv mongodb-linux-x86_64-2.6.5 /db/s1/mongodb
4.启动各节点
在每一个节点上做相同的操作
[root@node3 bin]# ./mongod -f /db/s1/conf/s1.cnf
[root@node4 bin]# ./mongod -f /db/s1/conf/s1.cnf
5.初始化副本集
登陆副本集中的当中一台机器,运行例如以下操,我这里登陆是的s1h1
[root@node3 bin]# ./mongo 192.168.56.103:27017
> use admin
>config={_id:"s1", members:[{_id:0,host:"192.168.56.103:27017"},{_id:1,host:"192.168.56.104:27017"}]}
>rs.initiate(config);
退出又一次登陆能够看到该机器为主节点
[root@node3 bin]# ./mongo 192.168.56.103:27017
MongoDB shell version: 2.6.5
connecting to: 192.168.56.103:27017/test
s1:PRIMARY>
登陆还有一台机器s1h2能够看到,该机器为该副本集的从节点
[root@node4 bin]# ./mongo 192.168.56.104:27017
MongoDB shell version: 2.6.5
connecting to: 192.168.56.104:27017/test
s1:SECONDARY>
到这里副本集s1配置完毕
-------------------配置副本集s2-------------------------------
1.创建文件夹
在s2h1上创建例如以下文件夹
[root@node3 db]# mkdir -p /db/s2/conf
[root@node3 db]# mkdir -p /db/s2/data
[root@node3 db]# mkdir -p /db/s2/log
在在s2h2上创建例如以下文件夹
[root@node4 db]# mkdir -p /db/s2/conf
[root@node4 db]# mkdir -p /db/s2/data
[root@node4 db]# mkdir -p /db/s2/log
2.创建配置文件
在s2h1和s2h2上创建s2.cnf,文件保存在conf文件夹下
port = 27018
fork = true
dbpath = /db/s2/data
logpath = /db/s2/log/logs
logappend = true
shardsvr = true
replSet = s2
3.安装mongodb
在在s2h1和s2h2上做相同的操作
[root@node3 soft]# tar -xvf mongodb-linux-x86_64-2.6.5.tgz
[root@node3 soft]# mv mongodb-linux-x86_64-2.6.5 /db/s2/mongodb
4.启动各节点
在每一个节点上做相同的操作
[root@node3 bin]# ./mongod -f /db/s2/conf/s2.cnf
[root@node4 bin]# ./mongod -f /db/s2/conf/s2.cnf
5.初始化副本集
登陆副本集中的当中一台机器,运行例如以下操,我这里登陆是的s2h1
[root@node3 bin]# ./mongo 192.168.56.103:27018
> use admin
>config={_id:"s2", members:[{_id:0,host:"192.168.56.103:27018"},{_id:1,host:"192.168.56.104:27018"}]}
>rs.initiate(config);
登陆s2h1能够看到,该节点为主节点
[root@node3 bin]# ./mongo 192.168.56.103:27018
MongoDB shell version: 2.6.5
connecting to: 192.168.56.103:27018/test
s2:PRIMARY>
登陆s2h2能够看到。该节点为副节点
[root@node4 bin]# ./mongo 192.168.56.104:27018
MongoDB shell version: 2.6.5
connecting to: 192.168.56.104:27018/test
s2:SECONDARY>
到这里副本集s2配置完毕
-------------------部署配置服务器01-------------------------------
1.创建文件夹
mkdir -p /db/config_server01/data
mkdir -p /db/config_server01/log
mkdir -p /db/config_server01/conf
2.安装mongodb
[root@node1 soft]# tar -xvf mongodb-linux-x86_64-2.6.5.tgz
[root@node1 soft]# mv mongodb-linux-x86_64-2.6.5 /db/config_server01/mongodb
3.创建配置文件
配置參数文件为config_server.cnf,内容例如以下:
port = 27017
fork = true
dbpath = /db/config_server01/data
logpath = /db/config_server01/log/logs
logappend = true
configsvr = true
directoryperdb = true
4.启动
#./mongod -f /db/config_server01/conf/config_server.cnf
-------------------部署配置服务器02-------------------------------
1.创建文件夹
mkdir -p /db/config_server02/data
mkdir -p /db/config_server02/log
mkdir -p /db/config_server02/conf
2.安装mongodb
[root@node1 soft]# tar -xvf mongodb-linux-x86_64-2.6.5.tgz
[root@node1 soft]# mv mongodb-linux-x86_64-2.6.5 /db/config_server02/mongodb
3.创建配置文件
配置參数文件为config_server.cnf,内容例如以下:
port = 27017
fork = true
dbpath = /db/config_server02/data
logpath = /db/config_server02/log/logs
logappend = true
configsvr = true
directoryperdb = true
4.启动
#./mongod -f /db/config_server02/conf/config_server.cnf
-------------------部署配置服务器03-------------------------------
1.创建文件夹
mkdir -p /db/config_server03/data
mkdir -p /db/config_server03/log
mkdir -p /db/config_server03/conf
2.安装mongodb
[root@node1 soft]# tar -xvf mongodb-linux-x86_64-2.6.5.tgz
[root@node1 soft]# mv mongodb-linux-x86_64-2.6.5 /db/config_server03/mongodb
3.创建配置文件
配置參数文件为config_server.cnf,内容例如以下:
port = 27017
fork = true
dbpath = /db/config_server03/data
logpath = /db/config_server03/log/logs
logappend = true
configsvr = true
directoryperdb = true
4.启动
#./mongod -f /db/config_server03/conf/config_server.cnf
-------------------部署路由服务器01-------------------------------
1.创建文件夹
mkdir -p /db/route_server01/data
mkdir -p /db/route_server01/log
mkdir -p /db/route_server01/conf
2.安装mongodb
[root@node2 soft]# tar -xvf mongodb-linux-x86_64-2.6.5.tgz
[root@node2 soft]# mv mongodb-linux-x86_64-2.6.5 /db/route_server01/mongodb
3.创建配置文件
配置參数文件为route_server.cnf,内容例如以下:
port = 27017
configdb=192.168.56.101:27017,192.168.56.101:27018,192.168.56.101:27019
logpath = /db/route_server01/log/logs
fork = true
4.启动
[root@node2 bin]# ./mongos -f /db/route_server01/conf/route_server.cnf
5.加入分片服务器
./mongo
mongos>use admin
mongos>db.runCommand({"addshard":"s1/192.168.56.103:27017,192.168.56.104:27017",allowLocal:true})
mongos>db.runCommand({"addshard":"s2/192.168.56.103:27018,192.168.56.104:27018",allowLocal:true})
6.查看分片情况
mongos> db.runCommand({listshards:1});db.runCommand({listshards:1});
{
"shards" : [
{
"_id" : "s1",
"host" : "s1/192.168.56.103:27017,192.168.56.104:27017"
},
{
"_id" : "s2",
"host" : "s2/192.168.56.103:27018,192.168.56.104:27018"
}
],
"ok" : 1
}
步骤5和步骤6仅仅须要在第一台路由服务器上运行就可以.
-------------------部署路由服务器02-------------------------------
1.创建文件夹
mkdir -p /db/route_server02/data
mkdir -p /db/route_server02/log
mkdir -p /db/route_server02/conf
2.安装mongodb
[root@node2 soft]# tar -xvf mongodb-linux-x86_64-2.6.5.tgz
[root@node2 soft]# mv mongodb-linux-x86_64-2.6.5 /db/route_server02/mongodb
3.创建配置文件
配置參数文件为route_server.cnf,内容例如以下:
port = 27018
configdb=192.168.56.101:27017,192.168.56.101:27018,192.168.56.101:27019
logpath = /db/route_server02/log/logs
fork = true
4.启动
[root@node2 bin]# ./mongos -f /db/route_server02/conf/route_server.cnf
-------------------部署路由服务器03-------------------------------
1.创建文件夹
mkdir -p /db/route_server03/data
mkdir -p /db/route_server03/log
mkdir -p /db/route_server03/conf
2.安装mongodb
[root@node2 soft]# tar -xvf mongodb-linux-x86_64-2.6.5.tgz
[root@node2 soft]# mv mongodb-linux-x86_64-2.6.5 /db/route_server03/mongodb
3.创建配置文件
配置參数文件为route_server.cnf,内容例如以下:
port = 27019
configdb=192.168.56.101:27017,192.168.56.101:27018,192.168.56.101:27019
logpath = /db/route_server03/log/logs
fork = true
4.启动
[root@node2 bin]# ./mongos -f /db/route_server03/conf/route_server.cnf
----------測试--------------------------
1.在路由服务器上运行
数据库hxl启用分片功能
mongos>db.runCommand({"enablesharding":"hxl"});
2.为集合person设置片键,这里使用id做hash分片
mongos>use admin
mongos>db.runCommand({"shardcollection":"hxl.person","key":{"_id":"hashed"}})
{ "collectionsharded" : "hxl.person", "ok" : 1 }
3.写入数据
mongos>use hxl
mongos>for (var i=0;i<100000;i++){db.person.insert({"name":"hxl"+i,"age":i})}
mongos> db.person.count();
4.查看数据分布情况
mongos> db.person.stats()
{
"sharded" : true,
"systemFlags" : 1,
"userFlags" : 1,
"ns" : "hxl.person",
"count" : 100000,
"numExtents" : 12,
"size" : 11200000,
"storageSize" : 22364160,
"totalIndexSize" : 7636384,
"indexSizes" : {
"_id_" : 3254048,
"_id_hashed" : 4382336
},
"avgObjSize" : 112,
"nindexes" : 2,
"nchunks" : 4,
"shards" : {
"s1" : {
"ns" : "hxl.person",
"count" : 49981,
"size" : 5597872,
"avgObjSize" : 112,
"storageSize" : 11182080,
"numExtents" : 6,
"nindexes" : 2,
"lastExtentSize" : 8388608,
"paddingFactor" : 1,
"systemFlags" : 1,
"userFlags" : 1,
"totalIndexSize" : 3826368,
"indexSizes" : {
"_id_" : 1627024,
"_id_hashed" : 2199344
},
"ok" : 1
},
"s2" : {
"ns" : "hxl.person",
"count" : 50019,
"size" : 5602128,
"avgObjSize" : 112,
"storageSize" : 11182080,
"numExtents" : 6,
"nindexes" : 2,
"lastExtentSize" : 8388608,
"paddingFactor" : 1,
"systemFlags" : 1,
"userFlags" : 1,
"totalIndexSize" : 3810016,
"indexSizes" : {
"_id_" : 1627024,
"_id_hashed" : 2182992
},
"ok" : 1
}
},
"ok" : 1
}
mongos>
能够看到刚才写入的10万条记录已经进行了分片,当中s1分配了49981,s2分配了50019.
集群启动顺序:
1.启动副本集s1
192.168.56.103:27017
192.168.56.104:27017
[root@node3 bin]# ./mongod -f /db/s1/conf/s1.cnf
[root@node4 bin]# ./mongod -f /db/s1/conf/s1.cnf
2.启动副本集s2
192.168.56.103:27018
192.168.56.104:27018
[root@node3 bin]# ./mongod -f /db/s2/conf/s2.cnf
[root@node4 bin]# ./mongod -f /db/s2/conf/s2.cnf
3.启动配置服务器
192.168.56.101
启动
#./mongod -f /db/config_server/conf/config_server.cnf
4.启动路由服务器
192.168.56.102
启动
[root@node2 bin]# ./mongos -f /db/route_server/conf/route_server.cnf
问题
1.从副本集默认是不可读取的,须要设置进行可读取.
s1:SECONDARY> show tables
2016-03-21T16:21:57.258+0800 error: { "$err" : "not master and slaveOk=false", "code" : 13435 } at src/mongo/shell/query.js:131
s1:SECONDARY> db.getMongo().setSlaveOk();
2.遇到配置服务器不同步的情况下,须要将之前的配置服务器的config文件复制到其它配置服务器的对应路径
2016-03-29T16:36:09.927+0800 [mongosMain] ERROR: could not verify that config servers are in sync :: caused by :: config servers 192
.168.56.101:27017 and 192.168.56.101:27018 differ: { chunks: "458d90243d2642fcdadcbd6ea0dea87a", collections: "7afff57cb1d2cdca4eaed
f6c462c96cd", databases: "8659f3bdedc73744e099a248f94854c0", shards: "6be1adb78a41e5ab733bd10119b0ce1d", version: "bf167dc1df1dac1f
3.配置服务器必须是1个或是3个,或基数个,否则路由服务器无法启动.