3
1

More than 3 years have passed since last update.

[デプロイエラー] Mysql2::Error: Table '〜' already exists: CREATE TABLE `〜`

Posted at

これは何

Railsでチーム開発中、Capistranoでの自動デプロイ時のエラー
「Mysql2::Error: Table '〜' already exists: CREATE TABLE 」がでた時の対処方法。
直訳すると、「既にテーブルがあるから無理だよ」ですね。

※未経験初心者が書いています。ここ違うよ!もっと良い方法あるよ!という事がありましたら、ご指摘いただけると幸いです。

対処方法

結論から言うと、データベースのスクラップアンドビルドです。あなた自身が想像と破壊の神シヴァになることだったのです。

真面目にやります。。。
まずはターミナルからSSHログインしてAWSに接続します。
ログイン後はmysqlに接続して中身を見てみましょう。

[ec2-user@]$ mysql -u root -p

# mysqlのパスワードです
Enter password:

mysqlに入ったら、本番環境のデータベースを見てみます。


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


mysql> show tables from sample_production;
+--------------------------------------------+
| Tables_in_sample_production                |
+--------------------------------------------+
| ar_internal_metadata                       |
| schema_migrations                          |
| user_addresses                             |
| users                                      |
+--------------------------------------------+

こちらをdropコマンドで削除します。

mysql> drop database sample_production;

mysql> exit

ディレクトリを移動し、データベースを作成します。

$ cd  /var/www/アプリ名/current

[ec2-user@ current]$ rails db:create RAILS_ENV=production;

# 必要であればこちらも
[ec2-user@ current]$ rails db:migrate RAILS_ENV=production;

あとは、
bundle exec cap production deploy
して完了です。

ありがとうございました!!

3
1
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
3
1