实战Replica Sets+Sharding方案采…
来源:互联网 发布:淘宝拍照技巧 编辑:程序博客网 时间:2024/05/16 08:07
mongo集群分片实战
服务器三台
创建数据库目录
主机1(172.19.0.215)配置文件
[root@mongodb115 ~]# mkdir -p/home/data/shard1_1
[root@mongodb115 ~]# mkdir -p/home/data/shard2_1
[root@mongodb115 ~]# mkdir -p/home/data/shard3_1
[root@mongodb115 ~]# mkdir -p/home/data/config
[root@mongodb115 ~]# mkdir -p/home/config
[root@mongodb115 ~]# mkdir -p/home/logs
进入到/home/config/目录下,分别编辑并创建以下配置文件,分别为:
shard2_1.conf
shard3_1.conf
config.conf
mongos.conf
刚开始不知道,就直接用cat
分片一
[root@215 config]# catshard1_1.conf
port=11731
dbpath=/home/data/shard1_1
logpath=/home/data/logs/shard1_1.log
directoryperdb=true
logappend=true
replSet=shard1
#bind_ip=172.19.0.215
shardsvr=true
fork=true
pidfilepath=/home/data/shard1_1.pid
maxConns=20000
oplogSize=100
noprealloc=true
nohttpinterface=true
分片二
[root@215 config]# cat shard2_1.conf
port=11732
dbpath=/home/data/shard2_1
logpath=/home/data/logs/shard2_1.log
directoryperdb=true
logappend=true
replSet=shard2
#bind_ip=172.19.0.215
shardsvr=true
fork=true
pidfilepath=/home/data/shard2_1.pid
maxConns=20000
oplogSize=100
noprealloc=true
nohttpinterface=true
分片三
[root@215config]# cat shard3_1.conf
port=11733
dbpath=/home/data/shard3_1
logpath=/home/data/logs/shard3_1.log
directoryperdb=true
logappend=true
replSet=shard3
#bind_ip=172.19.0.215
shardsvr=true
fork=true
pidfilepath=/home/data/shard3_1.pid
maxConns=20000
oplogSize=100
noprealloc=true
nohttpinterface=true
config server配置文件
[root@215 config]# catconfig.conf
port=30000
dbpath=/home/data/config
logpath=/home/data/logs/config.log
directoryperdb=true
configsvr=true
logappend=true
#bind_ip=172.19.0.215
fork=true
pidfilepath=/home/data/config.pid
maxConns=20000
oplogSize=100
noprealloc=true
nohttpinterface=true
Route Proces配置文件
[root@215 config]# cat mongos.conf
port=60000
logpath=/home/data/logs/mongos.log
logappend=true
configdb=172.19.0.215:30000,172.19.0.216:30000,172.19.0.217:30000
#bind_ip=172.19.0.215
fork=true
pidfilepath=/home/data/mongos.pid
chunkSize=5
maxConns=20000
主机2(172.19.0.216)配置文件
[root@216~]# mkdir -p/home/data/shard1_2
[root@216~]# mkdir -p/home/data/shard2_2
[root@216~]# mkdir -p/home/data/shard3_2
[root@216~]# mkdir -p/home/data/config
[root@216~]# mkdir -p/home/config
[root@216~]# mkdir -p /home/logs
进入到/home/config/目录下,分别编辑并创建以下配置文件,分别为:
shard1_2.conf
shard2_2.conf
shard3_2.conf
config.conf
mongos.conf
分片一
[root@216 config]# cat shard1_2.conf
port=11731
dbpath=/home/data/shard1_2
logpath=/home/data/logs/shard1_2.log
directoryperdb=true
logappend=true
replSet=shard1
#bind_ip=172.19.0.216
shardsvr=true
fork=true
pidfilepath=/home/data/shard1_2.pid
maxConns=20000
oplogSize=5000
noprealloc=true
nohttpinterface=true
分片二
[root@localhost config]# catshard2_2.conf
port=11732
dbpath=/home/data/shard2_2
logpath=/home/data/logs/shard2_2.log
directoryperdb=true
logappend=true
replSet=shard2
#bind_ip=172.19.0.216
shardsvr=true
fork=true
pidfilepath=/home/data/shard2_2.pid
maxConns=20000
oplogSize=5000
noprealloc=true
nohttpinterface=true
分片三:
[root@localhost config]# catshard3_2.conf
port=11733
dbpath=/home/data/shard3_2
logpath=/home/data/logs/shard3_2.log
directoryperdb=true
logappend=true
replSet=shard3
#bind_ip=172.19.0.216
shardsvr=true
fork=true
pidfilepath=/home/data/shard3_2.pid
maxConns=20000
oplogSize=5000
noprealloc=true
nohttpinterface=true
config server配置文件
[root@216 config]# catconfig.conf
port=30000
dbpath=/home/data/config
logpath=/home/data/logs/config.log
directoryperdb=true
configsvr=true
logappend=true
#bind_ip=172.19.0.216
fork=true
pidfilepath=/home/data/config.pid
maxConns=20000
oplogSize=5000
noprealloc=true
nohttpinterface=true
Route Proces配置文件
[root@216 config]# catmongos.conf
port=60000
logpath=/home/data/logs/mongos.log
logappend=true
configdb=172.19.0.215:30000,172.19.0.216:30000,172.19.0.217:30000
#bind_ip=172.19.0.216
fork=true
pidfilepath=/home/data/mongos.pid
chunkSize=5
maxConns=20000
#nohttpinterface=true
主机3(172.19.0.217)配置文件
[root@217~]# mkdir -p/home/data/shard1_3
[root@217~]# mkdir -p/home/data/shard2_3
[root@217~]# mkdir -p/home/data/shard3_3
[root@217~]# mkdir -p /home/data/config#config server目录
[root@217~]# mkdir -p /home/config#放配置文件
[root@217~]# mkdir -p /home/logs
分片一
[root@217
port=11731
dbpath=/home/data/shard1_3
logpath=/home/data/logs/shard1_3.log
directoryperdb=true
logappend=true
replSet=shard1
#bind_ip=172.19.0.217
shardsvr=true
fork=true
pidfilepath=/home/data/shard1_3.pid
maxConns=20000
oplogSize=5000
noprealloc=true
nohttpinterface=true
分片二
[root@217 config]# cat shard2_3.conf
port=11732
dbpath=/home/data/shard2_3
logpath=/home/data/logs/shard2_3.log
directoryperdb=true
logappend=true
replSet=shard2
#bind_ip=172.19.0.217
shardsvr=true
fork=true
pidfilepath=/home/data/shard2_3.pid
maxConns=20000
oplogSize=5000
noprealloc=true
nohttpinterface=true
分片三:
[root@217 config]# cat shard3_3.conf
port=11733
dbpath=/home/data/shard3_3
logpath=/home/data/logs/shard3_3.log
directoryperdb=true
logappend=true
replSet=shard3
#bind_ip=172.19.0.217
shardsvr=true
fork=true
pidfilepath=/home/data/shard3_3.pid
maxConns=20000
oplogSize=5000
noprealloc=true
nohttpinterface=true
config server配置文件
[root@217
port=30000
dbpath=/home/data/config
logpath=/home/data/logs/config.log
directoryperdb=true
configsvr=true
logappend=true
#bind_ip=172.19.0.217
fork=true
pidfilepath=/home/data/config.pid
maxConns=20000
oplogSize=5000
noprealloc=true
nohttpinterface=true
Route Proces配置文件
[root@217config]# catmongos.conf
port=60000
logpath=/home/data/logs/mongos.log
logappend=true
configdb=172.19.0.215:30000,172.19.0.216:30000,172.19.0.217:30000
#bind_ip=172.19.0.217
fork=true
pidfilepath=/home/data/mongos.pid
chunkSize=5
maxConns=20000
#nohttpinterface=true
解压mongodb文件,配置mongodb
wget http://fastdl.mongodb.org/linux/mongodb-linux-x86_64-2.0.4.tgz
主机1
[root@215 ~]# tar zxvfmongodb-linux-x86_64-2.0.4.tgz
[root@215 ~]# mvmongodb-linux-x86_64-2.0.4.tgz /home/mongodb
主机2
[root@216 ~]# tar zxvfmongodb-linux-x86_64-2.0.4.tgz
[root@216 ~]# mvmongodb-linux-x86_64-2.0.4.tgz/home/mongodb
主机3
[root@217 ~]# tar zxvfmongodb-linux-x86_64-2.0.4.tgz
[root@217 ~]# mvmongodb-linux-x86_64-2.0.4.tgz /home/mongodb
启动shard1,shard2,shard3
主机1
[root@215~]# /home/mongodb/bin/mongod -f/home/config/shard1_1.conf
[root@215~]# /home/mongodb/bin/mongod -f/home/config/shard2_1.conf
[root@215~]# /home/mongodb/bin/mongod -f/home/config/shard3_1.conf
主机2
[root@216~]# /home/mongodb/bin/mongod -f/home/config/shard1_2.conf
[root@216~]# /home/mongodb/bin/mongod -f/home/config/shard2_2.conf
[root@216~]# /home/mongodb/bin/mongod -f/home/config/shard3_2.conf
主机3
[root@217~]# /home/mongodb/bin/mongod -f/home/config/shard1_3.conf
[root@217~]# /home/mongodb/bin/mongod -f/home/config/shard2_3.conf
[root@217~]# /home/mongodb/bin/mongod -f/home/config/shard3_3.conf
[root@215~]#/home/mongodb/bin/mongos -f /home/config/mongos.conf
error command line: unknown option nohttpinterface)
这个是因为我们的配置文件config.conf中有一个参数读不到,这个参数nohttpinterface=true可以注释掉该参数。即可解决。如下代码
[root@localhostconfig]# cat config.conf
port=30000
dbpath=/home/data/config
logpath=/home/data/logs/config.log
directoryperdb=true
configsvr=true
logappend=true
#bind_ip=172.19.0.215
fork=true
pidfilepath=/home/data/config.pid
maxConns=20000
oplogSize=100
noprealloc=true
#nohttpinterface=true
)
配置ReplicaSets
配置分片1在172.19.0.215上配置
[root@215~]# /home/mongodb/bin/mongo --port11731
MongoDB shell version: 2.4.7
connecting to:127.0.0.1:11731/test
>config={_id:'shard1',members:[{_id:0,host:'172.19.0.215:11731',priority:2},{_id:1,host:'172.19.0.216:11731'},{_id:2,host:'172.19.0.217:11731',arbiterOnly:true}]}
rs.initiate(config)
(注释:此处在初始化分片的时候报错了,比较奇怪的是,后面的216,217都可以正常操作,且分片初始化的时候也能正常,唯有主机215报错,错误情况见图:这个地方要保证三台服务器是开启状态。解决方法就是把配置文件中的5000一个参数修改成oplogSize=100,
解决方法就是把配置文件中的一个参数修改成100,代码如下:
[root@215
port=11731
dbpath=/home/data/shard1_1
logpath=/home/data/logs/shard1_1.log
directoryperdb=true
logappend=true
replSet=shard1
#bind_ip=172.19.0.215
shardsvr=true
fork=true
pidfilepath=/home/data/shard1_1.pid
maxConns=20000
oplogSize=100
noprealloc=true
nohttpinterface=true
)
配置分片2在172.19.0.216上配置
[root@216~]#/home/mongodb/bin/mongo --port 11732
MongoDB shell version:2.4.7
connecting to:127.0.0.1:11732/test
>config={_id:'shard2',members:[{_id:0,host:'172.19.0.215:11732',arbiterOnly:true},{_id:1,host:'172.19.0.216:11732',priority:2},{_id:2,host:'172.19.0.217:11732'}]}
rs.initiate(config)
配置分片3在172.19.0.217上配置members里面的优先级priority值高的为主节点,对于仲裁点一定要加上arbiterOnly:true
[root@217~]#/home/mongodb/bin/mongo --port 11733
MongoDB shell version:2.4.7
connecting to:127.0.0.1:11733/test
>config={_id:'shard3',members:[{_id:0,host:'172.19.0.215:11733'},{_id:1,host:'172.19.0.216:11733',arbiterOnly:true},{_id:2,host:'172.19.0.217:11733',priority:2}]}
rs.initiate(config)
启动3台configserver
主机1
[root@215~]#/home/mongodb/bin/mongod -f/home/config/config.conf
主机2
[root@216~]#/home/mongodb/bin/mongod -f/home/config/config.conf
主机3
[root@217~]#/home/mongodb/bin/mongod -f/home/config/config.conf
启动3台RouteProcess
主机1
[root@215~]#/home/mongodb/bin/mongos -f/home/config/mongos.conf
主机2
[root@216~]#/home/mongodb/bin/mongos -f/home/config/mongos.conf
主机3
[root@217~]#/home/mongodb/bin/mongos -f/home/config/mongos.conf
配置shardCluser
[root@215~]#/home/mongodb/bin/mongo --port 60000
MongoDB shell version:2.4.7
connecting to:127.0.0.1:60000/test
mongos> useadmin
switched to dbadmin
mongos>db.runCommand({addshard:"shard1/172.19.0.215:11731,172.19.0.216:11731,172.19.0.217:11731"})
mongos>db.runCommand({addshard:"shard2/172.19.0.215:11732,172.19.0.216:11732,172.19.0.217:11732"})
mongos>db.runCommand({addshard:"shard3/172.19.0.215:11733,172.19.0.216:11733,172.19.0.217:11733"})
(注释:此处浪费我一段时间,上面的都能够正常启动,但是这个地方确报错,最好找同事帮忙查看后发现,每个IP后面的逗号多了一个空格,删除空格即可。各位如果在搭建的时候注意不要复制,如果复制要检查各个字母之后有没有空格和中文字母。)
接下来激活分片,如下面的代码所示:采用hash分片
db.runCommand({enablesharding:"test"})
db.runCommand({shardcollection:"test.users",key:{id:"hashed"}})
(注释:在最后这一个地方报错,见以下代码:
mongos> db.runCommand({enablesharding:"test"})
{ "ok" : 1 }
mongos>db.runCommand({shardcollection:"test.users",key:{id:"hashed"}})
{ "ok" : 0, "errmsg" : "shard keys must all be ascending"}
上面一行代码运行正常,下面这一行就提示碎片必须要按照升序。在百度查询之后,也没有找到类似错误的解决方法,最好在一个网友的点拨下,把第二行代码修改id:"hashed",修改后的语法为:
mongos>db.runCommand({shardcollection:"test.users",key:{id:1}})
{ "collectionsharded" : "test.users", "ok" : 1 }
mongos>
问题原因可能是版本的问题。有人说2.0.4的版本不支持分片。但是修改了之后就可以鸟!
)
验证分片是不是成功,见下文:
我们可以用任意一台连接mongodb,这里以215为例:
[root@215~]# /home/mongodb/bin/mongo --port 30000
MongoDB shell version: 2.0.4
connecting to: 127.0.0.1:30000/test
> show dbs
admin
config
local
>
这个地方如果你插入数据,会报错,如下文:
> usetext
switched to db text
> db.books.insert({x:"1",y:"2"})
can't create user databases on a --configsvr instance
>
这个时候我我检查插入语法,可是语法没有错呀,批量插入也是同样报这个错,最好在百度查询之后,经各位网友的指点,找到了结果,原因是:应该在路由里面去操作数据库,这个地方不能操作数据库,可是问题来了,哪个是我们的路由呢?记住,路由里面去showdbs
代码如下:进入路由
[root@215~]# /home/mongodb/bin/mongo --port 60000
MongoDB shell version: 2.0.4
connecting to: 127.0.0.1:60000/test
mongos> show dbs
config
test
mongos>
我们批量插入一段数据
mongos>use text
switched to db text
mongos> for(vari=1;i<2000;i++)db.books.insert({id:i,book_name:"PHP",book_id:"888"});
mongos> show collections
books
system.indexes
mongos> db.books.find()
{ "_id" : ObjectId("53d076484bbffdbd489d5801
{ "_id" : ObjectId("53d076484bbffdbd489d5802
{ "_id" : ObjectId("53d076484bbffdbd489d5803
我们可以看到,插入的数据已经成功了!
验证分片:
由于我们是做分片,所以我们应该去验证我们的分片是不是成功的,所以我们插入一条大的数据,202000条数据
mongos>show dbs
config
test
text
mongos> use test
switched to db test
mongos> show collections
system.indexes
users
mongos>db.users.count()
202000
mongos>
然后我们看一下分片的效果:
mongos>db.users.stats()
{
}
mongos>
注释:这个地方我们可以看到数据存在shard1分片中,并没有把202000条数据平均分布到其他的片键中,在查询资料和网友的沟通中,得知少量的数据有可能不会分配到其他的片键中,或者是版本的问题造成了没有平均分布插入数据。我们的版本是mongodb2.0.4,这个问题还要今后继续测试。也有人说是因为我们批量存入的数据是一样的,没有区分,所有mongodb没有按照不同的数据去存入。
我们输入:sh.status()
mongos>sh.status()
--- Sharding Status ---
mongos>
上面是我们的分片具体情况。
由于分片是昨天做的,今天早上来公司打开虚拟机的时候,发现又出了点问题。用60000的端口连接,报错如下:
[root@localhost ~]#
MongoDB shell version: 2.0.4
connecting to: 127.0.0.1:60000/test
mongos> show dbs
Thu Jul 24 09:36:50 uncaught exception: error { "$err" : "socketexception", "code" : 11002 }
最后问题竟然出现在启动的顺序上。
解决方案就是:
先确定我们的分片有没有问题,检查分片有没有问题运行netstat -tunlp查看我们的各个端口号是不是正常,发现正常呀,而且还多了一个61000的mongodb端口。可是我们也没有去设置这个端口啊,原来这个端口是自己生成的,具体是干嘛用的我还没有搞清楚!
[root@localhost ~]# ps axu | grepmongo
root
root
root
root
root
root
[root@localhost ~]# killall mongod
[root@localhost ~]# killall mongos
按照以下顺序启动,其他三台也是一样的先查出mongodb进程,然后杀死,然后和215同步一起启动:启动顺序如下
1,/home/mongodb/bin/mongod -f/home/config/shard1_1.conf
2,/home/mongodb/bin/mongod -f/home/config/shard2_1.conf
3,/home/mongodb/bin/mongod -f/home/config/shard3_1.conf
4,/home/mongodb/bin/mongod -f/home/config/config.conf
5,/home/mongodb/bin/mongos -f/home/config/mongos.conf
6,/home/mongodb/bin/mongos -f/home/config/mongos.conf
三台服务器全部启动之后,我们可以查看各个端口号是不是正常。在第六步没有启动的是时候连接的时候报错,其他三台正常连接,查看端口的时候发现少了一个端口。就重新启动了mongos.conf
全部启动之后我们进行连接:
[root@215~]# /home/mongodb/bin/mongo --port 60000
MongoDB shell version: 2.0.4
connecting to: 127.0.0.1:60000/test
mongos> show dbs
config
test
mongos> use test
switched to db test
mongos> show collections
system.indexes
users
mongos> db.users.find()
{ "_id" : ObjectId("53cf7f2239fa6a0f3720fa90
{ "_id" : ObjectId("53cf7f2239fa6a0f3720fa91
{ "_id" : ObjectId("53cf7f2239fa6a0f3720fa92
分片完成!
- 实战Replica Sets+Sharding方案采…
- 实战Replica Sets+Sharding方案
- 实战Replica Sets+Sharding方案采用配置文件
- 公司的实战Replica Sets+Sharding方案
- Replica Sets+Sharding方案之真枪实弹篇
- 公司的实战mongodb Replica Sets+Sharding方案采用配置文件2
- Centos上MongoDB数据库 Replica Sets +Sharding 方案实现
- MongoDB之Replica Sets + Sharding
- 第十章:Replica Sets + Sharding
- MongoDB Replica Sets + Sharding 方案 及 chunks块 和 片键分析
- (*)MongoDB Replica Sets + Sharding 方案 及 chunks块 和 片键分析
- MongoDB Replica Sets + Sharding 方案 及 chunks块 和 片键分析
- MongoDB学习整理之Replica Sets + Sharding
- Mongodb Replica Sets + Sharding/配置文件启动
- Mongodb的Replica Sets + Sharding Architect
- mongodb集群(Replica Sets+Sharding)
- MongoDB 常见集群部署Replica Sets + Sharding
- 第十章:MongoDB Replica Sets + Sharding搭建
- M​o​n​g​…
- MongoDB基本命令用
- 将博客搬至CSDN
- Hadoop Configuration详解
- mongodb的安装及主从复制
- 实战Replica Sets+Sharding方案采…
- Replica sets架构复制集(一)
- MongoDB auth认证
- Replica sets架构复制集(2)详解
- Replica sets架构复制集(3)详…
- 生成随机验证码
- HDU 2003 求绝对值
- 用python做自动化测试--对Java代码做单元测试 (2)-导入第三方jar包
- Linux系统下多个定时器的管理