Ruby on Rails Database.yml 文件的正确 MySQL 配置
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5872264/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Correct MySQL configuration for Ruby on Rails Database.yml file
提问by GeekedOut
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?
有什么明显的我做错了吗?
回答by Amokrane Chentir
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
回答by pangpang
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
回答by botibs
Use 'utf8mb4' as encoding to cover all unicode (including emojis)
使用 'utf8mb4' 作为编码覆盖所有 unicode(包括表情符号)
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)
回答by user3118220
If you can have an empty config/database.yml file then define ENV['DATABASE_URL'] variable, then It will work
如果你可以有一个空的 config/database.yml 文件然后定义 ENV['DATABASE_URL'] 变量,那么它会起作用
$ 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'
对于 Heroku:heroku 配置:设置 DATABASE_URL='mysql://root:[email protected]/my_db_name'
回答by Rick
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
回答by t q
None of these anwers worked for me, I found Werner Bihl's answer that fixed the problem.
这些答案都不适合我,我发现Werner Bihl的答案解决了这个问题。
为 Ruby on Rails 应用程序设置 mysql 数据库时出现“无法通过套接字'/var/run/mysqld/mysqld.sock'连接到本地 MySQL 服务器”错误