溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

如何解決MongoDB couldn‘t add user: not authorized on ‘your db‘ to execute command的問題

發(fā)布時間:2021-09-01 20:09:47 來源:億速云 閱讀:608 作者:chen 欄目:大數(shù)據(jù)

本篇內(nèi)容主要講解“如何解決MongoDB couldn‘t add user: not authorized on ‘your db‘ to execute command的問題”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學(xué)習(xí)“如何解決MongoDB couldn‘t add user: not authorized on ‘your db‘ to execute command的問題”吧!

解決 :MongoDB couldn‘t add user: not authorized on ‘your db‘ to execute command

二年前在服務(wù)器放的一個mongodb,現(xiàn)在早已經(jīng)忘記了驗證密碼,進不去了。如今記錄下恢復(fù)過程。

1. 先關(guān)閉mongodb進程:

方法一、 $ ./mongod --shutdown --dbpath /usr/local/mongo/data/db

方法二、 殺進程
$ ps -ef | grep mongo    #列出mongo的進程

       5451  5014  0 09:35 pts/11   00:00:00 /bin/sh /usr/local/mongodb/mongodb-linux-x86_64-ubuntu1604-3.2.8/bin/startmongodb.sh
       5452  5451  1 09:35 pts/11   00:00:03 ./mongod --dbpath /usr/local/mongodb/mongodb-linux-x86_64-ubuntu1604-3.2.8/data/db
       5539  5028  0 09:41 pts/12   00:00:00 grep --color=auto mongo

$ kill 5452

2.修改mongodb配置文件:

vi /etc/mongodb.conf

找到

security:
  authorization ,設(shè)置值為 disabled

security:
  authorization: disabled
# authorization: enabled

3.重啟數(shù)據(jù)庫

帶上數(shù)據(jù)庫目錄重啟才管用,不然啟動的是空數(shù)據(jù)庫,之前的數(shù)據(jù)就都找不到了。

現(xiàn)在看看服務(wù)器上的磁盤使用情況:

# du -ak / |sort -nrk 1 |head

25818080	/
12535712	/var
11605584	/var/lib
6638668	/var/lib/docker
6303520	/var/lib/docker/overlay2
4625676	/var/lib/mongodb
4549080	/www
3775880	/root
3242504	/www/server
3145744	/var/lib/mongodb/journal

通過我分析/var/lib/mongodb 這個目錄應(yīng)該就是我以前設(shè)置的數(shù)據(jù)庫目錄
# cd /var/lib/mongodb/ & ls -al
[1] 7142
total 1327156
drwxr-xr-x  4 mongodb mongodb      4096 Sep  8 15:56 .
drwxr-xr-x 48 root    root         4096 Dec  5  2019 ..
-rw-------  1 root    root     67108864 Nov  7  2018 admin.0
-rw-------  1 root    root     16777216 Nov  7  2018 admin.ns
-rw-------  1 root    root     67108864 Nov  8  2018 aoWechat.0
-rw-------  1 root    root     16777216 Nov  8  2018 aoWechat.ns
-rw-------  1 root    root     67108864 Nov  6  2019 aoWeixin.0
-rw-------  1 root    root     16777216 Oct 28  2019 aoWeixin.ns
drwxr-xr-x  2 root    root         4096 Sep  8 16:12 diagnostic.data
drwxr-xr-x  2 mongodb mongodb      4096 Sep  8 16:07 journal
-rw-------  1 mongodb mongodb  67108864 Sep  8 16:07 local.0
-rw-------  1 mongodb mongodb  16777216 Sep  8 16:07 local.ns
-rwxr-xr-x  1 mongodb mongodb         5 Sep  8 16:07 mongod.lock
-rw-------  1 root    root     67108864 Oct  1  2019 chat.0
-rw-------  1 root    root    134217728 Aug 20  2018 chat.1
-rw-------  1 root    root    268435456 Oct  1  2019 chat.2
-rw-------  1 root    root    536870912 Oct  1  2019 chat.3
-rw-------  1 root    root     16777216 Oct  1  2019 chat.ns
-rw-r--r--  1 root    root           69 Aug 14  2018 storage.bson

數(shù)據(jù)庫目錄找到了,現(xiàn)在帶目錄啟動數(shù)據(jù)庫

# mongod --dbpath /var/lib/mongodb

這個命令只是在終端臨時啟動數(shù)據(jù)庫,啟動之后另開一個終端窗口,進去登陸上數(shù)據(jù)庫

#:mongo
> > show dbs
admin         0.078GB
aoWechat   0.078GB
aoWeixin  0.078GB
local         0.078GB
smart     0.953GB

創(chuàng)建一個超級用戶:

db.createUser({user:“root”,pwd:“123456”,roles:[{role:“root”,db:“admin”}]})

退出數(shù)據(jù)庫,exit ,關(guān)閉數(shù)據(jù)庫。

修改配置文件啟動認(rèn)證模式。

vi /etc/mongod.conf


找到

security:
  authorization ,設(shè)置值為 enabled

security:
  authorization: enabled

現(xiàn)在我完整的配置如下:

# 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

systemLog:
  destination: file
  logAppend: true
  path: /var/log/mongodb/mongod.log

# network interfaces
net:
  port: 27017
  bindIp: 0.0.0.0

security:
  authorization: enabled
# authorization: disabled
# authorization: enabled

processManagement:
  fork: true

正式啟動數(shù)據(jù)庫,恢復(fù)完成。

/usr/bin/mongod -f /etc/mongod.conf

進入數(shù)據(jù)庫去看看,能查看數(shù)據(jù)了不。

# mongo
MongoDB shell version: 3.2.20
connecting to: test
> use admin
switched to db admin
> db.auth('root','123456')
1
> show tables
system.indexes
system.users
system.version
> show dbs
admin         0.078GB
aohuoWechat   0.078GB
aoshuoWeixin  0.078GB
local         0.078GB
smartchat     0.953GB
>

到此,相信大家對“如何解決MongoDB couldn‘t add user: not authorized on ‘your db‘ to execute command的問題”有了更深的了解,不妨來實際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進入相關(guān)頻道進行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI