Question

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');

?>
Was it helpful?

Solution 2

Try it without a space. eg.

echo $ssh->exec('mysql -h localhost -u root -ptesting');

Quoting from "man mysql":

--password[=password], -p[password]

OTHER TIPS

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 \r\n')){
    exit('Login to MySQL Failed');
}else{
    echo "Login to MySQL Success";
}

?>

\r\n is use for ENTER And the result :

Login to Server SuccessLogin to MySQL Success

Thankyou verymuch for everyone, who already help me..

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top