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

MySQL忘了账号密码,除了跑路,还能补救么?

830

MySQL数据库如果忘了账号的密码,除了重装和跑路,针对不同的版本,还是有很多方案可以解决的,社区的这篇文章,就给出了说明,以备不时之需,

https://www.modb.pro/db/429812?utm_source=index_ori


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

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

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

    -- 修改密码
    -- update mysql.user set grant_priv='Y',super_priv='Y' where user='root';
    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.7wojin


    (2) 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端也会同步修改。


      2. 忘记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"选项

        具体修改密码的步骤:

        (1) 登录MySQL数据库所在的服务器,停止MySQL服务。

        对于Linux服务器,使用"ps -ef|grep mysql"来查找MySQL服务的进程号,然后手工kill掉MySQL进程。

        对于Windows服务器,在cmd里输入services.msc打开"服务",使用"服务"来停止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) 如果在命令行只添加了--skip-grant-tables,那么在修改完密码后,删除该参数,其实无需重启MySQL服务,只需要执行flush privileges即可。


            (2) 从MySQL 8.0开始,必须去掉--skip-grant-tables才能远程登陆数据库。否则抛出如下报错:

              [root@mysql8 data]# mysql -uroot -p -h x.x.x.x   
              Enter password:
              ERROR 2003 (HY000): Can't connect to MySQL server on 'x.x.x.x' (111)


              (3) 从安全角度出发,建议加上--skip-networking,但是因为其是静态参数,所以将其剔除掉需要重启实例。


              (4) 加上--skip-networking,虽然可以屏蔽掉TCP连接,但对于本地其它用户,只要有Socket文件的可读权限,都能无密码登录,存在安全隐患。


              具体修改密码的步骤:

              (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列。


                          3. 去掉密码安全策略

                          一般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


                            4. 密码含特殊符合

                            修改密码含特殊符号,

                              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工具登录时候,直接输入密码也可以登录,无需转义。


                                      当然了,最靠谱的,还是别忘了密码。


                                      P. S. 如果你认为这篇文章有些帮助,还请不吝点下文章末尾的"点赞"和"在看",



                                      近期更新的文章:

                                      非标准数据块的表空间使用

                                      数据库安全的重要性

                                      CentOS 7.9安装Oracle 21c历险记

                                      Linux的10大危险命令

                                      你知道Oracle的数据文件大小有上限么?


                                      近期的热文:

                                      "红警"游戏开源代码带给我们的震撼


                                      文章分类和索引:

                                      公众号1000篇文章分类和索引

                                      文章转载自bisal的个人杂货铺,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

                                      评论