简体   繁体   中英

mysqli_connect(): (28000/1045): Access denied for user 'username'@'localhost' (using password: YES)

I get this error when i configure the database in the shared hosting server

mysqli_connect(): (28000/1045): Access denied for user 'username'@'localhost' (using password: YES)

but it is working properly in the localhost.

<?php 
session_start();



if($_SERVER['HTTP_HOST']=="localhost")
{
$serverIp="localhost";
$userName="user1";
$password="XXXXX";
$dbname="example";

}else
{


$serverIp="localhost";
$userName="username";
$password="password";
$dbname="dbname";
}

$cn=mysqli_connect($serverIp,$userName,$password) OR Die("Couldn't Connect - ".mysql_error());
$link=mysql_select_db($dbname,$cn)or Die("Couldn't SELCECT - ".mysql_error()); 
?> 

In most of the cases, this error can be connected with the recent change of cpanel password or migration of the server to new hosting server..

So this works fine when you connect remotely BUT when you try to access the DB via cpanel at phpmyadmin, you get this

 you had the following error with php myadmin: mysqli_connect(): (28000/1045): Access denied for user 'wmct4s'@'localhost' (using password: YES) Undefined index: auth_type

This will be solved simply by reset the password and restarted cpanel.

the command to restart cpanel is /scripts/restartsrv_cpsrvd

只需更改您的 cpanel 密码即可。

看起来您在 else 块中插入了错误的凭据,您可以尝试将与 if 块相同的凭据放入并检查是否有效

Your mistake was you use mysql and mysqli both . if you want use mysql then you can write:

$cn = mysql_connect($serverIp, 'mysql_user', 'mysql_password');
$link = mysql_select_db($dbName,$cn);

or if you want use mysqli then you can write:

$link= new mysqli($serverIp,$userName,$password,$dbName);

You don,t need to do anything let alone changing the pass. Change it, and will surely work. Thanks

I believe this i the php admin mysqli_connect error.

I found out this on my cpanel under MySQL Users No db_users.

This the admin panel. WHM in this case also has no db user.

restarting my host machine solved the problem. Then #sudo service mysql restart. Before all that resetting the password is compulsory.

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