MySQL快查-详细的重置mysql密码方法

MySQL快查

因为在日常工作学习中经常忘记mysql的一些语句、关键字、操作等内容,所以最近抽取时间写了以下关于mysql相关内容。相当于一本字典吧


本文

数据类型
运算符
常用函数
数据完整性
数据库的基本操作
对表本身的操作
对表中数据的操作
子查询
多表连接
索引
视图
预处理SQL语句
自定义函数与存储过程
在MySQL中编程


重置mysql密码


tip:主要针对mysql8

  1. 如果还直到原密码的话可以使用mysqladmin来修改密码(这样比较简单)
# 命令
mysqladmin -h主机 -u用户名 -p 原密码 password 新密码
# 如果mysql就安装在自己的机器上,可以不写"-h"选项;
# 如果是新安装的mysql(没有设置过密码)"-p"选项后面不用写原密码,
# 回车后会提示"Enter password",不用输入,直接回车

# 将root用户的密码由原来的admin修改为root
>> mysqladmin -uroot -padmin password root
mysqladmin: [Warning] Using a password on the command line interface can be insecure.
Warning: Since password will be sent to server in plain text, use ssl connection to ensure password safety.
# 这个警告是说密码是明文发送到mysql服务器的,建议使用ssl连接,不用理会

如果上面的方法不行或者忘了原密码,才需要使用下面的方法。
其实mysqladmin配合mysqld_safe也可以解决在忘记密码后重置密码,感兴趣的可以搜搜

  1. 使用mysql不带任何参数登陆
>mysql
ERROR 1045 (28000): Access denied for user 'cracal'@'localhost' (using password: NO)

如果出现上面的报错,就找到mysql的配置文件,在[mysqld]下添加"skip-grant-tables"

  • windows可以通过 右击此电脑–>管理–>服务和应用程序–>服务 找到mysql服务,如果你安装时没有改名的话一般会叫Mysqld*.*,双击后在弹出的对话框中的可执行文件的路径中查找my.ini的路径,复制打开即可,要管理员权限才能更改文件。
  • ubuntu一般是/etc/mysql/mysql.conf.d/mysqld.cnf。
  • centos一般是/etc/my.cnf.d/mysql-server.cnf。
  • 你问我linux为什么不是/etc/my.cnf或者/etc/mysql/my.cnf,因为我修改后不能启动mysql服务。如果你修改这两者之一也能达到目的也可以用。
    注意:添加skip-grant-tables后需要重启mysql服务:
    • windows可以在上面打开的服务界面可视化重启(或者用net命令重启)
    • linux使用systemctl restart mysql.service命令重启
...
[mysqld]
skip-grant-tables
...
  1. 登陆并后选择mysql
>>mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.23-0ubuntu0.20.04.1 (Ubuntu)

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

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> use mysql
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
  1. 清除密码
    为了描述和操作方便,不管之前的root账号有没有设置过密码都清除密码。
mysql> update user set authentication_string="" where user="root";
Query OK, 0 rows affected (0.00 sec)
Rows matched: 1  Changed: 0  Warnings: 0

  1. 重新设置密码
alter user '账号'@'主机' identified by '密码';
# 例 将localhost主机的root账号设置密码为root
mysql> alter user 'root'@'localhost' identified by 'root';

4.1 注意:如果报下面的错误的话,

mysql> alter user 'root'@'localhost' identified by 'root';
ERROR 1290 (HY000): The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement

可以使用flush privileges刷新特权

mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)

然后继续执行alter…

4.2 如果,报下面的错误

mysql> alter user 'root'@'localhost' identified by 'root';
ERROR 1524 (HY000): Plugin 'auth_socket' is not loaded

则执行如下语句

update  mysql.user set  `plugin`='mysql_native_password'  WHERE `user`='root' AND `host`='localhost';
# where子句可以不加

接着使用quit退出mysql,然后重启mysql服务,再使用不带任何参数的mysql命令登陆;选择mysql数据库后继续执行alter语句。
注:重启后执行修改密码可能还会报4.1的错,根据4.1的解决方法方可修改。

# 登陆mysql
>>mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 7
Server version: 8.0.23-0ubuntu0.20.04.1 (Ubuntu)

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

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数据库
mysql> use mysql
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
# 修改密码
mysql> alter user 'root'@'localhost' identified by 'root';
Query OK, 0 rows affected (0.01 sec)

一般来说,这些操作执行完毕便可修改密码了。如果你还遇到其他错误,可以根据报错信息搜索解决方案

  1. 退出mysql,删掉skip-grant-tables(如果之前加了的话),重启mysql服务,使用root账号登陆测试。
# 重启服务
>> sudo systemctl restart mysql.service 
# 登陆mysql
>> mysql -uroot -p
Enter password: # 键入密码
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.23-0ubuntu0.20.04.1 (Ubuntu)

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

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> 

以上!


如果使用show databases语句报下面的错误:

mysql> show databases;
ERROR 1449 (HY000): The user specified as a definer ('mysql.infoschema'@'localhost') does not exist

根据报错,可以知道"mysql.infoschema"用户不存在,所以我们要手动创建该用户

# 创建用户
# 原型:
create user '用户名'@'主机名' identified by '密码';
# 例子
mysql> create user 'mysql.infoschema'@'%' identified by 'root';
Query OK, 0 rows affected (0.01 sec)

# 授权
# 原型:
grant all privileges on 数据库名.表名 to '用户名'@'主机名';
# 例子
mysql> grant all privileges on *.* to 'mysql.infoschema'@'%';
Query OK, 0 rows affected (0.01 sec)

# 刷新
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)

# 测试
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)

猜你喜欢

转载自blog.csdn.net/weixin_45345384/article/details/116808261