docker build mongodb cluster
Three server addresses
service name | IP address | port |
---|---|---|
mongo1 | 172.17.3.135 | 27017 |
mongo2 | 172.17.3.123 | 27017 |
mongo3 | 172.17.3.99 | 27017 |
Create a configuration file
-
Create a configuration file on the 135 server
/qj/mongo1/conf/mongod.conf
-
Create a configuration file on the 123 server
/qj/mongo2/conf/mongod.conf
-
Create a configuration file on the 99 server
/qj/mongo3/conf/mongod.conf
mongo1
Service configuration file /qj/mongo1/conf/mongod.conf
# mongod.conf
# for documentation of all options, see:
# http://docs.mongodb.org/manual/reference/configuration-options/
# Where and how to store data.
storage:
dbPath: /data/db
journal:
enabled: true
directoryPerDB: true
wiredTiger:
engineConfig:
cacheSizeGB: 1
# engine:
# mmapv1:
# wiredTiger:
# where to write logging data.
systemLog:
destination: file
logAppend: true
path: /var/log/mongodb/mongod.log
# network interfaces
net:
port: 27017
bindIp: 0.0.0.0
# how the process runs
processManagement:
timeZoneInfo: /usr/share/zoneinfo
security:
keyFile: /data/db/keyfile
#operationProfiling:
replication:
replSetName: rs0
oplogSizeMB: 10000
#sharding:
## Enterprise-Only Options:
#auditLog:
#snmp:
Create keyfile
Generate keyfile:
openssl rand -base64 756 > keyfile
# 阿里的服务器上可能用不了这条命令,建议在本地生成这个文件
Then put the generated code into the 135 server /qj/mongo1/data/db/keyfile
, modify the permission to 600
chmod 600 /qj/mongo1/data/db/keyfile
create log file
sudo mkdir /qj/mongo1/log/
sudo touch /qj/mongo1/log/mongod.log
chmod 777 /qj/mongo1/log/mongod.log
# 这个容器,如果你启动失败,看不了日志文件,所以把它映射出来,方便查看问题。
135节点mongo1启动容器命令
docker run -d --name mongo1 --restart=always \
-p 27017:27017 \
-v /qj/mongo1/conf/mongod.conf:/etc/mongod.conf:ro \
-v /qj/mongo1/data/db:/data/db:rw \
-v /qj/mongo1/log/mongod.log:/var/log/mongodb/mongod.log \
-v /etc/localtime:/etc/localtime:ro \
registry-vpc.cn-beijing.aliyuncs.com/qianjia_public/mongo:4.0.5 \
--replSet "rs0" --config /etc/mongod.conf
mongo2
Service configuration file /qj/mongo2/conf/mongod.conf
# mongod.conf
# for documentation of all options, see:
# http://docs.mongodb.org/manual/reference/configuration-options/
# Where and how to store data.
storage:
dbPath: /data/db
journal:
enabled: true
directoryPerDB: true
wiredTiger:
engineConfig:
cacheSizeGB: 2
# engine:
# mmapv1:
# wiredTiger:
# where to write logging data.
systemLog:
destination: file
logAppend: true
path: /var/log/mongodb/mongod.log
# network interfaces
net:
port: 27017
bindIp: 0.0.0.0
# how the process runs
processManagement:
timeZoneInfo: /usr/share/zoneinfo
security:
keyFile: /data/db/keyfile
#operationProfiling:
replication:
replSetName: rs0
oplogSizeMB: 10000
#sharding:
## Enterprise-Only Options:
#auditLog:
#snmp:
Create keyfile
Generate keyfile:
openssl rand -base64 756 > keyfile
Then put the generated code into the 123 server /qj/mongo2/data/db/keyfile
, modify the permission to 600
chmod 600 /qj/mongo2/data/db/keyfile
create log file
sudo mkdir /qj/mongo2/log/
sudo touch /qj/mongo2/log/mongod.log
chmod 777 /qj/mongo2/log/mongod.log
123节点mongo2启动容器命令
docker run -d --name mongo2 --restart=always \
-p 27017:27017 \
-v /qj/mongo2/conf/mongod.conf:/etc/mongod.conf:ro \
-v /qj/mongo2/data/db:/data/db:rw \
-v /qj/mongo2/log/mongod.log:/var/log/mongodb/mongod.log \
-v /etc/localtime:/etc/localtime:ro \
registry-vpc.cn-beijing.aliyuncs.com/qianjia_public/mongo:4.0.5 \
--replSet "rs0" --config /etc/mongod.conf
mongo3
Service configuration file /qj/mongo3/conf/mongod.conf
# mongod.conf
# for documentation of all options, see:
# http://docs.mongodb.org/manual/reference/configuration-options/
# Where and how to store data.
storage:
dbPath: /data/db
journal:
enabled: true
directoryPerDB: true
wiredTiger:
engineConfig:
cacheSizeGB: 1
# engine:
# mmapv1:
# wiredTiger:
# where to write logging data.
systemLog:
destination: file
logAppend: true
path: /var/log/mongodb/mongod.log
# network interfaces
net:
port: 27017
bindIp: 0.0.0.0
# how the process runs
processManagement:
timeZoneInfo: /usr/share/zoneinfo
security:
keyFile: /data/db/keyfile
#operationProfiling:
replication:
replSetName: rs0
oplogSizeMB: 10000
#sharding:
## Enterprise-Only Options:
#auditLog:
#snmp:
Create keyfile
Generate keyfile:
openssl rand -base64 756 > keyfile
Then put the generated code into the 99 server /qj/mongo3/data/db/keyfile
, modify the permission to 600
chmod 600 /qj/mongo3/data/db/keyfile
create log file
sudo mkdir /qj/mongo3/log/
sudo touch /qj/mongo3/log/mongod.log
chmod 777 /qj/mongo3/log/mongod.log
99节点mongo3启动容器命令
docker run -d --name mongo3 --restart=always \
-p 27017:27017 \
-v /qj/mongo3/conf/mongod.conf:/etc/mongod.conf:ro \
-v /qj/mongo3/data/db:/data/db:rw \
-v /qj/mongo3/log/mongod.log:/var/log/mongodb/mongod.log \
-v /etc/localtime:/etc/localtime:ro \
registry-vpc.cn-beijing.aliyuncs.com/qianjia_public/mongo:4.0.5 \
--replSet "rs0" --config /etc/mongod.conf
Create a cluster
Enter the container of 135 hosts and start mongo
docker exec -it mongo1 bash
mongo
Create a cluster
config = {
_id:"rs0", members:[{
_id:0,host:"172.17.3.135:27017"},{
_id:1,host:"172.17.3.123:27017"},{
_id:2,host:"172.17.3.99:27017"}]}
Cluster initialization
rs.initiate(config)
determine the master
rs.isMaster()
View cluster status
rs.status()
On the other two hosts, you need to confirm the slave before continuing to view. After entering the container and starting mongo
rs.slaveOk()
Create super administrator user
on the master node
use admin
db.createUser({user: "admin", pwd: "admin", roles:[{role:"root", db:"admin"}] })
After the super user is established, show dbs
the database cannot be displayed directly after starting or connecting to mongo at this time, and you need to log in first
db.auth("admin","admin")
再次
show dbs
即可显示所有数据库