执行 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
执行before-deploy.sh
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
执行deploy.sh
docker-compose -f fates-mongo-compose.yaml up -d
yaml文件如下
version: '3.4'
services:
shard1:
image: mongo:4.0.5
# --shardsvr: 这个参数仅仅只是将默认的27017端口改为27018,如果指定--port参数,可用不需要这个参数
# --directoryperdb:每个数据库使用单独的文件夹
command: mongod --shardsvr --directoryperdb --replSet shard1
volumes:
- /etc/localtime:/etc/localtime
- /data/fates/mongo/shard1:/data/db
shard2:
image: mongo:4.0.5
command: mongod --shardsvr --directoryperdb --replSet shard2
volumes:
- /etc/localtime:/etc/localtime
- /data/fates/mongo/shard2:/data/db
shard3:
image: mongo:4.0.5
command: mongod --shardsvr --directoryperdb --replSet shard3
volumes:
- /etc/localtime:/etc/localtime
- /data/fates/mongo/shard3:/data/db
config1:
image: mongo:4.0.5
# --configsvr: 这个参数仅仅是将默认端口由27017改为27019, 如果指定--port可不添加该参数
command: mongod --configsvr --directoryperdb --replSet fates-mongo-config --smallfiles
volumes:
- /etc/localtime:/etc/localtime
- /data/fates/mongo/config1:/data/configdb
config2:
image: mongo:4.0.5
command: mongod --configsvr --directoryperdb --replSet fates-mongo-config --smallfiles
volumes:
- /etc/localtime:/etc/localtime
- /data/fates/mongo/config2:/data/configdb
config3:
image: mongo:4.0.5
command: mongod --configsvr --directoryperdb --replSet fates-mongo-config --smallfiles
volumes:
- /etc/localtime:/etc/localtime
- /data/fates/mongo/config3:/data/configdb
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
ports:
- 27017:27017
volumes:
- /etc/localtime:/etc/localtime
depends_on:
- config1
- config2
- config3
generate-keyfile.sh
#!/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
deploy-key.sh
docker-compose -f fates-mongo-compose-key.yaml up -d
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
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
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
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
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
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
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
volumes:
- /etc/localtime:/etc/localtime
- /data/fates/mongo/script/mongo-keyfile:/data/mongo-keyfile
depends_on:
- config1
- config2
- config3
容器启动需要一定时间,执行这一步时要等上一步把容器都启动起来,才能执行,执行不成功就多运行几遍
执行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"
没有出现
Connection refused
等错误就表示成功了
执行add-user.sh
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"
mongo {ip}
执行check.sh
docker-compose -f fates-mongo-compose.yaml exec mongos bash -c "echo -e 'use admin\n show users' | mongo"
执行check-key.sh
docker-compose -f fates-mongo-compose.yaml exec mongos bash -c "echo -e 'use admin\n show users' | mongo -u root -p root"
DIR=/data/fates
DATA_PATH="${DIR}/mongo"
DATA_DIR_LIST=('config1' 'config2' 'config3' 'shard1' 'shard2' 'shard3' 'script')
BASE_DIR=$(cd "$(dirname "$0")";pwd)
function check_directory() {
if [ ! -d "${DATA_PATH}" ]; then
echo "create directory: ${DATA_PATH}"
sudo 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}"
sudo mkdir -p "${DATA_PATH}/${SUB_DIR}"
else
echo "directory: ${DATA_PATH}/${SUB_DIR} already exists."
fi
done
echo "change directory owner to $USER:$USER"
sudo chown -R $USER:$USER "${DATA_PATH}"
}
function rm_old_directory() {
if [ -d "${DATA_PATH}" ]; then
echo "delete directory: ${DATA_PATH}"
sudo rm -rf ${DATA_PATH}
fi
}
function cp_script() {
cp "${BASE_DIR}"/* "${DATA_PATH}"/script
}
function deploy_no_auth() {
cd "${DATA_PATH}"/script
docker-compose -f fates-mongo-compose.yaml up -d
}
function deploy_with_auth() {
cd "${DATA_PATH}"/script
docker-compose -f fates-mongo-compose-key.yaml up -d
}
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
sudo chmod 600 mongo-keyfile
sudo chown 999 mongo-keyfile
else
echo 'mongo-keyfile already exists.'
fi
}
function config_shard() {
cd "${DATA_PATH}/script"
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"
}
function add_user() {
cd "${DATA_PATH}/script"
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"
}
function check() {
cd "${DATA_PATH}/script"
docker-compose -f fates-mongo-compose.yaml exec mongos bash -c "echo -e 'use admin\n show users' | mongo -u root -p root"
}
function main() {
rm_old_directory
check_directory
cp_script
generate_keyfile
deploy_no_auth
sleep 120
config_shard
add_user
deploy_with_auth
sleep 90
check
}
main