Press "Enter" to skip to content

Mariadb忘记root密码解决办法

主机环境:
[root@test ~]# lsb_release -a
LSB Version: :core-4.1-amd64:core-4.1-noarch
Distributor ID: CentOS
Description: CentOS Linux release 7.4.1708 (Core)
Release: 7.4.1708
Codename: Core
[root@test ~]# uname -a
Linux test 3.10.0-693.2.2.el7.x86_64 #1 SMP Tue Sep 12 22:26:13 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux

mariadb yum安装参考:https://www.tracymc.cn/archives/614

具体解决过程:
1.首先停止mysql服务进程:
[root@test ~]# systemctl stop mariadb
然后编辑mysql的配置文件my.cnf,找到[mysqld]这个模块,在最后面添加一段代码skip-grant-tables(忽略mysql权限问题,直接登录),最后保存退出.
[root@test ~]# cat /etc/my.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
skip-grant-tables    //mysqld下面新增这行
# 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

[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

启动mysql服务:
[root@test ~]# systemctl start mariadb
直接进入mysql数据库,使用mysql库的user表,然后修改mysql的root密码(这里将新密码改为123456):
将MySQL的登录设置修改回来,将刚才在[mysqld]的段中加上的skip-grant-tables删除或者注释:
[root@test ~]# cat /etc/my.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
#skip-grant-tables    //把这行注释
# 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

[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

重启mysql服务,并用新密码(123456)登录:

至此,密码找回完成.

Be First to Comment

发表评论

电子邮件地址不会被公开。 必填项已用*标注