Saturday, August 5, 2023

Solution for "error 1045: access denied for user 'root'@'localhost' (using password: no)"

The error message "1045: Access denied for user 'root'@'localhost' (using password: no)" indicates that you are trying to connect to the MariaDB database server as the 'root' user without providing a password, but the server is expecting one.

Here are some steps to troubleshoot and resolve the issue:

1. Check your password:

   Ensure that you are using the correct password for the 'root' user. By default, MariaDB sets an empty password for the 'root' user during installation. If you have set a password and forgotten it, you might need to reset it.


2. Provide the password in your PHP script:

   If you have set a password for the 'root' user, you need to provide it when connecting to the database using `mysqli`. Update your PHP script to include the correct password:


   <?php

   $servername = "localhost";

   $username = "root";

   $password = "your_root_password"; // Update this with the actual password

   $dbname = "your_database";


   // Create connection

   $conn = new mysqli($servername, $username, $password, $dbname);


   // Check connection

   if ($conn->connect_error) {

       die("Connection failed: " . $conn->connect_error);

   }


   echo "Connected successfully";


   // Close connection

   $conn->close();

   ?>



3. Verify MariaDB service status:

   Make sure the MariaDB service is running on your CentOS 8 system. You can check the status using the following command:

   sudo systemctl status mariadb


   If it's not running, start the service:

   sudo systemctl start mariadb


4. Check MariaDB user privileges:

   It's possible that the 'root' user does not have the necessary privileges to connect from 'localhost'. Log in to the MariaDB server as the root user:

   sudo mysql -u root


   Once logged in, check the user privileges:

   MariaDB [(none)]> SELECT user, host FROM mysql.user;

 Make sure there is an entry for 'root' user with 'localhost' as the host. If it's not there, you can add it:


   MariaDB [(none)]> CREATE USER 'root'@'localhost' IDENTIFIED BY 'your_root_password';

   MariaDB [(none)]> GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' WITH GRANT OPTION;

   MariaDB [(none)]> FLUSH PRIVILEGES;

 

   Remember to replace `'your_root_password'` with the actual password you want to set.


5. Firewall considerations:

   Ensure that there are no firewall rules blocking the connection to the MariaDB server on localhost.

After performing these steps, try running your PHP script again. It should connect to the MariaDB server without the access denied error.


If you still encounter issues with access denied, here are a few things to check:


1. Verify the MariaDB root password:

   If you are unable to log in as the root user using the correct password, it's possible that the password is incorrect. You can try resetting the root password following these steps:


   - Stop the MariaDB service:

   sudo systemctl stop mariadb

 

   - Start the MariaDB server without permission checks:

   sudo mysqld_safe --skip-grant-tables &


   - Connect to the MariaDB server as the root user:

   mysql -u root

 

   - Update the root user's password:

   USE mysql;

   UPDATE user SET password = PASSWORD('new_password') WHERE User = 'root';

   FLUSH PRIVILEGES;


   Replace `'new_password'` with your desired new password.


   - Exit the MariaDB prompt:

   EXIT;


   - Stop the MariaDB server:

   sudo pkill mysqld_safe


   - Start the MariaDB service again:

   sudo systemctl start mariadb


2. Check for other potential login issues:

   It's possible that there are other issues causing the access denied error. For example, the 'root' user might not be allowed to connect from 'localhost'. Make sure you have the correct host specified in your PHP script (e.g., `'localhost'`).


3. Verify PHP configuration:

   Ensure that you are using the correct PHP configuration (`php.ini`) and that it is loading the `mysqli` extension properly.


0 comments:

Post a Comment