1,停止你的mysql
sudo /usr/local/mysql/support-files/mysql.server stop
2,在终端输入
cd /usr/local/mysql/bin/ sudo ./mysqld_safe --skip-grant-tables
执行完毕会发现mysql服务被重启了,这时当前窗口不要关掉
3,重新打开一个终端输入命令如下:
cd /usr/local/mysql/bin/ ./mysql
输入完以上命令就可以进入到mysql命令状态下了,重新设置密码为‘123’:
FLUSH PRIVILEGES; SET PASSWORD FOR 'root'@'localhost' = PASSWORD('123');
注:以上设置完成后重启mysql后,测试密码是否可以正常连接,如果提示以下错误:
ERROR 1131 (42000): You are using MySQL as an anonymous user and anonymous users are not allowed to change passwords
解决方法:
使用语句更新成功:
>update mysql.user set authentication_string=password('XXXXXX') where user='root'; Query OK, 1 row affected, 1 warning (0.00 sec) Rows matched: 1 Changed: 1 Warnings: 1
>flush privileges;
详见:https://bugs.mysql.com/bug.php?id=79027, https://dev.mysql.com/doc/refman/5.7/en/resetting-permissions.html
重新启动服务:pkill mysqld && service mysqld start
以上设置后如果提示以下错误:
You must reset your password using ALTER USER statement before executing this statement.
解决方法:
安装完mysql 之后,登陆以后,不管运行任何命令,总是提示这个
step 1: SET PASSWORD = PASSWORD('your new password'); step 2: ALTER USER 'root'@'localhost' PASSWORD EXPIRE NEVER; step 3: flush privileges;
完成以上三步退出再登,使用新设置的密码就行了,以上除了your new password的自己修改成新密码外,其他原样输入即可