前提要求:拥有操作系统ROOT权限
目录
一、测试环境
1.1、系统版本
[root@3a5582579588 /]# cat /etc/os-release
NAME="CentOS Linux"
VERSION="8 (Core)"
ID="centos"
ID_LIKE="rhel fedora"
VERSION_ID="8"
PLATFORM_ID="platform:el8"
PRETTY_NAME="CentOS Linux 8 (Core)"
ANSI_COLOR="0;31"
CPE_NAME="cpe:/o:centos:centos:8"
HOME_URL="https://www.centos.org/"
BUG_REPORT_URL="https://bugs.centos.org/"
CENTOS_MANTISBT_PROJECT="CentOS-8"
CENTOS_MANTISBT_PROJECT_VERSION="8"
REDHAT_SUPPORT_PRODUCT="centos"
REDHAT_SUPPORT_PRODUCT_VERSION="8"
1.2、软件版本
Server version: 10.3.17-MariaDB MariaDB Server
二、直接开车
2.1、思路
在MariaDB/MySQL中,官方保留了一个特权模式,本文的方法就是通过这个官方的特权模式进行曲线修改密码
2.2、密码修改
2.2.1、停止数据库服务
[root@3a5582579588 /]# systemctl stop mariadb
[root@3a5582579588 /]# ps -ef | grep mariadb
root 2653 384 0 15:27 pts/1 00:00:00 grep --color=auto mariadb
[root@3a5582579588 /]# netstat -ntlp | grep mysqld
[root@3a5582579588 /]#
停止服务之后,需要通过ps查看是否保留相关进程,然后使用netstat命令查看端口占用情况,从上面的反馈信息来看,mariadb服务已经被杀的一干二净了,这时候就可以使用特权模式进行密码修改了
2.2.2、启动特权模式
mysqld_safe --skip-grant-tables &
执行完成之后就可以直接通过mysql命令登录数据库了,依次执行下面的命令即可
2.2.3、登录数据库
mysql
2.2.4、选择mysql数据库
use mysql
2.2.5、更新root密码
update user set password=password('xinyang123') where user='root';
其中xingyang123改为自己的密码即可
2.2.6、刷新权限并退出登录
flush privileges;exit;
2.3、操作记录
[root@3a5582579588 /]# mysql
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 12
Server version: 10.3.17-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)]> 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('xinyang123') where user='root';
Query OK, 0 rows affected (0.000 sec)
Rows matched: 3 Changed: 0 Warnings: 0
MariaDB [mysql]> flush privileges;
Query OK, 0 rows affected (0.000 sec)
MariaDB [mysql]> exit;
Bye
2.4、启动正常模式
2.4.1、停止特权模式
pkill mysqld
2.4.2、启动服务
systemctl start mariadb
启动成功之后,尝试使用特权模式的登录命令进行登录,然后发现登录失败,说明特权模式已经关闭了
[root@3a5582579588 /]# mysql
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
2.4.3、使用新密码登录
此时,新密码登录成功,密码修改完成!!!