天天看点

MongoDB分片副本集搭建

1、安装mongodb

tar xvf mongodb-linux-x86_64-enterprise-rhel70-3.6.2.tgz -C /usr/local/

cd /usr/local

mv mongodb-linux-x86_64-enterprise-rhel70-3.6.2/ mongodb

mkdir -p /usr/local/mongodb/conf

mkdir -p /usr/local/mongodb/mongos/log

mkdir -p /usr/local/mongodb/config/data

mkdir -p /usr/local/mongodb/config/log

mkdir -p /usr/local/mongodb/shard1/data

mkdir -p /usr/local/mongodb/shard1/log

mkdir -p /usr/local/mongodb/shard2/data

mkdir -p /usr/local/mongodb/shard2/log

mkdir -p /usr/local/mongodb/shard3/data

mkdir -p /usr/local/mongodb/shard3/log

chown -R mongo.mongo /usr/local/mongodb

2、config server配置服务器

vim /usr/local/mongodb/conf/config.conf

#配置文件内容

pidfilepath = /usr/local/mongodb/config/log/configsrv.pid

dbpath = /usr/local/mongodb/config/data

logpath = /usr/local/mongodb/config/log/congigsrv.log

logappend = true

bind_ip = 0.0.0.0

port = 21000

fork = true

configsvr = true

replSet=bsbgps

maxConns=20000

#启动三台服务器的config server

mongod -f /usr/local/mongodb/conf/config.conf

#初始化配置副本集

#连接

mongo --port 21000

#config变量

config = {

_id : "bsbgps",

members : [

{_id : 0, host : "188.188.1.135:21000" },

{_id : 1, host : "188.188.1.136:21000" },

{_id : 2, host : "188.188.1.137:21000" }

]

}

#初始化副本集

rs.initiate(config)

3、配置分片副本集

a.设置第一个分片副本集

vim /usr/local/mongodb/conf/shard1.conf

pidfilepath = /usr/local/mongodb/shard1/log/shard1.pid

dbpath = /usr/local/mongodb/shard1/data

logpath = /usr/local/mongodb/shard1/log/shard1.log

port = 27001

replSet=shard1

shardsvr = true

#启动三台服务器的shard1 server

mongod -f /usr/local/mongodb/conf/shard1.conf

#登陆非仲裁服务器,初始化副本集

mongo --port 27001

#使用admin数据库

use admin

#定义副本集配置,第三个节点的 "arbiterOnly":true 代表其为仲裁节点。

_id : "shard1",

{_id : 0, host : "188.188.1.135:27001" },

{_id : 1, host : "188.188.1.136:27001" },

{_id : 2, host : "188.188.1.137:27001" , arbiterOnly: true }

#初始化副本集配置

rs.initiate(config);

b.设置第二个分片副本集

vim /usr/local/mongodb/conf/shard2.conf

pidfilepath = /usr/local/mongodb/shard2/log/shard2.pid

dbpath = /usr/local/mongodb/shard2/data

logpath = /usr/local/mongodb/shard2/log/shard2.log

port = 27002

replSet=shard2

#启动三台服务器的shard2 server

mongod -f /usr/local/mongodb/conf/shard2.conf

mongo --port 27002

#定义副本集配置

_id : "shard2",

{_id : 0, host : "188.188.1.135:27002" , arbiterOnly: true },

{_id : 1, host : "188.188.1.136:27002" },

{_id : 2, host : "188.188.1.137:27002" }

c.设置第三个分片副本集

vim /usr/local/mongodb/conf/shard3.conf

pidfilepath = /usr/local/mongodb/shard3/log/shard3.pid

dbpath = /usr/local/mongodb/shard3/data

logpath = /usr/local/mongodb/shard3/log/shard3.log

port = 27003

replSet=shard3

#启动三台服务器的shard3 server

mongod -f /usr/local/mongodb/conf/shard3.conf

mongo --port 27003

_id : "shard3",

{_id : 0, host : "188.188.1.135:27003" },

{_id : 1, host : "188.188.1.136:27003" , arbiterOnly: true},

{_id : 2, host : "188.188.1.137:27003" }

4、配置路由服务器 mongos

先启动配置服务器和分片服务器,后启动路由实例:(三台机器)

vim /usr/local/mongodb/conf/mongos.conf

#内容,监听的配置服务器,只能有1个或者3个 configs为配置服务器的副本集名字

pidfilepath = /usr/local/mongodb/mongos/log/mongos.pid

logpath = /usr/local/mongodb/mongos/log/mongos.log

port = 20000

configdb = bsbgps/188.188.1.135:21000,188.188.1.136:21000,188.188.1.137:21000

#启动三台服务器的mongos server

mongos -f /usr/local/mongodb/conf/mongos.conf

5、启用分片

目前搭建了mongodb配置服务器、路由服务器,各个分片服务器,不过应用程序连接到mongos路由服务器并不能使用分片机制,还需要在程序里设置分片配置,让分片生效。

登陆任意一台mongos

mongo --port 20000

#串联路由服务器与分配副本集

sh.addShard("shard1/188.188.1.135:27001,188.188.1.136:27001,188.188.1.137:27001")

sh.addShard("shard2/188.188.1.135:27002,188.188.1.136:27002,188.188.1.137:27002")

sh.addShard("shard3/188.188.1.135:27003,188.188.1.136:27003,188.188.1.137:27003")

#查看集群状态

sh.status()

6、测试

#指定ywtestdb分片生效

db.runCommand( { enablesharding :"ywtestdb"});

#指定数据库里需要分片的集合和片键

db.runCommand( { shardcollection : "ywtestdb.testtb",key : {id: 1} } )

#插入测试数据

use ywtestdb

for(var i = 1; i <= 100; i++){

db.testtb.save({id:i,"Time":new Date().getFullYear()+"-"+(new Date().getMonth()+1)+"-"+new Date().getDate()+ " "+new Date().toLocaleTimeString()})

#查看分片情况如下,部分无关信息省掉了

db.testtb.stats();

参考博文:

<a href="http://www.ityouknow.com/mongodb/2017/08/05/mongodb-cluster-setup.html">http://www.ityouknow.com/mongodb/2017/08/05/mongodb-cluster-setup.html</a>

<a href="https://www.jianshu.com/p/baed80df9300">https://www.jianshu.com/p/baed80df9300</a>

     本文转自1321385590 51CTO博客,原文链接:http://blog.51cto.com/linux10000/2066780,如需转载请自行联系原作者