Each method has its applicable situation, choose the appropriate method according to the specific situation. Whichever method you choose, be sure to delete the temporary user and restart the service promptly after resetting the password MySQL
.
1. Use to mysqladmin
reset password
-
Out of service
# systemctl 启动的使用这个停止 $ sudo systemctl stop mysql # mac 本机,可以使用这个或可视化界面停止 $ sudo mysql.server stop
-
Use
mysqld_safe
the command to startMySQL
the service, skip the permission check, and you may need to press Enter after execution$ sudo mysqld_safe --skip-grant-tables &
-
Log in to
MySQL
the service, and usemysqladmin
the command to reset the password# 无需密码,会直接登入 $ mysql -u root # 刷新权限 mysql> FLUSH PRIVILEGES; # 这里的 'new_password' 是要设置的新密码,可以根据需要进行修改。此命令将修改 root 用户在本地 MySQL 服务上的密码。 mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'new_password'; # 修改好后退出 MySQL mysql> exit
-
Stop
MySQL
the service and restart the password to log in# 1、停止 MySQL 服务上面有,第一条就是 # 2、重启启动 MySQL 服务,并使用密码登入 # systemctl 启动的使用这个重新启动 $ sudo systemctl start mysql # mac 本机,可以使用这个或可视化界面启动 $ sudo mysql.server start # 3、密码登录 $ mysql -u root -p # 附:如果再次使用 mysql -u root 这样免密登录是不成功的,停止重启后会失效。
2. Use to mysqld
reset password
-
Out of service
# systemctl 启动的使用这个停止 $ sudo systemctl stop mysql # mac 本机,可以使用这个或可视化界面停止 $ sudo mysql.server stop
-
Use
mysqld_safe
the command to startMySQL
the service, skip the permission check, and press Enter after execution$ sudo mysqld_safe --skip-grant-tables &
-
Log in to
MySQL
the service, and create a new temporary user withroot
permissions# 无需密码,会直接登入 $ mysql -u root # 刷新权限 mysql> FLUSH PRIVILEGES; # 创建一个临时用户 temp_root 及 temp_password mysql> CREATE USER 'temp_root'@'localhost' IDENTIFIED BY 'temp_password'; # 设置权限 mysql> GRANT ALL PRIVILEGES ON *.* TO 'temp_root'@'localhost' WITH GRANT OPTION; # 修改好后退出 MySQL mysql> exit
-
Stop
MySQL
the service, restart the password login, and useALTER
the command to modifyroot
the user password# 1、停止 MySQL 服务上面有,第一条就是 # 2、重启启动 MySQL 服务,并使用密码登入 # systemctl 启动的使用这个重新启动 $ sudo systemctl start mysql # mac 本机,可以使用这个或可视化界面启动 $ sudo mysql.server start # 3、临时用户密码登录 $ mysql -u temp_root -p # 4、这里的 'new_password' 是要设置的新密码,可以根据需要进行修改。此命令将修改 root 用户在本地 MySQL 服务上的密码。 mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'new_password'; # 5、修改好后退出 MySQL mysql> exit # 6、停止 MySQL 服务上面有,第一条就是 # 附:如果再次使用 mysql -u root 这样免密登录是不成功的,停止重启后会失效。
-
mysqld_safe
Use the command again to startMySQL
the service, skip the permission check, and delete the temporary user$ sudo mysqld_safe --skip-grant-tables & $ mysql -u root mysql> FLUSH PRIVILEGES; mysql> REVOKE ALL PRIVILEGES, GRANT OPTION FROM 'temp_root'@'localhost'; mysql> DROP USER 'temp_root'@'localhost'; mysql> exit
-
Restart
MySQL
the service again androot
log in with the modified password# 1、停止 MySQL 服务上面有,第一条就是 # 2、重启启动 MySQL 服务,并使用密码登入 # systemctl 启动的使用这个重新启动 $ sudo systemctl start mysql # mac 本机,可以使用这个或可视化界面启动 $ sudo mysql.server start # 3、密码登录 $ mysql -u root -p
3. Use MySQL
the security mode to reset the password
-
Out of service
# systemctl 启动的使用这个停止 $ sudo systemctl stop mysql # mac 本机,可以使用这个或可视化界面停止 $ sudo mysql.server stop
-
start
MySQL
safe mode$ sudo mysqld_safe --skip-grant-tables --skip-networking &
-
Log in to
MySQL
the service, and usemysqladmin
the command to reset the password# 无需密码,会直接登入 $ mysql -u root mysql> FLUSH PRIVILEGES; # 这里的 'new_password' 是要设置的新密码,可以根据需要进行修改。此命令将修改 root 用户在本地 MySQL 服务上的密码。 mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'new_password'; # 修改好后退出 MySQL mysql> exit
-
stop
MySQL
safe mode$ sudo killall mysqld_safe # 如果在本地机器上执行上面命令无法停止安全模式,可以通过杀死进程方式解决 # 1、列出 mysqld_safe 的进程 $ ps aux | grep mysqld_safe # 2、拿到 PID 全部杀死,应该是第二列,就几位纯数字的,如果不知道可以单独输出 $ ps aux 看一下。 $ sudo kill -9 xxx xxx ...
-
Stop
MySQL
the service and restart the password to log in# 1、停止 MySQL 服务上面有,第一条就是,如果通过 $ sudo kill 可以一起就杀死了。 # 2、重启启动 MySQL 服务,并使用密码登入 # systemctl 启动的使用这个重新启动 $ sudo systemctl start mysql # mac 本机,可以使用这个或可视化界面启动 $ sudo mysql.server start # 3、密码登录 $ mysql -u root -p # 附:如果再次使用 mysql -u root 这样免密登录是不成功的,停止重启后会失效。
4. Use to my.cnf
reset password
-
This method
方式一
is basically the same as above, except that the skip permission instruction in the command is written inmy.cnf
, and then removed after modification. -
Before modifying,
my.cnf
be sure to closemysql
the process and close itmysql
, otherwise you may encountermysql
problemssock
that cannot be connected! -
Out of service
# systemctl 启动的使用这个停止 $ sudo systemctl stop mysql # mac 本机,可以使用这个或可视化界面停止 $ sudo mysql.server stop
-
open
my.cnf
, create one if none$ sudo vim /etc/my.cnf
After entering
vim
the editor to edit, find it[mysqld]
, pressi
the key on the keyboard to enter the editing mode, and[mysqld]
add in any line afterskip-grant-tables
to skip the password verification process. PressESC
to exit editing, key in:wq
, and press Enter to save and exit.[mysqld] skip-grant-tables
-
start
mysqld
service# systemctl 启动的使用这个重新启动 $ sudo systemctl start mysql # mac 本机,可以使用这个或可视化界面启动 $ sudo mysql.server start
-
Log in to
MySQL
the service, and usemysqladmin
the command to reset the password# 无需密码,会直接登入 $ mysql -u root # 这里的 'new_password' 是要设置的新密码,可以根据需要进行修改。此命令将修改 root 用户在本地 MySQL 服务上的密码。 mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'new_password'; # 刷新权限 mysql> FLUSH PRIVILEGES; # 修改好后退出 MySQL mysql> exit
-
Open it again
my.cnf
, remove what you just addedskip-grant-tables
, save and restartMySQL
the service, and log in with the new password.