所以在节点中我有:
const mongoose = require('mongoose');
mongoose.connect('mongodb://localhost:27017/master');
let db = mongoose.connection();
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function() {
// we're connected!
console.log('conn');
});
但是没有打印出来,如果我尝试从节点查询数据库,它会永久挂起而不会出现错误。我可以打开mongo shell并创建一个新的集合,插入项目等。
这是Mongo的日志:
2016-12-02T03:52:14.104+0000 I CONTROL [initandlisten] MongoDB starting : pid=1 port=27017 dbpath=/mongodb-master/ 64-bit host=16b45d9af5c4
2016-12-02T03:52:14.104+0000 I CONTROL [initandlisten] db version v3.2.11
2016-12-02T03:52:14.104+0000 I CONTROL [initandlisten] git version: 009580ad490190ba33d1c6253ebd8d91808923e4
2016-12-02T03:52:14.104+0000 I CONTROL [initandlisten] OpenSSL version: OpenSSL 1.0.2g 1 Mar 2016
2016-12-02T03:52:14.104+0000 I CONTROL [initandlisten] allocator: tcmalloc
2016-12-02T03:52:14.104+0000 I CONTROL [initandlisten] modules: none
2016-12-02T03:52:14.104+0000 I CONTROL [initandlisten] build environment:
2016-12-02T03:52:14.104+0000 I CONTROL [initandlisten] distmod: ubuntu1404
2016-12-02T03:52:14.104+0000 I CONTROL [initandlisten] distarch: x86_64
2016-12-02T03:52:14.104+0000 I CONTROL [initandlisten] target_arch: x86_64
2016-12-02T03:52:14.104+0000 I CONTROL [initandlisten] options: { net: { port: 27017 }, storage: { dbPath: "/mongodb-master/" } }
2016-12-02T03:52:14.111+0000 I STORAGE [initandlisten] wiredtiger_open config: create,cache_size=1G,session_max=20000,eviction=(threads_max=4),config_base=false,statistics=(fast),log=(enabled=true,archive=true,path=journal,compressor=snappy),file_manager=(close_idle_time=100000),checkpoint=(wait=60,log_size=2GB),statistics_log=(wait=0),
2016-12-02T03:52:14.683+0000 I CONTROL [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
2016-12-02T03:52:14.683+0000 I CONTROL [initandlisten]
2016-12-02T03:52:14.685+0000 I FTDC [initandlisten] Initializing full-time diagnostic data capture with directory '/mongodb-master/diagnostic.data'
2016-12-02T03:52:14.685+0000 I NETWORK [HostnameCanonicalizationWorker] Starting hostname canonicalization worker
2016-12-02T03:52:14.847+0000 I NETWORK [initandlisten] waiting for connections on port 27017
2016-12-02T03:52:25.898+0000 I NETWORK [initandlisten] connection accepted from 172.17.0.1:46140 #1 (1 connection now open)
2016-12-02T03:52:25.903+0000 I NETWORK [initandlisten] connection accepted from 172.17.0.1:46142 #2 (2 connections now open)
2016-12-02T03:52:25.905+0000 I NETWORK [initandlisten] connection accepted from 172.17.0.1:46144 #3 (3 connections now open)
2016-12-02T03:52:25.907+0000 I NETWORK [initandlisten] connection accepted from 172.17.0.1:46146 #4 (4 connections now open)
2016-12-02T03:52:25.909+0000 I NETWORK [initandlisten] connection accepted from 172.17.0.1:46148 #5 (5 connections now open)
答案 0 :(得分:0)
尝试:
this.connection = mongoose.createConnection('mongodb://localhost:27017', { server: { reconnectTries: Number.MAX_VALUE } });
但是,您必须以不同的方式创建模型。看看here
答案 1 :(得分:0)
通过使用node
卸载并重新安装mongodb
和brew
然后重新启动并重新安装(确保删除项目存储库中的node_modules
并重新安装)来解决此问题。