一、导出数据
- 整个数据库导出命令:
mongodump -u username -p password -h dbhost<:port> -d dbname -o dbdirectory
- 例子
mongodump -h 127.0.0.1:27017 -d test_db -o ./test_db
如果设有帐号密码请加上-u 和-p参数如下:
mongodump -u test -p passwd -h 127.0.0.1:27017 -d test_db -o ./test_db
- 单表导出
# root:帐号 xxxx:密码 game_server 数据库名称 sys_config表名称 sys_config.json要导出的表文件
mongoexport -u root -p xxxx -d game_server -c sys_config -o sys_config.json
#没有帐号密码的可以不要 -u -p参数
二、导入数据
- 整个数据库导入命令:
mongorestore -u username -p password -h <hostname><:port> -d dbname <path>
- 例子
mongorestore -h 127.0.0.1:27017 -d test_db ./test_db
如果设有帐号密码依然请加上-u 和-p参数如下:
mongorestore -u test -p passwd -h 127.0.0.1:27017 -d test_db ./test_db
- 单表导入
# root 帐号 xxx 密码 game_server 数据库名称 game_ip_list_config表名称(要恢复的表) game_ip_list_config.json文件名(源数据)
mongoimport -u root -p xxx --db game_server --collection game_ip_list_config --file game_ip_list_config.json
#没有帐号密码的可以不要 -u -p参数
三、可能遇到的错误
- Failed: error connecting to db server: server returned error on SASL authentication step: Authentication failed
需要添加 --authenticationDatabase admin 参数 ,如导入数据命令mongorestore --authenticationDatabase admin -u test -p passwd -h 127.0.0.1:27017 -d test_db ./test_db