简体   繁体   English

当我尝试远程访问mysql时,拒绝用户'root'@'localhost'的访问(使用密码:是)

[英]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. 我正在构建一个android应用程序,并希望使用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: 这是连接到mysql数据库的php文件:

<?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. 即使在运行android应用程序时,它也会按预期更新表。

But when I try executing the above php script remotely on my server it says: 但是,当我尝试在服务器上远程执行上述php脚本时,它说:

"Access denied for user 'root'@'localhost' (using password: YES)" “拒绝访问用户'root'@'localhost'(使用密码:是)”

even though I can use mysql from home on a terminal. 即使我可以在终端上在家中使用mysql。 The server is running Ubuntu 16.04. 服务器正在运行Ubuntu 16.04。

Go to MySQL ® Databases , Create a similar name for the database and username. 转到MySQL®数据库,为数据库和用户名创建一个相似的名称。

like this : 像这样 :

Database : myhost_mydbname 数据库 :myhost_mydbname

Username : myhost_mydbname 用户名 :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. 如果您使用的应用程序将在其他设备上,则不能使用“ localhost”作为主机。 As localhost refers to the current host you're on which in this case would be your android device. 由于localhost指的是您当前所在的主机,在这种情况下,它将是您的android设备。

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. 如果您与ubuntu服务器位于同一网络上,或者该服务器具有可公开访问的IP,则可以提供其IP地址作为主机。 (Which you can figure out by running ifconfig on your ubuntu server). (您可以通过在ubuntu服务器上运行ifconfig找出答案)。

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'; 但是我怀疑您的root帐户是否可以在localhost以外的任何地方访问,因此我建议您通过运行CREATE USER 'newuser'@'%' IDENTIFIED BY 'password';创建一个新的用户帐户CREATE USER 'newuser'@'%' IDENTIFIED BY 'password';

And use those credentials to login. 并使用这些凭据登录。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用MAMP拒绝用户&#39;root&#39;@&#39;localhost&#39;的访问(使用密码:是) - Access denied for user 'root'@'localhost' (using password: YES) using MAMP 用户&#39;root&#39;@&#39;localhost&#39;的访问被拒绝(使用密码:是) - Access denied for user 'root'@'localhost' (using password: YES) #1045-用户&#39;root&#39;@&#39;localhost&#39;的访问被拒绝(使用密码:是) - #1045 - Access denied for user 'root'@'localhost' (using password: YES) laravel 5用户“ root” @“ localhost”的访问被拒绝(使用密码:是) - laravel 5 Access denied for user 'root'@' localhost' (using password: YES) 用户 &#39;root&#39;@&#39;localhost&#39; 的访问被拒绝(使用密码:YES)&#39; - Access denied for user 'root'@'localhost' (using password: YES)' 连接到mysql时出现问题-用户&#39;root&#39;@&#39;localhost&#39;的访问被拒绝(使用密码:是) - problem connecting to mysql - Access denied for user 'root'@'localhost' (using password: YES) 警告:mysql_connect():用户&#39;root&#39;@&#39;localhost&#39;拒绝访问(使用密码:YES) - Warning: mysql_connect(): Access denied for user 'root'@'localhost' (using password: YES) 如何修复用户&#39;root&#39;@&#39;localhost&#39;的MySql异常访问被拒绝(使用密码:是) - How to fix MySql Exception Access denied for user 'root'@'localhost' (using password: YES) 无法连接到 MySQL:用户 &#39;u201944136_root&#39;@&#39;localhost&#39; 的访问被拒绝(使用密码:是) - Failed to connect to MySQL: Access denied for user 'u201944136_root'@'localhost' (using password: YES) 无法连接到 MySQL:用户 'root'@'localhost' 的访问被拒绝(使用密码:是) - Failed to connect to MySQL: Access denied for user 'root'@'localhost' (using password: YES)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM