96

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?

j0k
  • 22,600
  • 28
  • 79
  • 90
GeekedOut
  • 16,905
  • 37
  • 107
  • 185

6 Answers6

208

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
Amokrane Chentir
  • 29,907
  • 37
  • 114
  • 158
  • 4
    and host can't be `host: localhost` – Adrian C. Nov 27 '12 at 14:59
  • For security reasons, it's generally considered bad practice to use `root` as the production database user. To set up a dedicated user for your rails app, see the [MySQL docs about user creation](https://dev.mysql.com/doc/refman/5.7/en/adding-users.html). – Feliks Montez May 29 '17 at 21:13
  • 2
    Also for security reasons (if you're using version control) you should not store your database password in `database.yml`. Instead, do what user3118220 did and get it from your environment: `password: ENV['MY_RAILS_APP_DB_PASSWORD']`. – Feliks Montez May 29 '17 at 21:19
23

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
pangpang
  • 8,581
  • 11
  • 60
  • 96
4

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)

Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
botibs
  • 81
  • 3
2

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:my_password@127.0.0.1:3306/my_db_name

for Heroku: heroku config:set DATABASE_URL='mysql://root:my_password@host.com/my_db_name'

user3118220
  • 1,438
  • 12
  • 16
0

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
Rick
  • 12,606
  • 2
  • 43
  • 41
0

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

Getting "Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock'" error when setting up mysql database for Ruby on Rails app

Community
  • 1
  • 1
t q
  • 4,593
  • 8
  • 56
  • 91