デプロイ時に、MYSQLにdatabaseをcreateしたときに出たエラー #<Mysql2::Error::ConnectionError: Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)> Couldn't create database for {"adapter"=>"mysql2", "encoding"=>"utf8", "pool"=>5, "username"=>"sfc-b", "pass

今回のエラーは結構手強かった😅

 

デプロイ時に、AWSを使って、データベースを準備し、

ユニコーンでデプロイするときの流れのところで、

#<Mysql2::Error::ConnectionError: Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)>
Couldn't create database for {"adapter"=>"mysql2", "encoding"=>"utf8", "pool"=>5, "username"=>"sfc-b", "password"=>nil, "socket"=>"/tmp/mysql.sock", "database"=>"sfc-b_production"}, {:charset=>"utf8"}
(If you set the charset manually, make sure you have a matching collation)
Created database 'sfc-b_production'

 

というエラーがしつこく、、とてもしつこくでていた

 

database.ymlと照らし合わせてみると、

どうやら、本番環境が読み込めていないらしい。

 

 

# MySQL. Versions 5.0 and up are supported.
#
# Install the MySQL driver
# gem install mysql2
#
# Ensure the MySQL gem is defined in your Gemfile
# gem 'mysql2'
#
# And be sure to use new-style password hashing:
#
default: &default
adapter: mysql2
encoding: utf8
pool: 5
username: root
password:
socket: /tmp/mysql.sock

development:
<<: *default
database: sfc-b_development

# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
<<: *default
database: sfc-b_test

# As with config/secrets.yml, you never want to store sensitive information,
# like your database password, in your source code. If your source code is
# ever seen by anyone, they now have access to your database.
#
# Instead, provide the password as a unix environment variable when you boot
# for a full rundown on how to provide these environment variables in a
# production deployment.
#
# On Heroku and other platform providers, you may have a full connection URL
# available as an environment variable. For example:
#
# DATABASE_URL="mysql2://myuser:mypass@localhost/somedatabase"
#
# You can use this database configuration with:
#
# production:
# url: <%= ENV['DATABASE_URL'] %>
#
production:
<<: *default
database: sfc-b_production
username: root
password: <%= ENV['DATABASE_PASSWORD'] %>
socket: /var/lib/mysql/mysql.sock

 

どうしたら、ちゃんと読み込んでくれるのだろう。。。

GitHubにもしっかり

 

production:
<<: *default
database: sfc-b_production
username: root
password: <%= ENV['DATABASE_PASSWORD'] %>
socket: /var/lib/mysql/mysql.sock

 

この部分を追記したものをあげて、反映されているのに、

と悩んでいた。

もしかして、ローカルでいらんコードを書いていて、

それが邪魔しているのでは?

と思い、新しくnew_app

して、検証用のアプリも準備した。

しかし、その検証用のアプリでも

エラーになるのだった。。

 

ただ、これで、コードに問題はなく、

やはり、データベース側の読み込みがうまくいっていないという当初の

仮説に絞り込まれることになったのはよかった。

 

最後に行き着いたのは、

 

GitHubにあげたものを、キーを通して、ES2側にうまく渡っていないのでは」

 

という仮説にいきつき、GitHubから直接働きかけるのではなく、

「では直接、ローカルからデータベースに話しかける」

という結論が浮かんだ。

 

そのやり方は、

ターミナルから直接、コマンドをとおして伝えることだった。

 

そのやり方は

 

 

[ec2-user@ip-172-31-23-189 <リポジトリ名>] git pull origin master

 

これだ!

こうすることで、直接、EC2側に「database.ymlの記述変えたよ、反映してね」

と伝えることができるということらしいのだ。

 

このエラーは

 

「1ヶ月かかった」

 

という人もどうやら居た様なので、危機感を感じていたが、

なんとか丸一日で終戦してよかった☺️