配置mongodb分片集群

关于mongodb分片集群的逻辑架构图可以参考以下链接,说的很详细了

http://www.lanceyan.com/tech/arch/mongodb_shard1.html

下面直接进入部署

部署mongodb

cd /home/zlfzy/tmp
http://zlfapp.oss-cn-shenzhen.aliyuncs.com/mongodb-linux-x86_64-3.2.7.tgz
tar xf mongodb-linux-x86_64-3.2.7.tgz -C /home/zlfzy/app
ln -s /home/yxc/app/mongodb-linux-x86_64-3.2.7/ /home/yxc/app/mongodb
vim /etc/profile
export PATH=/home/yxc/app/mongodb/bin:$PATH:$HOME/bin
source /etc/profile

由于是测试环境,我只用了一台服务器,环境如下
包括三个shard,每个shard里面都有三个mongodb实例组成的副本集;3个config server,一个mongos
shard1:

10.27.233.29:10001
10.27.233.29:10002
10.27.233.29:10003

shard2:

10.27.233.29:20001
10.27.233.29:20002
10.27.233.29:20003

shard3:

10.27.233.29:30001
10.27.233.29:30002
10.27.233.29:30003

config_server:

10.27.233.29:40001
10.27.233.29:40002
10.27.233.29:40003

mongos:

10.27.233.29:50001

开始部署shard和副本集

cd /home/yxc/app/mongodb
mkdir {configsvr,mongos,shard}   #config_server,mongos,shard三个文件夹
cd shard
mkdir {shard1,shard2,shard3}
cd shard1
mkdir {10001,10002,10003}
cd 10001
mkdir {data,log}
vim 10001.conf
目录内容如下:
[root@zlfzy 10001]# ll
total 12
-rw-r--r-- 1 root root  167 Jun 10 23:46 10001.conf
drwxr-xr-x 4 root root 4096 Jun 11 00:40 data
drwxr-xr-x 2 root root 4096 Jun 10 23:28 log

[root@zlfzy 10001]# cat 10001.conf 
logpath=/home/zlfzy/app/mongodb/shard/shard1/10001/log/mongo.log
dbpath=/home/zlfzy/app/mongodb/shard/shard1/10001/data
logappend=true
fork=true
port=10001
replSet=shard1
mongod -f 10001.conf

即可启动一个mongodb实例,其它所有实例同理,只需要修改对应的端口和文件夹名对应即可。
注意shard2的三个实例,conf文件里面的要改为replSet=shard2,shard3同理。

下面开始配置副本集以及初始化

    mongo --port 10001
    >config={
    "_id":"shard1",
    "members":[
    {"_id":1,"host":"10.27.233.29:10001"},
    {"_id":2,"host":"10.27.233.29:10002"},
    {"_id":3,"host":"10.27.233.29:10003"}
    ]
    }
    
    rs.initiate(config)
    rs.status()
    exit

另外两个副本集同理
如此完成3个带副本集的分片的部署

下面开始部署config server

cd configsvr
mkdir {40001,40002,40003}
cd 40001
mkdir {data,log}
vim 40001.conf
[root@zlfzy 40001]# cat 40001.conf 
dbpath=/home/zlfzy/app/mongodb/configsvr/40001/data
logpath=/home/zlfzy/app/mongodb/configsvr/40001/log/cfgsvr_1.log
configsvr=true
fork=true
logappend=true
port=40001
mongod -f 40001.conf

另外两个端口40002,40003同理

下面开始配置mongos

cd mongos
mkdir 50001
cd 50001
mkdir log
vim 50001.conf
[root@zlfzy 50001]# cat 50001.conf 
logpath=/home/zlfzy/app/mongodb/mongos/50001/log/mongos_1.log
port=50001
fork=true
logappend=true
configdb=10.27.233.29:40001,10.27.233.29:40002,10.27.233.29:40003
mongos -f 50001.conf

完成集群部署


开始测试:

1、连接到mongos

[root@zlfzy 50001]# mongo --port 50001

2、添加分片到集群

mongos> use admin
switched to db admin
mongos> sh.addShard("shard1/10.27.233.29:10001")
{ "shardAdded" : "shard1", "ok" : 1 }
mongos> sh.addShard("shard2/10.27.233.29:20001")
{ "shardAdded" : "shard2", "ok" : 1 }
mongos> sh.addShard("shard3/10.27.233.29:30001")
{ "shardAdded" : "shard3", "ok" : 1 }

3、查看分片配置

mongos> db.runCommand({listshards:1})
{
    "shards" : [
        {
            "_id" : "shard1",
            "host" : "shard1/10.27.233.29:10001,10.27.233.29:10002,10.27.233.29:10003"
        },
        {
            "_id" : "shard2",
            "host" : "shard2/10.27.233.29:20001,10.27.233.29:20002,10.27.233.29:20003"
        },
        {
            "_id" : "shard3",
            "host" : "shard3/10.27.233.29:30001,10.27.233.29:30002,10.27.233.29:30003"
        }
    ],
    "ok" : 1
}

4、把zlfzy库开启分片,插入10000条测试数据

mongos> db.runCommand({"enablesharding":"zlfzy"})
{ "ok" : 1 }
mongos> sh.shardCollection("zlfzy.person",{_id:'hashed'})  
{ "collectionsharded" : "zlfzy.person", "ok" : 1 }
mongos> use zlfzy
switched to db zlfzy
mongos> for(var i=0;i<10000;i++){db.person.insert({name:"zlfzy"+i});} 
WriteResult({ "nInserted" : 1 })
mongos> db.person.count()
10000

5、登陆各个分片查看有各有多少条数据
shard1:

[root@zlfzy ~]# mongo --port 10001
shard1:PRIMARY> use zlfzy
switched to db zlfzy
shard1:PRIMARY> db.person.count()
3406
shard1:PRIMARY> exit
bye

shard2:

[root@zlfzy ~]# mongo --port 20001
shard2:PRIMARY> use zlfzy
switched to db zlfzy
shard2:PRIMARY> db.person.count()
3296
shard2:PRIMARY> exit
bye

shard3:

[root@zlfzy ~]# mongo --port 30001
shard3:PRIMARY> use zlfzy
switched to db zlfzy
shard3:PRIMARY> db.person.count()
3298
shard3:PRIMARY> exit
bye

可以看到,10000条数据被几乎平均分配到每个分片
每个分片都是有1个PRIMARY和两个SECONDARY组成的副本集,如果需要登陆副本集检查数据是否完整

[root@zlfzy ~]# mongo --port 20002
shard2:SECONDARY> rs.slaveOk();    #先执行这一步,因为SECONDARY默认不能读写,否则报错“not master and slaveOk=false”
shard2:SECONDARY> use zlfzy
switched to db zlfzy
shard2:SECONDARY> db.person.count()
3296
shard2:SECONDARY> exit
bye

6、查看分片状态

mongos> printShardingStatus()
--- Sharding Status --- 
  sharding version: {
    "_id" : 1,
    "minCompatibleVersion" : 5,
    "currentVersion" : 6,
    "clusterId" : ObjectId("593c1fd0c102af620da65add")
}
  shards:
    {  "_id" : "shard1",  "host" : "shard1/10.27.233.29:10001,10.27.233.29:10002,10.27.233.29:10003" }
    {  "_id" : "shard2",  "host" : "shard2/10.27.233.29:20001,10.27.233.29:20002,10.27.233.29:20003" }
    {  "_id" : "shard3",  "host" : "shard3/10.27.233.29:30001,10.27.233.29:30002,10.27.233.29:30003" }
  active mongoses:
    "3.2.7" : 1
  balancer:
    Currently enabled:  yes
    Currently running:  no
    Failed balancer rounds in last 5 attempts:  0
    Migration Results for the last 24 hours: 
        2 : Success
  databases:
    {  "_id" : "test",  "primary" : "shard1",  "partitioned" : true }
        test.person
            shard key: { "_id" : "hashed" }
            unique: false
            balancing: true
            chunks:
                shard1  2
                shard2  2
                shard3  2
            { "_id" : { "$minKey" : 1 } } -->> { "_id" : NumberLong("-6148914691236517204") } on : shard1 Timestamp(3, 2) 
            { "_id" : NumberLong("-6148914691236517204") } -->> { "_id" : NumberLong("-3074457345618258602") } on : shard1 Timestamp(3, 3) 
            { "_id" : NumberLong("-3074457345618258602") } -->> { "_id" : NumberLong(0) } on : shard2 Timestamp(3, 4) 
            { "_id" : NumberLong(0) } -->> { "_id" : NumberLong("3074457345618258602") } on : shard2 Timestamp(3, 5) 
            { "_id" : NumberLong("3074457345618258602") } -->> { "_id" : NumberLong("6148914691236517204") } on : shard3 Timestamp(3, 6) 
            { "_id" : NumberLong("6148914691236517204") } -->> { "_id" : { "$maxKey" : 1 } } on : shard3 Timestamp(3, 7) 

mongos> 

7、向集群中添加分片
继续上面的步骤,我创建了三个mongodb实例并组成副本集
端口分别为22221,22222,22223
把三个实例组成副本集:

[root@zlfzy shard4]# mongo --port 22221
> config={
... "_id":"shard4",
... "members":[
... {"_id":1,"host":"10.27.233.29:22221"},
... {"_id":2,"host":"10.27.233.29:22222"},
... {"_id":3,"host":"10.27.233.29:22223"}
... ]
... }
{
    "_id" : "shard4",
    "members" : [
        {
            "_id" : 1,
            "host" : "10.27.233.29:22221"
        },
        {
            "_id" : 2,
            "host" : "10.27.233.29:22222"
        },
        {
            "_id" : 3,
            "host" : "10.27.233.29:22223"
        }
    ]
}
> rs.initiate(config)
{ "ok" : 1 }
shard4:OTHER> rs.status()

把新分片shard4添加到集群

[root@zlfzy]# mongo --port 50001
mongos> sh.addShard("shard4/10.27.233.29:22221")
{ "shardAdded" : "shard4", "ok" : 1 }
mongos> db.printShardingStatus();
mongos> exit

登陆shard4分片看看,已经有数据同步过来

[root@zlfzy]# mongo --port 22221
shard4:PRIMARY> use zlfzy
switched to db zlfzy
shard4:PRIMARY> db.person.count()
1728
shard4:PRIMARY> exit
bye

8、如何确定数据库已启用分片?

[root@zlfzy]# mongo --port 50001
mongos> use zlfzy
switched to db zlfzy
mongos> db.runCommand({isdbgrid:1})
{ "isdbgrid" : 1, "hostname" : "zlfzy", "ok" : 1 }    #若isdbgrid字段的结果为1,则是分片,为0则不是。
mongos> 

9、分片集群启用账号密码认证
生成密钥

openssl rand -base64 741 > /opt/keyfile

把keyfile添加到所有集群机器(实例)的conf

/home/yxc/app/mongodb
for i in `find . -name "*.conf"`;do echo "keyFile=/opt/keyfile" >> $i;done  #因为我的集群都部署在一台机器,所以简单快捷

先关闭mongos

ss -lntup|grep 50001
kill $pid

然后关闭config server

cd /home/yxc/app/mongodb/configsvr
for i in `find . -type d -name "data"`;do mongod --shutdown --dbpath=$i;done

最后关闭shard

cd /home/yxc/app/mongodb/shard
for i in `find . -type d -name "data"`;do mongod --shutdown --dbpath=$i;done

首先启动shard

for i in `find . -name "*.conf"`;do mongod -f $i;done 

然后启动config server

cd /home/yxc/app/mongodb/configsvr
for i in `find . -name "*.conf"`;do mongod -f $i;done

最后启动mongos

mongos -f 50001.conf

验证:

[root@zlfzy]# mongo --port 50001
mongos> use admin
switched to db admin
mongos> show dbs
2017-06-12T17:49:53.338+0800 E QUERY    [thread1] Error: listDatabases failed:{
    "ok" : 0,
    "errmsg" : "not authorized on admin to execute command { listDatabases: 1.0 }",
    "code" : 13
} :
_getErrorWithCode@src/mongo/shell/utils.js:25:13
Mongo.prototype.getDBs@src/mongo/shell/mongo.js:62:1
shellHelper.show@src/mongo/shell/utils.js:760:19
shellHelper@src/mongo/shell/utils.js:650:15
@(shellhelp2):1:1

使用密码认证

mongos> use admin
switched to db admin
mongos> db.auth('admin','zlf88')
1
mongos> show dbs
admin   0.000GB
config  0.001GB
test    0.000GB
zlfzy   0.001GB
mongos> exit
bye

参考资料:

http://blog.csdn.net/zhurhyme/article/details/51967541
http://blog.csdn.net/xgjianstart/article/details/52192972
添加新评论