Linux 下通过sudo apt install mysql-server安装的mysql,默认情况下,只能通过 root 用户或sudo的方式才能登录到mysql的root用户,而且无需密码,也无法设置密码。

这是因为mysql默认对“mysql的root”用户使用auth_socket插件的认证。对某个mysql用户使用这个插件,将会只允许在本机上和该mysql用户的用户名相同的系统用户登录,这种登录方式无需也无法设置密码。换个说法就是,默认情况下,“mysql的root”用户,只允许本机上同样被称作“root”的用户登录,所以安装完成后必须通过sudo或切换到root用户才能登录“mysql的root”用户。

这种方式可能使某些人不习惯,为了让mysql的root用户,能像往常一样,通过任意系统用户、任意主机登录,只需将mysql的root用户验证插件修改。

则是默认情况下,mysql的root用户使用auth_socket插件验证,无法设置密码。

MySQL root@(none):mysql> SELECT `User`,`Host`,plugin,authentication_string FROM `user`;
+------------------+-----------+-----------------------+-------------------------------------------------------------------------------+
| User             | Host      | plugin                | authentication_string                                                         |
+------------------+-----------+-----------------------+-------------------------------------------------------------------------------+
| debian-sys-maint | localhost | caching_sha2_password | $A$005$P|    x1 ^Ew^A^A^X    `\rNk(N^GR9HPKy4yvB.RcWyyDuPMHGIrVSXzTjGR5BeZC85qTUP3 |
| mysql.infoschema | localhost | caching_sha2_password | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED        |
| mysql.session    | localhost | caching_sha2_password | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED        |
| mysql.sys        | localhost | caching_sha2_password | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED        |
| root             | localhost | auth_socket           |                                                                               |
+------------------+-----------+-----------------------+-------------------------------------------------------------------------------+

运行下面这条语句,将root账户的验证插件修改为caching_sha2_password,并通过密码12345登录

ALTER USER 'root'@'localhost' IDENTIFIED WITH caching_sha2_password BY '12345';

然后刷新权限使得设置生效

FLUSH PRIVILEGES;

设置完成后,

MySQL root@(none):mysql> SELECT `User`,`Host`,plugin,authentication_string FROM `user`;
+------------------+-----------+-----------------------+-------------------------------------------------------------------------------+
| User             | Host      | plugin                | authentication_string                                                         |
+------------------+-----------+-----------------------+-------------------------------------------------------------------------------+
| debian-sys-maint | localhost | caching_sha2_password | $A$005$P|    x1 ^Ew^A^A^X    `\rNk(N^GR9HPKy4yvB.RcWyyDuPMHGIrVSXzTjGR5BeZC85qTUP3 |
| mysql.infoschema | localhost | caching_sha2_password | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED        |
| mysql.session    | localhost | caching_sha2_password | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED        |
| mysql.sys        | localhost | caching_sha2_password | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED        |
| root             | localhost | caching_sha2_password | $A$019$1^Ac!u@^Qto                                                              |
+------------------+-----------+-----------------------+-------------------------------------------------------------------------------+

这样就可以用密码登录mysql的root用户了。

有些教程会教你使用mysql_native_password作为验证插件,这个是旧版本mysql提供的插件,使用中和caching_sha2_password体验一样。如果不是要通过一些不兼容caching_sha2_password的客户端连接mysql服务端,显然还是用新插件更安全。


rwxe
91 声望5 粉丝

no tengo trabajo.