Pergunta

I've got a web server with one host, and I'd like to use the database on another host. I'd like to use port forwarding to do this, and have already set up the forwarded port using

ssh -P -fg -L23307:myserver.net:3306 myname@myserver.net sleep 1d

This seems to be working properly (although if someone could tell me how to check, that would be great), but I can't get PHP to connect to MySQL through that port - it keeps trying to connect to its own local MySQL database (which isn't running).

$mlink = mysql_connect( "localhost:23307", "myusername", "mypassword" );
mysql_select_db( 'mydatabase', $mlink ) or die ( "Error - cannot connect to database localhost:23307.<br />Error: ".mysql_error() );  

As you can see, I'm not doing anything that complicated, so why does it keep trying to connect locally?

Foi útil?

Solução

So, turns out the answer was "don't trust people when they say they've opened the port on the firewall".

Anyone want a job?

Outras dicas

I faced the same issue. But there was no firewall problem involved. In fact, when you are doing SSH tunneling, you need not have to change any firewall setting.

I solved it by changing 'localhost' to '127.0.0.1' in the mysql_connect() parameter list. Refernce link - https://blog.rjmetrics.com/2009/01/06/php-mysql-and-ssh-tunneling-port-forwarding/

Excerpt -

Connecting via MySQL

It’s time to see all of our hard work pay off. From our local machine, we simply issue the following command:

$mysql -u sqluser -p -h 127.0.0.1 -P 3307

Notice that the MySQL host is 127.0.0.1, which is the same as the bind-address value on the remote server’s my.cnf file. It’s important that we use this value and not localhost, since we are ultimately accessing a forwarded TCP port, and specifying localhost causes MySQL to ignore TCP altogether and simply connect to the local server via a local socket. Accordingly, notice that we have specified port 3307 to make the connection; this is the TCP port we are forwarding.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top