Mysql2::Error::ConnectionError: Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)エラーの対処

AWSの本番環境で

rails db:migrate

を行った際に、発生したエラーです。

socket '/tmp/mysql.sock' が見つからないエラーのようです。

config/database.ymlで、本番環境のsocketの位置を確認します。

production:

# 中略

  socket: /var/lib/mysql/mysql.sock

エラー文の場所と違うことがわかりました。本番環境の設定を読み込んでいないようです。

原因は、本番環境を指定していなかったためで、以下の様にコマンドを修正すると、エラーが解消できました。

rails db:migrate RAILS_ENV=production

こちらも同様です。

rails db:migrate:status RAILS_ENV=production
rails db:seed RAILS_ENV=production