溫馨提示×

溫馨提示×

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

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

解決mongodb數(shù)據(jù)丟失的方法

發(fā)布時間:2020-06-29 11:48:45 來源:億速云 閱讀:1275 作者:清晨 欄目:編程語言

這篇文章將為大家詳細講解有關解決mongodb數(shù)據(jù)丟失的方法,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。

mongodb數(shù)據(jù)丟失后采用增加mongo數(shù)據(jù)庫的auth權限進行解決。

增加了auth權限之后,設置了用戶名和密碼,重啟node,發(fā)現(xiàn)如下錯誤!

2017-01-02T07:29:07.626753+00:00 app[web.1]: MongoError: auth failed

2017-01-02T07:29:07.626756+00:00 app[web.1]:     at Object.toError (/app/node_modules/mongoose/node_modules/mongodb/lib/mongodb/utils.js:114:11)

2017-01-02T07:29:07.626758+00:00 app[web.1]:     at /app/node_modules/mongoose/node_modules/mongodb/lib/mongodb/db.js:1156:31

2017-01-02T07:29:07.626759+00:00 app[web.1]:     at /app/node_modules/mongoose/node_modules/mongodb/lib/mongodb/db.js:1890:9

2017-01-02T07:29:07.626760+00:00 app[web.1]:     at Server.Base._callHandler 

(/app/node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/base.js:448:41)

2017-01-02T07:29:07.626762+00:00 app[web.1]:     at /app/node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/server.js:481:18

2017-01-02T07:29:07.626763+00:00 app[web.1]:     at MongoReply.parseBody 

(/app/node_modules/mongoose/node_modules/mongodb/lib/mongodb/responses/mongo_reply.js:68:5)

2017-01-02T07:29:07.626766+00:00 app[web.1]:     at null.<anonymous> 

(/app/node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/connection_pool.js:201:13)

2017-01-02T07:29:07.626765+00:00 app[web.1]:     at emit (events.js:95:17)

2017-01-02T07:29:07.626764+00:00 app[web.1]:     at null.<anonymous> 

(/app/node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/server.js:439:20)

2017-01-02T07:29:07.626767+00:00 app[web.1]:     at emit (events.js:98:17)

2017-01-02T07:29:07.413053+00:00 app[web.1]: Listen the port 11221

2017-01-02T07:29:07.623584+00:00 app[web.1]: /app/node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/base.js:246

2017-01-02T07:29:08.438433+00:00 heroku[web.1]: Process exited with status 8

這是因為mongodb 3.0的用戶機制改變了,數(shù)據(jù)庫賬戶需要重新設置一下。

具體方法如下:

方法一:

shell進入mongo,use admin,創(chuàng)建admin賬戶3.0創(chuàng)建新用戶方法 db.createUser,具體可查看官方文檔

use admin
db.createUser(
   {
     user: "root",
     pwd: "password",
     roles:
       [
         { role: "dbOwner", db: "你的數(shù)據(jù)庫名字" }
       ]
   }
)

創(chuàng)建一個超級管理員賬號,

這個root就可以操作你的數(shù)據(jù)庫了!

方法二:

首先關閉 auth權限

然后

use 你的數(shù)據(jù)庫表名
db.createUser(
   {
     user: "root",
     pwd: "password",
     roles:
       [
         { role: "dbOwner", db: "你的數(shù)據(jù)庫名字" }
       ]
   }
)

創(chuàng)建針對你數(shù)據(jù)庫的用戶名和密碼。

然后:

進入到你的數(shù)據(jù)庫表,use "你的數(shù)據(jù)庫表" 驗證剛才創(chuàng)建的root 賬戶

  db.auth(‘root’,‘pwd’)//成功連接

重啟mongodb,開啟auth=true;(開啟--auth)

重啟mongodb數(shù)據(jù)庫。首先關閉數(shù)據(jù)庫:

killall mongod

然后啟動!

mongodb數(shù)據(jù)庫重啟之后,重啟nodejs,發(fā)現(xiàn)還是會報錯誤!錯誤如下

throw new Error('invalid schema, expected mongodb'

經(jīng)過查詢,然后升級了一下mongodb和schema等等,然后在重啟nodejs,發(fā)現(xiàn)可以啟動了!因此,mongo數(shù)據(jù)庫博客安全性修改完畢。

關于解決mongodb數(shù)據(jù)丟失的方法就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

向AI問一下細節(jié)

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

AI