Centos7 MariaDB root账户密码重置

Centos7 MariaDB root账户密码重置

如果忘记了MariaDB的root账户密码,或者由于工作交接、人员变动导致MariaD的root账户密码丢失,我们就需要重置MariaDB的root账户密码。
重置MariaDB root账户密码的前提当然是能够登录到linux系统,且有足够的操作权限。本文以linux root账户为例,重置MariaDB的root账户密码。

[root@Centos7T ~]#mysql -uroot -p
Enter password:
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)

在重置MariaDB root账户密码前,需要先将MariaDB服务关闭。

[root@Centos7T ~]#systemctl stop mariadb
[root@Centos7T ~]#systemctl status mariadb
● mariadb.service - MariaDB database server
   Loaded: loaded (/usr/lib/systemd/system/mariadb.service; disabled; vendor preset: disabled)
  Drop-In: /etc/systemd/system/mariadb.service.d
           └─migrated-from-my.cnf-settings.conf
   Active: inactive (dead)

关闭MariaDB服务后,设定环境变量MYSQLD_OPTS="--skip-grant-tables",然后重启MariaDB服务。--skip-grant-tables将允许MariaDB在启动时跳过授权表,这将导致允许所有用户拥有权限访问所有表数据。因此遗失root账户密码时,采用该方式可以进入数据库操作。

[root@Centos7T ~]#systemctl set-environment MYSQLD_OPTS="--skip-grant-tables"
[root@Centos7T ~]#systemctl start mariadb
[root@Centos7T ~]#systemctl status mariadb
● mariadb.service - MariaDB database server
   Loaded: loaded (/usr/lib/systemd/system/mariadb.service; disabled; vendor preset: disabled)
  Drop-In: /etc/systemd/system/mariadb.service.d
           └─migrated-from-my.cnf-settings.conf
   Active: active (running) since Sun 2017-09-24 14:33:56 CST; 12s ago
  Process: 28270 ExecStartPost=/bin/sh -c systemctl unset-environment _WSREP_START_POSITION (code=exited, status=0/SUCCESS)
  Process: 28227 ExecStartPre=/bin/sh -c [ ! -e /usr/bin/galera_recovery ] && VAR= ||   VAR=`/usr/bin/galera_recovery`; [ $? -eq 0 ]   && systemctl set-environment _WSREP_START_POSITION=$VAR || exit 1 (code=exited, status=0/SUCCESS)
  Process: 28223 ExecStartPre=/bin/sh -c systemctl unset-environment _WSREP_START_POSITION (code=exited, status=0/SUCCESS)
 Main PID: 28238 (mysqld)
   Status: "Taking your SQL requests now..."
   CGroup: /system.slice/mariadb.service
           └─28238 /usr/sbin/mysqld --skip-grant-tables

重启服务后使用systemctl status mariadb命令观察MariaDB服务状态,可以看到在CGroup中一条状态28238 /usr/sbin/mysqld --skip-grant-tables,表明当前MariaDB服务启动已跳过grant table,任何账户都可以无需验证就能登录,包括匿名账户。

[root@Centos7T ~]#mysql -u user1
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 11
Server version: 10.2.8-MariaDB MariaDB Server

Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> exit;
Bye
[root@Centos7T ~]#mysql -u user2
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 12
Server version: 10.2.8-MariaDB MariaDB Server

Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> exit;
Bye
[root@Centos7T ~]#mysql
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 13
Server version: 10.2.8-MariaDB MariaDB Server

Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> exit;
Bye

当然我们还是以root账户登录,然后执行后续操作。

[root@Centos7T ~]#mysql -u root
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 14
Server version: 10.2.8-MariaDB MariaDB Server

Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]>

开始重置root账户密码:

MariaDB [(none)]> USE mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
MariaDB [mysql]> UPDATE user SET password=PASSWORD('NewPassword') WHERE User='root' AND Host = 'localhost';
Query OK, 1 row affected (0.01 sec)
Rows matched: 1  Changed: 1  Warnings: 0

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

MariaDB [mysql]> exit;
Bye

至此,root账户密码已经重置了,但事情并未结束。前面我们设定了--skip-grant-tables允许用户跳过授权表登录,因此我们还需要取消该项设置,同样在关闭MariaDB服务的状态下操作,最后重新启动MariaDB服务。

[root@Centos7T ~]#systemctl stop mariadb
[root@Centos7T ~]#systemctl unset-environment MYSQLD_OPTS
[root@Centos7T ~]#systemctl start mariadb
[root@Centos7T ~]#systemctl status mariadb
● mariadb.service - MariaDB database server
   Loaded: loaded (/usr/lib/systemd/system/mariadb.service; disabled; vendor preset: disabled)
  Drop-In: /etc/systemd/system/mariadb.service.d
           └─migrated-from-my.cnf-settings.conf
   Active: active (running) since Sun 2017-09-24 14:57:02 CST; 6s ago
  Process: 28718 ExecStartPost=/bin/sh -c systemctl unset-environment _WSREP_START_POSITION (code=exited, status=0/SUCCESS)
  Process: 28673 ExecStartPre=/bin/sh -c [ ! -e /usr/bin/galera_recovery ] && VAR= ||   VAR=`/usr/bin/galera_recovery`; [ $? -eq 0 ]   && systemctl set-environment _WSREP_START_POSITION=$VAR || exit 1 (code=exited, status=0/SUCCESS)
  Process: 28671 ExecStartPre=/bin/sh -c systemctl unset-environment _WSREP_START_POSITION (code=exited, status=0/SUCCESS)
 Main PID: 28685 (mysqld)
   Status: "Taking your SQL requests now..."
   CGroup: /system.slice/mariadb.service
           └─28685 /usr/sbin/mysqld

可以看到在CGroup中一条状态28685 /usr/sbin/mysqld,已经没有--skip-grant-tables了 .表明当前MariaDB服务启动将使用授权表,用户登录都需要经过验证才能进入MariaDB,至此,在Centos7上MariaDB的root账户面重置完成。

你可能感兴趣的:(linux)