更正Ruby on Rails Database.yml文件的MySQLconfiguration

我有这样的configuration:

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) 

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

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

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

你也可以这样做:

 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 

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

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

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

如果你有多个数据库进行testing和开发,这可能会有所帮助

 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 

没有一个这样的水pipe为我工作,我发现维尔纳比尔的答案,解决了这个问题。

在Ruby on Rails应用程序中设置mysql数据库时出现“无法通过套接字连接到本地MySQL服务器”/var/run/mysqld/mysqld.sock“”的错误