我有一个节点后端应用程序,它需要具有副本集的MongoDB。我为我的应用程序创建了一个docker映像,该映像仅运行该应用程序;还创建了一个DataGrid ItemSource = "{Binding TdStatusCol.View}"
文件,该文件运行MongoDB实例并配置其副本集。
这是我的docker-compose
文件:
docker-compose
在入口点中,我的bash脚本如下:
version: '2'
services:
mongod1:
image: mongo
command: mongod --replSet ${RS} --oplogSize 16 --smallfiles --noprealloc --bind_ip 0.0.0.0
ports:
- '27017:27017'
mongod2:
image: mongo
command: mongod --replSet ${RS} --oplogSize 16 --smallfiles --noprealloc --bind_ip 0.0.0.0
ports:
- '27018:27017'
mongod3:
image: mongo
command: mongod --replSet ${RS} --oplogSize 16 --smallfiles --noprealloc --bind_ip 0.0.0.0
ports:
- '27019:27017'
mongo-config:
image: mongo
depends_on:
- mongod1
- mongod2
- mongod3
volumes:
- ./scripts:/scripts
environment:
- MONGO1=mongod1
- MONGO2=mongod2
- MONGO3=mongod3
- USER=machine
- PWD=abc123456
- RS=rs0
entrypoint: ['/scripts/setup.sh']
现在在我的环境中,我可以在#!/bin/bash
mongodb1=$(getent hosts ${MONGO1} | awk '{ print $1 }')
mongodb2=$(getent hosts ${MONGO2} | awk '{ print $1 }')
mongodb3=$(getent hosts ${MONGO3} | awk '{ print $1 }')
port=${PORT:-27017}
echo "Waiting for startup.."
until mongo --host ${mongodb1}:${port} --eval 'quit(db.runCommand({ ping: 1 }).ok ? 0 : 2)' &>/dev/null; do
printf '.'
sleep 1
done
echo "Started.."
echo "setup.sh; time now: $(date +"%T")"
mongo --host ${mongodb1}:${port} <<EOF
use admin
db.createUser(
{
user: ${USER},
pwd: #{pwd},
roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
}
)
EOF
mongo --host ${mongodb1}:${port} <<EOF
var cfg = {
"_id": "${RS}",
"members": [
{
"_id": 0,
"host": "${mongodb1}:${port}"
},
{
"_id": 1,
"host": "${mongodb2}:${port}"
},
{
"_id": 2,
"host": "${mongodb3}:${port}"
}
]
};
rs.initiate(cfg, { force: true });
rs.config();
EOF
mongo --host ${mongodb2}:${port} <<EOF
db.setSlaveOk()
EOF
mongo --host ${mongodb3}:${port} <<EOF
db.setSlaveOk()
EOF
中连接到mongodb,但是应用程序无法连接到localhost:27017
。
我该如何解决?我应该使用docker网络吗?
答案 0 :(得分:1)
尝试将0.0.0.0更改为127.0.0.1或仅使用localhost。
顺便说一句,我无法通过不断获取Error: couldn't add user: not master
来从您的方法创建auth,因此我四处搜索,发现在调用rs.initiate()之前,以及如果mongod.conf
带有设置replication.replSetName: rs
已存在,您要连接的mongo实例将不知道谁是master或Primary。
因此我切换了createUser
和rs.initiate()
的顺序,并设置了延迟,然后工作正常。
感谢骨骼。
version: "3.3"
services:
mongo-admin:
image: mongo-express
container_name: "mongo-admin"
environment:
- ME_CONFIG_MONGODB_SERVER=db_manager,db_worker_1,db_worker_2
- ME_CONFIG_OPTIONS_EDITORTHEME=ambiance
- ME_CONFIG_BASICAUTH_USERNAME=$MONGOADMIN
- ME_CONFIG_BASICAUTH_PASSWORD=$MONGOPASS
ports:
- 8081:8081
restart: on-failure
depends_on:
- db_manager
- db_worker_1
- db_worker_2
- mongo-set-setup
db_manager:
image: mongo:4.0
container_name: db_manager
volumes:
- mongodb1:/data/db
- ./mongod.conf:/etc/mongod.conf
command: mongod --config /etc/mongod.conf
ports:
- "27017:27017"
restart: always
db_worker_1:
image: mongo:4.0
container_name: db_worker_1
volumes:
- mongodb2:/data/db
- ./mongod.conf:/etc/mongod.conf
command: mongod --config /etc/mongod.conf
ports:
- "27018:27017"
restart: always
db_worker_2:
image: mongo:4.0
container_name: db_worker_2
volumes:
- mongodb3:/data/db
- ./mongod.conf:/etc/mongod.conf
command: mongod --config /etc/mongod.conf
ports:
- "27019:27017"
restart: always
mongo-set-setup:
container_name: "mongo-set-setup"
image: mongo:4.0
depends_on:
- db_manager
- db_worker_1
- db_worker_2
volumes:
- ./setupmongo.sh:/scripts/setupmongo.sh
environment:
- MONGO1=db_manager
- MONGO2=db_worker_1
- MONGO3=db_worker_2
- USER=$MONGOUSER
- PASSWORD=$MONGOPASSWORD
- RS=rs
entrypoint: ["/scripts/setupmongo.sh"]
#
volumes:
mongodb1:
mongodb2:
mongodb3:
和外壳
#!/bin/bash
mongodb1=`getent hosts ${MONGO1} | awk '{ print $1 }'`
mongodb2=`getent hosts ${MONGO2} | awk '{ print $1 }'`
mongodb3=`getent hosts ${MONGO3} | awk '{ print $1 }'`
port=${PORT:-27017}
echo "Waiting for startup.."
until mongo --host ${mongodb1}:${port} --eval 'quit(db.runCommand({ ping: 1 }).ok ? 0 : 2)' &>/dev/null; do
printf '.'
sleep 1
done
echo "Started.."
echo ~~~~~~~~setup replic time now: `date +"%T" `
echo ${mongodb1}, ${mongodb2}, ${mongodb3}
mongo --host ${mongodb1}:${port} <<EOF
var cfg = {
"_id": "${RS}",
"protocolVersion": 1,
"members": [
{
"_id": 0,
"host": "${mongodb1}:${port}",
"priority": 2
},
{
"_id": 1,
"host": "${mongodb2}:${port}",
"priority": 0
},
{
"_id": 2,
"host": "${mongodb3}:${port}",
"priority": 0
}
]
};
rs.initiate(cfg, { force: true });
rs.reconfig(cfg, { force: true });
rs.slaveOk();
db.getMongo().setReadPref('nearest');
db.getMongo().setSlaveOk();
EOF
sleep 30
echo ${USER}, ${PASSWORD}
echo ~~~~~~~~~~setup user auth time now: `date +"%T" `
mongo --host ${mongodb1}:${port} <<EOF
use admin
db.createUser(
{
"user": "${USER}",
"pwd": "${PASSWORD}",
"roles": [ "readWrite"]
}
)
EOF