简体   繁体   English

使用Rails连接到远程MySQL主机的问题

[英]Problems connecting to remote MySQL host with Rails

I wanted to connect to a remote MySQL host (with rake db:create ), but Rails always considers it to be local. 我想连接到远程MySQL主机(使用rake db:create ),但Rails总是认为它是本地的。 Database.yml which uses the following config: Database.yml使用以下配置:

defaults: &defaults
  encoding: unicode
  adapter: mysql
  username: <username>
  password: *************
  port: 3306
  host: <remote ip address>

development:
  <<: *defaults
  database: <db name>
test: &test
  <<: *defaults
  database: <db name>
production:
  <<: *defaults
  database: <db name>

And always get this error when trying anything on the database: 在数据库上尝试任何操作时总是会出现此错误:

ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)

The config works as long as I use the local database (ie without the host/port part). 只要我使用本地数据库(即没有主机/端口部分),配置就可以工作。 Connecting to the remote MySQL server works fine with the given details. 连接到远程MySQL服务器与给定的细节工作正常。

Any ideas on what is going wrong ? 什么是错误的任何想法?

Edit : The problem only occurs with rake:db:create , other tasks work - The error message was really badly misleading. 编辑 :问题只发生在rake:db:create ,其他任务工作 - 错误消息非常误导。

You may need to enable MySQL server to accept remote request (by binding to host's ip address or all address format 0.0.0.0 ). 您可能需要启用MySQL服务器以接受远程请求(通过绑定到主机的IP地址或所有地址格式0.0.0.0 )。 Edit MySQL configuration file my.cnf on the remote host. 在远程主机上编辑MySQL配置文件my.cnf In Ubuntu, you can find the file in /etc/mysql/my.cnf 在Ubuntu中,您可以在/etc/mysql/my.cnf找到该文件

Change the value of bind-address: 更改bind-address的值:

bind-address            = 0.0.0.0

I found the same thing. 我发现了同样的事情。 I was using this: 我在用这个:

RAILS_ENV=production bundle exec rails console

And it was complaining about not being able to find /tmp/mysql.sock (which is only in the development section of database.yml ). 它抱怨无法找到/tmp/mysql.sock (仅在database.yml的开发部分)。 If I checked Rails.env from the console it correctly said production. 如果我从控制台检查Rails.env它正确地说生产。 The fix is as simple as: 修复很简单:

RACK_ENV=production bundle exec rails console

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM