[Centos7] 解决无法启动mongodb的错误

安装

在安装时,我查阅了这些资料
[官方] 在 Red Hat or CentOS 上安装 MongoDB 社区版
在 CentOS7 上引入 MongoDB 的方法

请以汉语进行同义改写,只需提供一个选项:

版本

Mongod的版本是v4.2.1。

$ mongod --version
db version v4.2.1
git version: edf6d45851c0b9ee15548f0f847df141764a317e
OpenSSL version: OpenSSL 1.0.1e-fips 11 Feb 2013
allocator: tcmalloc
modules: none
build environment:
    distmod: rhel70
    distarch: x86_64
    target_arch: x86_64

错误1

无法解除连接套接字文件/tmp/mongodb-27017.sock,操作不允许。

虽然我能够第一次启动,但是当我正在调整设置的时候,遇到了以下的错误… (´・ω・`)

$ sudo systemctl start mongod
Job for mongod.service failed because the control process exited with error code. See "systemctl status mongod.service" and "journalctl -xe" for details.

$ sudo systemctl status mongod
● mongod.service - MongoDB Database Server
   Loaded: loaded (/usr/lib/systemd/system/mongod.service; enabled; vendor preset: disabled)
   Active: failed (Result: exit-code) since 日 2019-11-17 14:03:40 JST; 30s ago
     Docs: https://docs.mongodb.org/manual
  Process: 3471 ExecStart=/usr/bin/mongod $OPTIONS (code=exited, status=14)
  Process: 3469 ExecStartPre=/usr/bin/chmod 0755 /var/run/mongodb (code=exited, status=0/SUCCESS)
  Process: 3466 ExecStartPre=/usr/bin/chown mongod:mongod /var/run/mongodb (code=exited, status=0/SUCCESS)
  Process: 3465 ExecStartPre=/usr/bin/mkdir -p /var/run/mongodb (code=exited, status=0/SUCCESS)
 Main PID: 2847 (code=exited, status=0/SUCCESS)

11月 17 14:03:40 localhost.localdomain systemd[1]: Starting MongoDB Database Server...
11月 17 14:03:40 localhost.localdomain mongod[3471]: about to fork child process, waiting until server is ready for connections.
11月 17 14:03:40 localhost.localdomain mongod[3471]: forked process: 3475
11月 17 14:03:40 localhost.localdomain systemd[1]: mongod.service: control process exited, code=exited status=14
11月 17 14:03:40 localhost.localdomain systemd[1]: Failed to start MongoDB Database Server.
11月 17 14:03:40 localhost.localdomain systemd[1]: Unit mongod.service entered failed state.
11月 17 14:03:40 localhost.localdomain systemd[1]: mongod.service failed.

我们来看一下日志吧。

$ sudo cat /var/log/mongodb/mongod.log

# * * 一部省略 * *
2019-11-17T14:16:17.192+0900 I  CONTROL  [main] ***** SERVER RESTARTED *****
2019-11-17T14:16:17.220+0900 I  CONTROL  [main] Automatically disabling TLS 1.0, to force-enable TLS 1.0 specify --sslDisabledProtocols 'none'
2019-11-17T14:16:17.332+0900 I  CONTROL  [initandlisten] MongoDB starting : pid=4687 port=27017 dbpath=/var/lib/mongo 64-bit host=localhost.localdomain
2019-11-17T14:16:17.332+0900 I  CONTROL  [initandlisten] db version v4.2.1
2019-11-17T14:16:17.332+0900 I  CONTROL  [initandlisten] git version: edf6d45851c0b9ee15548f0f847df141764a317e
2019-11-17T14:16:17.332+0900 I  CONTROL  [initandlisten] OpenSSL version: OpenSSL 1.0.1e-fips 11 Feb 2013
2019-11-17T14:16:17.332+0900 I  CONTROL  [initandlisten] allocator: tcmalloc
2019-11-17T14:16:17.332+0900 I  CONTROL  [initandlisten] modules: none
2019-11-17T14:16:17.332+0900 I  CONTROL  [initandlisten] build environment:
2019-11-17T14:16:17.332+0900 I  CONTROL  [initandlisten]     distmod: rhel70
2019-11-17T14:16:17.333+0900 I  CONTROL  [initandlisten]     distarch: x86_64
2019-11-17T14:16:17.333+0900 I  CONTROL  [initandlisten]     target_arch: x86_64
2019-11-17T14:16:17.333+0900 I  CONTROL  [initandlisten] options: { config: "/etc/mongod.conf", net: { bindIp: "127.0.0.1", port: 27017 }, processManagement: { fork: true, pidFilePath: "/var/run/mongodb/mongod.pid", timeZoneInfo: "/usr/share/zoneinfo" }, storage: { dbPath: "/var/lib/mongo", journal: { enabled: true } }, systemLog: { destination: "file", logAppend: true, path: "/var/log/mongodb/mongod.log" } }
2019-11-17T14:16:17.333+0900 E  NETWORK  [initandlisten] Failed to unlink socket file /tmp/mongodb-27017.sock Operation not permitted
2019-11-17T14:16:17.334+0900 F  -        [initandlisten] Fatal Assertion 40486 at src/mongo/transport/transport_layer_asio.cpp 693
2019-11-17T14:16:17.334+0900 F  -        [initandlisten]

***aborting after fassert() failure

我所关注的是这些(可能会成为线索的东西)。

# statusの結果
11月 17 14:03:40 localhost.localdomain systemd[1]: mongod.service: control process exited, code=exited status=14

# logの内容
2019-11-17T14:16:17.333+0900 E  NETWORK  [initandlisten] Failed to unlink socket file /tmp/mongodb-27017.sock Operation not permitted

解决办法

可靠的地方是在这里
StackExchange的MongoDB加载了,但是出现故障,返回状态=14

在我们的交流中写道,请按照以下方式…

被记录的内容

$ chown -R mongodb:mongodb /var/lib/mongodb
$ chown mongodb:mongodb /tmp/mongodb-27017.sock

只是,这次安装的是 mongod 而不是 mongodb 对吧?为什么呢?

所以,首先要确认准确的路径位置。

我真正做过的事情

$ which mongod
/usr/bin/mongod

# 正しい path は、/var/lib/mongodb じゃなくて /usr/bin/mongod と判明

因此,让我们尝试进行权限更改…

$ chown -R mongodb:mongodb /usr/bin/mongod
chown: ユーザ指定が不正: `mongodb:mongodb'

$ sudo chown -R mongodb:mongodb /usr/bin/mongod
chown: ユーザ指定が不正: `mongodb:mongodb'

据说连MongoDB用户也没有啊!!
哎呀…真的吗?
让我看看….

$ cat /etc/passwd
# ...略
mongod:x:997:993:mongod:/var/lib/mongo:/bin/false

…確實沒有這個。
不是mongodb,而是mongod呢…
不僅是路徑,還有用戶名呢。

这次一定要改变现状!(`・ω・´)

# sudo をつけないと権限不足だった
$ sudo chown -R mongod:mongod /usr/bin/mongod
$ sudo chown mongod:mongod /tmp/mongodb-27017.sock

chown的结果没有任何反应。
不过,对于Linux命令来说,“没有回应通常是好消息!”
所以很可能chown已经成功了。

现在,重新启动mongod!

$ systemctl start mongod.service
==== AUTHENTICATING FOR org.freedesktop.systemd1.manage-units ===
Authentication is required to manage system services or units.
Authenticating as: kame.piyo (kame)
Password: # <- sudo passwordを入力
==== AUTHENTICATION COMPLETE ===

# ....なぜか若干間が空く
# prompt が戻ってきたら status を確認してみよう
$ systemctl status mongod.service
● mongod.service - MongoDB Database Server
   Loaded: loaded (/usr/lib/systemd/system/mongod.service; enabled; vendor preset: disabled)
   Active: active (running) since 日 2019-11-17 14:18:34 JST; 8s ago
     Docs: https://docs.mongodb.org/manual
  Process: 4915 ExecStart=/usr/bin/mongod $OPTIONS (code=exited, status=0/SUCCESS)
  Process: 4912 ExecStartPre=/usr/bin/chmod 0755 /var/run/mongodb (code=exited, status=0/SUCCESS)
  Process: 4909 ExecStartPre=/usr/bin/chown mongod:mongod /var/run/mongodb (code=exited, status=0/SUCCESS)
  Process: 4908 ExecStartPre=/usr/bin/mkdir -p /var/run/mongodb (code=exited, status=0/SUCCESS)
 Main PID: 4918 (mongod)
   CGroup: /system.slice/mongod.service
           mq4918 /usr/bin/mongod -f /etc/mongod.conf

11月 17 14:18:25 localhost.localdomain systemd[1]: Starting MongoDB Database Server...
11月 17 14:18:25 localhost.localdomain mongod[4915]: about to fork child process, waiting until server is ready for connections.
11月 17 14:18:25 localhost.localdomain mongod[4915]: forked process: 4918
11月 17 14:18:34 localhost.localdomain systemd[1]: Started MongoDB Database Server.

牛牛^^

错误2

不存在的路径:找不到数据目录/data/db,终止。

刚开始成功启动的时候,我以为问题解决了,可是转瞬间…
这次却无法连接。

当你冷静地考虑一下,一般不会使用$ mongod这个命令。是的。
因此,针对这个错误(error2)的解决方案实际上是靠不住的。

只是因为其他人似乎也遇到了同样的现象,所以我决定保留这篇文章。

$ mongod
2019-11-17T14:57:06.422+0900 I  CONTROL  [main] Automatically disabling TLS 1.0, to force-enable TLS 1.0 specify --sslDisabledProtocols 'none'
2019-11-17T14:57:06.493+0900 I  CONTROL  [initandlisten] MongoDB starting : pid=6049 port=27017 dbpath=/data/db 64-bit host=localhost.localdomain
2019-11-17T14:57:06.493+0900 I  CONTROL  [initandlisten] db version v4.2.1
2019-11-17T14:57:06.493+0900 I  CONTROL  [initandlisten] git version: edf6d45851c0b9ee15548f0f847df141764a317e
2019-11-17T14:57:06.493+0900 I  CONTROL  [initandlisten] OpenSSL version: OpenSSL 1.0.1e-fips 11 Feb 2013
2019-11-17T14:57:06.493+0900 I  CONTROL  [initandlisten] allocator: tcmalloc
2019-11-17T14:57:06.493+0900 I  CONTROL  [initandlisten] modules: none
2019-11-17T14:57:06.494+0900 I  CONTROL  [initandlisten] build environment:
2019-11-17T14:57:06.494+0900 I  CONTROL  [initandlisten]     distmod: rhel70
2019-11-17T14:57:06.494+0900 I  CONTROL  [initandlisten]     distarch: x86_64
2019-11-17T14:57:06.494+0900 I  CONTROL  [initandlisten]     target_arch: x86_64
2019-11-17T14:57:06.494+0900 I  CONTROL  [initandlisten] options: {}
2019-11-17T14:57:06.498+0900 I  STORAGE  [initandlisten] exception in initAndListen: NonExistentPath: Data directory /data/db not found., terminating
2019-11-17T14:57:06.499+0900 I  NETWORK  [initandlisten] shutdown: going to close listening sockets...
2019-11-17T14:57:06.500+0900 I  NETWORK  [initandlisten] removing socket file: /tmp/mongodb-27017.sock
2019-11-17T14:57:06.500+0900 I  -        [initandlisten] Stopping further Flow Control ticket acquisitions.
2019-11-17T14:57:06.500+0900 I  CONTROL  [initandlisten] now exiting
2019-11-17T14:57:06.500+0900 I  CONTROL  [initandlisten] shutting down with code:100

哎呀…坐牢了

这次这里看起来很可疑呢…

# エラー内容から抜粋
2019-11-17T14:57:06.498+0900 I  STORAGE  [initandlisten] exception in initAndListen: NonExistentPath: Data directory /data/db not found., terminating

嗯,这样的目录确实不存在…

$ ls /data
ls: /data にアクセスできません: そのようなファイルやディレクトリはありません

第二项对策

这次这里可靠了。

    • mongoDB起動で「directory /data/db not found」のエラーが発生した

 

    Mac側のmongoが起動しなくなったらdbpathを疑う

做过的事情 (zuò guò de shì

首先确认正确的 dbpath。

$ cat /etc/mongod.conf
# ...省略
# Where and how to store data.
storage:
  dbPath: /var/lib/mongo
  journal:
    enabled: true
# 以下略

所以…是 dbPath: /var/lib/mongo

$ sudo mongod --dbpath /var/lib/mongo

如果能启动就好了。
但是…每次都要指定dbpath,是不是很麻烦呢…?

我再多调查一下,好吗…?

错误3

669:/var/lib/mongo/WiredTiger.turtle:handle-open:打开:权限被拒绝。

再见!

$ sudo service mongod start
Redirecting to /bin/systemctl start mongod.service
Job for mongod.service failed because the control process exited with error code. See "systemctl status mongod.service" and "journalctl -xe" for details.

让我们再看一下日志。 .)

$ sudo cat /var/log/mongodb/mongod.log

2019-11-17T15:14:09.272+0900 I  CONTROL  [main] ***** SERVER RESTARTED *****
2019-11-17T15:14:09.330+0900 I  CONTROL  [main] Automatically disabling TLS 1.0, to force-enable TLS 1.0 specify --sslDisabledProtocols 'none'
2019-11-17T15:14:09.403+0900 I  CONTROL  [initandlisten] MongoDB starting : pid=6904 port=27017 dbpath=/var/lib/mongo 64-bit host=localhost.localdomain
2019-11-17T15:14:09.403+0900 I  CONTROL  [initandlisten] db version v4.2.1
2019-11-17T15:14:09.403+0900 I  CONTROL  [initandlisten] git version: edf6d45851c0b9ee15548f0f847df141764a317e
2019-11-17T15:14:09.403+0900 I  CONTROL  [initandlisten] OpenSSL version: OpenSSL 1.0.1e-fips 11 Feb 2013
2019-11-17T15:14:09.403+0900 I  CONTROL  [initandlisten] allocator: tcmalloc
2019-11-17T15:14:09.403+0900 I  CONTROL  [initandlisten] modules: none
2019-11-17T15:14:09.403+0900 I  CONTROL  [initandlisten] build environment:
2019-11-17T15:14:09.404+0900 I  CONTROL  [initandlisten]     distmod: rhel70
2019-11-17T15:14:09.404+0900 I  CONTROL  [initandlisten]     distarch: x86_64
2019-11-17T15:14:09.404+0900 I  CONTROL  [initandlisten]     target_arch: x86_64
2019-11-17T15:14:09.404+0900 I  CONTROL  [initandlisten] options: { config: "/etc/mongod.conf", net: { bindIp: "127.0.0.1", port: 27017 }, processManagement: { fork: true, pidFilePath: "/var/run/mongodb/mongod.pid", timeZoneInfo: "/usr/share/zoneinfo" }, storage: { dbPath: "/var/lib/mongo", journal: { enabled: true } }, systemLog: { destination: "file", logAppend: true, path: "/var/log/mongodb/mongod.log" } }
2019-11-17T15:14:09.406+0900 I  STORAGE  [initandlisten] Detected data files in /var/lib/mongo created by the 'wiredTiger' storage engine, so setting the active storage engine to 'wiredTiger'.
2019-11-17T15:14:09.406+0900 I  STORAGE  [initandlisten] wiredtiger_open config: create,cache_size=487M,cache_overflow=(file_max=0M),session_max=33000,eviction=(threads_min=4,threads_max=4),config_base=false,statistics=(fast),log=(enabled=true,archive=true,path=journal,compressor=snappy),file_manager=(close_idle_time=100000,close_scan_interval=10,close_handle_minimum=250),statistics_log=(wait=0),verbose=[recovery_progress,checkpoint_progress],
2019-11-17T15:14:16.011+0900 E  STORAGE  [initandlisten] WiredTiger error (13) [1573971256:11482][6904:0x7f4ce72d9c00], wiredtiger_open: __posix_open_file, 669: /var/lib/mongo/WiredTiger.turtle: handle-open: open: Permission denied Raw: [1573971256:11482][6904:0x7f4ce72d9c00], wiredtiger_open: __posix_open_file, 669: /var/lib/mongo/WiredTiger.turtle: handle-open: open: Permission denied
2019-11-17T15:14:16.014+0900 E  STORAGE  [initandlisten] WiredTiger error (13) [1573971256:14409][6904:0x7f4ce72d9c00], wiredtiger_open: __posix_open_file, 669: /var/lib/mongo/WiredTiger.turtle: handle-open: open: Permission denied Raw: [1573971256:14409][6904:0x7f4ce72d9c00], wiredtiger_open: __posix_open_file, 669: /var/lib/mongo/WiredTiger.turtle: handle-open: open: Permission denied
2019-11-17T15:14:16.017+0900 E  STORAGE  [initandlisten] WiredTiger error (13) [1573971256:17051][6904:0x7f4ce72d9c00], wiredtiger_open: __posix_open_file, 669: /var/lib/mongo/WiredTiger.turtle: handle-open: open: Permission denied Raw: [1573971256:17051][6904:0x7f4ce72d9c00], wiredtiger_open: __posix_open_file, 669: /var/lib/mongo/WiredTiger.turtle: handle-open: open: Permission denied
2019-11-17T15:14:16.017+0900 W  STORAGE  [initandlisten] Failed to start up WiredTiger under any compatibility version.
2019-11-17T15:14:16.017+0900 F  STORAGE  [initandlisten] Reason: 13: Permission denied
2019-11-17T15:14:16.017+0900 F  -        [initandlisten] Fatal Assertion 28595 at src/mongo/db/storage/wiredtiger/wiredtiger_kv_engine.cpp 786
2019-11-17T15:14:16.017+0900 F  -        [initandlisten]

***aborting after fassert() failure

这次这里看起来可疑…我已经累了

2019-11-17T15:14:16.011+0900 E  STORAGE  [initandlisten] WiredTiger error (13) [1573971256:11482][6904:0x7f4ce72d9c00], wiredtiger_open: __posix_open_file, 669: /var/lib/mongo/WiredTiger.turtle: handle-open: open: Permission denied Raw: [1573971256:11482][6904:0x7f4ce72d9c00], wiredtiger_open: __posix_open_file, 669: /var/lib/mongo/WiredTiger.turtle: handle-open: open: Permission denied

第三种对策

当MongoDB突然无法启动时的解决方法。

因为已经很累了,所以我只是按照写着的方式试着做了一下。

做过的事情。

$ sudo chown -R mongod:mongod /var/lib/mongo
chown: `/var/lib/mongod' にアクセスできません: そのようなファイルやディレクトリはありません
# あっそうですよね

# pathを調べて...
$ which mongo
/usr/bin/mongo

# 権限変更して...
$ sudo chown -R mongod:mongod /usr/bin/mongo

# mongod起動して...
$ sudo service mongod start

# いざアクセス!
$ mongo
MongoDB shell version v4.2.1
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("65202a1c-c8e4-4a65-b53e-1c37351d757c") }
MongoDB server version: 4.2.1
Server has startup warnings:
2019-11-17T15:17:42.055+0900 I  CONTROL  [initandlisten]
2019-11-17T15:17:42.055+0900 I  CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2019-11-17T15:17:42.055+0900 I  CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2019-11-17T15:17:42.055+0900 I  CONTROL  [initandlisten]
2019-11-17T15:17:42.056+0900 I  CONTROL  [initandlisten]
2019-11-17T15:17:42.079+0900 I  CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2019-11-17T15:17:42.079+0900 I  CONTROL  [initandlisten] **        We suggest setting it to 'never'
2019-11-17T15:17:42.079+0900 I  CONTROL  [initandlisten]
2019-11-17T15:17:42.079+0900 I  CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2019-11-17T15:17:42.080+0900 I  CONTROL  [initandlisten] **        We suggest setting it to 'never'
2019-11-17T15:17:42.080+0900 I  CONTROL  [initandlisten]
---
Enable MongoDB's free cloud-based monitoring service, which will then receive and display
metrics about your deployment (disk utilization, CPU, operation statistics, etc).

The monitoring data will be available on a MongoDB website with a unique URL accessible to you
and anyone you share the URL with. MongoDB may use this information to make product
improvements and to suggest MongoDB products and deployment options to you.

To enable free monitoring, run the following command: db.enableFreeMonitoring()
To permanently disable this reminder, run the following command: db.disableFreeMonitoring()
---

>

为什么一切都顺利进行…
我不再有兴趣详细调查了

填補不足之處

后来我意识到,我好像混淆了 mongod 和 mongo。

    • mongod は、データベースを起動するために使うもの

 

    mongo は、起動したデータベースにアクセスするときに使うもの

听起来好像是这样的。

请按照以下顺序执行以下命令。

$ systemctl start mongod.service
$ mongo