忘记mysql或mariadb数据库密码的解决方案

主机环境

主机系统:

🍉CentOS Linux release 7.7.1908 (Core)

数据库版本:

🍐Server: MariaDB
🍍Server version: 5.5.64-MariaDB MariaDB Server

主机 ip地址 数据库e
db01 172.16.1.51 mariadb

 修改方案

添加--skip-grant-tables

在/etc/my.cnf.d/server.cnf中[mysqld]下面添加选项

[mysqld]
skip-grant-tables

重启数据库

[root@db01 ~]# systemctl restart mariadb

登录数据库

[root@db01 /]# mysql -uroot
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 99
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)]>

修改密码

MariaDB [(none)]> use mysql;    #切换至mysql数据库
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

MariaDB [mysql]> update user set password=password('123456') where user='root';
Query OK, 3 rows affected (0.00 sec)
Rows matched: 4  Changed: 3  Warnings: 0
Database changed MariaDB [mysql]>

注销掉配置文件中的选项

[root@db01 ~]# vim /etc/my.cnf.d/server.cnf
...
[mysqld]
#skip-grant-tables

重启数据库

[root@db01 ~]# systemctl restart mariadb

再次登录进行验证

# 创建登录密码:mysqladmin   -uroot password '123456'

输入密码123456即可进行登录

[root@db01 /]# mysql -uroot -p123456
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 100
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)]> 

 

参考:https://www.cnblogs.com/daisyyang/p/11068004.html

posted @ 2019-12-20 10:28  kerwin-  阅读(532)  评论(0编辑  收藏  举报