root密码丢失,我们需要将mysql以不检查权限的方式重新启动。
在mysql的配置文件(/etc/my.cnf)中,[mysqld]下添加一句skip-grant-tables,然后重新启动服务,就可以通过无密码的方式登录。
通过select password(new password);可以查询到加密后的密码,将它替换原来的密码,我们就可以通过新的密码进行操作了。
代码如下
vim /etc/my.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd
skip-grant-tables
[mysqld_safe]
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid
#
# include all files from the config directory
#
!includedir /etc/my.cnf.d
systemctl restart mariadb
mysql -u root -p
Enter password: (按enter键进入)
use mysql
update user set Password = password(‘000000‘) where user = ‘root‘
\q
vim /etc/my.cnf
删除掉skip-grant-tables
systemctl restart maridb
mysql -u root -p
Enter password:000000