[Rails] How to delete production environment MySQL data after putting it in the development environment

Introduction

When I put MySQL data into the development environment in the production environment, I was able to input it without any error, but when I erased it, an error occurred and I was addicted to it. It may be a little unusual case, but I decided to summarize it as a reference for other people.

Related Links

Please refer to the following for an article on how to put MySQL data in the production environment into the development environment in the first place.

error

When I executed the command to delete the DB itself in the development environment, ...

$ rails db:drop

I got the error ActiveRecord :: ProtectedEnvironmentError. .. ..

The meaning of this error is

I'm trying to operate a DB in a production environment, is that okay?

It's like a warning, you can continue the operation by entering an environment variable. I'm saying.

It's a development environment in the first place, but why does it come out as a production environment? ??

rails aborted!
ActiveRecord::ProtectedEnvironmentError: You are attempting to run a destructive action against your 'production' database.
If you are sure you want to continue, run the same command with the environment variable:
DISABLE_DATABASE_ENVIRONMENT_CHECK=1
bin/rails:4:in `<main>'
Tasks: TOP => db:migrate:reset => db:drop => db:check_protected_environments
(See full trace by running task with --trace)

Conclusion

I will write from the conclusion first,

In Rails5, environment variables are stored in MySQL, and by putting the data of the production environment into the development environment as it is, the variables of the production environment are stored in the environment variables of MySQL, so the above error occurred. It was.

So, the solution is to rewrite the MySQL environment variables from those in the production environment to those in the development environment.

Solution procedure

Connect to MySQL.

$ mysql -h db -u root -p

Check the existing database.

$ show databases;

Specify the target database.

$ use *******;

Display a list of tables in the database.

$ show tables;

I think there is ar_internal_metadata in the table list. Environment variables are stored in this.

+---------------------------------------------------+
| Tables_in_scm_development                         |
+---------------------------------------------------+
| ar_internal_metadata                              |
| ...                                               |
+---------------------------------------------------+

Looking at the contents of ar_internal_metadata,

$ select * from ar_internal_metadata;

I found the letters `` `production```.

+-------------+-------------+---------------------+---------------------+
| key         | value       | created_at          | updated_at          |
+-------------+-------------+---------------------+---------------------+
| environment | production  | 2020-01-10 09:37:29 | 2020-01-10 09:37:29 |
+-------------+-------------+---------------------+---------------------+

Modify this `production` to `` `development```.

$ update ar_internal_metadata set value='development'

Looking at the contents of ar_internal_metadata again,

$ select * from ar_internal_metadata;

The environment variable could be changed to `` `development```.

+-------------+-------------+---------------------+---------------------+
| key         | value       | created_at          | updated_at          |
+-------------+-------------+---------------------+---------------------+
| environment | development | 2020-01-10 09:37:29 | 2020-01-10 09:37:29 |
+-------------+-------------+---------------------+---------------------+

Get out of MySQL

$ exit

Execute the command to delete the DB again. Then I succeeded!

$ rails db:drop

Summary

It may be difficult for some people to understand because I wrote the procedure in a hurry, but I hope it will be useful for someone in need.

reference

Recommended Posts

[Rails] How to delete production environment MySQL data after putting it in the development environment
[Rails] How to reset the database in production environment (Capistrano version)
How to install Docker in the local environment of an existing Rails application [Rails 6 / MySQL 8]
[Rough explanation] How to separate the operation of the production environment and the development environment with Rails
[Rails] How to execute "rails db: create" etc. in production environment EC2
How to install Pry after building Rails development environment with Docker
How to delete large amounts of data in Rails and concerns
How to reflect seeds.rb in production environment
How to use MySQL in Rails tutorial
[Rails] Reset the database in the production environment
Rails6.0 ~ How to create an eco-friendly development environment
[Rails] How to use PostgreSQL in Vagrant environment
How to check Rails commands in the terminal
Introduce Docker to the development environment and test environment of existing Rails and MySQL applications
How to make a unique combination of data in the rails intermediate table
How to set the display time to Japan time in Rails
I get Mysql2 :: Error :: ConnectionError in the production environment
[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 ~.
How to check the WEB application created in the PC development environment on your smartphone
How to build Rails, Postgres, ElasticSearch development environment with Docker
[Rails] How to display information stored in the database in view
[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
[Docker] How to back up and restore the DB data of Rails application on docker-compose [MySQL]
[Rails] How to write in Japanese
[Programming beginner] What to do when rails s becomes an error in the local development environment
[Rails] Various ways to delete data
[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
After learning Progate, I tried to make an SNS application using Rails in the local environment
I want to display the images under assets/images in the production environment
How to introduce jQuery in Rails 6
When log data accumulates in Rails and the environment stops working
Install Rails in the development environment and create a new application
How to build Rails + Vue + MySQL environment with Docker [2020/09 latest version]
How to add the delete function
How to write the view when Vue is introduced in Rails?
How to install Swiper in Rails
How to execute with commands of normal development language in Docker development environment
[Rails] About the error that the image is not displayed in the production environment
How to change the maximum and maximum number of POST data in Spark
How to build a Ruby on Rails development environment with Docker (Rails 6.x)
[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 solve the local environment construction of Ruby on Rails (MAC)!
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
How to build a Ruby on Rails development environment with Docker (Rails 5.x)
Android development, how to check null in the value of JSON object
[Rails] How to apply the CSS used in the main app with Administrate
[Rails] How to solve the time lag of created_at after save method
How to implement search functionality in Rails
How to change app name in rails
How to get date data in Ruby
How to use custom helpers in rails
How to delete the wrong migration file
How to insert a video in Rails
How to delete the migration file NO FILE
[Rails] How to use the map method
[Docker] Development environment construction Rails6 / Ruby2.7 / MySQL8