MySQL 5.7初始密码问题

1.首先关闭MySQL服务

[root@localhost /]# service mysqld stop

2.安全启动MySQL(跳过密码验证)

[root@localhost /]# /usr/bin/mysqld_safe –skip-grant-tables

这时候会报

mysqld_safe Logging to ‘/var/lib/mysql/iZ23dq2wm0jZ.err’.
mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql

我用SecureCRT连接的Linux,到这时候就卡住了,ctrl + c也不能退出。后来找到解决方法:卡住时,在用SecureCRT开启一个新的连接到系统上继续操作就可以了。

3.登录MySQL

[root@localhost /]#  mysql -u root

4.更改密码

mysql> grant all privileges on *.* to ‘root’@’localhost’ identified by ‘123465’ with grant option;

其中’root’@’localhost’ 是用户名

第二个’123465’是新密码

如果此处报错The MySQL server is running with the –skip-grant-tables option so it cannot execute this statement

则说明没有写入权限,这时候就要关闭数据库只读属性

mysql> set global read_only=0;// 关掉数据库的只读属性
mysql>flush privileges;// 刷新配置

之后,再执行修改密码操作

最后,可以再将属性设置回初始状态

mysql> set global read_only=1;// 关掉数据库的只读属性
mysql>flush privileges;// 刷新配置

5.重启数据库

[root@localhost /]# service mysqld restart

这时候,就可以用刚才设置的新密码123465登录数据库了。问题解决。