暂无图片
暂无图片
7
暂无图片
暂无图片
暂无图片

mysql 各版本忘记密码等相关操作

原创 心在梦在²º²º 2022-07-06
3474

MYSQL 各版本密码相关操作

目录导读

一.MySQL修改密码和远程登录

1.1 各版本修改密码方法

8.0 以下版本修改密码和允许远程登陆

​ 5.5. 5.6. 5.7版本修改密码和允许远程登陆

-- 修改密码 update mysql.user set password=password('test') where user='root'; -- 适用于5.5~5.6 update mysql.user set authentication_string=password('test') where user='root'; -- 适用于 >= 5.7 <8.0 set password for root@'%'=password('test'); -- 适用于 <= 5.7 set password=password('test'); -- 修改当前用户的密码 flush privileges; -- 创建新用户,允许远程登录 grant all on *.* to root@'localhost' identified by 'test' with grant option; grant all on *.* to root@'%' identified by 'test' with grant option; flush privileges; select user,host,grant_priv,super_priv,password from mysql.user; -- 5.5~5.6 select user,host,grant_priv,super_priv,authentication_string,password_last_changed from mysql.user; -- >= 5.7

8.0 以上修改密码和允许远程登陆

-- 修改密码 alter user root@'localhost' identified with mysql_native_password by 'test'; -- 5.7也支持该命令 flush privileges; -- 允许远程登录 grant all on *.* to root@'localhost' with grant option; create user root@'%' identified with mysql_native_password by 'test'; grant all on *.* to root@'%' with grant option; flush privileges; select user,host,grant_priv,super_priv,authentication_string,password_last_changed from mysql.user; 注:mysql 8.0远程连接,在参数文件的[mysqld]下添加: default_authentication_plugin=mysql_native_password -- 删除用户: mysql> drop user yww@'localhost'; Query OK, 0 rows affected (0.01 sec)

测试5.7版本,以上修改密码操作,主从同步中,slave端也会同步修改。

二. 忘记MySQL的root密码后如何登陆数据库

-- 在MySQL中,若密码输入错误,则会返回以下信息: [root@mysql57 ~]# mysql -uroot -p Enter password: ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)

  在MySQL中,若密码丢失则无法直接找回,只能通过特殊方式来修改密码。
但是,首先需要确认是“root@localhost”还是“root@%”密码丢失,因为这是2个不同的用户,若其中一个丢失,那么可以使用另一个用户登录,然后修改密码。

方法一:使用–init-file选项

2.1 具体修改密码的步骤

1.登录MySQL数据库所在的服务器,停止MySQL服务。 对于Linux服务器,使用 ps -ef|grep mysql 来查找MySQL服务的进程号,然后手工kill掉MySQL进程。 对于Windows服务器,在cmd里输入services.msc打开 服务 ,使用service来停止MySQL服务,或在cmd里使用 net stop mysql 停止。 2.创建一个文本文件mysql-init.sql,文件内容写入密码修改语句。 -- MySQL 5.5和5.6版本使用以下语句: set password FOR 'root'@'%' =password('test'); set password FOR 'root'@'localhost' =password('test'); -- 从MySQL 5.7版本开始使用: alter user 'root'@'%' identified by 'test'; alter user 'root'@'localhost' identified by 'test'; 3.使用--init-file参数,启动MySQL实例: -- linux mysqld_safe --defaults-file=/etc/my.cnf --init-file=/tmp/mysql-init.sql & -- 若是Windows服务,则可以通过如下命令启动: D:\MySQL\mysql-8.0.15-win64\bin\mysqld --defaults-file=D:\MySQL\mysql-8.0.15-win64\data803314\mysql803314.ini --init-file=d:\mysql-init.sql --console 实例启动成功后,密码也就修改完毕,最后再删除mysql-init.sql文件。 4.重新以正常方式启动MySQL服务并验证新密码登录。

方法二:使用–skip-grant-tables选项

在启动MySQL数据库时使用--skip-grant-tables选项,表示启动MySQL服务时跳过权限表认证。通过这种方式启动后,在使用root用户连接到MySQL时将不需要密码。 -- 在使用skip-grant-tables时需要注意以下内容: 1.如果在my.cnf参数文件中只添加了--skip-grant-tables,那么在修改完密码后,删除该参数。其实无需重启MySQL服务,只需要执行flush privileges即可,但是8.0版本为了远程登录,还是需要重启mysql服务。 2.从MySQL 8.0开始,必须去掉--skip-grant-tables才能远程登陆数据库。否则抛出如下报错: [root@mysql8 data]# mysql -uroot -p -h 172.17.0.3 Enter password: ERROR 2003 (HY000): Can't connect to MySQL server on '172.17.0.3' (111)' 2.从安全角度出发,建议加上--skip-networking,但是因为其是静态参数,所以将其剔除掉需要重启实例。在MySQL 8.0版本里,如果你启用–skip-grant-tables参数,MySQL会默认把 --skip-networking参数打开,表示这时候数据库只能被本地的客户端连接。 3.虽然加上--skip-networking参数可以屏蔽掉TCP连接,但对于本地其它用户,只要有Socket文件的可读权限,都能无密码登录,存在安全隐患。

2.2 具体修改密码的步骤

-- 具体修改密码的步骤如下所示: 1.登录MySQL数据库所在的服务器,停止MySQL服务。 2.在参数文件的[mysqld]项下添加skip-grant-tables语句,或使用--skip-grant-tables选项重启MySQL服务: -- Linux启动MySQL服务: /var/lib/mysql57/mysql5719/bin/mysqld_safe --defaults-file=/etc/my.cnf --skip-grant-tables & -- Windows启动MySQL服务可以用命令行也可以用“服务”来启停,cmd命令行如下: D:\MySQL\mysql-5.7.19-win32\bin\mysqld --defaults-file=D:\MySQL\mysql-5.7.19-win32\mysql553308.ini --skip-grant-tables --console -- 注意,若MySQL是8.0且安装在Windows上,则需要加上--shared-memory参数: D:\MySQL\mysql-8.0.15-win64\bin\mysqld --defaults-file=D:\MySQL\mysql-8.0.15-win64\mysql803313.ini --skip-grant-tables --shared-memory --console 3.使用空密码的root用户连接到MySQL,并且修改root密码。注意,此时可以以任意一个密码登陆,也可以以一个空密码登陆MySQL: [root@mysql57 ~]# mysql -uroot Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> update mysql.user set authentication_string=password('test') where user='root'; Query OK, 0 rows affected, 1 warning (0.00 sec) Rows matched: 2 Changed: 0 Warnings: 1 mysql> alter user 'root'@'localhost' IDENTIFIED BY 'test'; ERROR 1290 (HY000): The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement mysql> flush privileges; Query OK, 0 rows affected (0.01 sec) mysql> alter user root@'%' IDENTIFIED BY 'test'; Query OK, 0 rows affected (0.00 sec) mysql> alter user root@'localhost' IDENTIFIED BY 'test'; Query OK, 0 rows affected (0.00 sec) MySQL [(none)]> select user,host,grant_priv,super_priv,authentication_string,password_last_changed from mysql.user; +-----------+-----------+------------+------------+-------------------------------------------+-----------------------+ | user | host | grant_priv | super_priv | authentication_string | password_last_changed | +-----------+-----------+------------+------------+-------------------------------------------+-----------------------+ | root | % | Y | Y | *827F67F8037D3F8F076544B6FFC6D56058166D8B | 2020-02-04 11:28:56 | | mysql.sys | % | N | N | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | 2019-07-18 10:52:13 | | root | localhost | Y | Y | *827F67F8037D3F8F076544B6FFC6D56058166D8B | 2020-02-04 11:28:58 | +-----------+-----------+------------+------------+-------------------------------------------+-----------------------+ 3 rows in set (0.00 sec) 4.刷新权限表,使得权限认证重新生效。刷新权限表的语句必须执行。 mysql> flush privileges; Query OK, 0 rows affected (0.01 sec) 5.重新使用新密码来登录MySQL。使用新密码成功登录后,再将MySQL服务器去掉--skip-grant-tables选项重启即可。 6.重启后验证新密码是否可用。

需要注意的是:

1.在MySQL 5.7以下版本中修改密码时,需要更新mysql.user表的Password列,尽管有authentication_string列,但是密码保存在Password列;而从MySQL 5.7开始,去掉了Password列,需要修改mysql.user表的authentication_string列。 -- 所以,在MySQL 5.7以下版本修改密码应该使用如下SQL: update mysql.user set password=password('test') where user='root'; -- 从MySQL 5.7开始可以使用: update mysql.user set authentication_string=password('test') where user='root'; 注意:从MySQL 8.0开始,password函数已经弃用。 2.从MySQL 5.7开始,不建议通过UPDATE的方式修改密码,更通用的其实是ALTER USER。但是,需要先通过flush privileges操作触发权限表的加载,然后才能使用alter user的方式来修改密码。 alter user root@'localhost' identified by 'test'; alter user root@'%' identified by 'test'; 3.可以使用如下命令查询密码: -- 查询密码,5.7以下 select user,host,grant_priv,super_priv,password,authentication_string from mysql.user; -- 查询密码,5.7以上 select user,host,grant_priv,super_priv,authentication_string,password_last_changed from mysql.user; 注意:从MySQL 5.7开始,在mysql.user表中新增了password_last_changed列,但是该列只有使用ALTER USER语句后才会更新password_last_changed列。

三.去掉密码安全策略

​ 一般yum方式安装的mysql,可能需要去掉密码验证策略

-- validate_password 是 mysql5.6以后可以引入的一个新密码校验插件, 管理用户密码长度. 强度等。 show variables like 'validate_password%'; show status like 'validate_password%'; set global validate_password_policy=0; #这个参数用于控制validate_password的验证策略 0–>low 1–>MEDIUM 2–>strong set global validate_password_policy=LOW; set global validate_password_length=1; show plugins; uninstall plugin validate_password; show variables like 'plugin_dir'; cd /usr/lib64/mysql/plugin mv validate_password.so validate_password.so_bk mv /component_validate_password.so component_validate_password.so_bk

四. 密码含特殊符合

-- 修改密码含特殊符合 update mysql.user set authentication_string=password('test!@#') where user='root'; flush privileges; -- 登录报错 [root@mysql57 ~]# mysql -uroot -ptest!@# -bash: !@#: event not found -- 解决办法: 1.将密码用单引号引起来 [root@mysql57 ~]# mysql -uroot -p'test!@#' mysql: [Warning] Using a password on the command line interface can be insecure. Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 27 Server version: 5.7.27-log 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> 2.在特殊字符&前面加上'\'来进行登录 [root@mysql57 ~]# mysql -uroot -ptest\!\@\# mysql: [Warning] Using a password on the command line interface can be insecure. Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 29 Server version: 5.7.27-log 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> 3.手动输入密码也可以,无需转义 [root@mysql57 ~]# mysql -uroot -p mysql: [Warning] Using a password on the command line interface can be insecure. Enter password: test!@# 4.navicat 工具登录时候,直接输入密码也可以登录,无需转义。
最后修改时间:2023-04-25 10:38:39
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
1人已赞赏
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论