28
27

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

[EC2]本番環境でDBの中身を削除する

Posted at

#はじめに
EC2サーバーにてrake db:seed RAILS_ENV=productionをしても前回seedさせたデータがテーブル内に残ってる為上手く反映されないのでテーブルの中身を削除する事にしました。

#本番環境のDBの操作
まずEC2サーバーにてmysqlサーバーを立ち上げる。

EC2サーバー
mysql -u root -p

DBの確認

mysql> show databases;
  Database                          
+-----------------------------------+
| Database                          |
+-----------------------------------+
| information_schema                |
| freemarket_sample_production      |
| mysql                             |
| performance_schema                |
+-----------------------------------+    

使用するDBの選択

mysql> use freemarket_sample_production;

テーブルの確認

mysql> show tables;
+---------------------------------------------+
| Tables_in_freemarket_sample_production      |  
+---------------------------------------------+
| ar_internal_metadata                        |
| brands                                      |
| categories                                  |
| credits                                     |
| items                                       |
| schema_migrations                           |
| sessions                                    |
| users                                       |
+---------------------------------------------+

テーブルの中身確認、今回はcategoriesテーブルの中身を削除します。

mysql> select * from categories;
+----+-----------------+---------------------+---------------------+----------+
| id | name            | created_at          | updated_at          | ancestry |
+----+-----------------+---------------------+---------------------+----------+
|  1 | レディース         | 2019-07-15 00:00:00 | 2019-07-15 00:00:00 | NULL     |
|  2 | メンズ            | 2019-07-15 00:00:00 | 2019-07-15 00:00:00 | NULL     |
|  3 | 本・CD            | 2019-07-15 00:00:00 | 2019-07-15 00:00:00 | NULL     |
+----+-----------------+---------------------+---------------------+----------+

後ほどseedコマンドでDBに追加をするので、テーブルの中身全てを削除。

mysql> delete from categories;

#まとめ
本番環境もローカルの時と同じ手順で操作出来ました。違うところといえばseedファイルを読み込むところぐらいでしょうか。これで無事本番環境でもseedファイルのデータを反映される事が出来たのでよかったです。
ですがいちいち操作をするのも手間になるかもしれませんので、seedファイルの修正などが終わってからseedコマンドをうつよう心掛けたいと思います。

28
27
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
28
27

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?