0

I'm able to connect to the remote database from a MySQL client and using terminal as well. But when trying to access the database from the Rails app, I got this error:

Mysql2::Error (Can't connect to MySQL server on 'myhost.com' (4))

My database.yml has the following configuration for development:

adapter: mysql2
encoding: utf8
reconnect: false
host: myhost.com
port: 3306
database: the_database
pool: 5
username: myusername
password: mypassword
socket: /var/lib/mysql/mysql.sock

I have changed the name of the host to post this question, as the username, password and database.

Googling around, I could see different reasons for not connecting to the database, but none of them are similar to mine and with this code 4 (is that an error code?).

I'm new to Rails, so I have no clue what should I do next in order to fix it. I have double checked my configuration, even the socket. I've personally checked on the server side and this is the right path to the socket.

I'm not sure if it's relevant, but this user has read only access to this database.

I'm using Rails 3.2.8 and Ruby 1.8.7.

rocir
  • 472
  • 1
  • 4
  • 9
  • You can't use unix socket communication except to a mysql server on the local machine. Try removing the `socket: /var/lib/mysql/mysql.sock` line. – Joachim Isaksson Jul 29 '13 at 05:04
  • Can you ping `myhost.com`? If you ssh into myhost.com and try connecting with the same database, username and password, does that work? – Vlad the Impala Jul 29 '13 at 06:14
  • @JoachimIsaksson, unfortunately it didn't solve the problem. – rocir Jul 31 '13 at 03:13
  • @VladtheImpala I can ping myhost.com. But when I try ssh, it gives timeout. But I don't think timeout is the problem that happens in the Rails app, since it gives the error instantaneously instead of waiting some time. What do you think? – rocir Jul 31 '13 at 03:21
  • did you ever resolve this? – sixty4bit Feb 02 '15 at 16:13
  • @sixty4bit Unfortunately, I haven't resolved this. – rocir Feb 10 '15 at 01:49

2 Answers2

2

I was having the same headache error (The error is "Can't connect to MySQL Server (4)) for about 3 weeks, finally i discovered that my problem was based on a firewall rule.

The solution was to ask to my hosting provider to open the port 3306 but in the OUT way.

I was using CPanel and the main point here was that i does not have any kind of control to manage the CPanel firewall.

Don´t try to expect as this is the big solution, because there are many things to be considered in configuration, but hey! i finally got it and it was all about the firewall of my provider.

Hope this help for someone.

Raul
  • 21
  • 2
0

You are using both a host which can be a remote host or the localhost but you also use socket which is a path to a UNIX Socket thats located in your servers filesystem. The socket is the prefered way to connect to your local database. If yo dont know the path tpo your mysql socket you can look it up in your mysql config file thats located in /etc/mysql/my.cnf in most cases.

If you want to connect to a remote host you should check if you can ping it and if you can you should try telneting the port like this:

telnet <hostname> 3306

Which will allow you to check if theres a deamon listening on the port your trying to connect to. If you cant connect to the server and you control the server there might be some firewall rules restricting access. Its definitfly not a mysql restricting problem because host restrictions causes other error messages.

davidb
  • 8,884
  • 4
  • 36
  • 72
  • Hello @davidb. The socket should be remove the socket line? I know the path to the socket in the MySQL server, which is a different path from my local machine. At the server it /var/lib/mysql/mysql.sock while locally it is /tmp/mysql.sock. But I think I should use the server side socket, right? – rocir Jul 31 '13 at 03:25
  • Also, I can ping my host and when I try telnet, it opens a strange message (Escape character is '^]'.) and then, in a different line, another strange thing (5.5.23-55?%!tHL#ijX??X0(w@vS|7 – rocir Jul 31 '13 at 03:28