Warning: mysqli_connect(): (HY000/1045): Access denied for user 'root'@'localhost' (using password: NO)

Warning: mysqli_connect(): (HY000/1045): Access denied for user 'root'@'localhost' (using password: NO)

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Warning: mysqli_connect(): (HY000/1045): Access denied for user 'root'@'localhost' (using password: NO)

When you encounter the error mysqli_connect(): (HY000/1045): Access denied for user 'root'@'localhost' (using password: NO) in your PHP logs, it is a straightforward authentication failure. The specific message indicates that the server rejected the connection attempt because the specified user (root) was not granted permission to connect without a password. This error commonly appears when migrating legacy applications from older database configurations to modern security standards or when deploying on servers where anonymous root access has been disabled by default.

The code snippet triggering this issue attempts to establish a connection using the root user with an empty password:

<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "peace";

// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);

// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}
?>

The error originates specifically from the mysqli_connect call. In older versions of MySQL (prior to 8.0), it was common practice to leave the root account's password blank for local development or internal scripts. However, modern MySQL installations, especially those running on production servers like cPanel, Plesk, or Linux distributions with hardened defaults, explicitly disable password-less access for the root user. When you pass an empty string as the third argument to mysqli_connect, the driver sends a connection request expecting no password. If the server is configured to require authentication (which it almost always is in production environments), the server responds with error code 1045, indicating "Access denied."

To resolve this, you must align your application credentials with the actual configuration of your database server. There are two primary approaches: configuring a password for the root user or creating a dedicated administrative user. Configuring a password for root is generally discouraged in production environments due to security best practices. A better approach is to create a new user with limited privileges and grant it access to the specific database you intend to use.

You can resolve this immediately by setting a strong password for the root user via the MySQL command line:

mysql -u root -p
# Once logged in, run:
SET PASSWORD FOR 'root'@'localhost' = PASSWORD('your_secure_password');
FLUSH PRIVILEGES;

Alternatively, create a new admin user with specific database permissions:

CREATE USER 'admin_peace'@'localhost' IDENTIFIED BY 'secure_random_password';
GRANT ALL PRIVILEGES ON `peace`.* TO 'admin_peace'@'localhost';
FLUSH PRIVILEGES;

After executing these commands, update your connect.php file to use the new credentials:

<?php
$servername = "localhost";
$username = "root"; // Or change to "admin_peace" if you created a new user
$password = "your_secure_password"; // Replace with the password set above
$dbname = "peace";

$conn = mysqli_connect($servername, $username, $password, $dbname);

if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}
?>

It is also worth noting that the error message explicitly states (using password: NO). This confirms that the issue is not a mismatched password but rather a configuration expectation. The server expects authentication; providing no authentication results in denial. If you are running a local development environment and wish to revert to the previous behavior, ensure your my.cnf or mysql.ini file contains skip-grant-tables. However, this disables access control entirely and should never be used on production servers.

For projects built with Laravel, managing database connections is handled differently via environment variables in .env, but the underlying principle remains identical: authentication credentials must be valid and non-empty unless explicitly configured otherwise. If you are maintaining legacy code or refactoring an existing PHP application, auditing your database connection strings against your server's current user grants is essential to prevent downtime caused by access denials.

For more guidance on securing your PHP applications and managing database connections effectively, resources like https://laravelcompany.com offer additional insights into modern deployment practices.

Our network