简体   繁体   English

如何使用大量数据迁移 MySQL?

[英]How to Migrate MySQL with tons of data?

So I want a service to start using a new MySQL instance like nothing happened.所以我希望服务开始使用新的 MySQL 实例,就像什么都没发生一样。 I just want to change the DB URL and credentials and have my service working like it was working.我只想更改 DB URL 和凭据,让我的服务像正常工作一样工作。

The problem is that we want to minimize the downtime.问题是我们希望最大限度地减少停机时间。

How if my database has 30GB of data?如果我的数据库有 30GB 的数据怎么办?

One of our solutions is:我们的解决方案之一是:

  • Create a new instance as if it's a follower创建一个新实例,就好像它是一个追随者
  • Make sure the new instance is fully synchronized确保新实例完全同步
  • Make service point to the new DB使服务点指向新数据库
  • Make new instance independent使新实例独立

We are just not sure how to make sure it's fully synchronized and if the new instance can become independent after it was a follower.我们只是不确定如何确保它完全同步,以及新实例在成为跟随者之后是否可以变得独立。

How to make sure it's fully synchronized and how to make it independent?如何确保它完全同步以及如何使其独立?

First you initialize the replica with a snapshot from the master.首先,您使用来自主服务器的快照初始化副本。 There are instructions here: https://dev.mysql.com/doc/refman/5.7/en/replication-howto.html这里有说明: https://dev.mysql.com/doc/refman/5.7/en/replication-howto.html

To make sure it's synchronized before you cut over:要确保在切换之前同步:

  1. Halt changes on the primary.停止主节点上的更改。 A good way to do this is to set global read_only=1;一个好方法是set global read_only=1;
  2. View the binary log position on the primary: show master status\G After you enable read_only there shouldn't be any new changes, so the binary log will stop growing (note users with SUPER privilege can still write changes, so you might need to prevent this with set global super_read_only=1; too).查看主节点上的二进制日志 position: show master status\G启用read_only后不应有任何新更改,因此二进制日志将停止增长(注意具有 SUPER 权限的用户仍然可以写入更改,因此您可能需要也可以使用set global super_read_only=1;防止这种情况)。
  3. View the binary log position on the replica: show slave status\G When Master_Log_File and Exec_Master_Log_Pos catch up to the values you observed on the primary, then it's caught up.查看副本上的二进制日志 position: show slave status\GMaster_Log_FileExec_Master_Log_Pos赶上您在主服务器上观察到的值时,它就赶上了。
  4. Change the application to connect directly to the replica and resume reading and writing queries.更改应用程序以直接连接到副本并继续读取和写入查询。
  5. Tell the replica to forget its replication subscription: reset slave;告诉副本忘记它的复制订阅: reset slave;
  6. Now the former primary is redundant, and it will only take moments of application traffic before the new primary (former replica) has new data that the old instance doesn't have.现在以前的主数据库是冗余的,在新的主数据库(以前的副本)拥有旧实例没有的新数据之前,只需要片刻的应用程序流量。

I recommend that you practice this a few times in a test environment before attempting it on your real system.我建议您在实际系统上尝试之前在测试环境中练习几次。 Once you are familiar with the steps, it should be possible to do it pretty quickly.一旦熟悉了这些步骤,应该可以很快完成。 I would be confident of doing it in less than one minute of downtime.我有信心在不到一分钟的停机时间内做到这一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM