[Ruby on rails + Mysql] Data migration procedure memo when switching from heroku to AWS

Contents

Write down as a memorandum about data migration when migrating the production environment of the application created by ruby ​​on rails from heroku to AWS.

Actually, I think that moving the server environment after the actual operation is a bad move because it takes a lot of risk and effort, but forgive me because it is a personal development.

environment

・ Ruby 2.7.1 ・ Rails 6.0.3.2 · Mysql 5.7

It is assumed that the AWS infrastructure is built in the following form

スクリーンショット 2021-01-11 10.27.31.png

heroku side: Export data from clearDB (Mysql)

Get database URL

$heroku config --app app name| grep DATABASE_URL
CLEARDB_DATABASE_URL:     mysql://<USER_NAME>:<PASSWORD>@< HOST >/<DB_NAME>?reconnect=true

Export data

Export the data. It will probably take a while, so wait patiently.

If dump.sql is output to the current directory, it is successful.

mysqldump -uUSER_NAME -pPASSWORD -h HOST -r dump.sql --single-transaction DB_NAME --skip-column-statistics

To briefly explain the contents of the options,

--single-transaction ・ ・ ・ Enclose the dump process in a transaction. Effective for maintaining data integrity. It seems that another option is required for DBs that include MyISAM tables.

--skip-column-statistics ・ ・ ・ Avoid errors that occur when dumping to a MySQL server earlier (such as 5.7) with mysqldump 8 or later. I'm not sure about the details. ..

Refer to the following article https://blog.pinkumohikan.com/entry/mysqldump-disable-column-statistics

There was also an article that wrote about frequently used options for mysqldump, so I'll post a link here.

** Frequently used mysqldump options and usage examples ** https://qiita.com/ryounagaoka/items/7be0479a36c97618907f

AWS side: Transfer sql to EC2

Premise ・ Ssh connection is established -If the client ssh/config is as follows

Host myapp
        HostName        IPAddress
        Port            22
        IdentityFile    ~/.ssh/myapp_rsa
        User            hoge

Hit the following command

scp source file myapp:/Forwarding directory

If you cannot transfer, refer to the following site etc. http://gallardolp570.hatenablog.com/entry/2014/11/17/205325

AWS side: Import sql to RDS

Confirm the endpoint, user name, and password to connect to in advance.

mysql -h endpoint-u username-p DB name> dump.sql

If you want to check the progress, connect with a pipe and attach a pv command. https://qiita.com/hiroq/items/efd8c3580c9c9457c869

If it is reflected, the import is successful. Thank you for your hard work. There are many parts that imitate what you see, so I would appreciate it if you could point out any mistakes.

Reference site

http://kayakuguri.github.io/blog/2015/09/10/mysql-postgres-import-export/

https://photo-tea.com/p/aws-ec2-to-rds-csv/

http://gallardolp570.hatenablog.com/entry/2014/11/17/205325

https://www.it-swarm-ja.tech/ja/mysql/mysqldump%E3%82%A8%E3%83%A9%E3%83%BC1045%E6%AD%A3%E3%81%97%E3%81%84%E3%83%91%E3%82%B9%E3%83%AF%E3%83%BC%E3%83%89%E3%81%AA%E3%81%A9%E3%81%AB%E3%82%82%E3%81%8B%E3%81%8B%E3%82%8F%E3%82%89%E3%81%9A%E3%82%A2%E3%82%AF%E3%82%BB%E3%82%B9%E3%81%8C%E6%8B%92%E5%90%A6%E3%81%95%E3%82%8C%E3%81%BE%E3%81%97%E3%81%9F/1043190719/

https://qiita.com/hiroq/items/efd8c3580c9c9457c869

https://blog.pinkumohikan.com/entry/mysqldump-disable-column-statistics

Recommended Posts

[Ruby on rails + Mysql] Data migration procedure memo when switching from heroku to AWS
[Ruby on Rails] From MySQL construction to database change
Deploy to Heroku [Ruby on Rails] Beginner
[Rails MySQL] How to reset DB on heroku
[Updated from time to time] Ruby on Rails Convenient methods
[Ruby On Rails] How to reset DB in Heroku
From 0 to Ruby on Rails environment construction [macOS] (From Homebrew installation to Rails installation)
From Ruby on Rails error message display to Japanese localization
[Ruby on Rails] Column restrictions when saving to DB (4 representatives)
[Ruby On Rails] How to search and save the data of the parent table from the child table
Ruby on Rails --From environment construction to simple application development on WSL2
How to use Ruby on Rails
[Rails / Heroku / MySQL] How to reset the DB of Rails application on Heroku
Ruby on Rails 6.0 environment construction memo
Upgrade from MYSQL5.7 to 8.0 on CentOS 6.7
Deploy Rails on Docker to heroku
What to do when Blocked Host: "host name" appears in Ruby on Rails
Environment construction of Ruby on Rails from 0 [Cloud9] (From Ruby version change to Rails installation)
Ruby on Rails ✕ Docker ✕ MySQL Introducing Docker and docker-compose to apps under development
[Personal memo] Summary of stumbling blocks when deploying Rails apps to AWS
[Ruby on Rails] Introduction of initial data
Ruby on Rails6 Practical Guide cp13 ~ cp15 [Memo]
[Ruby on Rails] How to use CarrierWave
Deploy to heroku with Docker (Rails 6, MySQL)
Ruby on Rails6 Practical Guide cp7 ~ cp9 [Memo]
Preparing to introduce jQuery to Ruby on Rails
[Ruby on Rails] How to use kaminari
[Ruby on Rails] Button to return to top
Memo for migration from java to kotlin
Ruby on Rails6 Practical Guide cp10 ~ cp12 [Memo]
Ruby on Rails6 Practical Guide cp16 ~ cp18 [Memo]
[Promotion of Ruby comprehension (1)] When switching from Java to Ruby, first understand the difference.
[heroku deployment procedure ③] From Ruby version specification to deployment and access to the application (complete)
Deploy to Ruby on Rails Elastic beanstalk (EB deploy)
[Beginner Procedure Manual 2] Ruby on Rails: Rails template creation
Ruby on Rails6 Practical Guide [Extensions] cp7 ~ cp9 [Memo]
[Ruby on Rails] How to display error messages
Ruby on Rails6 Practical Guide [Extensions] cp10 ~ cp12 [Memo]
Migration from Eclipse to IntelliJ (on the way)
Ruby on Rails6 Practical Guide [Extensions] cp3 ~ cp6 [Memo]
How to add / remove Ruby on Rails columns
When the Ruby on Rails terminal rolls back
Introducing New Relic to Rails apps on Heroku
How to save images on Heroku to S3 on AWS
[Personal memo] Ruby on Rails environment construction (Windows)
Note how to rollback Mysql deployed on Heroku
[Rails] Procedure for linking databases with Ruby On Rails
(Ruby on Rails6) Creating data in a table
Error deploying rails5 + Mysql to heroku with Docker-compose
[Ruby on Rails] How to install Bootstrap in Rails
[Ruby on Rails] How to use session method
[Ruby on Rails] If there are duplicate records when registering data, do not register
[Ruby on rails] When executing the heroku command, bash: heroku: command not found is displayed. [Rails tutorial]
[Rails AWS Docker] Build an existing Ruby on Rails + MySQL application with Docker and deploy it on AWS (6)
[Rails AWS Docker] Build an existing Ruby on Rails + MySQL application with Docker and deploy it on AWS (3)
[Rails AWS Docker] Build an existing Ruby on Rails + MySQL application with Docker and deploy it on AWS (2)
[Rails AWS Docker] Build an existing Ruby on Rails + MySQL application with Docker and deploy it on AWS (1)
[Ruby on Rails] How to stop when Rails server cannot be stopped by Ctrl + C
[Rails AWS Docker] Build an existing Ruby on Rails + MySQL application with Docker and deploy it on AWS (4)
[Ruby On Rails] De-root_path! Redirect notation from a nested "child" view to a nested "parent": show
MySQL migration memo (1)