话不多说直接写解决方案:
环境:centos7
数据库:mariadb
1.关闭mysql服务
[root@a ~]# systemctl stop mariadb
2.kill掉mysql进程
[root@a ~]# ps -ef |grep mariadb
root 8438 8420 0 06:22 pts/0 00:00:00 grep --color=auto mariadb
[root@a ~]# kill 8420
3.修改mysql配置文件
添加skip-grant-tables
[root@a ~]# vi /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
4.重启服务无密码登录
[root@a ~]# systemctl restart mariadb
[root@a ~]# mysql -uroot
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 5.5.64-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)]>
5.在mysql里面就可以重新设置密码了
MariaDB [(none)]> update mysql.user set Password=password('a') where User='root';
Query OK, 0 rows affected (0.00 sec)
Rows matched: 4 Changed: 0 Warnings: 0
6.登录成功
[root@a ~]# mysql -uroot -pa
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 3
Server version: 5.5.64-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)]>