Ssh – php unable to connect to MySQL via SSH tunnel

MySQLPHPsshssh-tunnel

I have executed both commands via SSH with no error and than when I try to connect to the MySQL server via SSH tunnel I get no results even if I do a simple query like select * from table.

I get no errors when I access the php script in live site.

I get this error: mysqli::mysqli(): (HY000/2002): Operation timed out when I access the php script hosted locally.

Both of them connect to the remote MySQL server of course. I have Apache and MySQL installed locally if that changes something.

Everything works fine when I connect via MySQL workbench.

I have also commented this line in my.cnf: #bind-address = 127.0.0.1

ssh -fNg -L 3307:127.0.0.1:3306 root@server_ip

mysql -h 127.0.0.1 -P 3307 -u root -p database_name

//php

$mysqli = new mysqli("127.0.0.1:3307", "root", "root", "database_name");

Best Answer

  1. mysqli constructor accepts 5th optional argumen specifying port. You should use

    $mysqli = new mysqli("127.0.0.1", "root", "root", "database_name", "3307");
    

instead.

  1. Did you verify you can connect to the port from the other host? Mysql is usually using unix domain socket instead of port (which you can forward too).

    ssh -fNg -L /var/run/mysqld/mysqld.sock:/var/run/mysqld/mysqld.sock root@server_ip