Linux下使用命令MySQL找回密码

检查mysql服务是否启动

ps -ef|grep -i mysql

在这里插入图片描述
如果启动则停止mysql服务

service mysqld stop

在这里插入图片描述

修改mysql的配置文件my.cnf

my.cnf配置文件的位置,一般在/etc/my.cnf,有些版本在/etc/mysql/my.cnf

vim /etc/my.cnf     # 可以用tab补全测试my.cnf是否在当前目录    vim /etc/my. + tab
# 如果vim命令不可用,使用vi命令
vi /etc/my.cnf
# 如果/etc目录下没有my.cnf文件,则
vim /etc/mysql/my.cnf

打开配置文件后按i进入编辑模式

在文件的[mysqld]标签下添加一句:skip-grant-tables,作用是登录mysql的时候跳过密码验证

# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/8.0/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 the leading "# " to disable binary logging
# Binary logging captures changes between backups and is enabled by
# default. It's default setting is log_bin=binlog
# disable_log_bin
#
# 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
#
# Remove leading # to revert to previous value for default_authentication_plugin,
# this will increase compatibility with older clients. For background, see:
# https://dev.mysql.com/doc/refman/8.0/en/server-system-variables.html#sysvar_default_authentication_plugin
# default-authentication-plugin=mysql_native_password
skip-grant-tables   #新加上的

datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock

log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid

然后按ESC输入:wq保存退出

:wq

重启数据库服务

service mysqld start

在这里插入图片描述

登录MySQL

mysql -u root   # 注意此处不需要加 -p

在这里插入图片描述

修改密码

选择mysql数据库

use mysql;

在这里插入图片描述

mysql8.0以下

运行修改密码语句

update user set authentication_string=password('新密码') where User='root';

注意:在mysql8.0以上下不能运行此命令,因为mysql8.0以上去除了user表中 password字段 和 password()方法
在这里插入图片描述

mysql8.0及以上

运行下面语句将密码置空(这一步最好置空,使用这一步设置的密码登录会报密码错误Access denied for user ‘root’@’localhost’ (using password: YES))

update user set authentication_string = '' where user = 'root';

在这里插入图片描述

退出

EXIT
# 或者
QUIT

修改my.cnf

vim /etc/my.cnf     # 可以用tab补全测试my.cnf是否在当前目录    vim /etc/my. + tab
# 如果vim命令不可用,使用vi命令
vi /etc/my.cnf
# 如果/etc目录下没有my.cnf文件,则
vim /etc/mysql/my.cnf

打开配置文件后按 i 进入编辑模式

删掉步骤2的语句 skip-grant-tables

# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/8.0/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 the leading "# " to disable binary logging
# Binary logging captures changes between backups and is enabled by
# default. It's default setting is log_bin=binlog
# disable_log_bin
#
# 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
#
# Remove leading # to revert to previous value for default_authentication_plugin,
# this will increase compatibility with older clients. For background, see:
# https://dev.mysql.com/doc/refman/8.0/en/server-system-variables.html#sysvar_default_authentication_plugin
# default-authentication-plugin=mysql_native_password
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock

log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid

然后按ESC输入:wq保存退出

:wq

重启服务器

service mysqld restart

在这里插入图片描述

登录MySQL

如图mysql -u root命令以不可使用,必须加上-p
在这里插入图片描述

mysql -u root -p    # 提示输入密码时直接敲回车,刚刚已经将密码置空了

在这里插入图片描述

修改密码

ALTER USER root@localhost IDENTIFIED BY '新密码';

在这里插入图片描述
注意:mysql8.0后密码规则进行了更改

密码必须包含字母(大小写都行),数字,特殊字符,密码长度不能小于8

详见MySQL8.0密码策略

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值