将现有数据库迁移到 Amazon RDS

发布于 2024-10-13 12:27:28 字数 36 浏览 8 评论 0 原文

如何将现有 MySQL 数据库导入 Amazon RDS?

How can I import existing MySQL database into Amazon RDS?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

不弃不离 2024-10-20 12:27:28

我在 AWS 文档上找到了 此页面,其中解释了如何使用 mysqldump 并将其通过管道传输到 RDS 实例。

这是他们的示例代码(在命令行/shell/ssh 中使用):
mysqldump acme | mysqldump acme | mysqldump acme | mysqldump acme | mysqldump acme | mysqldump acme mysql --host=主机名 --user=用户名 --password acme

其中 acme 是您要迁移的数据库,主机名/ username 是来自您的 RDS 实例的用户名。

您可以像连接常规 mysql 服务器一样连接到 RDS,只需确保将您的 EC2 IP 添加到您的安全组 此论坛帖子

我必须包含本地 mysqldump 的密码,所以我的命令最终看起来更像这样:
mysqldump --password=local_mysql_pass acme | mysqldump --password=local_mysql_pass acme | mysqldump --password=local_mysql_pass acme | mysql --host=主机名 --user=用户名 --password acme

FWIW,我刚刚完成了数据库的移动。我使用了 此 mysql 命令参考,例如创建用户和授予权限。

希望这有帮助!

I found this page on the AWS docs which explains how to use mysqldump and pipe it into an RDS instance.

Here's their example code (use in command line/shell/ssh):
mysqldump acme | mysql --host=hostname --user=username --password acme

where acme is the database you're migrating over, and hostname/username are those from your RDS instance.

You can connect to RDS as if it were a regular mysql server, just make sure to add your EC2 IPs to your security groups per this forum posting.

I had to include the password for the local mysqldump, so my command ended up looking more like this:
mysqldump --password=local_mysql_pass acme | mysql --host=hostname --user=username --password acme

FWIW, I just completed moving my databases over. I used this reference for mysql commands like creating users and granting permissions.

Hope this helps!

轻拂→两袖风尘 2024-10-20 12:27:28

导入数据有两种方式:

  1. mysqldump :如果您的数据大小小于1GB,您可以直接使用mysqldump命令将数据导入到RDS中。
  2. mysqlimport :如果您的数据大小超过 1GB 或任何其他格式,您可以将数据压缩为平面文件并使用 sqlimport 命令上传数据。

There are two ways to import data :

  1. mysqldump : If you data size is less than 1GB, you can directly make use of mysqldump command and import your data to RDS.
  2. mysqlimport : If your data size is more than 1GB or in any other format, you can compress the data into flat files and upload the data using sqlimport command.
失退 2024-10-20 12:27:28

我是 SqlYog 工具的忠实粉丝。它允许您连接到源和目标数据库并同步架构和/或数据。我还使用过 SQLWave,但改用了 SqlYog。自从我做出转变以来已经很久了,我已经不记得为什么要转变了。无论如何,这是我的两分钱。我知道有些人会反对我对 MySQL 的 Windows GUI 工具的建议。事实上,我非常喜欢 SqlYog 产品,所以我从 Wine 运行它(对我来说,在 Ubuntu 上的 Wine 上运行完美)。
博客可能会有所帮助。

在此处输入图像描述

I'm a big fan of the SqlYog tool. It lets you connect to your source and target databases and sync schema and/or data. I've also used SQLWave, but switched to SqlYog. Been so long since I made the switch that I can't remember exactly why I switched. Anyway, that's my two cents. I know some will object to my suggestion of Windows GUI tools for MySQL. I actually like the SqlYog product so much that I run it from Wine (works flawlessly from Wine on Ubuntu for me).
This blog might be helpful.

enter image description here

烂人 2024-10-20 12:27:28

GoSquared 工程帖子的快速摘要:

配置 + 启动

  • 在实例处于最低负载时选择维护时段和备份时段 选择
  • 是否选择多可用区(强烈建议用于自动故障转移和维护)
  • 启动 RDS 实例
  • 配置安全组这样你的应用程序等就可以访问新实例

数据迁移+准备

  1. 如果你还没有
  2. 运行mysqldump --single-transaction --master-data=2 -C -q dbname -u username -p > 则启用binlogging backup.sql 在旧实例上转储当前数据
  3. 运行 mysql -u username -p -h RDS_endpoint DB_name backup.sql 将数据导入到您的 RDS 实例中(这可能需要一段时间,具体取决于您的数据库大小)
  4. 同时,您当前的生产实例仍在提供查询服务 - 这是主数据的位置=2 并且 binlogging 出现
  5. 在你的 backup.sql 文件中,顶部会有一行,看起来像 CHANGE MASTER TO MASTER_LOG_FILE='mysql-bin.000003′, MASTER_LOG_POS=350789121;< /code>
  6. 以 SQL 文件形式获取 backup.sql 以来的差异 mysqlbinlog /var/log/mysql/mysql-bin.000003 --start-position=350789121 --base64-output=从不> output.sql
  7. 在 RDS 实例上运行这些查询以更新它cat output.sql | mysql -h RDS_endpoint -u username -p DB_name
  8. 通过在最新的 output.sql 文件末尾查找 end_log_pos 来获取新的日志位置。
  9. 获取自上次 output.sql 以来的差异(如步骤 6)并重复步骤 7 + 8。

实际迁移

  1. 让所有应用程序准备好使用新的 RDS 实例快速部署
  2. 获取最新的 来自 output.sql 的 end_log_pos
  3. 在旧实例上运行 FLUSH TABLES WITH READ LOCK; 以停止所有写入
  4. 开始使用新 RDS 实例部署应用程序
  5. 运行步骤 6上面的 -8 使用对旧服务器的最后一次查询来更新 RDS 实例

结论

使用此方法,您将有少量时间(取决于部署应用程序所需的时间 + MySQL 实例提供的写入次数) - 可能只有一两分钟),旧服务器的写入被拒绝,但您将获得一致的迁移,而不会出现读取停机时间。

此处提供了完整详细的帖子,解释了我们 (GoSquared) 如何以最短的停机时间(包括错误调试)迁移到 RDS:https://engineering.gosquared.com/migration-mysql-to-amazon-rds

A quick summary of a GoSquared Engineering post:

Configuration + Booting

  • Select a maintenance window and backup window when the instance will be at lowest load
  • Choose Multi-AZ or not (highly recommended for auto-failover and maintenance)
  • Boot your RDS instance
  • Configure security groups so your apps etc can access the new instance

Data migration + preparation

  1. Enable binlogging if you haven't already
  2. Run mysqldump --single-transaction --master-data=2 -C -q dbname -u username -p > backup.sql on the old instance to take a dump of the current data
  3. Run mysql -u username -p -h RDS_endpoint DB_name < backup.sql to import the data into your RDS instance (this may take a while depending on your DB size)
  4. In the meantime, your current production instance is still serving queries - this is where the master-data=2 and binlogging comes in
  5. In your backup.sql file, you'll have a line at the top that looks like CHANGE MASTER TO MASTER_LOG_FILE=’mysql-bin.000003′, MASTER_LOG_POS=350789121;
  6. Get the diff since backup.sql as an SQL file mysqlbinlog /var/log/mysql/mysql-bin.000003 --start-position=350789121 --base64-output=NEVER > output.sql
  7. Run those queries on your RDS instance to update it cat output.sql | mysql -h RDS_endpoint -u username -p DB_name
  8. Get the new log position by finding end_log_pos at the end of the latest output.sql file.
  9. Get the diff since the last output.sql (like step 6) and repeat steps 7 + 8.

The actual migration

  1. Have all your apps ready to deploy quickly with the new RDS instance
  2. Get the latest end_log_pos from output.sql
  3. Run FLUSH TABLES WITH READ LOCK; on the old instance to stop all writes
  4. Start deploying your apps with the new RDS instance
  5. Run steps 6-8 from above to update the RDS instance with the last queries to the old server

Conclusion

Using this method, you'll have a small amount of time (depending on how long it takes to deploy your apps + how many writes your MySQL instance serves - probably only a minute or two) with writes being rejected from your old server, but you will have a consistent migration with no read downtime.

A full and detailed post explaining how we (GoSquared) migrated to RDS with minimal downtime (including error debugging) is available here: https://engineering.gosquared.com/migrating-mysql-to-amazon-rds.

找回味觉 2024-10-20 12:27:28

我完全同意@SanketDangi。

有两种方法可以做到这一点,其中一种方法是使用 mysqldump 或 mysqlimport 。

我见过在云上恢复数据损坏时产生问题的情况。

然而,现在在云上导入应用程序变得更加容易。您尝试通过 ravello 将数据库服务器上传到公共云。

您可以使用 ravello 在 Amazon 上导入数据库服务器本身。

披露:我为 ravello 工作。

I am completely agree with @SanketDangi.

There are two ways of doing this one way is as suggested using either mysqldump or mysqlimport.

I have seen cases where it creates problem while restoring data on cloud gets corrupt.

However importing applications on cloud has became much easier now a days. You try uploading your DB server on to public cloud through ravello.

You can import your database server itself on Amazon using ravello.

Disclosure: I work for ravello.

儭儭莪哋寶赑 2024-10-20 12:27:28

最简单的例子:

# export local db to sql file:
mysqldump -uroot -p —-databases qwe_db > qwe_db.sql

# Now you can edit qwe_db.sql file and change db name at top if you want

# import sql file to AWS RDS:
mysql --host=proddb.cfrnxxxxxxx.eu-central-1.rds.amazonaws.com --port=3306 --user=someuser -p qwe_db < qwe_db.sql

Simplest example:

# export local db to sql file:
mysqldump -uroot -p —-databases qwe_db > qwe_db.sql

# Now you can edit qwe_db.sql file and change db name at top if you want

# import sql file to AWS RDS:
mysql --host=proddb.cfrnxxxxxxx.eu-central-1.rds.amazonaws.com --port=3306 --user=someuser -p qwe_db < qwe_db.sql
哆兒滾 2024-10-20 12:27:28

适用于 Mysql 的 AWS RDS 客户数据导入指南可在此处获取:http://aws.amazon.com/articles/2933

  • 创建包含要加载的数据的平面文件
  • 停止访问目标数据库实例的任何应用程序
  • 创建数据库快照
  • 禁用 Amazon RDS 自动备份
  • 使用 mysqlimport 加载数据
  • 再次启用自动备份

AWS RDS Customer data Import guide for Mysql is available here : http://aws.amazon.com/articles/2933

  • Create flat files containing the data to be loaded
  • Stop any applications accessing the target DB Instance
  • Create a DB Snapshot
  • Disable Amazon RDS automated backups
  • Load the data using mysqlimport
  • Enable automated backups again
老娘不死你永远是小三 2024-10-20 12:27:28

如果您使用终端,这对我有用:

mysqldump -u local_username -plocal_password local_db_name | mysql -h myRDS-at-amazon.rds.amazonaws.com -u rds-username -prds_password_xxxxx remote_db_name

然后我使用 MYSQL WorkBench(免费下载)来检查它是否正常工作,因为按提交后命令行是静态的,我可能可以将 -v 放在末尾以查看它是否有效输出

注意:-p后面没有空格

If you are using the terminal this is what worked for me:

mysqldump -u local_username -plocal_password local_db_name | mysql -h myRDS-at-amazon.rds.amazonaws.com -u rds-username -prds_password_xxxxx remote_db_name

and then i used MYSQL WorkBench (free download) to check it was working because the command line was static after pressing submit, i could have probably put -v at end to see it's output

Note: there is no space after -p

别再吹冷风 2024-10-20 12:27:28

这是我已经完成并取得成功的步骤。

获取所需数据库的 MySQLdump。

mysqldump -u username -p databasename --single-transaction --quick --lock-tables=false >databasename-backup-$(date +%F).sql

(不要忘记替换用户名为 root – 大多数情况下,数据库名称 -> 您要迁移到 RDS 的数据库的数据库名称)

出现提示后,输入您的密码。

完成后,从 MySQL 服务器登录到 RDS 实例(确保安全组配置为允许从 Ec2 到 RDS 的连接)

mysql -h 主机地址 -P 3306 -u rdsusername -p

(不要忘记将主机地址替换为地址您的 RDS 实例和 rdsusernmae 以及您的 RDS 实例的用户名,当提示时也提供密码)

您可以在 – Connectivity & 下找到该主机地址。安全->端点& AWS 控制台中 RDS 数据库下的端口。

登录后,使用 MySQL 命令创建数据库:

create database databasename;
\q

在 RDS 中创建数据库后,导入在步骤 1 中创建的 SQL 文件:

mysql -h 主机地址 -u rdsusername -p 数据库名称 backupfile.sql

这应该将 SQL 文件导入到 RDS 并将内容恢复到新数据库中。

参考来源:

Here are the steps which i have done and had sucess.

Take the MySQLdump of the needed database.

mysqldump -u username -p databasename --single-transaction --quick --lock-tables=false >databasename-backup-$(date +%F).sql

( Dont forget to replace the username as root – most of the times, and databasename -> Db name of database which you are going to migrate to RDS )

Once prompted, enter your password.

Once done, login to the RDS Instance from your MySQL server ( Make sure the security groups are configured to allow the connection from Ec2 to RDS )

mysql -h hostaddress -P 3306 -u rdsusername -p

( Dont forget to replace hostaddress with the address of your RDS Instance and rdsusernmae with username for your RDS Instance, when prompted give the password too )

You find that hostaddress under – Connectivity & security -> Endpoint & port under RDS Database From AWS Console.

Once logged in, create the database using MySQL commands :

create database databasename;
\q

Once Database is created in RDS, Import the SQL file created in Step 1 :

mysql -h hostaddress -u rdsusername -p databasename < backupfile.sql

This should import the SQL file to RDS and restore the contents into the new database.

Reference from : https://k9webops.com/blog/migrate-an-existing-database-on-mysql-mariadb-to-an-already-running-rds-instance-on-the-aws/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文