1

Try to connect to remote mysql via ssh2 and php.

$ssh_server='';
$ssh_port='';
$ssh_user='';
$ssh_password='';

$ssh_connection=ssh2_connect($ssh_server, $ssh_port);
$ssh_auth=ssh2_auth_password($ssh_connection, $ssh_user, $ssh_password);
$ssh_tunnel = ssh2_tunnel($ssh_connection, $ssh_server, $ssh_port);

$db_hostname = 'localhost';
$db_database = '';
$db_username = '';
$db_password = '';
$db_port = '3306';

$connection_mysql = new mysqli($db_hostname, $db_username, $db_password, $db_database, $db_port);

Tunnel is ok, but I see, that mysql use my local mysql DB, not remote via SSH. So, how can i use mysql via SSH?

OS windows with WAMP on it.

3

2 Answers 2

1

create private/public key and setup key authentication with ssh(aka passwordless authentication).

then run this code in your php script

shell_exec("ssh -f -L 3307:localhost:3306 [email protected] sleep 10 >> logfile");

then create database connection like:

$pdo = new PDO("mysql:host=$servername;dbname=$dbname;port=3307", $username, $password);
Sign up to request clarification or add additional context in comments.

1 Comment

it works if you install cygwin and append cygwin's bin folder to your path variable.
-1
$ssh_connection = ssh2_connect($ssh_server, $ssh_port);
ssh2_auth_password($ssh_connection, $ssh_user, $ssh_password);
$query="SET CHARACTER SET utf8; use ${db_database}; ${query};";
$query=str_replace('"', '\'', stripslashes($query));
$ssh_query="ssh -L 3307:${ssh_server}:${ssh_port}; echo \"${query}\" | mysql -u ${db_username} -h ${db_hostname} --password=${db_password}";
$result=ssh2_exec($ssh_connection, $ssh_query);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.