忘记数据库密码的解决方法

tech2022-08-29  114

需要用到的两个参数: --skip-grant-tables:跳过授权表 --skip-networking:跳过TCP/IP连接 //限制了远程登陆,只允许使用socket方式登录,避免造成安全问题

方法一:

可以在启动数据库的时候,加上--skip-grant-tables ~] # service mysqld start --skip-grant-tables --skip-networking 在启动数据库的时候跳过授权表 注意:必须使用service DEAMON start/restart,使用 systemctl不生效

方法二:

1. 关闭数据库 systemctl stop mysqld 2. 使用安全模式启动 mysqld_safe --skip-grant-tables --skip-networking & 或者 service mysqld start --skip-grant-tables --skip-networking 3. 登陆数据库并修改密码 [root@VM-16-80-centos ~]# mysql //无需密码就可以登录 Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 9 Server version: 10.3.17-MariaDB MariaDB Server Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. MariaDB [(none)]> create user root@'%' identified by 'redhat'; ERROR 1290 (HY000): The MariaDB server is running with the --skip-grant-tables option so it cannot execute this statement MariaDB [(none)]> flush privileges; //刷新授权表,因为上一句报错是因为目前处于安全模式中,无法读取到授权表,因此也就无法修改密码 Query OK, 0 rows affected (0.003 sec) MariaDB [(none)]> create user root@'%' identified by 'redhat'; Query OK, 0 rows affected (0.000 sec) //此处可以使用create,也可以使用alter,alter本来就可以用来修改密码 4. 重启数据库到正常模式 service mysqld rstart 或者 systemctl restart mysqld

启动过程:

方法三:

可以将--skip-grant-tables --skip-networking直接添加到/etc/my.cnf配置文件中,如下: [mysqld] skip-grant-tables skip-networking 然后restart数据库,直接可以无需密码登录 [root@VM-16-80-centos ~]# mysql Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 9 Server version: 10.3.17-MariaDB MariaDB Server Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. MariaDB [(none)]>

最新回复(0)