环境
配置服务器:
127.0.1.1:27017
127.0.1.1:27018
127.0.1.1:27019
路由服务器
127.0.0.1:27020
分片服务器
127.0.0.1:27021
127.0.0.1:27022
db:3.4.4
------------------------配置服务器---------------------------
1.配置服务器
程序路径
mkdir -P /opt/mongodb/config_server/c1/mongodb344
mkidr -P /opt/mongodb/config_server/c2/mongodb344
mkdir -p /opt/mongodb/config_server/c3/mongodb344
数据文件路径
mkdir -P /opt/mongodb/config_server/c1/mongodb344/data
mkidr -P /opt/mongodb/config_server/c2/mongodb344/data
mkdir -p /opt/mongodb/config_server/c3/mongodb344/data
配置文件路径
mkdir -P /opt/mongodb/config_server/c1/mongodb344/conf
mkidr -P /opt/mongodb/config_server/c2/mongodb344/conf
mkdir -p /opt/mongodb/config_server/c3/mongodb344/conf
日志文件路径
mkdir -P /opt/mongodb/config_server/c1/mongodb344/log
mkdir -P /opt/mongodb/config_server/c2/mongodb344/log
mkdir -P /opt/mongodb/config_server/c3/mongodb344/log
2.配置文件
[root@izwz9gxsbwybwg9n5xdi47z conf]# more config_server.cnf
port = 27017
fork = true
dbpath = /opt/mongodb/config_server/c1/mongodb344/data
logpath = /opt/mongodb/config_server/c1/mongodb344/log/logs
logappend = true
configsvr = true
directoryperdb = true
replSet=configdb
[root@izwz9gxsbwybwg9n5xdi47z conf]# more config_server.cnf
port = 27018
fork = true
dbpath = /opt/mongodb/config_server/c2/mongodb344/data
logpath = /opt/mongodb/config_server/c2/mongodb344/log/logs
logappend = true
configsvr = true
directoryperdb = true
replSet=configdb
[root@izwz9gxsbwybwg9n5xdi47z conf]# more config_server.cnf
port = 27019
fork = true
dbpath = /opt/mongodb/config_server/c3/mongodb344/data
logpath = /opt/mongodb/config_server/c3/mongodb344/log/logs
logappend = true
configsvr = true
directoryperdb = true
replSet=configdb
3.启动
./mongod -f /opt/mongodb/config_server/c1/mongodb344/conf/config_server.cnf
./mongod -f /opt/mongodb/config_server/c2/mongodb344/conf/config_server.cnf
./mongod -f /opt/mongodb/config_server/c3/mongodb344/conf/config_server.cnf
4.创建副本集
登陆其中一个节点执行:
./mongo 127.0.0.1:27017
configdb1={_id:'configdb',members:[
{_id:0,host:'127.0.0.1:27017',priority:3},
{_id:1,host:'127.0.0.1:27018',priority:1},
{_id:2,host:'127.0.0.1:27019',priority:2}]
}
rs.initiate(configdb1)
这里的configdb需要跟配置文件里replSet定义的一致.
-------------路由服务器---------------------------------
1.程序路径
mkdir -P /opt/mongodb/route_server/mongodb344
mkdir -P /opt/mongodb/route_server/mongodb344/conf
mkdir -P /opt/mongodb/route_server/mongodb344/log
2.配置文件
[root@izwz9gxsbwybwg9n5xdi47z conf]# more route_server.cnf
port=27020
configdb=configdb/127.0.0.1:27017,127.0.0.1:27018,127.0.0.1:27019
logpath=/opt/mongodb/route_server/mongodb344/logs
fork=true
3.启动
./mongos -f /opt/mongodb/route_server/mongodb344/conf/route_server.cnf
----------------------分片服务器----------------------------
1.程序路径
mkdir -P /opt/mongodb/shard1/mongodb344
mkdir -P /opt/mongodb/shard2/mongodb344
数据文件路径
mkdir -P /opt/mongodb/shard1/mongodb344/data
mkdir -P /opt/mongodb/shard2/mongodb344/data
配置文件路径
mkdir -P /opt/mongodb/shard1/mongodb344/conf
mkdir -P /opt/mongodb/shard2/mongodb344/conf
日志文件路径
/opt/mongodb/shard1/mongodb344/log
/opt/mongodb/shard2/mongodb344/log
2.配置文件
[root@izwz9gxsbwybwg9n5xdi47z conf]# more shard1.cnf
port = 27021
fork = true
dbpath = /opt/mongodb/shard1/mongodb344/data
logpath = /opt/mongodb/shard1/mongodb344/log/logs
logappend = true
shardsvr = true
[root@izwz9gxsbwybwg9n5xdi47z conf]# more shard2.cnf
port = 27022
fork = true
dbpath = /opt/mongodb/shard2/mongodb344/data
logpath = /opt/mongodb/shard2/mongodb344/log/logs
logappend = true
shardsvr = true
3.启动
./mongod -f /opt/mongodb/shard1/mongodb344/conf/shard1.cnf
./mongod -f /opt/mongodb/shard2/mongodb344/conf/shard2.cnf
--------------分片部署--------------------------------
1.在路由服务器上执行,添加分片服务器
./mongo 127.0.0.1:27020
mongos>use admin
mongos>db.runCommand({"addshard":"127.0.0.1:27021",allowLocal:true})
mongos>db.runCommand({"addshard":"127.0.0.1:27022",allowLocal:true})
2.查看分片情况
mongos> use adminuse admin
switched to db admin
mongos> db.runCommand({listshards:1});db.runCommand({listshards:1});
{
"shards" : [
{
"_id" : "shard0000",
"host" : "127.0.0.1:27021",
"state" : 1
},
{
"_id" : "shard0001",
"host" : "127.0.0.1:27022",
"state" : 1
}
],
"ok" : 1
}
mongos>
3.启用分片功能
在路由服务器上执行
是数据库hxl启用分片功能
mongos>db.runCommand({"enablesharding":"hxl"});
为集合person设置片键,这里使用id做hash分片
mongos>use admin
mongos>db.runCommand({"shardcollection":"hxl.person","key":{"_id":"hashed"}})
{ "collectionsharded" : "hxl.person", "ok" : 1 }
写入数据
mongos>use hxl
mongos>for (var i=0;i<100000;i++){db.person.insert({"name":"hxl"+i,"age":i})}
mongos> db.person.count();
100000
查看分片结果
mongos> db.printShardingStatus();
查看集合的分片记录情况
mongos> use hxl;
mongos> db.person.stats()
-- The End --
阅读(2947) | 评论(0) | 转发(0) |