十一月 5th, 2017

方法1:

1.停止MySQL服务

# kill `cat /var/run/mysqld/mysqld.pid`

或者

# pkill mysqld

2.创建一个密码赋值语句的文本文件

# vi mysql-init

ALTER USER ‘root’@’localhost’ IDENTIFIED BY ‘MyNewPass!6’;

在加载授权表之前重置密码。

3.使用—init-file选项启动MySQL服务

# mysqld –init-file=mysql-init –user=mysql &

4.删除文本文件,使用新密码连接MySQL

# rm -f mysql-init

# mysql -uroot -p ‘MyNewPass!6’

5.停止MySQL服务并正常启动

# kill `cat /var/run/mysqld/mysqld.pid`

# systemctl start mysqld

方法2:

1.停止MySQL服务

# kill `cat /var/run/mysqld/mysqld.pid`

# pkill mysqld

2.使用–skip-grant-tables –skip-networking选项启动MySQL服务

# mysqld –skip-grant-tables –skip-networking –user=mysql

–skip-grant-tables:跳过授权表认证

–skip-networking:加了跳过授权表选项后所有的人都可以无密码登录,这是很不安全的,此选项不监听网络,防止恶意登录。

3.无密码连接MySQL

# mysql

4.重置密码

mysql> FLUSH PRIVILEGES;

mysql> ALTER USER ‘root’@’localhost’ IDENTIFIED BY ‘MyNewPass!6’;

5.停止MySQL服务并正常启动

# kill `cat /var/run/mysqld/mysqld.pid`

# systemctl startmysqld

简单、快捷、有效,记得收藏哦!万一哪天root密码找不到了呢!

TAGS:

Comments are closed.