php - connect to the mysql database using phpseclib library -


i have connect vps using phpscelib library.now want connect existing database.please me ?

<?php set_include_path(get_include_path() . path_separator . 'phpseclib');  include('net/ssh2.php');   $ssh = new net_ssh2('192.ccc.ccc.ccc'); if (!$ssh->login('ccc', 'cccc')) { exit('login failed'); }  echo $ssh->exec("i need put mysql commands here"); ?> 

first, wouldn't better allow remote access user mysql? however, don't know reasons.

the common transparent way create ssh tunnel. can done in 2 different ways. if mysql port (3306) isn't open on mysql machine, you'll need reverse ssh tunnel has opened remote machine. log mysql machine , issue following command:

ssh -r 12345:localhost:3306 user@php_machine -n 

if mysql port open on remote machine tunnel can opened php machine:

ssh -f user@mysql_machine -l 12345:mysql_machine:3306 -n 

regardless of way tunnels has been created, php application can use pdo , connect localhost port 12345.

$pdo = new pdo('mysql:host=localhost;port=12345;dbname=test', $user, $password); 

all traffic crypted through tunnel.


if want issue couple of simple commands might use following alternative.

the simplest unsecure way use following command:

echo $ssh->exec('mysql -uuser -ppassword database -e "sql command"'); 

this insecure because other users on system see password.

you can workaround security issue using expect. expect program can pass password mysql in more secure way. make sure expect installed on remote system. here comes example using show tables command on database test:

include('net/ssh2.php');  $ssh = new net_ssh2('192.xxx.xxx.xxx'); if (!$ssh->login('ssh_user', 'ssh_password')) { exit('login failed'); }  echo $ssh->exec('expect <<eof # disable command output log_user 0 # start mysqldump process spawn mysql -uthe_user -p test -e "show tables" # wait password prompt expect "password:" # send password (mind \r) send "the_password\r" # enable output again log_user 1 # echo outout until end expect eof eof '); 

to further understand what's going, i've wrote blog article that.


Comments

Popular posts from this blog

assembly - 8086 TASM: Illegal Indexing Mode -

Java, LWJGL, OpenGL 1.1, decoding BufferedImage to Bytebuffer and binding to OpenGL across classes -

javascript - addthis share facebook and google+ url -