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

28 篇文章 0 订阅

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

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

    

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

    

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

    

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

     

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

     

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

            我们发现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,如下所示:

    

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

  • 6
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

luffy5459

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值