# MongoDB FAQ

# 问题

# 安装完成后无法成功启动 mongodb

过程: a. sudo systemctl start mongod b. sudo systemctl status mongod

● mongod.service - MongoDB Database Server
   Loaded: loaded (/lib/systemd/system/mongod.service; disabled; vendor preset: enabled)
   Active: failed (Result: exit-code) since Sat 2020-02-22 21:48:01 CST; 19s ago
     Docs: https://docs.mongodb.org/manual
  Process: 32363 ExecStart=/usr/bin/mongod --config /etc/mongod.conf (code=exited, status=14)
 Main PID: 32363 (code=exited, status=14)

Feb 22 21:48:01 iZuf64nlbmin7jqu2comp5Z systemd[1]: Started MongoDB Database Server.
Feb 22 21:48:01 iZuf64nlbmin7jqu2comp5Z systemd[1]: mongod.service: Main process exited, code=exited, status=14/n/a
Feb 22 21:48:01 iZuf64nlbmin7jqu2comp5Z systemd[1]: mongod.service: Failed with result 'exit-code'.

可以看到 Active: failed

找问题,看日志

2020-02-22T21:41:47.151+0800 I  CONTROL  [main] Automatically disabling TLS 1.0, to force-enable TLS 1.0 specify --sslDisabledProtocols 'none'
2020-02-22T21:41:47.287+0800 I  CONTROL  [initandlisten] MongoDB starting : pid=32196 port=27017 dbpath=/var/lib/mongodb 64-bit host=iZuf64nlbmin7jqu2comp5Z
2020-02-22T21:41:47.287+0800 I  CONTROL  [initandlisten] db version v4.2.3
2020-02-22T21:41:47.287+0800 I  CONTROL  [initandlisten] git version: 6874650b362138df74be53d366bbefc321ea32d4
2020-02-22T21:41:47.287+0800 I  CONTROL  [initandlisten] OpenSSL version: OpenSSL 1.1.1  11 Sep 2018
2020-02-22T21:41:47.287+0800 I  CONTROL  [initandlisten] allocator: tcmalloc
2020-02-22T21:41:47.287+0800 I  CONTROL  [initandlisten] modules: none
2020-02-22T21:41:47.287+0800 I  CONTROL  [initandlisten] build environment:
2020-02-22T21:41:47.287+0800 I  CONTROL  [initandlisten]     distmod: ubuntu1804
2020-02-22T21:41:47.287+0800 I  CONTROL  [initandlisten]     distarch: x86_64
2020-02-22T21:41:47.287+0800 I  CONTROL  [initandlisten]     target_arch: x86_64
2020-02-22T21:41:47.287+0800 I  CONTROL  [initandlisten] options: { config: "/etc/mongod.conf", net: { bindIp: "127.0.0.1", port: 27017 }, processManagement: { timeZoneInfo: "/usr/share/zoneinfo" }, storage: { dbPath: "/var/lib/mongodb", journal: { enabled: true } }, systemLog: { destination: "file", logAppend: true, path: "/var/log/mongodb/mongod.log" } }
2020-02-22T21:41:47.287+0800 E  NETWORK  [initandlisten] Failed to unlink socket file /tmp/mongodb-27017.sock Operation not permitted
2020-02-22T21:41:47.287+0800 F  -        [initandlisten] Fatal Assertion 40486 at src/mongo/transport/transport_layer_asio.cpp 683
2020-02-22T21:41:47.287+0800 F  -        [initandlisten]

可以看到Failed to unlink socket file /tmp/mongodb-27017.sock Operation not permitted,网上搜索发现这个文件的所有者是 root

srwx------  1 root root    0 Feb 22 21:47  mongodb-27017.sock

所以

方法一 更改权限

chown mongodb:mongodb /tmp/mongodb-27017.sock
=> 改为
srwx------  1 mongodb mongodb    0 Feb 22 21:47  mongodb-27017.sock

同时确保/var/lib/mongodb, /var/log/mongodb 的权限也正确

方法二 直接删除rm tmp/mongodb-27017.sock

再次启动,可正常启动

● mongod.service - MongoDB Database Server
   Loaded: loaded (/lib/systemd/system/mongod.service; disabled; vendor preset: enabled)
   Active: active (running) since Sat 2020-02-22 21:56:21 CST; 8s ago
     Docs: https://docs.mongodb.org/manual
 Main PID: 32585 (mongod)
   CGroup: /system.slice/mongod.service
           └─32585 /usr/bin/mongod --config /etc/mongod.conf

# Mac 无法连接 Ecs MongoDB

a.需要在 Ecs 控制台设置安全组规则,放开 27017:27017 b.需要更改 mongodb 配置为,因为默认仅有本地连接

net:
  port: 27017
  bindIp: 127.0.0.1

改为

net:
  port: 27017
  bindIp: 0.0.0.0
陕ICP备20004732号-3