文档课题:mysql实现root用户免密码登录.
数据库:mysql 5.7.42
1、方案一
1.1、修改my.cnf文件
--添加如下配置跳过授权表
skip-grant-tables

1.2、重启mysqld服务
# systemctl stop mysqld
# systemctl start mysqld

1.3、修改密码
# mysql -uroot -p
说明:此时不需要输入密码.
mysql> use mysql
--设置密码
mysql> update user set authentication_string=password('mysql5721_4U'),password_expired='N',password_last_changed=now() where user='root';
--刷新授权表
mysql> flush privileges

1.4、还原my.cnf配置
说明:退出mysql且删除配置文件里的skip-grant-tables,然后重启mysql服务.

2、方案二
[root@leo-mysql5721 ~]# vi .my.cnf
添加如下内容:
[client]
host=localhost
user=root 
password=mysql_4U
port=3306

[root@leo-mysql5721 ~]# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 5.7.21 MySQL Community Server (GPL)

Copyright (c) 2000, 2018, 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> select user();
+----------------+
| user()         |
+----------------+
| root@localhost |
+----------------+
1 row in set (0.00 sec)