DBA应用技巧中如何利用MySQL技能学习MongoDB,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。

(1)、mongorestore恢复工具  我们先看一下此工具的帮助信息:

双击代码全选123456789101112131415161718192021222324[root@localhost bin]# ./mongorestore --helpusage: ./mongorestore [options] [directory or filename to restore from]options:--help produce help message-v [ --verbose ] be more verbose (include multiple times for moreverbosity e.g. -vvvvv)-h [ --host ] arg mongo host to connect to ( <set name>/s1,s2 for sets)--port arg server port. Can also use --host hostname:port--ipv6 enable IPv6 support (disabled by default)-u [ --username ] arg username-p [ --password ] arg password--dbpath arg directly access mongod database files in the givenpath, instead of connecting to a mongod server -needs to lock the data directory, so cannot be usedif a mongod is currently accessing the same path--directoryperdb if dbpath specified, each db is in a separatedirectory-d [ --db ] arg database to use-c [ --collection ] arg collection to use (some commands)--objcheck validate object before inserting--filter arg filter to apply before inserting--drop drop each collection before import--oplogReplay replay oplog for point-in-time restore[root@localhost bin]#

  例如我们先将”foo”库删除了:

双击代码全选123456789101112[root@localhost bin]# ./mongoMongoDB shell version: 1.8.1connecting to: test> use fooswitched to db foo> db.dropDatabase();{ "dropped" : "foo", "ok" : 1 }> show dbsadmin 0.0625GBlocal (empty)test 0.0625GB>

  然后下面我们将演示如何恢复这个库:

双击代码全选123456789101112131415161718[root@localhost bin]# ./mongorestore --directoryperdb /data/dumpconnected to: 127.0.0.1Sun Apr 22 12:01:27 /data/dump/foo/t1.bsonSun Apr 22 12:01:27 going into namespace [foo.t1]Sun Apr 22 12:01:27 2 objects foundSun Apr 22 12:01:27 /data/dump/foo/t2.bsonSun Apr 22 12:01:27 going into namespace [foo.t2]Sun Apr 22 12:01:27 1 objects foundSun Apr 22 12:01:27 /data/dump/foo/system.users.bsonSun Apr 22 12:01:27 going into namespace [foo.system.users]Sun Apr 22 12:01:27 1 objects foundSun Apr 22 12:01:27 /data/dump/foo/system.indexes.bsonSun Apr 22 12:01:27 going into namespace [foo.system.indexes]Sun Apr 22 12:01:27 { name: "_id_", ns: "foo.system.users", key: { _id: 1 }, v: 0 }Sun Apr 22 12:01:27 { name: "_id_", ns: "foo.t2", key: { _id: 1 }, v: 0 }Sun Apr 22 12:01:27 { name: "_id_", ns: "foo.t1", key: { _id: 1 }, v: 0 }Sun Apr 22 12:01:27 3 objects found[root@localhost bin]#

  通过工具返回信息,我们可以看到foo中的数据已经被恢复回来了, 接下来我们到库里去验证一下:

双击代码全选1234567891011[root@localhost bin]# ./mongoMongoDB shell version: 1.8.1connecting to: test> use fooswitched to db foo> show collections;system.indexessystem.userst1t2>

  结果证明foo库表已经被成功恢复回来了。

关于DBA应用技巧中如何利用MySQL技能学习MongoDB问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注亿速云行业资讯频道了解更多相关知识。