MongoDB分片式高可用集群搭建
文章目录
一. 配置SSM免密登录
ssh-keygen //生成公钥和私钥,三次回车就行
ssh-copy-id 172.16.0.65 //将授权池文件远程发送
三台机器上都要执行
二. 关闭防火墙
firewalld|iptable 和seLinux都要关闭
CentOS7中:
systemctl stop firewalld.service //临时关闭防火墙
systemctl disable firewalld.service //禁止开机开启防火墙
firewall-cmd --state //查看防火墙状态
关闭seLinux
/etc/selinux/config
将SELINUX的值修改为disable,重启
CentOS6中:
service iptables stop
三. 上传文件和解压
tar -zxvf [安装包] -C [指定目录]
四. 创建文件目录
mkdir -p /data/mongodb/mongos/{log,conf}
mkdir -p /data/mongodb/mongoconf/{data,log,conf}
mkdir -p /data/mongodb/shard1/{data,log,conf}
mkdir -p /data/mongodb/shard2/{data,log,conf}
mkdir -p /data/mongodb/shard3/{data,log,conf}
touch /data/mongodb/mongos/log/mongos.log
touch /data/mongodb/mongoconf/log/mongoconf.log
touch /data/mongodb/shard1/log/shard1.log
touch /data/mongodb/shard2/log/shard2.log
touch /data/mongodb/shard3/log/shard3.log
五. 配置复制集
在/home/kqbigdata/mongodb-3.6.15/data/mongoconf/conf中创建以下文件mongoconf.conf,三台机器都创建
dbpath=/home/kqbigdata/mongodb-3.6.15/data/mongoconf/data
logpath=/home/kqbigdata/mongodb-3.6.15/data/mongoconf/log/mongoconf.log
logappend=true
bind_ip=0.0.0.0
port=21000
journal=true
fork=true
syncdelay=60
oplogSize=1000
configsvr=true
replSet=cqsm1
三台机器都在bin目录下启动mongd
./mongod -f /home/kqbigdata/mongodb-3.6.15/data/mongoconf/conf/mongoconf.conf
进入任意一台机器初始化副本集
./mongo 172.16.0.67:21000 如果端口不是默认的27017一定要加ip和端口号
use admin 进入admin数据库中进行初始化配置
config = {_id:"cqsm1",members:[
{_id:0,host:"172.16.0.67:21000"},
{_id:1,host:"172.16.0.66:21000"},
{_id:2,host:"172.16.0.65:21000"},]
}
cqsm1:SECONDARY> rs.initiate(config);
{
"info" : "try querying local.system.replset to see current configuration",
"ok" : 0,
"errmsg" : "already initialized",
"code" : 23,
"codeName" : "AlreadyInitialized",
"operationTime" : Timestamp(1575533876, 3),
"$gleStats" : {
"lastOpTime" : Timestamp(1575533861, 1),
"electionId" : ObjectId("7fffffff0000000000000001")
},
"$clusterTime" : {
"clusterTime" : Timestamp(1575533876, 3),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
cqsm1:PRIMARY>
六. 配置shard集群
1.shard1配置
在/home/kqbigdata/mongodb-3.6.15/data/shard1/conf中创建文件shard1.conf
dbpath=/home/kqbigdata/mongodb-3.6.15/data/shard1/data
logpath=/home/kqbigdata/mongodb-3.6.15/data/shard1/log/shard1.log
bind_ip=0.0.0.0
port=22001
logappend=true
#nohttpinterface=true
fork=true
oplogSize=4096
journal=true
#engine=wiredTiger
#cacheSizeGB=38G
smallfiles=true
shardsvr=true
replSet=shard1
启动mongod
./mongod -f /home/kqbigdata/mongodb-3.6.15/data/shard1/conf/shard.conf 三台都启动
成功:
child process started successfully, parent exiting
在第二台机器上初始化shard1
./mongo 172.16.0.66:22001
>config = {_id:"shard1",members:[
{_id:0,host:"172.16.0.67:22001"},
{_id:1,host:"172.16.0.66:22001"},
{_id:2,host:"172.16.0.65:22001"},]
}
rs.initiate(config)
出现错误:
No host described in new configuration 1 for replica set shard1 maps to this node
检查发现config中的端口号有没有写错
> rs.initiate(config)
{
"ok" : 0,
"errmsg" : "No host described in new configuration 1 for replica set shard1 maps to this node",
"code" : 93,
"codeName" : "InvalidReplicaSetConfig"
}
> config = {_id:"shard1",members:[
...
... {_id:0,host:"172.16.0.67:22001"},
...
... {_id:1,host:"172.16.0.66:22001"},
...
... {_id:2,host:"172.16.0.65:22001"},]
...
... }
{
"_id" : "shard1",
"members" : [
{
"_id" : 0,
"host" : "172.16.0.67:22001"
},
{
"_id" : 1,
"host" : "172.16.0.66:22001"
},
{
"_id" : 2,
"host" : "172.16.0.65:22001"
}
]
}
> rs.initiate(config)
{ "ok" : 1 }
2.shard2配置
在/home/kqbigdata/mongodb-3.6.15/data/shard2/conf中创建文件shard2.conf
dbpath=/home/kqbigdata/mongodb-3.6.15/data/shard2/data
logpath=/home/kqbigdata/mongodb-3.6.15/data/shard2/log/shard2.log
bind_ip=0.0.0.0
port=22002
logappend=true
#nohttpinterface=true
fork=true
oplogSize=4096
journal=true
#engine=wiredTiger
#cacheSizeGB=38G
smallfiles=true
shardsvr=true
replSet=shard2
分别在三个节点是启动进程
./mongod -f /home/kqbigdata/mongodb-3.6.15/data/shard2/conf/shard.conf
在第一台机器进行shard2的初始化
config = {_id:"shard2",members:[
{_id:0,host:"172.16.0.67:22002"},
{_id:1,host:"172.16.0.66:22002"},
{_id:2,host:"172.16.0.65:22002"},]
}
rs.initiate(config)
3.shard3的配置
在/home/kqbigdata/mongodb-3.6.15/data/shard3/conf中创建文件shard3.conf
dbpath=/home/kqbigdata/mongodb-3.6.15/data/shard3/data
logpath=/home/kqbigdata/mongodb-3.6.15/data/shard3/log/shard3.log
bind_ip=0.0.0.0
port=22003
logappend=true
#nohttpinterface=true
fork=true
oplogSize=4096
journal=true
#engine=wiredTiger
#cacheSizeGB=38G
smallfiles=true
shardsvr=true
replSet=shard3
三个节点都启动
./mongod -f /home/kqbigdata/mongodb-3.6.15/data/shard3/conf/shard.conf
初始化
./mongo 172.16.0.67:22003
config = {_id:"shard3",members:[
{_id:0,host:"172.16.0.67:22003"},
{_id:1,host:"172.16.0.66:22003"},
{_id:2,host:"172.16.0.65:22003"},]
}
rs.initiate(config)
七. 配置mongos服务器
目前三台服务器的配置服务器和分片服务器均已启动,配置三台mongos服务器,由于mongos服务器的配置是从内存中加载,所以自己没有存在数据目录configdb连接为配置服务器集群。
在/home/kqbigdata/mongodb-3.6.15/data/mongos/conf中创建文件mongos.conf
logpath=/home/kqbigdata/mongodb-3.6.15/data/mongos/log/mongos.log
logappend=true
bind_ip=0.0.0.0
port=20000
maxConns=1000
configdb=cqsm1/172.16.0.65:21000,172.16.0.66:21000,172.16.0.67:21000
fork=true
三台机器均开启mongod
./mongos -f /home/kqbigdata/mongodb-3.6.15/data/mongos/conf/mongos.conf
出现错误:
Error parsing INI config file: unrecognised option 'configdb'
首先查看配置文档有没有错误,还有启动命令是mongos不是mongod
八.串联路由服务器
目前搭建了mongodb配置服务器、路由服务器,各个分片服务器,不过应用程序连接到mongos路由服务器并不能使用分片机制,还需要在程序里设置分片配置,让分片生效。
登陆任意一台mongos
>./mongo 172.16.0.67:20000
>use admin
>sh.addShard("shard1/172.16.0.65:22001,172.16.0.66:22001,172.16.0.67:22001");
>sh.addShard("shard2/172.16.0.65:22002,172.16.0.66:22002,172.16.0.67:22002");
>sh.addShard("shard3/172.16.0.65:22003,172.16.0.66:22003,172.16.0.67:22003");
查看集群状态:
mongos> sh.status()
--- Sharding Status ---
sharding version: {
"_id" : 1,
"minCompatibleVersion" : 5,
"currentVersion" : 6,
"clusterId" : ObjectId("5de8bd325989697ceb9f1e0a")
}
shards:
{ "_id" : "shard1", "host" : "shard1/172.16.0.65:22001,172.16.0.66:22001,172.16.0.67:22001", "state" : 1 }
{ "_id" : "shard2", "host" : "shard2/172.16.0.65:22002,172.16.0.66:22002,172.16.0.67:22002", "state" : 1 }
{ "_id" : "shard3", "host" : "shard3/172.16.0.65:22003,172.16.0.66:22003,172.16.0.67:22003", "state" : 1 }
active mongoses:
"3.6.15" : 3
autosplit:
Currently enabled: yes
balancer:
Currently enabled: yes
Currently running: no
Failed balancer rounds in last 5 attempts: 0
Migration Results for the last 24 hours:
No recent migrations
databases:
{ "_id" : "config", "primary" : "config", "partitioned" : true }
九.测试 启用集合分片生效
目前配置服务、路由服务、分片服务、副本集服务都已经串联起来了,但我们的目的是希望插入数据,数据能够自动分片。连接在mongos上,准备让指定的数据库、指定的集合分片生效。
登陆任意一台mongos
db.runCommand( { enablesharding :"testdb"});
或
mongos> sh.enablesharding("testdb")
指定需要分片的集合和片键
db.runCommand( { shardcollection : "testdb.table1",key : {"name": "hashed"} } );
或
mongos> sh.shardcollection("testdb.table1", {"name": "hashed"})
指定之后会创建指定的数据库,进入数据库,插入数据
use testdb;
for(i=1;i<=100000;i++){db.table1.insert({"id":i,"name":"penglei"})}; //插入数据
db.table1.aggregate([{$group : {_id : "$name", totle : {$sum : 1}}}]) //查看插入的总数据
查看集群状态
db.table1.stats();
十.干净的退出
1.进入admin数据关闭
>use admin
>db.shutdownServer()
2.使用mongod命令关闭
mongod --shutdown --dbpath
./mongod --shutdown --dbpath /home/kqbigdata/mongodb-3.6.15/data/mongoconf/data/
3.ps -ef | grep mongo kill [PID]
注意:如果意外退出,会报错
错误:
child process failed, exited with error number 51
To see additional information in this output, start without the "--fork" option.
解决方案
删除 mongod.lock 文件和日志文件 mongodb.log.2014-11-17T06-55-20 ,如果有必要把 log日志全部删除
使用命令netstat -ntlp 查看mongod占用端口,使用kill -9 [PID]杀掉进程