After two days of agonizing, finally achieved their desired effect, it is through the docker-compose deploy the latest mongodb replicaSet, mainly to test the latest 4.2 multi-document transactions, will share with you the entire steps below:
docker-compose.yml follows:
version: '3.4' services: mongo: container_name: mongodb image: mongo:latest hostname: mongodb volumes: - /data/mongodb/mongod.conf:/etc/mongod.conf - /data/mongodb/data/db/:/data/db/ - /data/mongodb/data/log/:/var/log/mongodb/ - /data/mongodb/initdb.d/:/docker-entrypoint-initdb.d/ environment: - MONGO_INITDB_ROOT_USERNAME=root - MONGO_INITDB_ROOT_PASSWORD=123456 ports: - "27017:27017" healthcheck: test: test $$(echo "rs.initiate().ok || rs.status().ok" | mongo -u root -p '123456' --quiet) -eq 1 interval: 10s start_period: 30s command: ["--replSet", "rs0", "--bind_ip_all"]
Description:
1, /data/mongodb/mongod.conf files need built in advance, as follows, mainly to enable verification, if you do not mount the file, replicaset in the streaking
# 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
# 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: 127.0.0.1
# how the process runs
processManagement:
timeZoneInfo: /usr/share/zoneinfo
security:
authorization: enabled
#operationProfiling:
#replication:
#sharding:
## Enterprise-Only Options:
#auditLog:
#snmp:
2, scripts in /data/mongodb/initdb.d This directory is used to initialize, provided that the former does not perform any data, I created in the directory create-app-user.sh to initialize custom database [yourdb user name] and [password] [123456] yourAdmin, reads as follows:
#!/usr/bin/env bash echo "Creating mongo users..." mongo admin -u root -p 123456 << EOF db.createUser({user: 'admin', pwd: '123456', roles: [{role: 'userAdminAnyDatabase', db: 'admin'}]}); db.grantRolesToUser( "admin", [ "clusterAdmin" ] ); EOF mongo admin -u admin -p 123456 << EOF use yourdb db.createUser({user: 'yourdbAdmin', pwd: '123456', roles:[{role:'readWrite',db:'yourdb'}]}) EOF echo "Mongo users created for yourdb."
3, can be used to test monog client login:
mongo -u root -p 123456
After a successful login, a drop like this:
Execution show dbs; you can see their databases built, built himself a test user and password, you can use:
mongo -u yourdbAdmin -p 123456 --authenticationDatabase yourdb