mkdir -p /home/mongodb/conf
mkdir -p /home/mongodb/mongos/log
mkdir -p /home/mongodb/config/data
mkdir -p /home/mongodb/config/log
mkdir -p /home/mongodb/shard1/data
mkdir -p /home/mongodb/shard1/log
mkdir -p /home/mongodb/shard2/data
mkdir -p /home/mongodb/shard2/log
mkdir -p /home/mongodb/shard3/data
mkdir -p /home/mongodb/shard3/log
#config.conf
pidfilepath = /home/mongodb/config/log/configsrv.pid
dbpath = /home/mongodb/config/data
logpath = /home/mongodb/config/log/congigsrv.log
logappend = true
bind_ip = 0.0.0.0
port = 23000
fork = true
configsvr = true
replSet=config-server
maxConns=20000
配置config-server:
启动三台服务器的config server
mongod -f /home/mongodb/conf/config.conf
登录任意一台配置服务器,初始化配置副本集
#连接
mongo --port 23000
#config变量
config = {
_id : "config-server",
members : [
{_id : 0, host : "192.168.147.134:23000" },
{_id : 1, host : "192.168.147.135:23000" },
{_id : 2, host : "192.168.147.136:23000" }
]
}
#初始化副本集
rs.initiate(config)
#shard1.conf
pidfilepath = /home/mongodb/shard1/log/shard1.pid
dbpath = /home/mongodb/shard1/data
logpath = /home/mongodb/shard1/log/shard1.log
logappend = true
bind_ip = 0.0.0.0
port = 23001
fork = true
replSet=shard1-server
shardsvr = true
maxConns=20000
配置shard1-server: ##shard2-server shard3-server 类似
启动三台服务器的shard1 server
mongod -f /home/mongodb/conf/shard1.conf
登陆任意一台服务器,初始化副本集
mongo --port 23001
#使用admin数据库
use admin
#定义副本集配置,第三个节点的 "arbiterOnly":true 代表其为仲裁节点。
config = {
_id : "shard1-server",
members : [
{_id : 0, host : "192.168.147.135:23001" },
{_id : 1, host : "192.168.147.134:23001" },
{_id : 2, host : "192.168.147.136:23001", arbiterOnly: true }
]
}
#初始化副本集配置
rs.initiate(config);
#shard2.conf
pidfilepath = /home/mongodb/shard2/log/shard2.pid
dbpath = /home/mongodb/shard2/data
logpath = /home/mongodb/shard2/log/shard2.log
logappend = true
bind_ip = 0.0.0.0
port = 23002
fork = true
replSet=shard2-server
shardsvr = true
maxConns=20000
#shard3.conf
pidfilepath = /home/mongodb/shard3/log/shard3.pid
dbpath = /home/mongodb/shard3/data
logpath = /home/mongodb/shard3/log/shard3.log
logappend = true
bind_ip = 0.0.0.0
port = 23003
fork = true
replSet=shard3-server
shardsvr = true
maxConns=20000
#mongos.conf
pidfilepath = /home/mongodb/mongos/log/mongos.pid
logpath = /home/mongodb/mongos/log/mongos.log
logappend = true
bind_ip = 0.0.0.0
port = 40000
fork = true
configdb = config-server/192.168.147.134:23000,192.168.147.135:23000,192.168.147.135:23000
maxConns=20000
##配置mongos
启动三台服务器的mongos server
mongos -f /home/mongodb/conf/mongos.conf
5、启用分片
目前搭建了mongodb配置服务器、路由服务器,各个分片服务器,不过应用程序连接到mongos路由服务器并不能使用分片机制,还需要在程序里设置分片配置,让分片生效。
登陆任意一台mongos
mongo --port 40000
#使用admin数据库
user admin
#串联路由服务器与分配副本集
sh.addShard("shard1-server/192.168.147.135:23001,192.168.147.134:23001,192.168.147.136:23001")
sh.addShard("shard2-server/192.168.147.135:23002,192.168.147.134:23002,192.168.147.136:23002")
sh.addShard("shard3-server/192.168.147.135:23003,192.168.147.134:23003,192.168.147.136:23003")
#查看集群状态
sh.status()
###启动关闭
mongodb的启动顺序是,先启动配置服务器,在启动分片,最后启动mongos.
mongod -f /usr/local/mongodb/conf/config.conf
mongod -f /usr/local/mongodb/conf/shard1.conf
mongod -f /usr/local/mongodb/conf/shard2.conf
mongod -f /usr/local/mongodb/conf/shard3.conf
mongod -f /usr/local/mongodb/conf/mongos.conf
关闭时,直接killall杀掉所有进程
killall mongod
killall mongos
测试:
我们设置testdb的 table1 表需要分片,根据 id 自动分片到 shard1 ,shard2,shard3 上面去。要这样设置是因为不是所有mongodb 的数据库和表 都需要分片!
#指定testdb分片生效
db.runCommand( { enablesharding :"testdb"});
#指定数据库里需要分片的集合和片键
db.runCommand( { shardcollection : "testdb.table1",key : {id: 1} } )
for (var i = 1; i <= 100000; i++){
db.table1.save({id:i,"test1":"testval1"})}
通过 db.table1.stats()查看 数据落到了那个shard上面
参考:
https://zhuanlan.zhihu.com/p/28600032
http://www.mongodb.org.cn/tutorial/53.html
本文转自 674591788 51CTO博客,原文链接:http://blog.51cto.com/mrdeng/1971578