在 CentOS 7 上重置 MariaDB root密码

忘记MariaDB的root密码,怎么办?
如何重置MariaDB数据库的密码?

on  it road.com

在 CentOS 7 上重置 MariaDB 10.3 root密码

检查我们的 MariaDB 服务器的版本。

[root@mariadb-01 ~]# mysql --version
mysql  Ver 15.1 Distrib 10.3.12-MariaDB, for Linux (x86_64) using readline 5.1

我们启用了 mariadb.service ,因此 MariaDB 实例已在系统启动期间自动启动。
在此自动启动过程中,权限表已加载到内存中。

要重置 root 用户的密码,我们需要在不加载权限表的情况下启动 MariaDB 实例。

因此,首先我们必须停止 MariaDB 服务。

[root@mariadb-01 ~]# systemctl stop mariadb.service

现在,我们可以在不加载权限表的情况下以安全模式启动 MariaDB 数据库,如下所示。

[root@mariadb-01 ~]# mysqld_safe --skip-grant-tables --skip-networking &
[1] 7444
[root@mariadb-01 ~]# 190215 22:59:09 mysqld_safe Logging to '/var/lib/mysql/mariadb-01.onitroad.com.err'.
190215 22:59:09 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql

以 root 用户身份连接 MariaDB 实例。

[root@mariadb-01 ~]# mysql -u root
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 8
Server version: 10.3.12-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]>

这一次,它没有要求任何密码。

我们以 root 用户身份成功登录到 MariaDB 实例,无需任何密码。

在为 root 用户设置新密码之前,我们必须将权限表加载到内存中。

MariaDB [(none)]> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.002 sec)

现在,我们可以为 MariaDB root 用户设置一个新密码。

MariaDB [(none)]> ALTER USER 'root'@'localhost' IDENTIFIED BY 'a';
Query OK, 0 rows affected (0.001 sec)

再次重新加载权限表。

MariaDB [(none)]> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.000 sec)

MariaDB root 用户的新密码已设置。

从 MariaDB 提示符退出。

MariaDB [(none)]> exit
Bye

目前,MariaDB 实例正在安全模式下运行,因此,我们必须以正常模式重新启动它。

杀死 MariaDB 实例进程如下。

[root@mariadb-01 ~]# kill $(cat /var/lib/mysql/mariadb-01.onitroad.com.pid)

以正常模式启动 MariaDB 服务。

[root@mariadb-01 ~]# systemctl start mariadb

使用新密码以 root 用户身份登录到 MariaDB 数据库。

[root@mariadb-01 ~]# mysql -u root -p
Enter password:
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 8
Server version: 10.3.12-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]>

我们的新 root 密码工作正常。

我们已成功在 CentOS 7 上重置 MariaDB root 用户的密码。

日期:2020-09-17 00:11:09 来源:oir作者:oir