[Warning] root@localhost is created with an empty password ! Please consider switching off the解决办法

    如题所示,当我们在ubuntu1804中,通过默认的源安装数据库mysql之后,直接就可以通过mysql -uroot就可以登录了,因为他默认生成的密码是空的。

    如下所示,ubuntu1804系统自带的默认mysql源就是5.7.30版本:

    [Warning] root@localhost is created with an empty password ! Please consider switching off the解决办法_第1张图片

    如果要安装5.7版本的mysql,无需考虑其他, 直接apt install mysql-server就完了:

    [Warning] root@localhost is created with an empty password ! Please consider switching off the解决办法_第2张图片

    安装过程,会将所需的依赖全部安装上,包括libaio1,无需再手动安装。安装完成即启动成功,生成的密码是空的。

    [Warning] root@localhost is created with an empty password ! Please consider switching off the解决办法_第3张图片

    进入/var/log/mysql,查看error.log文件,发现password关键字这里的提示信息就是本文的标题部分:

    [Warning] root@localhost is created with an empty password ! Please consider switching off the解决办法_第4张图片 

    我们在命令行,可以直接输入mysql -uroot即可进入数据库中,并且不用更改root的密码就可以进行一些列的操作。

    [Warning] root@localhost is created with an empty password ! Please consider switching off the解决办法_第5张图片 

    查询mysql数据库中的user表,查看关键信息:

    [Warning] root@localhost is created with an empty password ! Please consider switching off the解决办法_第6张图片        我们发现root用户的plugin是使用的auth_socket,就是说,只要系统切换到root用户下,可以直接登录。如果我们在其他用户比如test,那么我们无法登录,更加无法远程登录。

    如何解决这个空密码的问题呢, 我们需要更改plugin为mysql_native_password,顺便更改authentication_string这个字段,也就是密码字段。更改了之后,不要忘记flush privileges,就是将权限写入数据库,而不是留在会话中。

mysql> update user set plugin='mysql_native_password',authentication_string=password('root') where user='root';     
Query OK, 1 row affected, 1 warning (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 1

mysql> select user,plugin,authentication_string from user;
+------------------+-----------------------+-------------------------------------------+
| user             | plugin                | authentication_string                     |
+------------------+-----------------------+-------------------------------------------+
| root             | mysql_native_password | *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B |
| mysql.session    | mysql_native_password | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE |
| mysql.sys        | mysql_native_password | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE |
| debian-sys-maint | mysql_native_password | *EABE427EFF3FACDFC5EDB2A3892AB57D77645CF6 |
+------------------+-----------------------+-------------------------------------------+
4 rows in set (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)

mysql> exit
Bye

     最后,我们再次使用空密码登录,发现就失效了,只能使用密码登录的方式了:

root@huali:/var/log/mysql# mysql -uroot
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
root@huali:/var/log/mysql# mysql -uroot -p
Enter password: 
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
root@huali:/var/log/mysql# mysql -uroot -proot
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 6
Server version: 5.7.30-0ubuntu0.18.04.1 (Ubuntu)

Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

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

mysql> 

     温馨提示:如果需要远程登录,还记得修改user表中的user=root的host为%,另外mysql配置文件(/etc/mysql/mysql.cnf.d/mysqld.cnf)中bind-adress也需要修改为0.0.0.0,如下所示:

    [Warning] root@localhost is created with an empty password ! Please consider switching off the解决办法_第7张图片

    另外,解决本文警告的问题,也就是关于空密码的问题,还有一种办法,就是停止mysql服务,service mysql stop,然后删除默认生成的数据库文件:rm -rf /var/lib/mysql/*, 最后执行mysqld --initialize --user=root,即可生成一个随机的密码。我们使用随机密码登录数据库,然后无法做任何操作,只能先修改密码,这时候通过alter user 'root'@'localhost' identified by 'root'即可。

你可能感兴趣的:(linux,mysql,ubuntu1804,mysql,empty,password,plugin,native_password)