Centos7 之 MariaDB(Mysql) root密码忘记的解决办法
MariaDB(Mysql) root密码忘记的解决办法
1.首先先关闭mariadb数据库的服务
# 关闭mariadb服务命令(mysql的话命令就是将mariadb换成mysql)
[root@node ~]# systemctl stop mariadb
# 通过进行查询服务或者通过端口查询服务还是否存在
[root@node ~]# ps aux|grep mariadb
root 6852 0.0 0.0 112704 964 pts/2 S+ 23:19 0:00 grep --color=auto mariadb
[root@node ~]# netstat -lntup|grep 3306
2.修改配置
修改/etc/my.cnf文件,在[mysqld]下添加 skip-grant-tables , 再启动mysql
# 开启mariadb服务
[root@node ~]# systemctl start mariadb
# 查看服务已经开启
[root@node ~]# ps aux|grep mariadb
mysql 7104 1.1 5.6 972516 80672 ? Sl 23:22 0:00 /usr/libexec/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib64/mysql/plugin --log-error=/var/log/mariadb/mariadb.log --pid-file=/var/run/mariadb/mariadb.pid --socket=/var/lib/mysql/mysql.sock
root 7141 0.0 0.0 112704 964 pts/2 S+ 23:22 0:00 grep --color=auto mariadb
# 查看端口也正在使用
[root@node ~]# netstat -lntup|grep 3306
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 7104/mysqld
3.进入数据库并更新密码
# 免密码可进入
root@node ~]# mysql -u root
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 5.5.60-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)]> update mysql.user set Password=password('123') where User='root';
Query OK, 1 row affected (0.01 sec)
Rows matched: 4 Changed: 1 Warnings: 0
MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> quit;
Bye
4.还原需要密码的配置
# 重新修改为原来配置并重启服务
[root@node ~]# vi /etc/my.cnf
[root@node ~]# systemctl restart mariadb
# 可以看到没有密码现在无法进入
[root@node ~]# mysql -u root
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
# 使用新密码进入mariadb数据库
[root@node ~]# mysql -u root -p
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 3
Server version: 5.5.60-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)]>
同理此问题也可以解决其他linux版本的mariadb(mysql)密码忘记问题