Correct MySQL configuration for Ruby on Rails Database.yml file

MysqlRuby on-RailsRubyYaml

Mysql Problem Overview


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?

Mysql Solutions


Solution 1 - Mysql

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 - Mysql

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 - Mysql

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 - Mysql

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'

Solution 5 - Mysql

If you have multiple databases for testing and development this might help

development:
  adapter: mysql2
  encoding: utf8
  reconnect: false
  database: DBNAME
  pool: 5
  username: usr
  password: paswd
  shost: localhost
test:
  adapter: mysql2
  encoding: utf8
  reconnect: false
  database: DBNAME
  pool: 5
  username: usr
  password: paswd
  shost: localhost
production:
  adapter: mysql2
  encoding: utf8
  reconnect: false
  database: DBNAME
  pool: 5
  username: usr
  password: paswd
  shost: localhost

Solution 6 - Mysql

None of these anwers worked for me, I found Werner Bihl's answer that fixed the problem.

https://stackoverflow.com/questions/19530089/getting-cant-connect-to-local-mysql-server-through-socket-var-run-mysqld-mys

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionGeekedOutView Question on Stackoverflow
Solution 1 - MysqlAmokrane ChentirView Answer on Stackoverflow
Solution 2 - MysqlpangpangView Answer on Stackoverflow
Solution 3 - MysqlbotibsView Answer on Stackoverflow
Solution 4 - Mysqluser3118220View Answer on Stackoverflow
Solution 5 - MysqlRickView Answer on Stackoverflow
Solution 6 - Mysqlt qView Answer on Stackoverflow