MySQL Remote Host Issues with Rails

I wanted to connect to a remote MySQL host (with rake db:create ), but Rails always considers it local. Database.yml that uses the following configuration:

 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 something in the database:

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

The configuration works as long as I use the local database (i.e. without the host / port part). Connecting to a remote MySQL server works fine with data.

Any ideas on what's going wrong?

Edit : The problem only arises with rake:db:create , other tasks work - the error message is really misleading.

+6
database mysql ruby-on-rails
source share
2 answers

You may need to enable the MySQL server to receive a remote request (by binding to the host IP address or the entire address format 0.0.0.0 ). Modify the MySQL my.cnf configuration file on the remote host. In Ubuntu you can find the file in /etc/mysql/my.cnf

Change the value of bind-address:

 bind-address = 0.0.0.0 
+1
source share

I found the same thing. I used this:

 RAILS_ENV=production bundle exec rails console 

And he complained that he could not find /tmp/mysql.sock (which is only in the database.yml development section). If I checked Rails.env from the console, this produced the production correctly. The fix is ​​as simple as:

 RACK_ENV=production bundle exec rails console 
0
source share

All Articles