方法一(通用):
1.在my.cnf或my.ini中的[mysqld]字段加入skip-grant-tables,免认证登录
2.重启mysql服务,进入CLI模式
mysql -uroot
3.在mysql命令行修改root密码
use mysql;
update user set authentication_string=password('newpassword') where user='root';
-- for old version
-- update user set Password=password('newpassword') where user='root';
flush privileges;
4.去掉my.cnf或my.ini中的skip-grant-tables,重启mysqld
方法二(Debian系统):
类Debian系统可直接使用/etc/mysql/debian.cnf中[client]节提供的密码登录
# 查看系统保留密码
cat /etc/mysql/debian.cnf
# 使用系统用户登录
mysql -udebian-sys-maint -p
扩展阅读 — MySQL用户操作相关语句:
-- 创建用户
create user 'tester'@'%' identified by 'password';
-- 修改密码
alter user 'tester'@'%' identified by 'new12345';
-- 管理权限
grant all privileges on `testdb`.* to 'tester'@'%' with grant option;
remove all privileges on `testdb`.* from 'tester'@'%';
-- 刷新授权
flush privileges;