官方文档: https://mariadb.com/kb/en/authentication-from-mariadb-104/

mariadb 10.4.x 版本后,在执行 mysql_secure_installation 命令设置完 root 密码后,依旧可以无需输入密码直接登录 mariadb.官方资料:Using unix_socket means that if you are the system root user, you can login as root@locahost without a password.

解决方法

1.编辑主配置文件 /etc/my.cnf ,在该文件最后加上如下配置:

1
2
[mysql_install_db]
auth_root_authentication_method=normal

2.直接执行 mysql 命令登录 mariadb ,接着执行:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
[root@VM-8-3-centos ~]# mysql
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 5
Server version: 10.6.4-MariaDB-log MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> ALTER USER root@localhost IDENTIFIED VIA mysql_native_password USING PASSWORD("verysecret");
Query OK, 0 rows affected (0.001 sec)

MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.001 sec)

MariaDB [(none)]> exit
Bye

3.重启下 mariadb.service :

1
systemctl restart mariadb.service

4.再次尝试直接登录:

1
2
[root@VM-8-3-centos ~]# mysql
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)

使用密码 verysecret 登录如下:

[root@VM-8-3-centos ~]# mysql -uroot -p
Enter password:
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 5
Server version: 10.6.4-MariaDB-log MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]>
```bash