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

5

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.

361

Answer

Solution:

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 !

744

Answer

Solution:

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 runningifconfig 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 runningCREATE USER 'newuser'@'%' IDENTIFIED BY 'password';

And use those credentials to login.

People are also looking for solutions to the problem: php get array values as new variable for ldap

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.