我一直在寻找类似的问题,并试图找到我的解决方案;不幸的是,它仍然没有用。似乎已经完成了所有必要的事情。
好的,我的问题是,我的AngularCLI应用程序' aied',访问名为' mockup'的本地MongoDB。 docker-compose.yml如下,
version: '3' # specify docker-compose version
# Define the services/containers to be run
services:
aied: #name of the second service
build: .
environment:
- MONGO_URI=mongodb://db:27017/mockup
volumes:
- .:/usr/src/app
ports:
- "3000:3000" #specify ports forewarding
depends_on:
- db
links:
- db
db:
image: mongo
environment:
- MONGODB_USER=username
- MONGODB_PASS=admin
- MONGO_INITDB_DATABASE=mockup
- MONGO_DATA_DIR=/data/db
ports:
- "27017:27017"
volumes:
- ./data/db:/data/db
command: mongod --smallfiles --logpath=/dev/null # --quiet
volumes:
db:
server.js的部分如下
//const db = "mongodb://user:pass@ds247347.mlab.com:47347/mockup";
const db= "mongodb://db:27017/mockup";
mongoose.Promise = global.Promise;
mongoose.connect(db, function(err){
if(err){
console.error("can not receive data from mongodb ... " );
console.error("Error! " + err);
} else { console.log("... connected to "+db.toString());}
});
router.get('/models', function(req, res){
console.log('Get request for all models');
mdl.find({})
.exec(function(err, mdls){
if (err){
console.log("Error retrieving models");
}else {
res.json(mdls);
}
console.log('mdls length: ' + mdls.length);
});
});
在" docker-compose up"之后,我得到了以下内容,
Attaching to mockup_db_1, mockup_aied_1
aied_1 |
aied_1 | > mock-up@0.0.0 start /usr/src/app
aied_1 | > node server
aied_1 |
aied_1 | (node:15) DeprecationWarning: `open()` is deprecated in mongoose >= 4.11.0, use `openUri()` instead, or set the `useMongoClient` option if using `connect()` or `createConnection()`. See http://mongoosejs.com/docs/connections.html#use-mongo-client
aied_1 | Server running on localhost:3000
aied_1 | ... connected to mongodb://db:27017/mockup
aied_1 | Get request for all models
aied_1 | mdls length: 0
问题是,docker-compose与我在mLab的远程MongoDB工作正常,它无法使用我当地的MongoDB访问。
首先,我使用" docker ps"检查正在运行的容器,例如,
bdcebd0b0e0b mockup_aied
"npm start" About an hour ago Up 17 seconds
0.0.0.0:3000->3000/tcp mockup_aied_1
583f719d9f4a mongo
"docker-entrypoint.s…" About an hour ago Up 18 seconds
0.0.0.0:27017->27017/tcp mockup_db_1
其次,使用" docker exec -it mockup_db_1 mongo",我进入了mongo shell,
> show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
> use mockup
switched to db mockup
> show collections
> db.collection.count()
0
问题很明显,没有收集(数据)被添加到正在运行的mongodb容器中。
有人能帮帮我吗?非常感谢你。
答案 0 :(得分:0)
是的,在朋友的帮助下,我终于解决了它。 变化是: - 使用mongo的另一个端口,例如27027; - mongodump从本地端口27017输出我的数据 - mongorestore将数据丢弃到端口27027
然后它起作用。