Mongodb

在 ubuntu 16.04 中添加管理員使用者後,mongodb 服務將無法啟動

  • August 12, 2017

我有一個ubuntu 16.04,我昨天已經安裝mongodbsudo aptitude install mongodb;它在開始時工作得很好。我決定向 中添加身份驗證資訊mongodb,因此我mongo在 shell 中打開了一個會話,並輸入了以下命令:

use admin
db.createUser({user:"admin", pwd:"admin123", roles:[{role:"root", db:"admin"}]})

它表示已成功添加使用者。我進入exit設置配置以便在啟動服務時使用身份驗證。我編輯了vim /lib/systemd/system/mongod.service;它現在包含:

[Unit]
Description=High-performance, schema-free document-oriented database
After=network.target
Documentation=https://docs.mongodb.org/manual

[Service]
User=mongodb
Group=mongodb
ExecStart=/usr/bin/mongod --quiet --auth 

[Install]
WantedBy=multi-user.target

之後我進入systemd daemon-reload了shell,然後service mongod restart;但不幸的是mongodb從未開始。當我嘗試mongo使用 command連接時mongo -u admin -p admin123 --authenticationDatabase admin,它向我顯示:

mongo -u admin -p admin123 --authenticationDatabase admin
MongoDB shell version: 2.6.10
connecting to: test
2017-08-08T10:37:52.751+0430 warning: Failed to connect to 127.0.0.1:27017, reason: errno:111 Connection refused
2017-08-08T10:37:52.751+0430 Error: couldn't connect to server 127.0.0.1:27017 (127.0.0.1), connection attempt failed at src/mongo/shell/mongo.js:146
exception: connect failed

它說沒有伺服器正在執行(connect failed);test另外我不知道為什麼它在我插入時試圖連接admin?!但是命令systemctl status mongodb顯示如下:

● mongodb.service - An object/document-oriented database
  Loaded: loaded (/lib/systemd/system/mongodb.service; enabled; vendor preset: enabled)
  Active: inactive (dead) since Mon 2017-08-07 16:02:30 IRDT; 18h ago
    Docs: man:mongod(1)
Main PID: 11367 (code=exited, status=0/SUCCESS)

Aug 07 15:33:14 ZiZi systemd[1]: Started An object/document-oriented database.
Aug 07 16:02:30 ZiZi systemd[1]: Stopping An object/document-oriented database...
Aug 07 16:02:30 ZiZi systemd[1]: Stopped An object/document-oriented database.

文件中沒有寫入日誌/var/log/mongodb/mongodb.log

當我進入ExecStartshell時,我可以成功啟動mongo

   sudo /usr/bin/mongod --quiet --auth 
2017-08-08T10:53:27.358+0430 [initandlisten] MongoDB starting : pid=29830 port=27017 dbpath=/data/db 64-bit host=ZiZi
2017-08-08T10:53:27.359+0430 [initandlisten] db version v2.6.10
2017-08-08T10:53:27.359+0430 [initandlisten] git version: nogitversion
2017-08-08T10:53:27.359+0430 [initandlisten] OpenSSL version: OpenSSL 1.0.2g  1 Mar 2016
2017-08-08T10:53:27.359+0430 [initandlisten] build info: Linux lgw01-12 3.19.0-25-generic #26~14.04.1-Ubuntu SMP Fri Jul 24 21:16:20 UTC 2015 x86_64 BOOST_LIB_VERSION=1_58
2017-08-08T10:53:27.359+0430 [initandlisten] allocator: tcmalloc
2017-08-08T10:53:27.359+0430 [initandlisten] options: { security: { authorization: "enabled" }, systemLog: { quiet: true } }
2017-08-08T10:53:27.360+0430 [initandlisten] journal dir=/data/db/journal
2017-08-08T10:53:27.360+0430 [initandlisten] recover : no journal files present, no recovery needed
2017-08-08T10:53:27.431+0430 [initandlisten] waiting for connections on port 27017

編輯1:

我已使用此處mongodb描述的說明重新安裝;它現在是最新版本:

mongod --version
   db version v3.4.7
   git version: cf38c1b8a0a8dca4a11737581beafef4fe120bcd
   OpenSSL version: OpenSSL 1.0.2g  1 Mar 2016
   allocator: tcmalloc
   modules: none
   build environment:
       distmod: ubuntu1604
       distarch: x86_64
       target_arch: x86_64

我再次添加了身份驗證;我/lib/systemd/system/mongod.service現在是這樣的:

[Unit]
Description=High-performance, schema-free document-oriented database
After=network.target
Documentation=https://docs.mongodb.org/manual

[Service]
User=mongodb
Group=mongodb
ExecStart=/usr/bin/mongod --auth --config /etc/mongod.conf
# file size
LimitFSIZE=infinity
# cpu time
LimitCPU=infinity
# virtual memory size
LimitAS=infinity
# open files
LimitNOFILE=64000
# processes/threads
LimitNPROC=64000
# total threads (user+kernel)
TasksMax=infinity
TasksAccounting=false

# Recommended limits for for mongod as specified in
# http://docs.mongodb.org/manual/reference/ulimit/#recommended-settings

[Install]
WantedBy=multi-user.target

這是我的/etc/mongod.conf

# mongod.conf

# for documentation of all options, see:
#   http://docs.mongodb.org/manual/reference/configuration-options/

# Where and how to store data.
storage:
 dbPath: /var/lib/mongodb
 journal:
   enabled: true
#  engine:
#  mmapv1:
#  wiredTiger:

# where to write logging data.
systemLog:
 destination: file
 logAppend: true
 path: /var/log/mongodb/mongod.log

# network interfaces
net:
 port: 27017
 bindIp: 127.0.0.1


#processManagement:

security:
 auth: true

#operationProfiling:

#replication:

#sharding:

## Enterprise-Only Options:

#auditLog:

#snmp:

使用者添加過程成功:

> use admin
switched to db admin
> db.createUser(
...   {
...     user: "myUserAdmin",
...     pwd: "abc123",
...     roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
...   }
... )
Successfully added user: {
   "user" : "myUserAdmin",
   "roles" : [
       {
           "role" : "userAdminAnyDatabase",
           "db" : "admin"
       }
   ]
}

我已重新載入systemctl並重新啟動mongod服務,但mongod服務仍然無法啟動。我沒有--quiet選項,但/var/log/mongodb/mongod.log文件中沒有日誌。這是service mongod status返回:

service mongod status
● mongod.service - High-performance, schema-free document-oriented database
  Loaded: loaded (/lib/systemd/system/mongod.service; enabled; vendor preset: enabled)
  Active: failed (Result: exit-code) since Wed 2017-08-09 16:12:42 IRDT; 1min 0s ago
    Docs: https://docs.mongodb.org/manual
 Process: 2030 ExecStart=/usr/bin/mongod --auth --config /etc/mongod.conf (code=exited, status=2)
Main PID: 2030 (code=exited, status=2)

Aug 09 16:12:42 ZiZi systemd[1]: Stopped High-performance, schema-free document-oriented database.
Aug 09 16:12:42 ZiZi systemd[1]: Started High-performance, schema-free document-oriented database.
Aug 09 16:12:42 ZiZi systemd[1]: mongod.service: Main process exited, code=exited, status=2/INVALIDARGUMENT
Aug 09 16:12:42 ZiZi systemd[1]: mongod.service: Unit entered failed state.
Aug 09 16:12:42 ZiZi systemd[1]: mongod.service: Failed with result 'exit-code'.

這意味著我有一個INVALIDARGUMENT我無法檢測到的。有什麼幫助嗎?

mongod 沒有啟動可能有多種原因,但是..

首先,為什麼你沒有得到任何 mongodb.log 行的原因是 ---quiet參數。我不會使用它,因為它使辨識問題的根源變得更加困難。所以刪除它並重試,這次日誌文件中應該有幾行說明出了什麼問題。

其次,至少在我的 Ubuntu mongo 安裝中,啟動服務行ExecStart=/usr/bin/mongod --config /etc/mongod.conf意味著所有配置都在 /etc/mongod.conf -file 中完成,無論如何配置服務的最佳位置是什麼,因為該文件在更新期間不會被覆蓋。

將 auth:true 更改為(在 mongod.conf 中)為 authorization:enabled (記住在授權字之前保留這些空格字元)。

引用自:https://dba.stackexchange.com/questions/182919