1、sudo vim /etc/mysql/mysql.conf.d/mysqld.cnf
2、在上面的文件中的[mysqld] 的最后补充一句:skip-grant-tables
保存并退出
3、重启mysql服务:sudo service mysql restart
4、进入mysql并修改root的密码
依次输入以下命令
(1)mysql
(2)use mysql
(3)UPDATE mysql.user SET authentication_string=md5(‘123456’) WHERE User=‘root’ AND Host =‘localhost’;
其他:
//之前看其他解决方案都是
UPDATE mysql.user SET authentication_string=password(‘123456’) WHERE User=‘root’ AND Host =‘localhost’;
我个人用着一条语句会报ERROR 1064 (42000): You have an error in your SQL syntax;的错。
然后试了另一种:ALTER USER ‘root’@‘localhost’ IDENTIFIED BY ‘123456’; 会报另一个错:ERROR 1290 (HY000): The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement说不能在跳过认证的模式下执行这条语句
把password替换成md5就成功了。