Mysql5.7 开始新版的mysql数据库下的user表中已经没有password字段了,保持密码的字段变成了authentication_string。
一、忘记密码后无密码登录修改密码
1、mysql 配置文件目录添加如下命令行跳过密码:/etc/my.cnf
vim /etc/my.cnf
插入:skip-grant-tables
[mysqld]
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
socket=/tmp/mysql.sock
user=mysql
port=3306
skip-grant-tables
2、重启mysql 服务
service mysql restart;
3、登录mysql 不输入密码
mysql -u root -p
4、免密登录进入后进行修改
先查询下用户的host是%还是localhost,根据实际修改。root用户是%,修改命令如下:
use mysql;
update user set authentication_string='' where user='root'
alter user 'root'@'%' identified by '123456';
##报错1396的话,就执行下面这条命令:
##ALTER USER 'test'@'%' IDENTIFIED WITH mysql_native_password BY 'test123!@#';
flush privileges;
quit;
此处有两点需要注意:
1、不需要 flush privileges 来刷新权限。
2、密码要包含大写字母,小写字母,数字,特殊符号。
修改成功; 重新使用用户名密码登录即可;
5、记得去/etc/my.cnf 把skip-grant-tables 删掉
[mysqld]
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
socket=/tmp/mysql.sock
user=mysql
port=3306
6、重启mysql服务
service mysql restart;
二、知道密码,然后修改密码
mysql -u root -p
use mysql;
update user set authentication_string='' where user='root'
alter user 'root'@'%' identified by '123456';
quit;