警告:mysqli_connect(): (HY000/1045):用户'username'@'localhost'被拒绝访问(使用密码:YES)

时间:2022-09-19 21:12:52

Warning: mysqli_connect(): (HY000/1045): Access denied for user 'username'@'localhost' (using password: YES) in C:\Users\xampp\htdocs\PHP_Login_Script\config.php on line 6

警告:mysqli_connect():(HY000/1045):拒绝访问的用户的用户名@“localhost”(使用密码:是的)C:\Users\xampp\htdocs\ PHP_Login_Script \配置。php在第6行

Warning: mysqli_real_escape_string() expects parameter 1 to be mysqli, boolean given in C:\Users\xampp\htdocs\PHP_Login_Script\login.php on line 10

警告:mysqli_real_escape_string mysqli()预计参数1,布尔在C:\Users\xampp\htdocs\ PHP_Login_Script \登录。php的地铁十号线

Warning: mysqli_real_escape_string() expects parameter 1 to be mysqli, boolean given in C:\Users\xampp\htdocs\PHP_Login_Script\login.php on line 11

警告:mysqli_real_escape_string mysqli()预计参数1,布尔在C:\Users\xampp\htdocs\ PHP_Login_Script \登录。php在第11行

Warning: mysqli_query() expects parameter 1 to be mysqli, boolean given in C:\Users\xampp\htdocs\PHP_Login_Script\login.php on line 15

警告:mysqli_query mysqli()预计参数1,布尔在C:\Users\xampp\htdocs\ PHP_Login_Script \登录。php在第15行

Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, null given in C:\Users\xampp\htdocs\PHP_Login_Script\login.php on line 16

警告:mysqli_fetch_array mysqli_result()预计参数1,给出零C:\Users\xampp\htdocs\ PHP_Login_Script \登录。php在16行

Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, null given in C:\Users\xampp\htdocs\PHP_Login_Script\login.php on line 19

警告:mysqli_num_rows mysqli_result()预计参数1,给出零C:\Users\xampp\htdocs\ PHP_Login_Script \登录。php在19行

I'm getting this error above on localhost even if my config file is like this:

即使我的配置文件是这样的,我也会在localhost上得到这个错误:

<?php

    define("DB_HOST", "localhost");
    define("DB_USER", "root");
    define("DB_PASSWORD", "");
    define("DB_DATABASE", "databasename");

    $db = mysqli_connect(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_DATABASE);

?>

This used to work, but now it doesn't anymore. Is there any problem with this code or is it not working now? This is the tutorial I am working on right now.

这曾经起作用,但现在不再起作用了。这段代码有什么问题吗?这是我现在正在学习的教程。

16 个解决方案

#1


10  

That combination of username, host, and password is not allowed to connect to the server. Verify the permission tables (reloading grants if required) on the server and that you're connecting to the correct server.

用户名、主机和密码的组合不允许连接到服务器。验证服务器上的权限表(如果需要,重新加载授予),并确认您正在连接到正确的服务器。

#2


11  

If youre running wamp update

如果您正在运行wamp更新

define("DB_HOST", "localhost");

To your machines ip address (mine is 192.168.0.25);

到您的机器的ip地址(我的是192.168.0.25);

define("DB_HOST", "192.168.0.25");

You can find it on window by typing ipconfig in your console or ifconfig on mac/linux

通过在控制台输入ipconfig或在mac/linux上输入ifconfig,您可以在窗口中找到它

#3


6  

mysqli_connect(DB_SERVER,DB_USERNAME,DB_PASSWORD,DB_DATABASE);

mysqli_connect(DB_SERVER DB_USERNAME、DB_PASSWORD DB_DATABASE);

use DB_HOST instead of DB_SERVER

使用DB_HOST而不是DB_SERVER

#4


5  

Look at your code You defined DB_HOST and querying DB_SERVER. DB_USER and DB_USERNAME Use this code

查看您定义的DB_HOST和查询DB_SERVER的代码。DB_USER和DB_USERNAME使用此代码

define("DB_SERVER", "localhost");
define("DB_USER", "root");
define("DB_PASSWORD", "");
define("DB_DATABASE", "databasename");

$connect = mysqli_connect(DB_SERVER , DB_USER, DB_PASSWORD, DB_DATABASE);

#5


3  

you defIne as DB_USER but use as DB_USERNAME. Also php says username@localhost cant access. Not root@localhost.

定义为DB_USER,但使用为DB_USERNAME。php还说username@localhost不能访问。不是root@localhost。

Change your define or connect paramater.

更改定义或连接参数。

#6


3  

Make sure that your password doesn't have special characters and just keep a plain password (for ex: 12345), it will work. This is the strangest thing that I have ever seen. I spent about 2 hours to figure this out.

确保你的密码没有特殊字符,只保留一个普通的密码(例如:12345)就可以了。这是我所见过的最奇怪的事情。我花了2个小时才算出来。

Note: 12345 mentioned below is your plain password

注意:下面提到的12345是您的普通密码

GRANT ALL PRIVILEGES ON dbname.* TO 'yourusername'@'%' IDENTIFIED BY '12345';

#7


1  

This is your code:

这是你的代码:

<?php

    define("DB_HOST", "localhost");
    define("DB_USER", "root");
    define("DB_PASSWORD", "");
    define("DB_DATABASE", "databasename");

    $db = mysqli_connect(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_DATABASE);

?>

The only error that causes this message is that:

导致这条消息的唯一错误是:

  1. you're defining a DB_USER but you're calling after as DB_USERNAME.
  2. 您正在定义一个DB_USER,但是您在后面调用的是DB_USERNAME。

Please be more careful next time.

下次请多加小心。

It is better for an entry-level programmer that wants to start coding in PHP not to use what he or she does not know very well.

对于希望开始用PHP编写代码的初级程序员来说,最好不要使用他或她不太了解的内容。

ONLY as advice, please try to use (for the first time) code more ubiquitous.

仅作为建议,请尝试使用(第一次)更普遍的代码。

ex: do not use the define() statement, try to use variables declaration as $db_user = 'root';

例:不要使用define()语句,尽量使用变量声明作为$db_user = 'root';

Have a nice experience :)

有一个美好的经历:)

#8


1  

The same issue faced me with XAMPP 7 and opencart Arabic 3. Replacing localhost by the actual machine name reslove the issue.

同样的问题也出现在XAMPP 7和opencart阿拉伯语3中。将localhost替换为实际的机器名reslove问题。

#9


0  

There is a typo error in define arguments, change DB_HOST into DB_SERVER and DB_USER into DB_USERNAME:

在定义参数时出现了一个typo错误,将DB_HOST转换为DB_SERVER和DB_USER到DB_USERNAME:

<?php

    define("DB_SERVER", "localhost");
    define("DB_USERNAME", "root");
    define("DB_PASSWORD", "");
    define("DB_DATABASE", "databasename");
    $db = mysqli_connect(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_DATABASE);

?>

#10


0  

Same problem occurd with me, with AWS RDS MySQL. Just now, answered similar question here. Looked various sources, but as of this thread, almost all lack of answer. While this thread helped me, tweak in mind to update hostnames at server. Access your SSH and follow the steps:

同样的问题也发生在我身上,AWS RDS MySQL。刚才,我也回答了类似的问题。看了各种来源,但就这条线而言,几乎都没有答案。虽然这个线程对我有帮助,但是请在服务器上修改主机名。访问您的SSH并遵循以下步骤:

cd /etc/
sudo nano hosts

Now, Appending here your hostnames: For example:

现在,在这里加上您的主机名:例如:

127.0.0.1 localhost
127.0.0.1 subdomain.domain.com [if cname redirected to endpoints]
127.0.0.1 xxxxxxxx.xxxx.xxxxx.rds.amazonaws.com [Endpoints]

and now, configuring config/database.php as follows:

现在,配置配置/数据库。php如下:

$active_group = 'default';
$query_builder = TRUE;

$db['default'] = array(
    'dsn'   => '',
    'hostname' => '35.150.12.345',
    'username' => 'user-name-here',
    'password' => 'password-here',
    'database' => 'database-name-here',
    'dbdriver' => 'mysqli',
    'dbprefix' => '',
    'pconnect' => FALSE,
    'db_debug' => TRUE,
    'cache_on' => FALSE,
    'cachedir' => '',
    'char_set' => 'utf8',
    'dbcollat' => 'utf8_general_ci',
    'swap_pre' => '',
    'encrypt' => FALSE,
    'compress' => FALSE,
    'stricton' => FALSE,
    'failover' => array(),
    'save_queries' => TRUE
);

where 35.150.12.345 is your IPv4 Public IP, located at ec2-dashboard > Network Interfaces > Description : RDSNetworkInterface >> IPv4 Public IP('35.150.12.345') there.

其中35.150.12.345为IPv4公共IP,位于ec -dashboard >网络接口>描述:RDSNetworkInterface >> > IPv4公共IP('35.150.12.345')。

Note: Please note that only IPV4 will work at 'hostname' option. hostname, cname, domains will output database connection error.

注意:请注意,只有IPV4可以使用“hostname”选项。主机名、cname、域将输出数据库连接错误。

#11


0  

The question has been answered above, Just to make you feel comfortable using all four string at the same time in a connection

上面已经回答了这个问题,只是为了让您在连接中同时使用这四个字符串时感到舒服

<?php
  define("DB_HOST", "localhost");
  define("DB_USER", "root");
  define("DB_PASSWORD", "");
  define("DB_DATABASE", "databasename");

  $db = new mysqli(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_DATABASE);
  // you could test connection eventually using a if and else conditional statement, 
  // feel free to take out the code below once you see Connected!
  if ($db) {
    echo "Connected!";
  } else {
    echo "Connection Failed";
  }
?>

#12


0  

I had this error trying to connect with a different user than root I'd set up to limit access.

我在尝试连接一个与我设置为限制访问的root用户不同的用户时出现了这个错误。

In mysql I'd set up user MofX with host % and it wouldn't connect.

在mysql中,我设置用户MofX与主机%,它不会连接。

When I changed the user's host to 127.0.0.1, as in 'MofX'@'127.0.0.1', it worked.

当我将用户的主机更改为127.0.0.1时,就像在“MofX”@“127.0.0.1”中那样,它成功了。

#13


-1  

try

试一试

define("DB_PASSWORD", null);

and those are warnings try

这些都是警告

$db = @mysqli_connect(DB_SERVER,DB_USERNAME,DB_PASSWORD,DB_DATABASE);

But i will recommend you to set a root password

但是我建议您设置一个根密码

#14


-1  

Do you have two connections at the same time? If so, close every time you don't need it.

你同时有两个接头吗?如果是的话,每次不需要的时候就关门。

#15


-1  

$db = mysqli_connect(DB_SERVER,DB_USERNAME,DB_PASSWORD,DB_DATABASE);

Put " DB_HOST " instead of " DB_SERVER " also 
put " DB_USER " Instead of " DB_USERNAME " 

than it working fine !

好极了!

#16


-2  

Try changing:

试着改变:

define("DB_SERVER", "localhost");

to this:

:

define("DB_SERVER", "127.0.0.1");

#1


10  

That combination of username, host, and password is not allowed to connect to the server. Verify the permission tables (reloading grants if required) on the server and that you're connecting to the correct server.

用户名、主机和密码的组合不允许连接到服务器。验证服务器上的权限表(如果需要,重新加载授予),并确认您正在连接到正确的服务器。

#2


11  

If youre running wamp update

如果您正在运行wamp更新

define("DB_HOST", "localhost");

To your machines ip address (mine is 192.168.0.25);

到您的机器的ip地址(我的是192.168.0.25);

define("DB_HOST", "192.168.0.25");

You can find it on window by typing ipconfig in your console or ifconfig on mac/linux

通过在控制台输入ipconfig或在mac/linux上输入ifconfig,您可以在窗口中找到它

#3


6  

mysqli_connect(DB_SERVER,DB_USERNAME,DB_PASSWORD,DB_DATABASE);

mysqli_connect(DB_SERVER DB_USERNAME、DB_PASSWORD DB_DATABASE);

use DB_HOST instead of DB_SERVER

使用DB_HOST而不是DB_SERVER

#4


5  

Look at your code You defined DB_HOST and querying DB_SERVER. DB_USER and DB_USERNAME Use this code

查看您定义的DB_HOST和查询DB_SERVER的代码。DB_USER和DB_USERNAME使用此代码

define("DB_SERVER", "localhost");
define("DB_USER", "root");
define("DB_PASSWORD", "");
define("DB_DATABASE", "databasename");

$connect = mysqli_connect(DB_SERVER , DB_USER, DB_PASSWORD, DB_DATABASE);

#5


3  

you defIne as DB_USER but use as DB_USERNAME. Also php says username@localhost cant access. Not root@localhost.

定义为DB_USER,但使用为DB_USERNAME。php还说username@localhost不能访问。不是root@localhost。

Change your define or connect paramater.

更改定义或连接参数。

#6


3  

Make sure that your password doesn't have special characters and just keep a plain password (for ex: 12345), it will work. This is the strangest thing that I have ever seen. I spent about 2 hours to figure this out.

确保你的密码没有特殊字符,只保留一个普通的密码(例如:12345)就可以了。这是我所见过的最奇怪的事情。我花了2个小时才算出来。

Note: 12345 mentioned below is your plain password

注意:下面提到的12345是您的普通密码

GRANT ALL PRIVILEGES ON dbname.* TO 'yourusername'@'%' IDENTIFIED BY '12345';

#7


1  

This is your code:

这是你的代码:

<?php

    define("DB_HOST", "localhost");
    define("DB_USER", "root");
    define("DB_PASSWORD", "");
    define("DB_DATABASE", "databasename");

    $db = mysqli_connect(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_DATABASE);

?>

The only error that causes this message is that:

导致这条消息的唯一错误是:

  1. you're defining a DB_USER but you're calling after as DB_USERNAME.
  2. 您正在定义一个DB_USER,但是您在后面调用的是DB_USERNAME。

Please be more careful next time.

下次请多加小心。

It is better for an entry-level programmer that wants to start coding in PHP not to use what he or she does not know very well.

对于希望开始用PHP编写代码的初级程序员来说,最好不要使用他或她不太了解的内容。

ONLY as advice, please try to use (for the first time) code more ubiquitous.

仅作为建议,请尝试使用(第一次)更普遍的代码。

ex: do not use the define() statement, try to use variables declaration as $db_user = 'root';

例:不要使用define()语句,尽量使用变量声明作为$db_user = 'root';

Have a nice experience :)

有一个美好的经历:)

#8


1  

The same issue faced me with XAMPP 7 and opencart Arabic 3. Replacing localhost by the actual machine name reslove the issue.

同样的问题也出现在XAMPP 7和opencart阿拉伯语3中。将localhost替换为实际的机器名reslove问题。

#9


0  

There is a typo error in define arguments, change DB_HOST into DB_SERVER and DB_USER into DB_USERNAME:

在定义参数时出现了一个typo错误,将DB_HOST转换为DB_SERVER和DB_USER到DB_USERNAME:

<?php

    define("DB_SERVER", "localhost");
    define("DB_USERNAME", "root");
    define("DB_PASSWORD", "");
    define("DB_DATABASE", "databasename");
    $db = mysqli_connect(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_DATABASE);

?>

#10


0  

Same problem occurd with me, with AWS RDS MySQL. Just now, answered similar question here. Looked various sources, but as of this thread, almost all lack of answer. While this thread helped me, tweak in mind to update hostnames at server. Access your SSH and follow the steps:

同样的问题也发生在我身上,AWS RDS MySQL。刚才,我也回答了类似的问题。看了各种来源,但就这条线而言,几乎都没有答案。虽然这个线程对我有帮助,但是请在服务器上修改主机名。访问您的SSH并遵循以下步骤:

cd /etc/
sudo nano hosts

Now, Appending here your hostnames: For example:

现在,在这里加上您的主机名:例如:

127.0.0.1 localhost
127.0.0.1 subdomain.domain.com [if cname redirected to endpoints]
127.0.0.1 xxxxxxxx.xxxx.xxxxx.rds.amazonaws.com [Endpoints]

and now, configuring config/database.php as follows:

现在,配置配置/数据库。php如下:

$active_group = 'default';
$query_builder = TRUE;

$db['default'] = array(
    'dsn'   => '',
    'hostname' => '35.150.12.345',
    'username' => 'user-name-here',
    'password' => 'password-here',
    'database' => 'database-name-here',
    'dbdriver' => 'mysqli',
    'dbprefix' => '',
    'pconnect' => FALSE,
    'db_debug' => TRUE,
    'cache_on' => FALSE,
    'cachedir' => '',
    'char_set' => 'utf8',
    'dbcollat' => 'utf8_general_ci',
    'swap_pre' => '',
    'encrypt' => FALSE,
    'compress' => FALSE,
    'stricton' => FALSE,
    'failover' => array(),
    'save_queries' => TRUE
);

where 35.150.12.345 is your IPv4 Public IP, located at ec2-dashboard > Network Interfaces > Description : RDSNetworkInterface >> IPv4 Public IP('35.150.12.345') there.

其中35.150.12.345为IPv4公共IP,位于ec -dashboard >网络接口>描述:RDSNetworkInterface >> > IPv4公共IP('35.150.12.345')。

Note: Please note that only IPV4 will work at 'hostname' option. hostname, cname, domains will output database connection error.

注意:请注意,只有IPV4可以使用“hostname”选项。主机名、cname、域将输出数据库连接错误。

#11


0  

The question has been answered above, Just to make you feel comfortable using all four string at the same time in a connection

上面已经回答了这个问题,只是为了让您在连接中同时使用这四个字符串时感到舒服

<?php
  define("DB_HOST", "localhost");
  define("DB_USER", "root");
  define("DB_PASSWORD", "");
  define("DB_DATABASE", "databasename");

  $db = new mysqli(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_DATABASE);
  // you could test connection eventually using a if and else conditional statement, 
  // feel free to take out the code below once you see Connected!
  if ($db) {
    echo "Connected!";
  } else {
    echo "Connection Failed";
  }
?>

#12


0  

I had this error trying to connect with a different user than root I'd set up to limit access.

我在尝试连接一个与我设置为限制访问的root用户不同的用户时出现了这个错误。

In mysql I'd set up user MofX with host % and it wouldn't connect.

在mysql中,我设置用户MofX与主机%,它不会连接。

When I changed the user's host to 127.0.0.1, as in 'MofX'@'127.0.0.1', it worked.

当我将用户的主机更改为127.0.0.1时,就像在“MofX”@“127.0.0.1”中那样,它成功了。

#13


-1  

try

试一试

define("DB_PASSWORD", null);

and those are warnings try

这些都是警告

$db = @mysqli_connect(DB_SERVER,DB_USERNAME,DB_PASSWORD,DB_DATABASE);

But i will recommend you to set a root password

但是我建议您设置一个根密码

#14


-1  

Do you have two connections at the same time? If so, close every time you don't need it.

你同时有两个接头吗?如果是的话,每次不需要的时候就关门。

#15


-1  

$db = mysqli_connect(DB_SERVER,DB_USERNAME,DB_PASSWORD,DB_DATABASE);

Put " DB_HOST " instead of " DB_SERVER " also 
put " DB_USER " Instead of " DB_USERNAME " 

than it working fine !

好极了!

#16


-2  

Try changing:

试着改变:

define("DB_SERVER", "localhost");

to this:

:

define("DB_SERVER", "127.0.0.1");