ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)

Hello,
I installed MariaDB on Debian. Then I ran the following command:

# mariadb-secure-installation

Then I followed the steps as follows:

1- Press Enter to proceed with the default root database user password.

2- Press N and press Enter to enable password authentication when prompted to switch to Unix socket authentication.

3- Press Y and then Enter to change the default root database user password.

4- Press Y and then Enter to remove anonymous users.

5- Disable remote access to the root database user.

6- Remove the default test database.

7- Reload privileges to save changes.

8- Restart the MariaDB service to apply your configuration changes.

I logged into MariaDB and ran the following commands:

# mariadb -u root

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

Then:

# systemctl restart mariadb
# systemctl status mariadb

I got:

May 30 04:49:41 WEB-Server2 mariadbd[162536]: 2025-05-30  4:49:41 0 [Note] Server socket created on IP: '0.0.0.0'.
May 30 04:49:41 WEB-Server2 mariadbd[162536]: 2025-05-30  4:49:41 0 [Note] Server socket created on IP: '::'.
May 30 04:49:41 WEB-Server2 mariadbd[162536]: 2025-05-30  4:49:41 0 [Note] /usr/sbin/mariadbd: ready for connections.
May 30 04:49:41 WEB-Server2 mariadbd[162536]: Version: '10.11.11-MariaDB-0+deb12u1'  socket: '/run/mysqld/mysqld.sock'  port: 3306  Debian 12
May 30 04:49:41 WEB-Server2 systemd[1]: Started mariadb.service - MariaDB 10.11.11 database server.
May 30 04:49:41 WEB-Server2 /etc/mysql/debian-start[162552]: Upgrading MySQL tables if necessary.
May 30 04:49:41 WEB-Server2 mariadbd[162536]: 2025-05-30  4:49:41 3 [Warning] Access denied for user 'root'@'localhost' (using password: NO)
May 30 04:49:41 WEB-Server2 /etc/mysql/debian-start[162561]: Checking for insecure root accounts.
May 30 04:49:41 WEB-Server2 mariadbd[162536]: 2025-05-30  4:49:41 4 [Warning] Access denied for user 'root'@'localhost' (using password: NO)
May 30 04:49:41 WEB-Server2 debian-start[162564]: ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)

What is wrong?

Thank you.

Something in your debian-start script is expecting there to be no password on the root user. There is one.

Hello,
Thank you so much for your reply.
I edited the /etc/mysql/debian.cnf file as below:

[client]
host     = localhost
user     = root
password = Passw0rd@1
socket   = /var/run/mysqld/mysqld.sock
[mysql_upgrade]
host     = localhost
user     = root

But it didn’t matter.

Probably is not a good idea to change that file. I would revert back

Since you make securities changes to root@localhost, try running again below, to apply your changes

# mariadb-secure-installation

Hello,
The problem was not solved:

2025-05-30  8:30:48 4 [Warning] Access denied for user 'root'@'localhost' (using password: NO)

Using password: NO
indicates that it didnt try to use the password.

Yes. How do I tell it to use a password?

well i agree with Zensei that you probably shouldnt be generating this file manually.

I am not an expert in this connector, but at an absolute guess, the part of your code throwing errors is the update part, so presumably youd need to put the password in that section of the configuration.

People seems to be having same issues as you. Check these links maybe there is a solution there for you.

[https://askubuntu.com/questions/1320922/how-i-can-ask-mysql-to-use-password]

https://forum.iredmail.org/topic17975-fatal-error-upgrade-failed-when-mysql-starts.html

Hello,
Thanks again.
In the case of the first link, I did this method and it didn’t work. In the case of the second link, it uses unix_socket and when you use mariadb-secure-installation, it is better to change unix_socket to mysql_native_password.

The strange thing is that I have two similar servers and everything is fine on one server, but this error exists on the second server.

Hello,
I created another user with a simple password and the problem was solved:

MariaDB [(none)]> CREATE USER 'laravel_user'@'%' IDENTIFIED BY 'simplepass123';
MariaDB [(none)]> GRANT ALL PRIVILEGES ON laravel.* TO 'laravel_user'@'%';
MariaDB [(none)]> FLUSH PRIVILEGES;

Then I updated the .env and config/database.php files. I think using special characters creates problems.