在安装mysql时,初始化以后会有一个root的临时密码,一般需要在数据库启动后要修改密码,但是偶尔会碰到初始化的时候忘记记录密码的情况,以下办法可以解决:
免密码登陆,
vim /etc/my.cnf
在【mysqld】下添加:skip-grant-tables
重启mysql服务: service mysqld restart
登录数据库mysql -u root -p //提示输入密码时直接敲回车选择mysql数据库
登录后先看清楚保存密码的字段名称:
mysql> desc mysql.user;
如果有password字段,则 :
UPDATE user SET Password = password ( 'new-password' ) WHERE User = 'root' ;
退出后删除 /etc/my.cnf中skip-grant-tables参数,重启数据库就可以了
如果是authentication_string字段,则:
UPDATE user SET plugin='mysql_native_password' WHERE User = 'root'; ---修改加密方式
update user set authentication_string = '' where user = 'root'; ---将密码置空
select Host,User,plugin,authentication_string from mysql.user where User='root';
然后删除 /etc/my.cnf中skip-grant-tables参数,重启数据库
登录数据库mysql -u root -p //提示输入密码时直接敲回车选择mysql数据库,修改密码:
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'new_password';
FLUSH PRIVILEGES;
退出重新登录即可。