首先,打开mysql的配置文件:/etc/my.cnf
在 [mysqld] 下面添加 skip-grant-tables
;然后 :wq
保存退出
连接mysql服务,再更改密码
[root@localhost mysql]# mysql -uroot
# 刷新mysql
mysql> flush privileges;
# 修改密码
mysql> alter user "root"@"localhost" identified by "root";
# 刷新mysql
mysql> flush privileges;
如果,报错:
ERROR 1290 (HY000): The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement
是因为加了免密码权限造成的,那如何解决;往下看:
我们新建一个用户,记住其密码
#首先连接mysql
[root@localhost ~]# mysql -uroot
#进入 mysql 库
mysql> use mysql;
# 刷新mysql
mysql> flush privileges;
#新建一个用户 ( starsky 是新用户名 123456 是密码 )
mysql> create user `starsky`@`%` identified by '123456';
#给予权限
mysql> grant all on *.* to `starsky`@`%` with grant option;
# 再给一个客户端连接mysql权限
mysql> alter user 'starsky'@'%' identified with mysql_native_password by '123456';
# 退出
mysql> quit
#连接mysql
[root@localhost ~]# mysql -ustarsky -p123456
# 刷新mysql
mysql> flush privileges;
# 再修改密码
mysql> alter user "root"@"localhost" identified by "root";
如果到这里,还继续报错:
ERROR 1227 (42000): Access denied; you need (at least one of) the CREATE USER privilege(s) for this operation
或
ERROR 1396 (HY000): Operation ALTER USER failed for ‘root’@‘localhost’
那最后的解决办法就是使用 Navicat 连接mysql,把新建的mysql 用户 starsky 的密码赋予给root
首先,把 mysql 配置文件的 skip-grant-tables 去掉,然后重启 MySQL 服务。
Navicat 连接MySQL报错解决:
1130-host ‘xxx.xxx.xxx.xxx‘ is not allowed to connect to this MySQL server
https://blog.csdn.net/qq_39408664/article/details/119346632
2003 - Can‘t connect to MYSQL server on ‘xxx.xxx.xxx.xxx‘ ( 10038 )
https://blog.csdn.net/qq_39408664/article/details/118707777
1251 -Client does not support authentication protocol requested by server consider upgrading MYSQL client
https://blog.csdn.net/qq_39408664/article/details/118728976
按照上面方法替换 root 密码,然后重启 MySQL 服务,即可连接。