我正在使用mongo服务器,其中有三个正在运行的docker,另一个映射所有三个mongo docker并从中创建副本。 配置相同的是:
mongo1:
build: mongodb/
hostname: mongo1
expose:
- "27017"
ports:
- "ip:27017:27017"
# links:
# - mongo2:mongo2
# - mongo3:mongo3
restart: always
volumes:
- ./data/mongodb/mongo1:/data/db
entrypoint: [ "/usr/bin/mongod", "--replSet=rs", "--noauth", "--rest", "--httpinterface", "--dbpath", "/data/db","--port","27017","--bind_ip","0.0.0.0" ]
mem_limit: 1024m
mongo2:
build: mongodb/
hostname: mongo2
expose:
- "27018"
ports:
- "ip:27018:27017"
restart: always
volumes:
- ./data/mongodb/mongo2:/data/db
entrypoint: [ "/usr/bin/mongod", "--replSet=rs", "--rest", "--httpinterface", "--dbpath", "/data/db","--port","27017","--bind_ip","0.0.0.0" ]
# mem_limit: 512m
mongo3:
build: mongodb/
hostname: mongo3
expose:
- "27019"
ports:
- "ip:27019:27017"
restart: always
volumes:
- ./data/mongodb/mongo3:/data/db
entrypoint: [ "/usr/bin/mongod", "--replSet=rs", "--rest", "--httpinterface", "--dbpath", "/data/db","--port","27017","--bind_ip","0.0.0.0" ]
# mem_limit: 512m
其中ip是我的服务器ip。 设置副本的设置文件是:
mongosetup:
build: mongodb/
links:
- mongo1:mongo1
- mongo2:mongo2
- mongo3:mongo3
volumes:
- ./mongodb/scripts:/scripts
restart: always
entrypoint: [ "bash", "/scripts/mongosetup.sh" ]
mem_limit: 256m
脚本mongosetup.sh:
#!/bin/bash
MONGODB1=`ping -c 1 mongo1 | head -1 | cut -d "(" -f 2 | cut -d ")" -f 1`
MONGODB2=`ping -c 1 mongo2 | head -1 | cut -d "(" -f 2 | cut -d ")" -f 1`
MONGODB3=`ping -c 1 mongo3 | head -1 | cut -d "(" -f 2 | cut -d ")" -f 1`
echo SETUP.sh time now: `date +"%T" `
mongo --host ${MONGODB1}:27017 <<
var cfg = {
"_id": "rs",
"version": 1,
"members": [
{
"_id": 0,
"host": "${MONGODB1}:27017",
"priority": 2
},
{
"_id": 1,
"host": "${MONGODB2}:27018",
"priority": 0
},
{
"_id": 2,
"host": "${MONGODB3}:27019",
"priority": 0
}
]
};
rs.initiate(cfg, { force: true });
rs.reconfig(cfg, { force: true });
db.getMongo().setReadPref('nearest');
现在,当我尝试从测试服务器(具有所有这些docker)连接时,它正在工作。来自我的节点js服务器的连接URL:
var uri = "mongodb://ip:27017,ip:27018,ip:27019/stagedb/?replicaSet=rs";
mongoose.connect(uri);
其中ip是我的测试服务器ip。
我还尝试从mongo shell连接
mongo --host rs/ip:27017,ip:27018,ip:27019 stagedb
但是当尝试从远程服务器连接测试服务器mongo时,其连接字符串与上面的抛出错误相同:
数据库连接错误MongoError:首次连接时无法连接到服务器[172.17.0.5:27017] [MongoError:连接71到172.17.0.5:27017超时]
当我尝试连接到测试服务器ip时,IP 172.17.0.5是docker。
同样来自本地mongo shell,它给出的错误为:
2017-06-12T14:22:55.531 + 0530 W NETWORK 5000毫秒后无法连接到172.17.0.2:27017,放弃了。
2017-06-12T14:23:00.535 + 0530 W NETWORK 5000毫秒后无法连接到172.17.0.5:27017,放弃了。
2017-06-12T14:23:05.495 + 0530 W NETWORK [ReplicaSetMonitorWatcher] 5000毫秒后无法连接到172.17.0.3:27017,放弃了。
2017-06-12T14:23:05.498 + 0530 E QUERY错误:连接失败到副本集rs / test.elanic.co:27017,test.elanic.co:27018,test.elanic.co:27019 在connect(src / mongo / shell / mongo.js:181:14) 在(连接):1:6在src / mongo / shell / mongo.js:181 异常:连接失败
任何人都知道为什么它试图直接映射到docker ip或任何其他问题。