docker-compose 安装mongodb集群==多台服务器
服务器
192.168.5.201 192.168.5.202 192.168.5.203
=========openssl生成文件报错====================
ssl生成文件报错:
[root@localhost ~]# openssl rand -base64 741 > key.file openssl: error while loading shared libraries: libssl.so.1.1: cannot open shared object file: No such file or directory 这是由于openssl库的位置不正确造成的。 解决方法: 在root用户下执行: ln -s /usr/local/lib64/libssl.so.1.1 /usr/lib64/libssl.so.1.1 ln -s /usr/local/lib64/libcrypto.so.1.1 /usr/lib64/libcrypto.so.1.1
==========以下每台都执行========================
镜像
docker pull mongo:4.2.7
网络
docker network create --subnet=10.20.0.0/24 mongodbnet
mkdir -p /home/soft/mongoCluster
Config-Server 配置文件
路径:vi /home/soft/mongoCluster/mongod.conf
storage: dbPath: /data/db journal: enabled: true systemLog: destination: file logAppend: true path: /var/log/mongodb/mongod.log net: bindIp: 0.0.0.0 processManagement: timeZoneInfo: /usr/share/zoneinfo replication: replSetName: cfg sharding: clusterRole: configsvr security: keyFile: /data/mongodb/key.file authorization: enabled
Mongos 配置文件
路径:vi /home/soft/mongoCluster/mongos.conf
systemLog: destination: file logAppend: true path: /var/log/mongodb/mongos.log net: port: 27020 bindIp: 0.0.0.0 processManagement: fork: true timeZoneInfo: /usr/share/zoneinfo security: keyFile: /data/mongodb/key.file sharding: configDB: cfg/192.168.5.201:27019,192.168.5.202:27019,192.168.5.203:27019
路径:vi /home/soft/mongoCluster/first_mkdir.sh
mkdir -p /home/soft/mongodbcompose/conf/config mkdir -p /home/soft/mongodbcompose/conf/db mkdir -p /home/soft/mongodbcompose/shard1/config mkdir -p /home/soft/mongodbcompose/shard1/db mkdir -p /home/soft/mongodbcompose/shard1/backup mkdir -p /home/soft/mongodbcompose/shard2/config mkdir -p /home/soft/mongodbcompose/shard2/db mkdir -p /home/soft/mongodbcompose/shard2/backup mkdir -p /home/soft/mongodbcompose/mongos/config mkdir -p /home/soft/mongodbcompose/mongos/db
生成认证文件 root 用户 三台共用一个
openssl rand -base64 741 > key.file chmod 600 key.file chown 999 key.file mv key.file /home/soft/mongodbcompose
文件复制脚本
路径:vi /home/soft/mongoCluster/second_mv.sh
#!/bin/bash for item in /home/soft/mongodbcompose/ do for item2 in `ls $item` do if [ $item2 = 'mongos' ] then echo $item2 cp mongos.conf $item$item2/config/mongos.conf else echo "no" cp mongod.conf $item$item2/config/mongod.conf fi done done
docker-compose 文件
路径:vi /home/soft/mongoCluster/docker-compose.yaml
version: '2' services: rs_config_server: image: mongo:4.2.7 command: mongod -f /etc/mongod/mongod.conf --configsvr --replSet "rs-config-server" --bind_ip_all volumes: - /home/soft/mongocompose/file/mongodb.key:/data/mongodb/mongodb.key - /home/soft/mongocompose/conf/config:/etc/mongod - /home/soft/mongocompose/conf/db:/data/db ports: - "10021:27019" networks: - mongodbs restart: always container_name: rs_config_server ulimits: nofile: soft: 300000 hard: 300000 rs_shard_server1: image: mongo:4.2.7 command: mongod -f /etc/mongod/mongod.conf --directoryperdb --shardsvr --replSet "rs-shard1-server" --bind_ip_all volumes: - /home/soft/mongocompose/file/mongodb.key:/data/mongodb/mongodb.key - /home/soft/mongocompose/shard1/config:/etc/mongod - /home/soft/mongocompose/shard1/db:/data/db ports: - "10031:27018" networks: - mongodbs restart: always container_name: rs_shard_server1 ulimits: nofile: soft: 300000 hard: 300000 rs_shard_server2: image: mongo:4.2.7 command: mongod -f /etc/mongod/mongod.conf --directoryperdb --shardsvr --replSet "rs-shard2-server" --bind_ip_all volumes: - /home/soft/mongocompose/file/mongodb.key:/data/mongodb/mongodb.key - /home/soft/mongocompose/shard2/config:/etc/mongod - /home/soft/mongocompose/shard2/db:/data/db ports: - "10041:27018" networks: - mongodbs restart: always container_name: rs_shard_server2 ulimits: nofile: soft: 300000 hard: 300000 rs_shard_server3: image: mongo:4.2.7 command: mongod -f /etc/mongod/mongod.conf --directoryperdb --shardsvr --replSet "rs-shard3-server" --bind_ip_all volumes: - /home/soft/mongocompose/file/mongodb.key:/data/mongodb/mongodb.key - /home/soft/mongocompose/shard3/config:/etc/mongod - /home/soft/mongocompose/shard3/db:/data/db ports: - "10051:27018" networks: - mongodbs restart: always container_name: rs_shard_server3 ulimits: nofile: soft: 300000 hard: 300000 rs_mongos_server: image: mongo:4.2.7 command: mongos -f /etc/mongod/mongos.conf --configdb rs-config-server/192.168.5.202:10021,192.168.5.203:10021,192.168.5.201:10021 --bind_ip_all ports: - "10011:27017" networks: - mongodbs volumes: - /home/soft/mongocompose/file/mongodb.key:/data/mongodb/mongodb.key - /home/soft/mongocompose/mongos/config:/etc/mongod - /home/soft/mongocompose/mongos/db:/data/db restart: always container_name: rs_mongos_server ulimits: nofile: soft: 300000 hard: 300000 # Swarm 跨主机网络网络 networks: mongodbs: external: true
======文件构建完成,开始执行脚本===================
// 进入临时目录 cd /home/soft/mongoCluster/ // 授权目录下sh等文件 chmod 777 *.sh // 创建目录 ./first_mkdir.sh // 惟一密钥文件复制。都是用同一个文件,并授权 // key.file 文件分别复制到相应的目录 chmod 600 /home/soft/mongodbcompose/key.file chown 999 /home/soft/mongodbcompose/key.file // 将配置文件移动到相应的目录 ./second_mv.sh // 启动容器 docker-compose up -d
添加副本集
配置服务器
连接任意一个节点 mongo --host 192.168.5.201 --port 10021
docker exec -it rs_config_server bash mongo --port 27019 rs.initiate({ _id: "rs-config-server", configsvr: true, members: [ { _id : 0, host : "192.168.5.201:10021" }, { _id : 1, host : "192.168.5.202:10021" }, { _id : 2, host : "192.168.5.203:10021" }, ] });
分片1
连接任意一个节点 mongo --host 192.168.5.201 --port 10031
docker exec -it rs_shard_server1 bash mongo --port 27018 rs.initiate({ _id: "rs-shard1-server", members: [ { _id : 0, host : "192.168.5.201:10031" }, { _id : 1, host : "192.168.5.202:10031" }, { _id : 2, host : "192.168.5.203:10031" }, ] });
分片2
连接任意一个节点 mongo --host 192.168.5.201 --port 10041
docker exec -it rs_shard_server2 bash mongo --port 27018 rs.initiate({ _id: "rs-shard2-server", members: [ { _id : 0, host : "192.168.5.201:10041" }, { _id : 1, host : "192.168.5.202:10041" }, { _id : 2, host : "192.168.5.203:10041" }, ] });
配置mongos
确保mongos服务起来之后,连接到192.168.1.125:10011执行以下命令添加分片服务器信息
docker exec -it rs_mongos_server bash // 启动副本集 mongos -f /etc/mongod/mongos.conf --configdb rs-config-server/192.168.5.201:10021,192.168.5.202:10021,192.168.5.203:10021 mongo --port 27020 sh.addShard("rs-shard1-server/192.168.5.201:10031,192.168.5.202:10031,192.168.5.203:10031") sh.addShard("rs-shard2-server/192.168.5.201:10041,192.168.5.202:10041,192.168.5.203:10041")
添加用户认证
连接任意的mongos
use admin db.createUser( { user:"root", pwd:"root123456", roles:[{role:"root",db:"admin"}] } )
验证是否创建成功
db.auth('root','root123456')
=============如果一开始就加上了密码。那就不需要下面步骤了=======================
将配置文件的用户认证全部打开 security: cd /home/soft/mongocompose/file vi mongod.conf 去掉# 注释 vi mongos.conf 重新覆盖之前的配置文件 // 停掉服务 docker-compose stop ./second_mv.sh 删除存储目录lock文件 mongod.lock find /home/soft/mongocompose/ -name mongod.lock rm -rf /home/soft/mongocompose/conf/db/mongod.lock rm -rf /home/soft/mongocompose/shard1/db/mongod.lock rm -rf /home/soft/mongocompose/shard2/db/mongod.lock rm -rf /home/soft/mongocompose/shard3/db/mongod.lock chmod 400 mongodb.key chown 999 mongodb.key // 启动服务 docker-compose start // 查看服务是否启动正常 docker ps -a | grep rs // 验证 docker exec -it rs_mongos_server mongo --port 27017 use admin db.auth('root','root123456') // 插入些许测试数据 for(var i=1;i<200;i++){db.books.insert({id:i,book_name:"PHP",book_id:"888"});} // 查询插入结果 db.books.find().count(); 能查询到数据,集群成功