Correct MySQL configuration for Ruby on Rails Database.yml file

124,049

Solution 1

You should separate the host from the port number. You could have something, like:

development:
  adapter: mysql2
  encoding: utf8
  database: my_db_name
  username: root
  password: my_password
  host: 127.0.0.1
  port: 3306

Solution 2

You also can do like this:

default: &default
  adapter: mysql2
  encoding: utf8
  username: root
  password:
  host: 127.0.0.1
  port: 3306

development:
  <<: *default
  database: development_db_name

test:
  <<: *default
  database: test_db_name

production:
  <<: *default
  database: production_db_name

Solution 3

Use 'utf8mb4' as encoding to cover all unicode (including emojis)

default: &default
  adapter: mysql2
  encoding: utf8mb4
  collation: utf8mb4_bin
  username: <%= ENV.fetch("MYSQL_USERNAME") %>
  password: <%= ENV.fetch("MYSQL_PASSWORD") %>
  host:     <%= ENV.fetch("MYSQL_HOST") %>

(Reference1) (Reference2)

Solution 4

If you can have an empty config/database.yml file then define ENV['DATABASE_URL'] variable, then It will work

$ cat config/database.yml
 
$ echo $DATABASE_URL
mysql://root:[email protected]:3306/my_db_name

for Heroku: heroku config:set DATABASE_URL='mysql://root:[email protected]/my_db_name'

Share:
124,049
GeekedOut
Author by

GeekedOut

Recently started working on http://www.problemio.com Thank you to all the people on StackOverflow who have helped me. Could not have made the progress I did without your help!! EDIT: I LOOOOOOOOOOVE STACKOVERFLOW. THANK YOU TO ALL THE AMAZING PEOPLE HERE!!!!

Updated on March 28, 2020

Comments

  • GeekedOut
    GeekedOut about 4 years

    I have this configuration:

    development:
      adapter: mysql2
      encoding: utf8
      database: my_db_name
      username: root
      password: my_password
      host: mysql://127.0.0.1:3306
    

    And I am getting this error:

    Unknown MySQL server host 'mysql://127.0.0.1:3306' (1)
    

    Is there something obvious that I am doing incorrectly?