mysql如何重置root密码

[TOC]

Linux系统

1.停止mysql服务(下面提供几种常见的方式,根据实际情况挑选一种吧)

# service mysql stop
# systemctl stop mysql
# mysqladmin -uroot -p -S /tmp/mysql.sock shutdown
# ps -ef | grep mysqld | grep -v grep | awk '{print $2}' | xargs kill -9 2> /dev/null

2.带 --skip-grant-tables 参数启动(若无mysqld_safe文件可尝试在/etc/my.cnf的[mysqld]添加skip-grant-tables 再启动服务)

mysqld_safe --defaults-file=/etc/my.cnf --skip-grant-tables &

3.通过mysql命令直接进入(此时登录不需要密码),并设置新密码(mysql8请尝试 ALTER user 'root'@'%' IDENTIFIED BY 'Aa123456';

[root@localhost]# mysql
  mysql> update mysql.user set authentication_string = PASSWORD('Aa123456') where user = 'root' and host='localhost';
  mysql> flush privileges;
  mysql> exit

4.重新启动mysql服务(停止命令可参考第1步,下面提供几种常见的启动命令仅供参考)

service mysql stop

# service mysql start
# systemctl start mysql
# mysqld_safe --defaults-file=/etc/my.cnf &

5.使用新密码登录

mysql -uroot -pAa123456

Windows系统

1.以管理员身份运行cmd,并停止运行的mysql服务

net stop mysql57

2.跳过权限检查启动mysql(不要关掉cmd窗口),若无mysqld_safe文件可尝试在/etc/my.cnf的[mysqld]添加skip-grant-tables 再启动服务

c:\opt\mysql\bin\mysql.exe --defaults-file="d:\mysql_3406\my_3406.ini" --skip-grant-tables

3.重开一个cmd窗口(以管理员身份运行,密码为空),重置root用户密码(5.7之前为password字段)

c:\opt\mysql\bin\mysql.exe -uroot -p
  mysql> update mysql.user set authentication_string=PASSWORD('Aa123456') where user='root' and host='localhost';
  mysql> flush privileges;
  mysql> quit;

4.关掉第2步打开的cmd窗口并在任务管理器杀掉mysqld进程,然后启动mysql服务

tasklist | find /I "mysqld.exe"
taskkill /F /IM mysqld.exe
net start mysql57

5.使用新密码登录

mysql.exe -uroot -pAa123456
Copyright © www.sqlfans.cn 2023 All Right Reserved更新时间: 2023-09-22 16:20:07

results matching ""

    No results matching ""