I've three servers A , B, and C, plus my localhost. Each server is inside a proper network, and server C should connect to a mysql DB in server A on port 3306 to perform some tests.
The scenario is as follows:
- In server C I run
PHP
scripts to open amysql connection
to B (which is not a mysql-server) - I can ssh from localhost to server B
- I can ssh from localhost to server A
Server A has a mysql server (obviously), but server B hasn't a mysql server.
I've tried the following method:
Map remote B:3306 to localhost:3337 , using:
ssh -R 3306:localhost:3337 b-user@B-server
Map localhost:3337 to A:3306 , using:
ssh -L 3337:localhost:3306 a-user@A-server`
Test php script to open connection to B:
$host= '<B-server>'; $pwd = '<mysql-pwd>'; $user= '<mysql-user>'; $db = "my_test_db"; try { $pdo = new PDO("mysql:host={$host};dbname={$db};port=3306", $user, $pwd); } catch (PDOException $e) { print "Error!: " . $e->getMessage() . "<br/>"; die(); }
When I run the above php script, I've get the error:
Error!: SQLSTATE[HY000] [2002] Connection refused
There's something wrong in port usages?
UPDATE
In server B I've installed mysql-client
, and then testing connection to server A goes well:
mysql -u <sql-user> -p<sql-pass> --host 127.0.0.1 -P 3306
Indeed communication between A and B via localhost is working.