今天要帮公司搭建一个MongoDB的集群,记录一下搭建的过程。
系统环境
Ubuntu 18.04.1 LTS
安装Docker
sudo apt install -y docker.io
安装完毕,查看一下Docker版本:
dafengge0913@ubuntu:~$ docker -v
Docker version 18.06.1-ce, build e68fc7a
拉取MongoDB镜像
sudo docker pull mongo
查看镜像:
dafengge0913@ubuntu:~$ sudo docker images mongo
REPOSITORY TAG IMAGE ID CREATED SIZE
mongo latest 525bd2016729 4 weeks ago 383MB
使用Docker启动MongoDB
sudo docker run -p 27017:27017 -v $PWD/db:/data/db -d mongo:latest
查看运行状态:
dafengge0913@ubuntu:~/mongo$ sudo docker ps -a --no-trunc
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
bd27d09493c1fd663014410bf1cdf7227326cccb28733738311aa1cfc0cb7647 mongo:latest "docker-entrypoint.sh mongod" 23 seconds ago Up 22 seconds 0.0.0.0:27017->27017/tcp distracted_snyder
连接容器中的MongoDB:
sudo docker run -it mongo:latest mongo --host 172.17.0.1
搭建副本集和集群
集群包含两组分片副本集
每个副本集一个主节点、一个从节点、一个仲裁节点
配置副本集包含三个节点
使用一个mongos路由
为了方便 写一个启动脚本
#!/bin/sh
sudo docker run -p 27001:27018 -v $PWD/db1:/data/db -d mongo:latest --shardsvr --replSet rs1 --port 27018 --bind_ip 0.0.0.0
sudo docker run -p 27002:27018 -v $PWD/db2:/data/db -d mongo:latest --shardsvr --replSet rs1 --port 27018 --bind_ip 0.0.0.0
sudo docker run -p 27003:27018 -v $PWD/db3:/data/db -d mongo:latest --shardsvr --replSet rs1 --port 27018 --bind_ip 0.0.0.0
sudo docker run -p 27004:27018 -v $PWD/db4:/data/db -d mongo:latest --shardsvr --replSet rs2 --port 27018 --bind_ip 0.0.0.0
sudo docker run -p 27005:27018 -v $PWD/db5:/data/db -d mongo:latest --shardsvr --replSet rs2 --port 27018 --bind_ip 0.0.0.0
sudo docker run -p 27006:27018 -v $PWD/db6:/data/db -d mongo:latest --shardsvr --replSet rs2 --port 27018 --bind_ip 0.0.0.0
sudo docker run -p 27007:27018 -v $PWD/db_config_1:/data/db -d mongo:latest --configsvr --dbpath /data/db --replSet rsc --port 27018 --bind_ip 0.0.0.0
sudo docker run -p 27008:27018 -v $PWD/db_config_2:/data/db -d mongo:latest --configsvr --dbpath /data/db --replSet rsc --port 27018 --bind_ip 0.0.0.0
sudo docker run -p 27009:27018 -v $PWD/db_config_3:/data/db -d mongo:latest --configsvr --dbpath /data/db --replSet rsc --port 27018 --bind_ip 0.0.0.0
sudo docker run -p 27100:27018 -v $PWD/db_mongos_1:/data/db -d mongo:latest mongos --port 27018 --configdb rsc/172.17.0.1:27007,172.17.0.1:27008,172.17.0.1:27008 --bind_ip 0.0.0.0
初始化副本集rs1
sudo docker run -it mongo:latest mongo --host 172.17.0.1 --port 27001
> use admin
switched to db admin
> config = {_id:"rs1",members:[{_id:0,host:"172.17.0.1:27001"},{_id:1,host:"172.17.0.1:27002"},{_id:2,host:"172.17.0.1:27003",arbiterOnly:true}]}
{
"_id" : "rs1",
"members" : [
{
"_id" : 0,
"host" : "172.17.0.1:27001"
},
{
"_id" : 1,
"host" : "172.17.0.1:27002"
},
{
"_id" : 2,
"host" : "172.17.0.1:27003",
"arbiterOnly" : true
}
]
}
> rs.initiate(config)
{
"ok" : 1,
"operationTime" : Timestamp(1545715075, 1),
"$clusterTime" : {
"clusterTime" : Timestamp(1545715075, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
初始化副本集rs2
sudo docker run -it mongo:latest mongo --host 172.17.0.1 --port 27004
> use admin
> config = {_id:"rs2",members:[{_id:0,host:"172.17.0.1:27004"},{_id:1,host:"172.17.0.1:27005"},{_id:2,host:"172.17.0.1:27006",arbiterOnly:true}]}
> rs.initiate(config)
初始化配置副本集
sudo docker run -it mongo:latest mongo --host 172.17.0.1 --port 27007
> use admin
> config = {_id:"rsc",members:[{_id:0,host:"172.17.0.1:27007"}, {_id:1,host:"172.17.0.1:27008"},{_id:2,host:"172.17.0.1:27009"}]}
> rs.initiate(config)
通过mongos添加分片关系到configsvr
sudo docker run -it mongo:latest mongo --host 172.17.0.1 --port 27100
mongos> use admin
switched to db admin
mongos> db.runCommand({addshard:"rs1/172.17.0.1:27001,172.17.0.1:27002,172.17.0.1:27003"})
{
"shardAdded" : "rs1",
"ok" : 1,
"operationTime" : Timestamp(1545719532, 6),
"$clusterTime" : {
"clusterTime" : Timestamp(1545719532, 6),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
mongos> db.runCommand({addshard:"rs2/172.17.0.1:27004,172.17.0.1:27005,172.17.0.1:27006"})
{
"shardAdded" : "rs2",
"ok" : 1,
"operationTime" : Timestamp(1545719538, 5),
"$clusterTime" : {
"clusterTime" : Timestamp(1545719538, 5),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
mongos>
停止集群
写一个停止用的脚本:
#!/bin/sh
sudo docker stop $(sudo docker ps -a | grep mongo | grep Up | awk '{print $1}')