**创建目录
在每台机器上操作此步骤
一.在编写容器文件之前的注意事项:**
1.yaml文件的指令前端不能使用tab键只能使用空格
2.storage: 指令的对接只能使用 : 不能使用 = 冒号的后面要跟着一个空格
3.docker-compose.yaml 文件中netwok指定一个网桥 最好不要使用默认的
- 生成网桥的指令:docker network create **** (****:是指网桥的名称)
4.生成容器后查看网桥的容器IP
- docker network inspect **** (****:指容器的name 或 id )
5.根据查看的网桥IP在墙上开放端口
- ufw allow from 172.19.0.0/16 (172.19.0.0/16:容器的IP 和使用的端口)
**cd /home/seeyii 当前用户
mkdir mongoCluster 项目文件夹
cd mongoCluster
vi mongod.conf 配置文件**
storage:
dbPath: /data/db
journal:
enabled: true
systemLog:
destination: file
logAppend: true
path: /var/log/mongodb/mongod.log
net:
maxIncomingConnections: 10000
replication:
oplogSizeMB: 10240
# security:
# keyFile: /data/mongodb/key.file
# authorization: enabled
vi mongos.conf 路由配置文件
systemLog:
destination: file
logAppend: true
path: /var/log/mongodb/mongos.log
net:
maxIncomingConnections: 10000
# security:
# keyFile: /data/mongodb/key.file
创建目录
vi first_mkdir.sh
mkdir -p /database_po/vol/conf/config
mkdir -p /database_po/vol/conf/db
mkdir -p /database_po/vol/shard1/config
mkdir -p /database_po/vol/shard1/db
mkdir -p /database_po/vol/shard1/backup
mkdir -p /database_po/vol/shard2/config
mkdir -p /database_po/vol/shard2/db
mkdir -p /database_po/vol/shard2/backup
mkdir -p /database_po/vol/mongos/config
mkdir -p /database_po/vol/mongos/db
生成认证文件 root 用户 多台共用一个
openssl rand -base64 741 > key.file
chmod 600 key.file
chown 999 key.file
mv key.file /database/vol
将配置文件放到指定位置
vi second_mv.sh
#!/bin/bash
for item in mongos shard1 shard2 conf
do
if [ $item = 'mongos' ]
then
echo "mongos"
cp mongos.conf /database_po/vol/$item/config/mongos.conf
else
echo "mongod"
cp mongod.conf /database_po/vol/$item/config/mongod.conf
fi
done
docker-compose.yaml 容器编排
将文件放在 mongoCluster目录中
启动 docker-compose up -d (-d :是指后台运行 up 会在当前的目录下自动寻找docker-compose.yaml 文件并执行 -f 可以自定义文件的位置)
**添加副本集
配置服务器**
连接任意一个节点 mongo --host 192.168.1.125 --port 20021
rs.initiate({
_id: "ms-config-server",
configsvr: true,
members: [
{ _id : 0, host : "192.168.1.125:20021" },
{ _id : 1, host : "192.168.1.126:20021" },
{ _id : 2, host : "192.168.1.127:20021" },
]
});
分片1
连接任意一个节点 mongo --host 192.168.1.125 --port 20031
rs.initiate({
_id: "ms-shard1-server",
members: [
{ _id : 0, host : "192.168.1.125:20031" },
{ _id : 1, host : "192.168.1.126:20031" },
{ _id : 2, host : "192.168.1.127:20031" },
]
});
分片2
连接任意一个节点 mongo --host 192.168.1.125 --port 20041
rs.initiate({
_id: "ms-shard2-server",
members: [
{ _id : 0, host : "192.168.1.125:20041" },
{ _id : 1, host : "192.168.1.126:20041" },
{ _id : 2, host : "192.168.1.127:20041" },
]
});
配置mongos
确保mongos服务起来之后,连接到192.168.1.125:10011执行以下命令添加分片服务器信息
sh.addShard("ms-shard1-server/192.168.1.125:20031,192.168.1.126:20031,192.168.1.127:20031")
sh.addShard("ms-shard2-server/192.168.1.125:20041,192.168.1.126:20041,192.168.1.127:20041")
添加用户认证
use admin db.createUser( { user:"root", pwd:"123456", roles:[{role:"root",db:"admin"}] } )
db.auth('root','123456')
security``:
删除 docker-compose rm
停止 docker-compose stop
重启 docker-compose restart
启动 docker-compose start
docker-compose 文件
docker-compose.yaml
version: '2'
services:
ms_config_server:
image: mongo:3.4
command: mongod -f /etc/mongod/mongod.conf --configsvr --replSet "ms-config-server"
volumes:
- /database_po/vol/key.file:/data/mongodb/key.file
- /database_po/vol/conf/config:/etc/mongod
- /database_po/vol/conf/db:/data/db
ports:
- "20021:27019"
restart:
always
container_name:
ms_config_server
ulimits:
nofile:
soft: 300000
hard: 300000
networks:
- study_net
ms_shard_server1:
image: mongo:3.4
command: mongod -f /etc/mongod/mongod.conf --directoryperdb --shardsvr --replSet "ms-shard1-server" --wiredTigerCacheSizeGB 13
volumes:
- /database_po/vol/key.file:/data/mongodb/key.file
- /database_po/vol/shard1/config:/etc/mongod
- /database_po/vol/shard1/db:/data/db
ports:
- "20031:27018"
restart:
always
container_name:
ms_shard_server1
ulimits:
nofile:
soft: 300000
hard: 300000
networks:
- study_net
ms_shard_server2:
image: mongo:3.4
command: mongod -f /etc/mongod/mongod.conf --directoryperdb --shardsvr --replSet "ms-shard2-server" --wiredTigerCacheSizeGB 13
volumes:
- /database_po/vol/key.file:/data/mongodb/key.file
- /database_po/vol/shard2/config:/etc/mongod
- /database_po/vol/shard2/db:/data/db
ports:
- "20041:27018"
restart:
always
container_name:
ms_shard_server2
ulimits:
nofile:
soft: 300000
hard: 300000
networks:
- study_net
ms_mongos_server:
image: mongo:3.4
command: mongos -f /etc/mongod/mongos.conf --configdb ms-config-server/192.168.1.125:20021,192.168.1.126:20021,192.168.1.127:20021
ports:
- "20011:27017"
volumes:
- /database_po/vol/key.file:/data/mongodb/key.file
- /database_po/vol/mongos/config:/etc/mongod
- /database_po/vol/mongos/db:/data/db
restart:
always
container_name:
ms_mongos_server
ulimits:
nofile:
soft: 300000
hard: 300000
networks:
- study_net
networks:
study_net:
external: true
手机扫一扫
移动阅读更方便
你可能感兴趣的文章