如何将数据库从 Amazon RDS MySQL 实例导出到本地实例?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30217299/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 21:00:16  来源:igfitidea点击:

How to export database from Amazon RDS MySQL instance to local instance?

mysqlamazon-web-servicesamazon-ec2exportamazon-rds

提问by Srini K

AWS documentation has this pagethat talks about importing and exporting data from MySQL server, but it's mostly about import. The only thing I see in their documentation is a way to export 5.6 data using replication, which is documented here. I was wondering if there is a simpler way to export data using mysqldump and load in local database. The database that I want to export is not huge, may be 1GB, so size is not a issue.

AWS 文档有这个页面讨论从 MySQL 服务器导入和导出数据,但主要是关于导入。我在他们的文档中看到的唯一内容是使用复制导出 5.6 数据的方法,此处记录了该方法。我想知道是否有更简单的方法来使用 mysqldump 导出数据并加载到本地数据库中。我要导出的数据库并不大,可能只有 1GB,所以大小不是问题。

回答by TJ-

Sure.

当然。

Take the dump from the remote RDS Server:

从远程 RDS 服务器获取转储:

mysqldump -h rds.host.name -u remote_user_name -p remote_db > dump.sql

When prompted for password, provide the password for user=remote_user_name (remote server)

当提示输入密码时,提供 user=remote_user_name(远程服务器)的密码

Upload it to your local mySql instance:

将其上传到本地 mySql 实例:

mysql -u local_user_name -p local_db < dump.sql

Also, if you own an ec2server in the same region, I'd suggest take a dump there. zip the file and then scpit to your local machine. Typically, the compressed version of the file would be much smaller and you'd be able to transfer it quicker.

另外,如果您ec2在同一地区拥有一台服务器,我建议您在那里进行转储。将文件压缩,然后将scp其压缩到您的本地计算机。通常,文件的压缩版本会小得多,您可以更快地传输它。

回答by Krishan Kumar Mourya

To export db from RDS

从 RDS 导出数据库

mysqldump -h rds.host.name -u remote_user_name -p remote_db > remote_db.sql

When prompted for password, provide the password

当提示输入密码时,提供密码

To import db on RDS

在 RDS 上导入数据库

mysql -h rds.host.name -u remote_user_name -p remote_db < remote_db.sql

When prompted for password, provide the password

当提示输入密码时,提供密码

回答by tm1701

Another very easy option is by using the MySql Workbench. In the toolbar select 'Database' and 'Data export'. Select the right options, the target file ... and you're done! Easy does it!

另一个非常简单的选择是使用 MySql Workbench。在工具栏中选择“数据库”和“数据导出”。选择正确的选项、目标文件……就大功告成了!轻而易举!

回答by Kh?i Lê

The best way to export data from RDS is create new EC2 instance to connect and dump mysql.

从 RDS 导出数据的最佳方法是创建新的 EC2 实例来连接和转储 mysql。

  1. Create new EC2 Linux2 instance
  2. Connect SSH
  3. Install Docker

    • Update the installed packages and package cache on your instance.

      sudo yum update -y

    • Install the most recent Docker Community Edition package.
    • Amazon Linux 2.

      sudo amazon-linux-extras install docker

    • Amazon Linux.

      sudo yum install docker

    • Start the Docker service.

      sudo service docker start

    • Add the ec2-user to the docker group so you can execute Docker commands without using sudo.

      sudo usermod -a -G docker ec2-user

    • Log out and log back in again to pick up the new docker group permissions. You can accomplish this by closing your current SSH terminal window and reconnecting to your instance in a new one. Your new SSH session will have the appropriate docker group permissions.

    • Verify that the ec2-user can run Docker commands without sudo.

      docker info

  4. Run mysql container

    docker run -it --network some-network --rm mysql mysql -h some-mysql -u example-user -p

  5. Run dump sql

    mysqldump -h host -u use_name -P 3306 -p --databases db_name | gzip > db_name.gz

  6. Copy file from container to host

    docker cp container_id:/home /home/ec2-user/sql_backup

  1. 创建新的 EC2 Linux2 实例
  2. 连接SSH
  3. 安装 Docker

    • 更新实例上已安装的软件包和软件包缓存。

      须藤 yum 更新 -y

    • 安装最新的 Docker 社区版软件包。
    • 亚马逊Linux 2。

      sudo amazon-linux-extras 安装 docker

    • 亚马逊 Linux。

      须藤 yum 安装码头工人

    • 启动 Docker 服务。

      须藤服务泊坞窗启动

    • 将 ec2-user 添加到 docker 组,这样您就可以在不使用 sudo 的情况下执行 Docker 命令。

      sudo usermod -a -G docker ec2-user

    • 注销并重新登录以获取新的 docker 组权限。您可以通过关闭当前的 SSH 终端窗口并在新窗口中重新连接到您的实例来完成此操作。您的新 SSH 会话将具有适当的 docker 组权限。

    • 验证 ec2-user 可以在没有 sudo 的情况下运行 Docker 命令。

      码头工人信息

  4. 运行mysql容器

    docker run -it --network some-network --rm mysql mysql -h some-mysql -u example-user -p

  5. 运行转储sql

    mysqldump -h host -u use_name -P 3306 -p --databases db_name | gzip > db_name.gz

  6. 将文件从容器复制到主机

    docker cp container_id:/home /home/ec2-user/sql_backup