前言
在实际生产环境中,请注意数据安全,本例使用的方法为通过修改MySQL配置文件权限认证的方法进入mysql后修改user表,修改期间其他人访问数据库同样无需认证,所以使用此方法前请在服务器上配置其他用户禁止访问,具体方法比较多,按实际情况选择
案例
Linux版本:Linux legend_never_dies 3.10.0-1127.el7.x86_64 #1 SMP Tue Mar 31 23:36:51 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux
MySQL版本:mysql Ver 14.14 Distrib 5.7.31, for Linux (x86_64) using EditLine wrapper
Linux操作用户:root
网上找了一圈,方法大同小异,开篇的原因是有些方法的mysql命令是旧版本的,所以更新一下。
1编辑mysql的配置文件
1.1进入配置文件
[root@legend_never_dies ~]# vim /etc/my.cnf
1.2 添加跳过认证参数
在[mysqld]下面添加skip-grant-tables,wq保存退出
[mysqld]
skip-grant-tables
1.3 重启mysql
[root@legend_never_dies ~]# systemctl restart mysqld
1.4 进入mysql
[root@legend_never_dies ~]# mysql
1.5 切换用户并修改user表
mysql> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> update mysql.user set authentication_string=password("123456") where user='root' and host='localhost';
Query OK, 0 rows affected, 1 warning (0.00 sec)
Rows matched: 1 Changed: 0 Warnings: 1
mysql> \q
Bye
1.6 退出并恢复mysql配置
[root@legend_never_dies ~]# vim /etc/my.cnf
将skip-grant-tables备注或删除,wq保存退出
[mysqld]
#skip-grant-tables
1.7 重启mysqld服务
[root@legend_never_dies ~]# systemctl restart mysqld