天天看点

Centos-mongodb副本集加分片配置

Centos升级到7之后,发现无法使用iptables控制Linuxs的端口,google之后发现Centos 7使用firewalld代替了原来的iptables。下面记录如何使用firewalld开放Linux端口:

firewall-cmd --zone=public --add-port=27017/tcp --permanent
firewall-cmd --zone=public --add-port=27018/tcp --permanent
firewall-cmd --zone=public --add-port=20000/tcp --permanent
firewall-cmd --zone=public --add-port=30000/tcp --permanent           

重启防火墙

firewall-cmd --reload           

复制集加分片(192.168.1.222、192.168.1.223、192.168.1.224)

mkdir /usr/local/mongodb/data/log1
mkdir /usr/local/mongodb/data/log2
mkdir /usr/local/mongodb/data/shard1
mkdir /usr/local/mongodb/data/shard2
mkdir /usr/local/mongodb/data/config
           

1、配置shard1复制集(3台都运行)

/usr/nacp/mongodb-linux-x86_64-rhel70-3.4.0/bin/mongod --shardsvr --dbpath /usr/local/mongodb/data/shard1 --logpath=/usr/local/mongodb/data/log1/shard1.log --logappend --port 27017 --replSet shard1 -fork
/usr/nacp/mongodb-linux-x86_64-rhel70-3.4.0/bin/mongo 192.168.1.222:27017
use admin
#定义副本集配置
db.runCommand({"replSetInitiate":{"_id":"shard1","members":[{"_id":1,"host":"192.168.1.222:27017"},{"_id":2,"host":"192.168.1.223:27017"},{"_id":3,"host":"192.168.1.224:27017"}]}});
rs.status();           

2、配置shard2复制集(3台都运行)

/usr/nacp/mongodb-linux-x86_64-rhel70-3.4.0/bin/mongod --shardsvr --dbpath /usr/local/mongodb/data/shard2 --logpath=/usr/local/mongodb/data/log2/shard2.log --logappend --port 27018  --replSet shard2 -fork
/usr/nacp/mongodb-linux-x86_64-rhel70-3.4.0/bin/mongo 192.168.1.224:27018
use admin
#定义副本集配置
config = { _id:"shard2", members:[
    {_id:0,host:"192.168.1.222:27018"},
    {_id:1,host:"192.168.1.223:27018"},
    {_id:2,host:"192.168.1.224:27018"}
    ]
}
rs.initiate(config);           

3、配置config(3台都运行)

/usr/nacp/mongodb-linux-x86_64-rhel70-3.4.0/bin/mongod --port 20000 --configsvr --dbpath=/usr/local/mongodb/data/config --logpath=/usr/local/mongodb/data/config/config.log --logappend --replSet config --fork 
/usr/nacp/mongodb-linux-x86_64-rhel70-3.4.0/bin/mongo 192.168.1.223:20000
use admin
#定义副本集配置
db.runCommand({"replSetInitiate":{"_id":"config","members":[{"_id":1,"host":"192.168.1.222:20000"},{"_id":2,"host":"192.168.1.223:20000"},{"_id":3,"host":"192.168.1.224:20000"}]}});           

4、配置mongodb路由(3台都运行)

/usr/nacp/mongodb-linux-x86_64-rhel70-3.4.0/bin/mongos --port 30000 --configdb config/192.168.1.222:20000,192.168.1.223:20000,192.168.1.224:20000 --logpath=/usr/local/mongodb/data/mongos.log --logappend --fork           

配置shard

/usr/nacp/mongodb-linux-x86_64-rhel70-3.4.0/bin/mongo 192.168.1.222:30000
use admin
db.runCommand({ addshard:"shard1/192.168.1.222:27017,192.168.1.223:27017,192.168.1.224:27017" })
db.runCommand({ addshard:"shard2/192.168.1.222:27018,192.168.1.223:27018,192.168.1.224:27018" })           

设置分片数据库:

db.runCommand({"enablesharding": "test"})            

设置表分片基于字段hash设置表分片基于字段

db.runCommand({"shardcollection": "test.t1", key: { id:1}})           

打印数据库分片信息:

db.printShardingStatus()              

打印服务器信息:

db.serverStatus()            

测试

use test
for (var i = 1; i <= 10000; i++) db.t1.save({id:i,"test1":"testval1"});