Correct MySQL configuration for Ruby on Rails Database.yml file

前端 未结 6 563
终归单人心
终归单人心 2020-12-02 08:03

I have this configuration:

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


        
相关标签:
6条回答
  • 2020-12-02 08:40

    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

    0 讨论(0)
  • 2020-12-02 08:42

    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
    
    0 讨论(0)
  • 2020-12-02 08:45

    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)

    0 讨论(0)
  • 2020-12-02 08:54

    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
    
    0 讨论(0)
  • 2020-12-02 08:55

    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'

    0 讨论(0)
  • 2020-12-02 09:00

    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
    
    0 讨论(0)
提交回复
热议问题