环境介绍
os: centos7
docker: 18.09.0
mongo: 4.0.5
1. 清理旧数据(如果需要)
执行 clean-deploy.sh
删除之前的容器
删除数据目录
vi clean-deploy.sh clean-deploy.sh
DIR=/data/fates
DATA_PATH="${DIR}/mongo"
PWD='kinnylee'
# 第一次执行没有旧数据,不需要执行这步
docker-compose -f fates-mongo-compose.yaml down
if [ -d "${DATA_PATH}" ]; then
echo "delete directory: ${DATA_PATH}"
echo ${PWD} | sudo -S rm -rf ${DATA_PATH}
fi
2. 创建数据目录
vi before-deploy.sh before-deploy.sh
DIR=/data/fates
DATA_PATH="${DIR}/mongo"
PWD='kinnylee'
DATA_DIR_LIST=('config1' 'config2' 'config3' 'shard1' 'shard2' 'shard3' 'script')
function check_directory() {
if [ ! -d "${DATA_PATH}" ]; then
echo "create directory: ${DATA_PATH}"
echo ${PWD} | sudo -S mkdir -p ${DATA_PATH}
else
echo "directory ${DATA_PATH} already exists."
fi
cd "${DATA_PATH}"
for SUB_DIR in ${DATA_DIR_LIST[@]}
do
if [ ! -d "${DATA_PATH}/${SUB_DIR}" ]; then
echo "create directory: ${DATA_PATH}/${SUB_DIR}"
echo "${PWD}" | sudo -S mkdir -p "${DATA_PATH}/${SUB_DIR}"
else
echo "directory: ${DATA_PATH}/${SUB_DIR} already exists."
fi
done
echo "change directory owner to $USER:$USER"
echo "${PWD}" | sudo -S chown -R $USER:$USER "${DATA_PATH}"
}
check_directory
3. 部署docker容器
3.2 授权模式
1. vi generate-keyfile.sh generate-keyfile.sh
生成keyfile文件
#!/bin/bash
DATA_PATH=/data/fates/mongo
PWD='kinnylee'
function check_directory() {
if [ ! -d "${DATA_PATH}" ]; then
echo "directory: ${DATA_PATH} not exists, please run before-depoly.sh."
fi
}
function generate_keyfile() {
cd "${DATA_PATH}/script"
if [ ! -f "${DATA_PATH}/script/mongo-keyfile" ]; then
echo 'create mongo-keyfile.'
openssl rand -base64 756 -out mongo-keyfile
echo "${PWD}" | sudo -S chmod 600 mongo-keyfile
echo "${PWD}" | sudo -S chown 999 mongo-keyfile
else
echo 'mongo-keyfile already exists.'
fi
}
check_directory
generate_keyfile
vi fates-mongo-compose-key.yaml fates-mongo-compose-key.yaml
version: '3.4'
services:
shard1:
image: mongo:4.0.5
# --shardsvr: 这个参数仅仅只是将默认的27017端口改为27018,如果指定--port参数,可用不需要这个参数
# --directoryperdb:每个数据库使用单独的文件夹
command: mongod --shardsvr --directoryperdb --replSet shard1 --keyFile /data/mongo-keyfile
privileged: true
volumes:
- /etc/localtime:/etc/localtime
- /data/fates/mongo/shard1:/data/db
- /data/fates/mongo/script/mongo-keyfile:/data/mongo-keyfile
shard2:
image: mongo:4.0.5
command: mongod --shardsvr --directoryperdb --replSet shard2 --keyFile /data/mongo-keyfile
privileged: true
volumes:
- /etc/localtime:/etc/localtime
- /data/fates/mongo/shard2:/data/db
- /data/fates/mongo/script/mongo-keyfile:/data/mongo-keyfile
shard3:
image: mongo:4.0.5
command: mongod --shardsvr --directoryperdb --replSet shard3 --keyFile /data/mongo-keyfile
privileged: true
volumes:
- /etc/localtime:/etc/localtime
- /data/fates/mongo/shard3:/data/db
- /data/fates/mongo/script/mongo-keyfile:/data/mongo-keyfile
config1:
image: mongo:4.0.5
# --configsvr: 这个参数仅仅是将默认端口由27017改为27019, 如果指定--port可不添加该参数
command: mongod --configsvr --directoryperdb --replSet fates-mongo-config --smallfiles --keyFile /data/mongo-keyfile
privileged: true
volumes:
- /etc/localtime:/etc/localtime
- /data/fates/mongo/config1:/data/configdb
- /data/fates/mongo/script/mongo-keyfile:/data/mongo-keyfile
config2:
image: mongo:4.0.5
command: mongod --configsvr --directoryperdb --replSet fates-mongo-config --smallfiles --keyFile /data/mongo-keyfile
privileged: true
volumes:
- /etc/localtime:/etc/localtime
- /data/fates/mongo/config2:/data/configdb
- /data/fates/mongo/script/mongo-keyfile:/data/mongo-keyfile
config3:
image: mongo:4.0.5
command: mongod --configsvr --directoryperdb --replSet fates-mongo-config --smallfiles --keyFile /data/mongo-keyfile
privileged: true
volumes:
- /etc/localtime:/etc/localtime
- /data/fates/mongo/config3:/data/configdb
- /data/fates/mongo/script/mongo-keyfile:/data/mongo-keyfile
mongos:
image: mongo:4.0.5
# mongo3.6版默认绑定IP为127.0.0.1,此处绑定0.0.0.0是允许其他容器或主机可以访问
command: mongos --configdb fates-mongo-config/config1:27019,config2:27019,config3:27019 --bind_ip 0.0.0.0 --port 27017 --keyFile /data/mongo-keyfile
ports:
- 27017:27017
privileged: true
volumes:
- /etc/localtime:/etc/localtime
- /data/fates/mongo/script/mongo-keyfile:/data/mongo-keyfile
depends_on:
- config1
- config2
- config3
vi deploy-key.sh
docker-compose -f fates-mongo-compose-key.yaml up -d
4. 配置分片信息 容器启动需要一定时间,执行这一步时要等上一步把容器都启动起来,才能执行,执行不成功就多运行几遍
执行shard-config.sh shard-config.sh
docker-compose -f fates-mongo-compose.yaml exec config1 bash -c "echo 'rs.initiate({_id: \"fates-mongo-config\",configsvr: true, members: [{ _id : 0, host : \"config1:27019\" },{ _id : 1, host : \"config2:27019\" }, { _id : 2, host : \"config3:27019\" }]})' | mongo --port 27019"
docker-compose -f fates-mongo-compose.yaml exec shard1 bash -c "echo 'rs.initiate({_id: \"shard1\",members: [{ _id : 0, host : \"shard1:27018\" }]})' | mongo --port 27018"
docker-compose -f fates-mongo-compose.yaml exec shard2 bash -c "echo 'rs.initiate({_id: \"shard2\",members: [{ _id : 0, host : \"shard2:27018\" }]})' | mongo --port 27018"
docker-compose -f fates-mongo-compose.yaml exec shard3 bash -c "echo 'rs.initiate({_id: \"shard3\",members: [{ _id : 0, host : \"shard3:27018\" }]})' | mongo --port 27018"
docker-compose -f fates-mongo-compose.yaml exec mongos bash -c "echo 'sh.addShard(\"shard1/shard1:27018\")' | mongo"
docker-compose -f fates-mongo-compose.yaml exec mongos bash -c "echo 'sh.addShard(\"shard2/shard2:27018\")' | mongo"
docker-compose -f fates-mongo-compose.yaml exec mongos bash -c "echo 'sh.addShard(\"shard3/shard3:27018\")' | mongo"
5. 创建用户(授权模式)
执行add-user.sh add-user.sh
需要添加用户登录权限执行这一步,不需要登录的不用执行这一步
脚本中管理员用户名和密码都是root,可自行修改
docker-compose -f fates-mongo-compose.yaml exec mongos bash -c "echo -e 'use admin\n db.createUser({user:\"root\",pwd:\"root\",roles:[{role:\"root\",db:\"admin\"}]})' | mongo"
docker-compose -f fates-mongo-compose.yaml exec mongos bash -c "echo -e 'use admin\n show user' | mongo"
6. 验证
通过客户端连接验证是否成功 mongodb-compass-1.26.1-win32-x64.zip
测试部 > Docker-compose部署单机版本分片mongo > image2023-1-3 16:21:35.png
mongodb://root:root@192.168.2.73:27017/admin
测试部 > Docker-compose部署单机版本分片mongo > image2023-1-3 16:22:12.png
docker一直Restarting无法进入容器打印日志报错chown: changing ownership of ‘/data/db‘: Permission denied
解决方式:输入语句
vim /etc/sysconfig/selinux
把SELINUX=enforcing 改为 SELINUX=disabled退出保存一下就可以了。
Docker-compose部署单机版本分片mongo
最新推荐文章于 2024-08-16 10:15:00 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)