使用phpseclib通过SSH连接到MySQL的PHP代码
问题描述:
I want connect to MySQL on server, but the server only can be access with SSH. I use phpseclib to connect with SSH. And it was successfull. But when try connect to MySQL it said : Access Denied. So, I have these code. Anyone please help me..
<?php
//data for connection to SSH
//host : 10.81.229.31
//user : root
//pass : abcde
//here's data for connection to MySQL
//host = 10.81.229.31
//username = root
//password = abcde
//database name = testing
include('Net/SSH2.php');
//connecting to server with SSH
$ssh = new Net_SSH2('10.81.229.31');
if (!$ssh->login('root', 'abcde')) {
exit('Login Failed');
}else{
echo "Login Success";
}
//connecting to MySQL
echo $ssh->exec('mysql -h 10.81.229.31 -u root -p testing');
?>
答
Try it without a space. eg.
echo $ssh->exec('mysql -h localhost -u root -ptesting');
Quoting from "man mysql":
--password[=password], -p[password]
答
When you are ssh'd inside of the box you should reference the database as localhost
echo $ssh->exec('mysql -h localhost -u root -ptesting');
答
<?php
//data for connection to SSH
//host : 10.81.229.31
//user : root
//pass : abcde
//here's data for connection to MySQL
//host = 10.81.229.31
//username = root
//password = abcde
//database name = testing
include('Net/SSH2.php');
//connecting to server with SSH
$ssh = new Net_SSH2('10.81.229.31');
if (!$ssh->login('root', 'abcde')) {
exit('Login to Server Failed');
}else{
echo "Login to Server Success";
}
//connecting to MySQL
if (!$ssh->exec('mysql -u root -ptesting
')){
exit('Login to MySQL Failed');
}else{
echo "Login to MySQL Success";
}
?>
is use for ENTER And the result :
Login to Server SuccessLogin to MySQL Success
Thankyou verymuch for everyone, who already help me..