Press "Enter" to skip to content

MongoDB搭建副本集

端口分配如下:
192.168.1.2/mongos1(30000)/config1(20000)/rs1-master(10001)/rs2-arbiter(10002)/rs3-slaver(10003)
192.168.1.3/mongos2(30000)/config1(20000)/rs1-slaver(10001)/rs2-master(10002)/rs3-arbiter(10003)
192.168.1.4/mongos3(30000)/config3(20000)/rs1-arbiter(10001)/rs2-slaver(10002)/rs3-master(10003)

MongoDB安装步骤略,直接解压就行,具体安装路径为/home/app,mongodb版本为mongodb-linux-x86_64-rhel62-3.2.6。

三台都要:
//配置文件存放路径
mkdir -p /home/app/mongodb/config
//config server相关路径
mkdir -p /home/app/mongodb/config/data
mkdir -p /home/app/mongodb/config/logs&&touch /home/app/mongodb/config/logs/config.log
//mongos相关路径
mkdir -p /home/app/mongodb/mongos/logs&&touch /home/app/mongodb/mongos/logs/mongos.log

192.168.1.2
mkdir -p /home/app/mongodb/rs1-master
mkdir -p /home/app/mongodb/rs1-master/data
mkdir -p /home/app/mongodb/rs1-master/logs
mkdir -p /home/app/mongodb/rs2-arbiter
mkdir -p /home/app/mongodb/rs2-arbiter/data
mkdir -p /home/app/mongodb/rs2-arbiter/logs
mkdir -p /home/app/mongodb/rs3-slaver
mkdir -p /home/app/mongodb/rs3-slaver/data
mkdir -p /home/app/mongodb/rs3-slaver/logs

192.168.1.3
mkdir -p /home/app/mongodb/rs1-slaver
mkdir -p /home/app/mongodb/rs1-slaver/data
mkdir -p /home/app/mongodb/rs1-slaver/logs
mkdir -p /home/app/mongodb/rs2-master
mkdir -p /home/app/mongodb/rs2-master/data
mkdir -p /home/app/mongodb/rs2-master/logs
mkdir -p /home/app/mongodb/rs3-arbiter
mkdir -p /home/app/mongodb/rs3-arbiter/data
mkdir -p /home/app/mongodb/rs3-arbiter/logs

192.168.1.4
mkdir -p /home/app/mongodb/rs1-arbiter
mkdir -p /home/app/mongodb/rs1-arbiter/data
mkdir -p /home/app/mongodb/rs1-arbiter/logs
mkdir -p /home/app/mongodb/rs2-slaver
mkdir -p /home/app/mongodb/rs2-slaver/data
mkdir -p /home/app/mongodb/rs2-slaver/logs
mkdir -p /home/app/mongodb/rs3-master
mkdir -p /home/app/mongodb/rs3-master/data
mkdir -p /home/app/mongodb/rs3-master/logs

config server配置服务器(三台都要)
mongodb3.4以后要求配置服务器也创建副本集,不然集群搭建不成功
/home/app/mongodb-linux-x86_64-rhel62-3.2.6/bin/mongod --fork --configsvr --replSet configReplSet --dbpath /home/app/mongodb/config/data --logpath /home/app/mongodb/config/logs/config.log --port 20000
上述步骤执行完到任意一台机器上执行如下命令:
/home/app/mongodb-linux-x86_64-rhel62-3.2.6/bin/mongo --port 20000

config = {
_id: "configReplSet",
configsvr: true,
version: 1,
members: [
{ _id : 0, host : "192.168.1.2:20000" },
{ _id : 1, host : "192.168.1.3:20000" },
{ _id : 2, host : "192.168.1.4:20000" }
]
}
rs.initiate(config);

mongos(三台机器都要)
/home/app/mongodb-linux-x86_64-rhel62-3.2.6/bin/mongos --configdb 192.168.1.2:20000,192.168.1.3:20000,192.168.1.4:20000 --fork --logpath /home/app/mongodb/mongos/logs/mongos.log --port 30000

配置分片副本集(三台机器)
rs1-master(192.168.1.2)
vi /home/app/mongodb/conf/rs1-master.conf
dbpath=/home/app/mongodb/rs1-master/data
logpath=/home/app/mongodb/rs1-master/logs/rs1-master.log
pidfilepath=/home/app/mongodb/rs1-master/rs1-master.pid
directoryperdb=true
logappend=true
replSet=rs1
bind_ip=192.168.1.2
port=10001
oplogSize=10000
fork=true
noprealloc=true

/home/app/mongodb-linux-x86_64-rhel62-3.2.6/bin/mongod -f /home/app/mongodb/conf/rs1-master.conf

rs1-slaver(192.168.1.3)
vi /home/app/mongodb/conf/rs1-slaver.conf
dbpath=/home/app/mongodb/rs1-slaver/data
logpath=/home/app/mongodb/rs1-slaver/logs/rs1-slaver.log
pidfilepath=/home/app/mongodb/rs1-slaver/rs1-slaver.pid
directoryperdb=true
logappend=true
replSet=rs1
bind_ip=192.168.1.3
port=10001
oplogSize=10000
fork=true
noprealloc=true

/home/app/mongodb-linux-x86_64-rhel62-3.2.6/bin/mongod -f /home/app/mongodb/conf/rs1-slaver.conf

rs1-arbiter(192.168.1.4)
vi /home/app/mongodb/conf/rs1-arbiter.conf
dbpath=/home/app/mongodb/rs1-arbiter/data
logpath=/home/app/mongodb/rs1-arbiter/logs/rs1-arbiter.log
pidfilepath=/home/app/mongodb/rs1-arbiter/rs1-arbiter.pid
directoryperdb=true
logappend=true
replSet=rs1
bind_ip=192.168.1.4
port=10001
oplogSize=10000
fork=true
noprealloc=true

/home/app/mongodb-linux-x86_64-rhel62-3.2.6/bin/mongod -f /home/app/mongodb/conf/rs1-arbiter.conf

/home/app/mongodb-linux-x86_64-rhel62-3.2.6/bin/mongo --host 192.168.1.2 --port 10001(192.168.1.2上面执行)
config = {
_id: "rs1",
members: [
{ _id : 0, host : "192.168.1.2:10001" },
{ _id : 1, host : "192.168.1.3:10001" },
{ _id : 2, host : "192.168.1.4:10001" , arbiterOnly: true }
]
}

rs.initiate(config);

rs2-arbiter(192.168.1.2)
vi /home/app/mongodb/conf/rs2-arbiter.conf
dbpath=/home/app/mongodb/rs2-arbiter/data
logpath=/home/app/mongodb/rs2-arbiter/logs/rs2-arbiter.log
pidfilepath=/home/app/mongodb/rs2-arbiter/rs2-arbiter.pid
directoryperdb=true
logappend=true
replSet=rs2
bind_ip=192.168.1.2
port=10002
oplogSize=10000
fork=true
noprealloc=true

/home/app/mongodb-linux-x86_64-rhel62-3.2.6/bin/mongod -f /home/app/mongodb/conf/rs2-arbiter.conf

rs2-master(192.168.1.3)
vi /home/app/mongodb/conf/rs2-master.conf
dbpath=/home/app/mongodb/rs2-master/data
logpath=/home/app/mongodb/rs2-master/logs/rs2-master.log
pidfilepath=/home/app/mongodb/rs2-master/rs2-master.pid
directoryperdb=true
logappend=true
replSet=rs2
bind_ip=192.168.1.3
port=10002
oplogSize=10000
fork=true
noprealloc=true

/home/app/mongodb-linux-x86_64-rhel62-3.2.6/bin/mongod -f /home/app/mongodb/conf/rs2-master.conf

rs2-slaver(192.168.1.4)
vi /home/app/mongodb/conf/rs2-slaver.conf
dbpath=/home/app/mongodb/rs2-slaver/data
logpath=/home/app/mongodb/rs2-slaver/logs/rs2-slaver.log
pidfilepath=/home/app/mongodb/rs2-slaver/rs2-slaver.pid
directoryperdb=true
logappend=true
replSet=rs2
bind_ip=192.168.1.4
port=10002
oplogSize=10000
fork=true
noprealloc=true

/home/app/mongodb-linux-x86_64-rhel62-3.2.6/bin/mongod -f /home/app/mongodb/conf/rs2-slaver.conf

/home/app/mongodb-linux-x86_64-rhel62-3.2.6/bin/mongo --host 192.168.1.3 --port 10002(192.168.1.3上面执行)
config = {
_id: "rs2",
members: [
{ _id : 0, host : "192.168.1.2:10002" , arbiterOnly: true },
{ _id : 1, host : "192.168.1.3:10002" },
{ _id : 2, host : "192.168.1.4:10002" }
]
}

rs.initiate(config);

rs3-slaver(192.168.1.2)
vi /home/app/mongodb/conf/rs3-slaver.conf
dbpath=/home/app/mongodb/rs3-slaver/data
logpath=/home/app/mongodb/rs3-slaver/logs/rs3-slaver.log
pidfilepath=/home/app/mongodb/rs3-slaver/rs3-slaver.pid
directoryperdb=true
logappend=true
replSet=rs3
bind_ip=192.168.1.2
port=10003
oplogSize=10000
fork=true
noprealloc=true

/home/app/mongodb-linux-x86_64-rhel62-3.2.6/bin/mongod -f /home/app/mongodb/conf/rs3-slaver.conf

rs3-arbiter(192.168.1.3)
vi /home/app/mongodb/conf/rs3-arbiter.conf
dbpath=/home/app/mongodb/rs3-arbiter/data
logpath=/home/app/mongodb/rs3-arbiter/logs/rs3-arbiter.log
pidfilepath=/home/app/mongodb/rs3-arbiter/rs3-arbiter.pid
directoryperdb=true
logappend=true
replSet=rs3
bind_ip=192.168.1.3
port=10003
oplogSize=10000
fork=true
noprealloc=true

/home/app/mongodb-linux-x86_64-rhel62-3.2.6/bin/mongod -f /home/app/mongodb/conf/rs3-arbiter.conf

rs3-master(192.168.1.4)
vi /home/app/mongodb/conf/rs3-master.conf
dbpath=/home/app/mongodb/rs3-master/data
logpath=/home/app/mongodb/rs3-master/logs/rs3-master.log
pidfilepath=/home/app/mongodb/rs3-master/rs3-master.pid
directoryperdb=true
logappend=true
replSet=rs3
bind_ip=192.168.1.4
port=10003
oplogSize=10000
fork=true
noprealloc=true

/home/app/mongodb-linux-x86_64-rhel62-3.2.6/bin/mongod -f /home/app/mongodb/conf/rs3-master.conf

/home/app/mongodb-linux-x86_64-rhel62-3.2.6/bin/mongo --host 192.168.1.4 --port 10003(192.168.1.4上面执行)
config = {
_id: "rs3",
members: [
{ _id : 0, host : "192.168.1.2:10003" },
{ _id : 1, host : "192.168.1.3:10003" , arbiterOnly: true },
{ _id : 2, host : "192.168.1.4:10003" }
]
}

rs.initiate(config);

启用分片
目前搭建了mongodb配置服务器、路由服务器,各个分片服务器,不过应用程序连接到mongos路由服务器并不能使用分片机制,还需要在程序里设置分片配置,让分片生效
登陆任意一台mongos
/home/app/mongodb-linux-x86_64-rhel62-3.2.6/bin/mongo --host 192.168.1.2 --port 30000
#串联路由服务器与分配副本集
sh.addShard("rs1/192.168.1.2:10001,192.168.1.3:10001,192.168.1.4:10001")
sh.addShard("rs2/192.168.1.2:10002,192.168.1.3:10002,192.168.1.4:10002")
sh.addShard("rs3/192.168.1.2:10003,192.168.1.3:10003,192.168.1.4:10003")

目前配置服务、路由服务、分片服务、副本集服务都已经串联起来了,但我们的目的是希望插入数据,数据能够自动分片。连接在mongos上,准备让指定的数据库、指定的集合分片生效。
#指定testdb分片生效
db.runCommand( { enablesharding :"testdb"});
#指定数据库里需要分片的集合和片键
sh.shardCollection("testdb.users", { "id" : 1 } )
{ "collectionsharded" : "testdb.users", "ok" : 1 }
(或者db.runCommand( { shardcollection : "testdb.uesrs",key : {id: 1} } ))
设置testdb的 users 表需要分片,根据 id 自动分片到 rs1,rs2,rs3上面去。
测试分片配置结果
/home/app/mongodb-linux-x86_64-rhel62-3.2.6/bin/mongo --host 192.168.1.2 --port 30000
#使用testdb
use testdb;
#插入测试数据
for(i=1;i<=200000;i++) db.users.insert({uid:i,name:"test",age:11})
#查看分片情况如下,部分无关信息省掉了
db.users.stats();

Be First to Comment

发表评论

电子邮件地址不会被公开。 必填项已用*标注