Binding MySQL to local port over SSH - works in console, not via PHP shell_exec() in Mac OSX -


i running osx 10.6.7 , trying connect remote mysql server via ssh run php scripts. currently, run following commands no problem:

ssh -i /users/xxxx/key.pem user@data.server.com -l 53306:localhost:3306 -f sleep 60 >> logfile mysql -u user -p -h 127.0.0.1 -p 53306 

after authenticate password, works perfectly. (so long it's before sleep timeout, of course.)

when run php script, however...

$shell = shell_exec("ssh -i /users/xxxx/key1.pem user@data.server.com -l 53306:localhost:3306 -f sleep 60 >> logfile");  $mysqli = mysqli_connect('127.0.0.1', 'user', 'password', 'database', 53306); 

i connection refused. when omit shell_exec(), same error. when echo $shell, nothing outputted.

key1.pem has chmod 400 permission, copy of key.pem, , ran chown _www on it. same errors if try use key.pem or key1.pem. tried 440 permissions no luck.

thanks help; driving me nuts.

why need password if you're using private key? mean password key file? think problem because ssh accept authentication using passwords in tty , php *exec functions hasn't it.

for sleep question can try -n option of ssh, useful tunnels , doesn't require remote command executed. try establish tunnel using shell , connect tunnel php.

update:

i tried , work on mac

<? exec('ssh -f -n -n -l 5500:localhost:3306 your-host-name &> /dev/null'); var_dump(mysql_connect('127.0.0.1:5500', 'root')); 

you should add -i option , replace hostname.

this output on computer

resource(9) of type (mysql link) 

but keep in mind you'll have couple of problems this. &> /dev/null statement makes exec function return immediately, suppress output (mostly errors) when execute script multiple times, because every new request try open new connection, succeed cannot setup tunnel because port 5500 in use. after while you'll have lot of ssh process pending on machine.

to avoid should try connect mysql before ssh call , if can't setup tunnel , redo mysql connect call. in way can solve problem.


Comments

Popular posts from this blog

c# - SharpSVN - How to get the previous revision? -

c++ - Is it possible to compile a VST on linux? -

url - Querystring manipulation of email Address in PHP -