[RUBY] [Rails] How to reset the database in production environment (Capistrano version)



・ Develop applications using Rails -Implementing automatic deployment with Capistrano ・ We are building a server on AWS EC2 -Using MySQL with RDS


In the development environment, I always recreated the database with rails db: migrate: reset, but I implemented it from the question of how to do it in the production environment.

Reset the production database

First, use the terminal to go to the hierarchy of your application folder on EC2. Since it is implemented in Capistrano, basically work in the current directory so as not to make mistakes.


[[email protected] App name]cd current


RAILS_ENV=production DISABLE_DATABASE_ENVIRONMENT_CHECK=1 bundle exec rails db:drop

The database has now been cleared.

Create database again

This time, I will talk on the assumption that MySQL is installed in RDS. First of all, the terminal will continue to connect to mysql with the same directory.

mysql -u (master username) -p -h (endpoint) "-U" is the user name, "-p" is the password input, and "-h" is the option to indicate the connection destination information. Check the "Endpoint" in the RDS menu and enter it. For "Password", enter the password set in "Master password information".


mysql -u root -p -h rds-mysql-server.xxx.ap-northeast-1.rds.amazonaws.com

When you type this command, you will be prompted to enter the password, so when you enter it, the connection to mysql is completed successfully.

Recreate the database with the same name as the deleted application name.


mysql>create database application name;

Run migrate again in the production environment.


bundle exec rails db:migrate RAILS_ENV=production

This completes the database reset. After that, hit the capistrano command in the production environment to finish


bundle exec cap production deploy

Thank you for reading

Recommended Posts

[Rails] How to reset the database in production environment (Capistrano version)
[Rails] How to display information stored in the database in view
[Rails] How to delete production environment MySQL data after putting it in the development environment
How to reflect seeds.rb in production environment
[Rails] How to execute "rails db: create" etc. in production environment EC2
[Rails] How to use PostgreSQL in Vagrant environment
How to check Rails commands in the terminal
How to set the display time to Japan time in Rails
[Ruby On Rails] How to reset DB in Heroku
[Rails] How to display an image in the view
For those who want to use MySQL for the database in the environment construction of Rails6 ~.
[Rough explanation] How to separate the operation of the production environment and the development environment with Rails
Super easy in 2 steps! How to install devise! !! (rails 5 version)
[Rails] How to write in Japanese
Create a database in a production environment
How to introduce jQuery in Rails 6
How to install Swiper in Rails
[Rails] What to do if you accidentally install bundle in the production environment in your local environment
[Rails / Routing] How to refer to the controller in the directory you created
[Rails / Heroku / MySQL] How to reset the DB of Rails application on Heroku
How to install the language used in Ubuntu and how to build the environment
I want to display the images under assets/images in the production environment
How to write the view when Vue is introduced in Rails?
How to implement search functionality in Rails
How to use custom helpers in rails
How to insert a video in Rails
[Rails] How to use the map method
How to use MySQL in Rails tutorial
[rails] How to configure routing in resources
How to get the date in java
How to implement ranking functionality in Rails
How to use environment variables in RubyOnRails
How to use credentials.yml.enc introduced in Rails 5.2
Display the background image in the production environment
How to build Rails 6 environment with Docker
Gorigori beginners summarized how to operate the database using rails console Ntiunus
[Rails] How to get the user information currently logged in with devise
How to get the value after "_" in Windows batch like Java -version
How to get the setting value (property value) from the database in Spring Framework
How to debug the processing in the Ruby on Rails model only on the console
How to display the text entered in text_area in Rails with line breaks
[Rails] How to apply the CSS used in the main app with Administrate
[Rails] How to decide the destination by "rails routes"
How to check the logs in the Docker container
[Rails] I tried to raise the Rails version from 5.0 to 5.2
How to link Rails6 Vue (from environment construction)
[Rails] How to use select boxes in Ransack
Rails5.1 + puma SSL connection in local production environment
How to install Titan2D (v4.2.0) in virtual environment
How to translate Rails into Japanese in general
How to prevent direct URL typing in Rails
How to add sound in the app (swift)
How to delete the database when recreating the application
How to separate .scss by controller in Rails
How to conditionally add html.erb class in Rails
How to implement a like feature in Rails
How to easily create a pull-down in Rails
500 Internal Server Error occurs in Rails production environment
How to use JQuery in js.erb of Rails6
[Rails MySQL] How to reset DB on heroku
[Rails] Added in devise: username not added to database