忘记数据库密码的解决方法

14 篇文章 0 订阅
6 篇文章 0 订阅
需要用到的两个参数:
--skip-grant-tables:跳过授权表
--skip-networking:跳过TCP/IP连接			//限制了远程登陆,只允许使用socket方式登录,避免造成安全问题

方法一:

可以在启动数据库的时候,加上--skip-grant-tables

~] # service mysqld start --skip-grant-tables --skip-networking

在启动数据库的时候跳过授权表
注意:必须使用service DEAMON start/restart,使用 systemctl不生效

在这里插入图片描述

方法二:

1. 关闭数据库
systemctl stop mysqld

2. 使用安全模式启动
mysqld_safe --skip-grant-tables --skip-networking &
或者
service mysqld start --skip-grant-tables --skip-networking

3. 登陆数据库并修改密码
[root@VM-16-80-centos ~]# mysql			//无需密码就可以登录
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 9
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)]> create user root@'%' identified by 'redhat';
ERROR 1290 (HY000): The MariaDB server is running with the --skip-grant-tables option so it cannot execute this statement
MariaDB [(none)]> flush privileges;		//刷新授权表,因为上一句报错是因为目前处于安全模式中,无法读取到授权表,因此也就无法修改密码
Query OK, 0 rows affected (0.003 sec)

MariaDB [(none)]> create user root@'%' identified by 'redhat';
Query OK, 0 rows affected (0.000 sec)
//此处可以使用create,也可以使用alter,alter本来就可以用来修改密码

4. 重启数据库到正常模式
service mysqld rstart
或者
systemctl restart mysqld

在这里插入图片描述
启动过程:
在这里插入图片描述

方法三:

可以将--skip-grant-tables --skip-networking直接添加到/etc/my.cnf配置文件中,如下:
[mysqld]
skip-grant-tables
skip-networking

然后restart数据库,直接可以无需密码登录
[root@VM-16-80-centos ~]# mysql
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 9
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)]> 

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值