Home>
What i don't understand

I'm building a Rails app production environment on EC2.
unicornAt startupMySQLI get a connection error with, but I can't figure out the cause even after reviewing the settings.

Command executed
ec2-user @ ip ~ $bundle exec unicorn_rails -c config/unicorn.rb -E production -D
error
master failed to start, check stderr log for details
Log contents
Contents of /unicorn.stderr.log
I, [2020-09-14T06: 51: 56.022146 # 21711] INFO-: Refreshing Gem list
I, [2020-09-14T06: 51: 58.226567 # 21711] INFO-: listening on addr = 0.0.0.0:3000 fd = 9
E, [2020-09-14T06: 51: 58.336893 # 21711] ERROR-: Access denied for user'Username' @'localhost' (using password: YES) (Mysql2 :: Error :: ConnectionError)
・
・
・
ps -aef | grep mysql results
ec2-user 6564 6489 0 03:31 pts/1 00:00:00 grep --color = auto mysql
mysql 28647 1 0 September 14? 00:05:16/usr/sbin/mysqld
databese.yml
default:&default
  adapter: sqlite3
  pool:<% = ENV.fetch ("RAILS_MAX_THREADS") {5}%>
  timeout: 5000
development:
  adapter: mysql2
  encording: utf8
  database: app name_development
  pool: 5
  username: username
  password: Password (set locally)
  host: localhost
#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: test:
  adapter: mysql2
  encoding: utf8
  reconnect: false
  database: app name_test
  pool: 5
  username: username
  password: Password (set locally)
  host: localhost
production:
<<: * default
  database:<% = Rails.application.credentials.db [: database]%>
  username:<% = Rails.application.credentials.db [: username]%>
  password:<% = Rails.application.credentials.db [: password]%>
  socket:<% = Rails.application.credentials.db [: socket]%>
credentials.yml
db:
  database: app name
  username: root
  password: Password (set on the instance)
  socket: /var/lib/mysql/mysql.sock

master.keyI've reset it, but I don't know what other settings to review.

I would appreciate it if you could teach me just the files that should be checked.
Thanks for your cooperation.


Work environment

  • macOS Catalina
  • Amazon Linux2
  • EC2
  • MySQL
  • Ruby on Rails6
Postscript

With the suggestions of yu_1985database.ymlWas rewritten.

default:&default
  adapter: mysql2
  pool:<% = ENV.fetch ("RAILS_MAX_THREADS") {5}%>
  timeout: 5000
  encording: utf8
  database: app name
  pool: 5
  username: root
  password: password
  host: localhost


When I tried to start it in this state, I could see such an error in the log.
(master failed to start, check stderr log for detailsThere is no change in the appearance)

I, [2020-09-16T06: 46: 26.296957 # 8946] INFO-: Refreshing Gem list
bundler: failed to load command: unicorn_rails (/home/ec2-user/.rbenv/versions/2.6.6/bin/unicorn_rails)
NoMethodError: Cannot load database configuration:
undefined method `[]'for nil: NilClass
・
・
・

We are currently investigating this error.

If i know about this error, please teach me.

  • Answer # 1

    The environment construction was restarted from the beginning.
    Thank you for your reply.

  • Answer # 2

    I wrote a lot in the question, but when I looked at the connection settings, I somehow understood.

    -E productionI'm trying to start with, so I'm trying to connect to the DB by reading the settings in production.

    The production settings
    defaultIn addition to, it is written to use four pieces of information in credential.db.

    production:
    <<: * default
      database:<% = Rails.application.credentials.db [: database]%>
      username:<% = Rails.application.credentials.db [: username]%>
      password:<% = Rails.application.credentials.db [: password]%>
      socket:<% = Rails.application.credentials.db [: socket]%>

    on the other handdefaultIs

    default:&default
      adapter: sqlite3
      pool:<% = ENV.fetch ("RAILS_MAX_THREADS") {5}%>
      timeout: 5000

    So the adaptersqlite3It remains.
    I think that production also needs to have adapter set to mysql2 like other environments.
    In other environments, the specified encoding and host should also be included in the settings properly.