简体   繁体   中英

Access denied for user 'root'@'localhost' (using password: YES) when i try accessing mysql remotely

I am building an android application and want to store user credentials in a table using mysql. I have a server located elsewhere and have created the tables and configured everything as needed.

Here is the php file that connects to the mysql database:

<?php

$db_name="mydbname";
$mysql_user="root";
$mysql_pass="mypassword";
$server_name="localhost";

$con=mysqli_connect($server_name, $mysql_user, $mysql_user, $db_name);
if(!$con){
    echo "Connection error...".mysqli_connect_error();

}
else{
    echo "Successful";
}

?>

When I execute this locally on my system at home (with the necessary changes, of course) it works perfectly and returns 'Successful'. Even on running the android application it updates the tables as expected.

But when I try executing the above php script remotely on my server it says:

"Access denied for user 'root'@'localhost' (using password: YES)"

even though I can use mysql from home on a terminal. The server is running Ubuntu 16.04.

Go to MySQL ® Databases , Create a similar name for the database and username.

like this :

Database : myhost_mydbname

Username : myhost_mydbname

NextStep : Select ALL PRIVILEGES on the Add User To Database [Add Button] ! And then, Try this :

$db_name="myhost_mydbname";
$mysql_user="myhost_mydbname";
$mysql_pass="mypassword";
$server_name="localhost";

I hope this method will help you !

If you're on an app which would be on a different device you can't use "localhost" as your host. As localhost refers to the current host you're on which in this case would be your android device.

If you're on the same network as the ubuntu server or if it has a publicly accessible ip you can give its ip address as host. (Which you can figure out by running ifconfig on your ubuntu server).

But I doubt your root account will be accesible from anywhere else but localhost so I suggest you create a new user account by running CREATE USER 'newuser'@'%' IDENTIFIED BY 'password';

And use those credentials to login.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM