更正Ruby on Rails Database.yml文件的MySQL配置

时间:2011-05-03 15:57:56

标签: mysql ruby-on-rails ruby yaml

我有这个配置:

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

我收到了这个错误:

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

有什么明显的东西我做错了吗?

6 个答案:

答案 0 :(得分:190)

您应该将主机与端口号分开。 你可以拥有一些东西,比如:

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

答案 1 :(得分:16)

您也可以这样做:

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

答案 2 :(得分:2)

如果你可以有一个空的config / database.yml文件然后定义ENV ['DATABASE_URL']变量,那么它会工作

$ cat config/database.yml
 
$ echo $DATABASE_URL
mysql://root:my_password@127.0.0.1:3306/my_db_name
Heroku的

: heroku config:设置DATABASE_URL='mysql://root:my_password@host.com/my_db_name'

答案 3 :(得分:1)

使用'utf8mb4'作为编码来覆盖所有unicode(包括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

答案 4 :(得分:0)

如果您有多个数据库用于测试和开发,这可能会有所帮助

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

答案 5 :(得分:0)