如果忘记了root密码要如何登陆数据库呢?
数据库版本不同采用的方式不同,在这里使用mysql-8.0.17
修改配置文件:
vim /etc/my.cnf
[mysqld]
...
skip_grant_tables=1 #所有的权限都存放在表里,这里表示跳过权限表,可以免密登陆
...
重启mysql服务后可以直接通过mysql登陆
systemctl restart mysqld
mysql #登陆mysql
修改root密码:
# 数据库用户的信息都放在mysql数据库下的user表中
# 因此在user表中修改root的密码就可以了
# mysql 5.7.9以后废弃了password字段和password()函数;
# 使用authentication_string:字段表示用户密码
# 以下命令不能修改密码:
use mysql; #切换到mysql数据库
update mysql.user set authentication_string=password('123qqq…A') \
where user = 'root' and host = 'localhost' #不存在password函数,会报错
update mysql.user set authentication_string='123qqq…A' \
where user = 'root' and host = 'localhost' # 要求authentication_string是加密过的,无法通过这个密码登录数据库
# 以下命令可以修改密码:
update mysql.user set authentication_string='' where user='root' #把authentication_string置为空,这个操作一定要做。
ALTER user 'root'@'localhost' IDENTIFIED BY 'qq123..A' #修改密码
写总结的第三十五天!!!