PHP MySql (1045) 拒绝用户访问

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21437773/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 04:09:09  来源:igfitidea点击:

PHP MySql (1045) Access Denied For User

phpmysql

提问by Baknik

I've tried to search for an existing answer to this problem, but the answers I find have not worked thus far.

我试图寻找这个问题的现有答案,但到目前为止我找到的答案还没有奏效。

I've been attempting to use PHP to connect to a MySql database. My web host uses cPanel on Linux. The code I'm using to do this seems standard enough:

我一直在尝试使用 PHP 连接到 MySql 数据库。我的网络主机在 Linux 上使用 cPanel。我用来执行此操作的代码似乎足够标准:

$mysqli = new mysqli("localhost", "cPanelUsername_dbUsername", "dbPassword", "cPanelUsername_dbName");

I've been getting the following error:

我一直收到以下错误:

Failed to connect to MySQL: (1045) Access denied for user 'cPanelUsername_dbUsername'@'localhost' (using password: YES)Access denied for user 'cPanelUsername'@'localhost' (using password: NO) 
  • "localhost"is the host server where the MySql server is located (it seems like this works)
  • "cPanelUsername"is my cpanel username
  • "dbUsername"is the database user, which I added to the database with all permissions granted
  • "dbPassword"is the database password for dbUsername
  • "dbName"is the database name
  • “localhost”是MySql服务器所在的主机服务器(好像是这样的)
  • “cPanelUsername”是我的 cPanel 用户名
  • “dbUsername”是数据库用户,我将其添加到数据库中并授予所有权限
  • “dbPassword”是 dbUsername 的数据库密码
  • “dbName”是数据库名称

I ended up adding my cPanel username before the dbName and dbUsername after searching for answers to this issue elsewhere.

在其他地方搜索此问题的答案后,我最终在 dbName 和 dbUsername 之前添加了我的 cPanel 用户名。

It looks like I have everything set up correctly but it's not connecting (with the error above). I don't have any direct control over the server that I wouldn't have to ask my web host about, which may take a few days to get sorted out. Do I have something wrong with my connection code?

看起来我已经正确设置了所有内容,但它没有连接(出现上述错误)。我对服务器没有任何直接控制权,我不必询问我的网络主机,这可能需要几天时间才能解决。我的连接代码有问题吗?

采纳答案by TrojanHorse

First check the database that you gave the proper user access to your database, which is given from Add User to databases from Mysql database section in cpanel.

首先检查您授予对数据库的正确用户访问权限的数据库,这是从将用户添加到来自 cpanel 中 Mysql 数据库部分的数据库中提供的。

after that check it again,

之后再次检查,

first try normal connection code in php,

首先在php中尝试正常的连接代码,

$con = mysql_connect("localhost","cpanel_username","cpanel_password");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

回答by elixenide

In cPanel, make sure that:

在 cPanel 中,确保:

  1. The database user cPanelUsername_dbNameexists, with the password dbPassword
  2. The database you want to use exists.
  3. The user cPanelUsername_dbNameis allowed to access the database.
  4. The user cPanelUsername_dbNameis allowed to access the database from localhost, 127.0.0.1, and the IP address of your server.
  1. 数据库用户cPanelUsername_dbName存在,有密码dbPassword
  2. 您要使用的数据库存在。
  3. cPanelUsername_dbName允许用户访问数据库。
  4. 用户cPanelUsername_dbName被允许从访问数据库localhost127.0.0.1和你的服务器的IP地址。

Your MySQL connections may use 127.0.0.1or the IP address of your server, and MySQL will reject the connection if access isn't granted for the specific IP address used.

您的 MySQL 连接可能会使用127.0.0.1您的服务器的 IP 地址,如果未授予所使用的特定 IP 地址的访问权限,MySQL 将拒绝该连接。

回答by Johny Sulliven

check the database name spelling at your phpMyAdmin. Usually the name is in format user_dbname.

检查数据库名称拼写在您的phpMyAdmin. 通常名称是 format user_dbname

For example:

例如:

cpanel username: Hyman,  
database created: student

In your phpscript, the dbnameshould be Hyman_student

在你的php脚本中,dbname应该是Hyman_student

回答by Jamezuh

This worked for me:

这对我有用:

  • Depending on what MySQL version you have, make sure you have matching password and hostname on your PHP file, config.inc.php file.
  • 根据您拥有的 MySQL 版本,请确保您的 PHP 文件 config.inc.php 文件中具有匹配的密码和主机名。

If you need to change the password for MySQL 5.7.6 and later:

如果您需要更改 MySQL 5.7.6 及更高版本的密码:

ALTER USER 'root'@'localhost' IDENTIFIED BY 'MyNewPass';

MySQL 5.7.5 and earlier:

MySQL 5.7.5 及更早版本:

SET PASSWORD FOR 'root'@'localhost' = PASSWORD('MyNewPass');
  • If you are trying to access your localhost server from a different machine i.e. (emulator, another computer), make sure you are using the actual IP address of the the localhost, DO NOT USE localhostas the hostname. Because this is like telling the machine to connect to itself - but the server is on a different IP address.
  • 如果您尝试从另一台机器(即模拟器、另一台计算机)访问您的 localhost 服务器,请确保您使用的是localhost的实际 IP 地址,不要使用localhost作为主机名。因为这就像告诉机器连接到自己 - 但是服务器位于不同的 IP 地址上。