- 以root用户登录linux,修改
/etc/my.cnf
vim /etc/my.cnf
在[mysqld]的段中加上一句:skip-grant-tables
[mysqld] datadir=/var/lib/mysql socket=/var/lib/mysql/mysql.sock skip-grant-tables
- 退出保存,重新启动mysqld
service mysqld start 或 systemctl start mysqld
- 登录并修改MySQL的root密码
命令如下:
//进入 mysq lbin路径 [root@iZ8vb3n2dl2w360w65xtf3Z bin]# pwd /usr/local/mysql/bin [root@iZ8vb3n2dl2w360w65xtf3Z bin]# ./mysql -uroot -p Enter password: //直接回车 use mysql;
修改密码
update user set password = password('new-password') where user = 'root' ; or update user set authentication_string= password('new-password') where user = 'root' ;
- 刷新权限
flush privileges ;
- 还原/etc/my.cnf
- 重启mysql服务
其中在重新登录mysql时,你可能遇到如下问题:
问题一:
ERROR 2002 (HY000): Can’t connect to local MySQL server through socket ‘/var/lib/mysql/mysql.sock’ (2)
然后去找这个文件,发现/var/lib/mysql/
路径下没有mysql.sock文件。而该文件存在于/tmp/mysql.sock !
解决方法:
① 修改my.cnf
[mysqld] socket = /tmp/mysql.sock
② 软链
ln -s /tmp/mysql.sock /var/lib/mysql/mysql.sock
问题二:
ERROR 1054 (42S22): Unknown column 'password' in 'field list'
使用如下方式修改密码:
update user set authentication_string= password('new-password') where user = 'root' ;
问题三:
ERROR 1862 (HY000): Your password has expired. To log in you must change it using a client that supports expired passwords.
解决过程如下:
① 修改my.cnf,将skip-grant-tables
放开,重启服务,登录mysql;
查看用户root信息:
select * from user where user = 'root'\G;
② 使用如下命令修改password_expired
use mysql; update user set password_expired='N' where user='root'; flush privileges;