Linux中的mysql忘记密码

戏子无情 提交于 2020-01-01 11:21:24

随着社交账户的越来越多,密码的数量也是很多,这个时候就有人会经常忘记密码。忘记密码是一件很头疼的事情,接下来我就带大家看看在linux中安装了mysql,密码忘了怎么办?
在这里插入图片描述
想必遇到上面的报错心情是这样的
在这里插入图片描述
那别急接下来我将传授你一篇秘籍,从此妈妈再也不用担心我记不住密码了
在这里插入图片描述
废话不多说让我们开始


1、登入我们的linux系统

这一步就不用我教了,不然我会看不起的

1.1、查看mysql服务状态并关闭mysql服务

首先使用以下命令查看自己的mysql服务是否为启动的状态。

[root@centos ~]# systemctl status mysql;

在这里插入图片描述
如果你运行的结果为一下active ,那么恭喜你,请先把mysql服务关了。如果运行结果为dead接下来的这步关服务就不用做了,怎么关服务接着往下看。

[root@centos ~]# systemctl stop mysql;
//执行完以上命令后再执行以下命令查看服务是否真正的关闭
[root@centos ~]# systemctl status mysql;

1.2 修改mysql的配置文件

在mysql服务关闭的状态,我们进入etc下的my.conf进行配置文件的修改

[root@centos ~]#vim /etc、my.conf

在配置文件中的[mysql]标签下写入以下信息skip-grant-tables 意思是:我们再次登陆的时候将会跳过授权的表格,也就是再次登录不会让我们输入密码了。
修改如下


# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.6/en/server-configuration-defaults.html

[mysqld]
#
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
#         
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
skip-grant-tables  //在此处进行添加
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M 
# read_rnd_buffer_size = 2M
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Recommended in standard MySQL setup
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES

"/etc/my.cnf" 30L, 1083C         

以上修改完直接保存退出即可。
注意:添加的位置尽量靠前,或者和我的位置一样,放在后面的话,由于配置文件顺序读,可能造成服务起不来的情况。

2、操作mysql

2.1、先将mysql服务启起来

启动代码如下:

[root@centos ~]# systemctl start mysql;//建议执行以下命令查看是否真正的启动起来
[root@centos ~]# systemctl status mysql;

2.2、登录mysql修改密码

运行以下命令,我们会发现不需要密码直接将登进mysql。

[root@centos ~]# mysql;

我们将数据库切换到mysql下

[root@centos ~]# use mysql;

mysql库中的user表中存储了mysql用户密码以及登录的权限,我们对其进行修改

[root@centos ~]# update user set password=PASSWORD("此处填写您的新密码") where user='root';
//出现类似以下消息表示修改成功
Query OK, 5 rows affected (0.00 sec)
Rows matched: 5  Changed: 5  Warnings: 0

此时退出mysql;

[root@centos ~]# exit;

注意
步骤1.2中的配置skip-grant-tables删除掉保存。重新启动mysql服务再次用新密码登录即可大功告成。

[root@VM_0_12_centos ~]# mysql -u root -p //执行此命令
Enter password: //输入新修改的密码
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 7
Server version: 5.6.46 MySQL Community Server (GPL)

Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> 

在这里插入图片描述


跨越大山大海只为和你相遇

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!