tags:

views:

1511

answers:

4

Hi ,

i read one post regarding this topic, i also want to do the same,

i have my database on remote linux machine, and i want to connect using ssh and php funcitons,(i am currently using ssh2 library for that) i tried using mysql_connect, but it gives me cant access (although i have granted permission) when i tried using this function:

$connection = ssh2_connect('SERVER IP', 22);

ssh2_auth_password($connection, 'username', 'password');

$tunnel = ssh2_tunnel($connection, 'DESTINATION IP', 3307);

$db = mysqli_connect('127.0.0.1', 'DB_USERNAME', 'DB_PASSWORD', 
                         'dbname', 3307, $tunnel)
    or die ('Fail: '.mysql_error());

i got error"mysqli_connect() expects parameter 6 to be string, resource given", can anyone please help me on this

+2  A: 

According to the docs, that last parameter is supposed to be a socket or pipe name, something like '/var/run/mysql/mysql.sock'. Since you're not connecting using a UNIX socket, that doesn't apply to you... so try just leaving it out.

David Zaslavsky
+2  A: 

Some help is in this SO question.

Connect to a MySQL server over SSH in PHP

Also this article could help you.

Tunnelling MySQL over SSH

Ólafur Waage
A: 

i referred to abve question also, but it is not working for, please tell if there are some seetings or conditions which i have to check on my local machine.

Stuck up here for long time.....

A: 

Hey , even i tried it by doing ssh both by root credentials and and public private key pair, but it allows me to conect through command line but not through php code. I tried by creating tunnel also(by using ssh2 functions),ans running shell commands from php code(system,exec etc), nothing worked. Finally i tried ssh2 function to execute shell command and it finally worked :) Here is code, if it helps you:----

$connection = ssh2_connect($remotehost, '22'); if (ssh2_auth_password($connection, $user,$pass)) { echo "Authentication Successful!\n"; } else { die('Authentication Failed...'); }

$stream=ssh2_exec($connection,'echo "select * from zingaya.users where id=\"1606\";" | mysql'); stream_set_blocking($stream, true); while($line = fgets($stream)) { flush(); echo $line."\n"; }

it worked for me try this if want to use php functions specifically.