MongoDB 備份(mongodump)與恢復(mongorestore)
備份:使用mongodump命令導出所有數據庫到指定目錄
參數說明:
--host:MongoDB所在服務器IP。
--port:MongoDB所在服務器端口。
-d:需要備份的數據庫實例。
-o:備份的數據存放位置。
-u : 指定登錄用戶
-p : 指定登錄用戶的密碼
-c : 指定要備份的集合
--authenticationDatabase 驗證數據庫名稱
如果備份出現這個錯誤
Failed: error connecting to db server: server returned error on SASL authentication step: Authentication failed
看這個帖子 mongodb 使用mongodump備份
1.備份指定的庫
執行命令備份成功, -d dbname
[root@MongoDB ~]# mongodump --host 127.0.0.1 --port 27017 -u admin -p 123456 -d db1 -o /tmp/mongobak/ --authenticationDatabase admin writing db1.user to writing db1.chat to done dumping db1.user (3 documents) done dumping db1.chat (3 documents) [root@MongoDB ~]# lsdb1
2.備份指定的集合
-d dbname
-c collection_name
[root@MongoDB mongobak]# mongodump --host 127.0.0.1 --port 27017 -u admin -p 123456 -d db1 -c chat -o /tmp/mongobak/ --authenticationDatabase admin writing db1.chat to done dumping db1.chat (3 documents) [root@MongoDB mongobak]# ls db1
3.備份所有的庫
不加 -d -c 參數
[root@MongoDB mongobak]# mongodump --host 127.0.0.1 --port 27017 -u admin -p 123456 -o /tmp/mongobak/ --authenticationDatabase admin 2019-04-14T00:49:41.752+0800 writing admin.system.users to 2019-04-14T00:49:41.778+0800 done dumping admin.system.users (1 document) 2019-04-14T00:49:41.778+0800 writing admin.system.version to 2019-04-14T00:49:41.806+0800 done dumping admin.system.version (2 documents) 2019-04-14T00:49:41.806+0800 writing db1.user to 2019-04-14T00:49:41.806+0800 writing db1.chat to 2019-04-14T00:49:41.839+0800 done dumping db1.user (3 documents) 2019-04-14T00:49:41.840+0800 done dumping db1.chat (3 documents) [root@MongoDB mongobak]# ls admin db1
恢復:使用mongorestore命令來導入備份的數據。
參數說明: -h --host:MongoDB所在服務器IP。 --port:MongoDB所在服務器端口。 -d:需要恢復的數據庫實例。
-u : 指定登錄用戶
-p : 指定登錄用戶的密碼
-c : 指定要恢復的集合
--drop :恢復的時候把之前集合drop掉
不用-o ,直接指定存放備份monggo數據的目錄
如果恢復命令出現這個錯誤
Failed: error connecting to db server: server returned error on SASL authentication step: Authentication failed
在命令加上 --authenticationDatabase admin
--authenticationDatabase 驗證數據庫名稱
1.恢復所有的庫
[root@MongoDB mongobak]# mongorestore --host 127.0.0.1 --port 27017 -u admin -p 123456 /tmp/mongobak/
2.恢復指定的庫
-d 指定恢復的庫名字
刪除數據庫
> use db1 switched to db db1 > db.dropDatabase() { "dropped" : "db1", "ok" : 1 }
[root@MongoDB mongobak]# mongorestore --host 127.0.0.1 --port 27017 -u admin -p 123456 -d db1 /tmp/mongobak/db1/ --authenticationDatabase admin
> use db1 switched to db db1 > show tables chat user > db.user.find() { "_id" : ObjectId("5ca7a4b0219efd687462f965"), "id" : 1, "name" : "jack", "age" : 73 } { "_id" : ObjectId("5ca7a4c4219efd687462f968"), "id" : 4, "name" : "xiaogang", "age" : 34, "hobby" : [ "籃球" ] } { "_id" : ObjectId("5ca7a4c4219efd687462f969"), "id" : 5, "name" : "ben", "age" : 24 }
3.恢復指定的集合
-c 指定恢復的集合名字
刪除集合先
> use db1 switched to db db1 > db.user.drop() true > show tables chat
恢復命令
[root@MongoDB mongobak]# mongorestore --host 127.0.0.1 --port 27017 -u admin -p 123456 -d db1 -c user /tmp/mongobak/db1/user.bson --authenticationDatabase admin
恢復
> use db1 switched to db db1 > show tables chat user