1、停止 mysql 服务
service mysqld stop
停止 mysql 服务
2、修改 mysql 服务启动配置 /etc/my.cnf
vi /etc/my.cnf
修改 my.cnf 文件 添加一行 skip-grant-tables
"/etc/my.cnf" 31L, 1261C written
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/8.0/en/server-configuration-defaults.html
[mysqld]
skip-grant-tables
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
#
# Remove the leading "# " to disable binary logging
# Binary logging captures changes between backups and is enabled by
# default. It's default setting is log_bin=binlog
# disable_log_bin
#
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
#
# Remove leading # to revert to previous value for default_authentication_plugin,
# this will increase compatibility with older clients. For background, see:
# https://dev.mysql.com/doc/refman/8.0/en/server-system-variables.html#sysvar_default_authentication_plugin
# default-authentication-plugin=mysql_native_password
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
按 esc
键 输入 :wq
保存退出
3、重启 mysql 服务
service mysqld restart
4、进入 mysql 清空密码
登陆mysql: mysql -u root
切换至权限数据库:use mysql;
清空密码:update user set authentication_string='' where user='root';
退出mysql:exit;

5、重复 2、3步,不同的是 2是添加 skip-grant-tables
,这里是 移除屏蔽的代码 skip-grant-tables
,重启mysql 服务 使 my.cnf 文件生效
6、修改密码
进入mysql:mysql -u root -p
提示需要输入密码的时候直接回车即可(上面已经将密码清空)
进入mysql后,切换数据库至mysql:use mysql;
设置密码:ALTER USER 'root'@'%' IDENTIFIED BY '新密码' ;
让密码马上生效:FLUSH PRIVILEGES;
使用navicat测试一下新密码,ok了。

网友评论