如果Mac端的Mongo无法启动,怀疑是dbpath的问题

有一天,Mongo突然无法连接了。

$ mongo
MongoDB shell version v3.6.4
connecting to: mongodb://127.0.0.1:27017
2018-08-03T23:11:23.982+0900 W NETWORK  [thread1] Failed to connect to 127.0.0.1:27017, in(checking socket for error after poll), reason: Connection refused
2018-08-03T23:11:23.987+0900 E QUERY    [thread1] Error: couldn't connect to server 127.0.0.1:27017, connection attempt failed :
connect@src/mongo/shell/mongo.js:251:13
@(connect):1:6
exception: connect failed

现在…不是…MySQL,而是在Mac上有数据库,不会出现这么多问题吗…。

请确认mongod.conf中指定的dbpath是否与/data/db以外的路径相同。

这只是一种可能性,但在我的情况下,导致问题的原因是在启动 mongod 时,访问的 dbpath 路径有误。

暂时试着运行mongod。

2018-08-03T23:13:20.920+0900 I CONTROL  [initandlisten] build environment:
2018-08-03T23:13:20.920+0900 I CONTROL  [initandlisten]     distarch: x86_64
2018-08-03T23:13:20.920+0900 I CONTROL  [initandlisten]     target_arch: x86_64
2018-08-03T23:13:20.920+0900 I CONTROL  [initandlisten] options: {}
2018-08-03T23:13:20.923+0900 I STORAGE  [initandlisten] exception in initAndListen: NonExistentPath: Data directory /data/db not found., terminating
2018-08-03T23:13:20.923+0900 I CONTROL  [initandlisten] now exiting
2018-08-03T23:13:20.923+0900 I CONTROL  [initandlisten] shutting down with code:100

没有找到数据目录/data/db,所以被告知要关闭。我想,根本就没有/data/db这样的路径!于是我开始搜索,找到了数据可能存在的地方:/usr/local/etc/mongod.conf。

尝试执行命令:vim /usr/local/etc/mongod.conf

$ vim /usr/local/etc/mongod.conf
  1 systemLog:
  2   destination: file
  3   path: /usr/local/var/log/mongodb/mongo.log
  4   logAppend: true
  5 storage:
  6   dbPath: /usr/local/var/mongodb
  7 net:
  8   bindIp: 127.0.0.1

存储:在这里写下了数据的存储位置。因此,只需在启动mongod时明确指定该路径即可。

尝试执行 mongod –dbpath /usr/local/var/mongodb/

$ mongod --dbpath /usr/local/var/mongodb/
...(省略)...
2018-08-03T23:40:02.087+0900 I CONTROL  [initandlisten] build environment:
2018-08-03T23:40:02.087+0900 I CONTROL  [initandlisten]     distarch: x86_64
2018-08-03T23:40:02.087+0900 I CONTROL  [initandlisten]     target_arch: x86_64
2018-08-03T23:40:02.087+0900 I CONTROL  [initandlisten] options: { storage: { dbPath: "/usr/local/var/mongodb/" } }
2018-08-03T23:40:02.091+0900 I -        [initandlisten] Detected data files in /usr/local/var/mongodb/ created by the 'wiredTiger' storage engine, so setting the active storage engine to 'wiredTiger'.
...(省略)...
2018-08-03T23:40:03.477+0900 I FTDC     [initandlisten] Initializing full-time diagnostic data capture with directory '/usr/local/var/mongodb/diagnostic.data'
2018-08-03T23:40:03.478+0900 I NETWORK  [initandlisten] waiting for connections on port 27017

4. 狠狠地打击mongo

$ mongo
MongoDB shell version v3.6.4
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.6.4
Server has startup warnings:
2018-08-03T23:40:03.453+0900 I CONTROL  [initandlisten]
2018-08-03T23:40:03.454+0900 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2018-08-03T23:40:03.454+0900 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2018-08-03T23:40:03.454+0900 I CONTROL  [initandlisten]
2018-08-03T23:40:03.454+0900 I CONTROL  [initandlisten] ** WARNING: This server is bound to localhost.
2018-08-03T23:40:03.454+0900 I CONTROL  [initandlisten] **          Remote systems will be unable to connect to this server.
2018-08-03T23:40:03.454+0900 I CONTROL  [initandlisten] **          Start the server with --bind_ip <address> to specify which IP
2018-08-03T23:40:03.454+0900 I CONTROL  [initandlisten] **          addresses it should serve responses from, or with --bind_ip_all to
2018-08-03T23:40:03.454+0900 I CONTROL  [initandlisten] **          bind to all interfaces. If this behavior is desired, start the
2018-08-03T23:40:03.454+0900 I CONTROL  [initandlisten] **          server with --bind_ip 127.0.0.1 to disable this warning.
2018-08-03T23:40:03.454+0900 I CONTROL  [initandlisten]
>

嗨,你好。我终于进来了…。