昨天,今天,明天,每天的每天,你是否都多懂得一点点...

星期日, 十一月 29, 2020

[网络技术]把Mlab的数据库迁移到Mongodb Atlas

Mlab 被 mongodb 兼并已经一年多了。

现在迎来了最后期限。现在要着手把数据转移了。

我这里使用 mongodump 和 mongorestore 两个命令来进行转移。mongodump 用来从Mlab 的原数据库中把数据库保存成本地文件。 而 mongorestore 则正好相反, 把本地的文件恢复到 mongo altas 中。 

使用如下命令进行备份。

[code]
mongodump --host ds036577.mlab.com --port 36577 -u <username> -p <password> --db <dbname>
[/code]

以上的 hostname 和 port number 可以登录 Mlab 查到。

然后使用以下命令恢复备份到 mongo atlas

[code]
mongorestore --uri mongodb+srv://fennng:<PASSWORD>@cluster0.yfsui.azure.mongodb.net 
[/code]
以上的连接字串可以登录你的 mongodb atlas 查到, 如下





在使用以上命令之前, 请确保你运行命令的环境的IP地址已经被授权

如下图



还有一个非常重要的地方, 也是让我在这个备份过程中整整卡了一天的地方, 因为得到以下错语

 error connecting to host: could not connect to server: connection() : auth error: sasl conversation error: unable to authenticate using mechanism "SCRAM-SHA-1": (AtlasError) bad auth : Authentication failed.


而我又很确定我的密码是正确的, 因为用 mongo shell 是成功的, 所以密码绝对没有错。 害我试了千万种方式, 把不同的 collection string 组合了个遍。

最后发现, 因为我的密码里面有特殊字符, 在使用 --uri 输入密码的情况,我的密码部分必须先 url encode. 否则密码不正确, 是无论如何也连不上的。

 
--
Feng

其它博客地址

此博客的同步博客地址: http://fengnz.wordpress.com
这里进入我的MSN SPACE.