MongoDB 备份恢复
本文最后更新于 2024-10-15,文章内容可能已经过时。
下载工具
前往 MongoDB官网 下载数据库备份恢复工具 MongoDB Command Line Database Tools Download
下载完成后进入 bin 目录 有一堆文件 如 mongodump mongorestore
备份
备份需要使用 mongodump 工具,具体使用方式可参考官网
mongodump -h ${host}:${port} -u ${username} -p ${passworld} -d ${db} -o ${targetPath}恢复
恢复需要使用 mongorestore 工具,具体使用方式可参考官网
mongorestore -h ${host}:${port} -u ${username} -p ${passworld} -d ${db} ${path}若出现报错:
Failed: can't create session: could not connect to server: connection() error occured during connection handshake: auth error: sasl conversation error: unable to authenticate using mechanism "SCRAM-SHA-1": (AuthenticationFailed) Authentication failed.
解决方法:
添加参数:--authenticationDatabase admin在连接mongo时,使用参数 --authenticationDatabase,会认证 -u 和 -p 参数指定的账户和密码。如果没有指定验证数据库,mongo使用连接字符串中指定的DB作为验证数据块
${host} 为数据库IP或域名地址
${port} 为数据库端口,通常为 27017
${username} 为数据库用户名,没有设置可不填
${passworld} 为数据库密码,没有设置可不填
${db} 为需要备份的数据库,若需要全部备份可不填
${targetPath} 为输出路径,默认当前目录