mongodb常用操作

以下是在mongodb3.2实际操作。
数据备份:
mongodump --host IP --port 端口 -u 用户名 -p 密码 -d 数据库 -o 文件存在路径

[dev@aws17-pg ~]$ mongodump --host 10.0.1.12 --port 27020 -d applock -o /home/dev/backup/
2017-02-15T03:39:38.115-0500    writing applock.lockdetail to 
2017-02-15T03:39:41.116-0500    [######..................]  applock.lockdetail  932703/3502968  (26.6%)
2017-02-15T03:39:44.158-0500    [############............]  applock.lockdetail  1854275/3502968  (52.9%)
2017-02-15T03:39:47.116-0500    [###################.....]  applock.lockdetail  2832777/3502968  (80.9%)
2017-02-15T03:39:49.174-0500    [########################]  applock.lockdetail  3502968/3502968  (100.0%)
2017-02-15T03:39:49.174-0500    done dumping applock.lockdetail (3502968 documents)

数据还原:
mongorestore -h IP --port 端口 -u 用户名 -p 密码 -d 数据库 --drop 文件存在路径
--drop的意思是,先删除所有的记录,然后恢复。

[root@pg-test ~]# mongorestore -d applock /root/applock
connected to: 127.0.0.1
2017-02-15T17:20:50.057+0800 /root/applock/lockdetail.bson
2017-02-15T17:20:50.057+0800    going into namespace [applock.lockdetail]
2017-02-15T17:20:50.110+0800    Created collection applock.lockdetail with options: { "create" : "lockdetail" }
2017-02-15T17:20:53.034+0800        Progress: 52164170/622513894    8%  (bytes)
2017-02-15T17:20:56.000+0800        Progress: 104398989/622513894   16% (bytes)
2017-02-15T17:20:59.016+0800        Progress: 156726761/622513894   25% (bytes)
2017-02-15T17:21:02.015+0800        Progress: 201102195/622513894   32% (bytes)
2017-02-15T17:21:05.030+0800        Progress: 252299457/622513894   40% (bytes)
2017-02-15T17:21:08.033+0800        Progress: 306753671/622513894   49% (bytes)
2017-02-15T17:21:11.020+0800        Progress: 361233088/622513894   58% (bytes)
2017-02-15T17:21:14.027+0800        Progress: 408729106/622513894   65% (bytes)
2017-02-15T17:21:17.011+0800        Progress: 462108350/622513894   74% (bytes)
2017-02-15T17:21:20.000+0800        Progress: 512628006/622513894   82% (bytes)
2017-02-15T17:21:23.013+0800        Progress: 570320886/622513894   91% (bytes)
3502968 objects found
2017-02-15T17:21:25.962+0800    Creating index: { key: { _id: 1 }, name: "_id_", ns: "applock.lockdetail" }

查看版本:
db.version()

关闭:
use admin
db.shutdownServer()

另外:
如果mongodb启动不起来,报:child process failed, exited with error number 1,或者其它数字,检查是否目录都存在且具有写入权限,另外尝试用repaire修复

slave执行show dbs
rs.slaveOk()
不然会报错 Error: listDatabases failed:{ "note" : "from execCommand", "ok" : 0, "errmsg" : "not master" }

标签: none

添加新评论