Linux - MySql忘记用户名、密码

作者: 来源:网络 点击数: 发布时间:2021年10月31日

1, 关闭mysql服务

/etc/rc.d/init.d/mysqld stop


2,使用 –skip-grant-tables选项启动mysql服务

(1)打开文件 mysqld

vi /etc/rc.d/init.d/mysqld

(2)编辑文件 mysqld,在下面运行启动的语句里增加--skip-grant-tables

/usr/bin/mysqld_safe --skip-grant-tables --datadir="$datadir" --socket="$socketfile"

--log-error="$errlogfile" --pid-file="$mypidfile"

加入--skip-grant-tables的意思是启动mysql服务的时候跳 过权限表认证。启动后,连接到mysql的root不需要口令

 

3,重新启动mysql服务

/etc/rc.d/init.d/mysqld start

 

4,登录mysql

 mysql -u root -h 127.0.0.1 -P3306 -p

备注:直接回车进入就好,此时不输入密码也可以登录mysql

 

5,修改root用户的密码

mysql> select host,user,password from mysql.user;

mysql> update mysql.user set password=password('123456') where user='root'; 

(新版本mysql57需要用这个语句update MySQL.user set authentication_string=password('123456') where user='root' ;)

mysql> flush privileges;

mysql> quit


6,重新启动mysql,就可以使用新密码登录了

/etc/rc.d/init.d/mysqld restart   //重新启动mysql

mysql -u root -h 127.0.0.1 -P3306 -p

输入密码:123456