安装MariaDB后的登录问题

最近,我在Ubuntu 18.04上测试了MariaDB数据库服务器,发现 在Ubuntu上安装 MariaDB数据库服务后,登录MariaDB不需要输入root用户的密码。 MariaDB作为MySQL的一个分支,每次登录MariaDB服务之前,都应该提示输入密码。而现在只需安装数据库即可提供root用户访问权限,而无需输入密码。

即使我运行命令

sudo mysql_secure_installation 

也不需要输入root帐户密码。但这样的话,所有依赖MariaDB数据库root密码进行身份验证的应用程序和服务都没法正常工作。例如 phpMyAdmin和MySQL Workbench 就没法正常工作。

Google之后,我发现现在MariaDB使用unix_socket插件进行身份验证,而不是密码。即使您设置了密码,也会将其忽略。要重新启用密码验证,请按照以下步骤操作:

1. 通过运行以下命令登录MariaDB服务器

sudo mysql -u root

注意,现在登录是不需要密码的。

2. 进入数据库服务器之后,运行以下命令以禁用root用户的插件身份验证

use mysql;
update user set plugin='' where User='root';
flush privileges;
exit

3. 重新启动并运行以下命令以设置新密码。

sudo systemctl restart mariadb.service 

4. 服务重启之后,运行以下命令登录MariaDB服务器并创建新的root密码。

sudo mysql_secure_installation

5. 出现提示时,请按照下面的建议答案回答问题。

  • Enter current password for root (enter for none): 直接敲回车
  • Set root password? [Y/n]: Y
  • New password: 输入你的密码
  • Re-enter new password: 重复你输入的密码
  • Remove anonymous users? [Y/n]: Y
  • Disallow root login remotely? [Y/n]: Y
  • Remove test database and access to it? [Y/n]:  Y
  • Reload privilege tables now? [Y/n]:  Y

6. 现在,您应该可以使用密码身份验证登录了。其他应用程序现在应该可以使用root密码身份验证了。运行以下命令进行登录测试。

sudo mysql -u root -p

7. 然后输入密码,MariaDB会显示:

Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 11
Server version: 10.1.25-MariaDB-1 Ubuntu 18.04

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

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

MariaDB [(none)]>

Okie,结束收工!

留下评论

您的电子邮箱地址不会被公开。 必填项已用*标注