Mysql忘记密码_如何重置Mysql密码重置mariadb密码

1.先将MySQL停止

查看 Mariadb 状态

命令:systemctl status mariadb #查看状态

[root@localhost ~]# systemctl status mariadb
● mariadb.service - MariaDB database server
   Loaded: loaded (/usr/lib/systemd/system/mariadb.service; enabled; vendor preset: disabled)
   Active: active (running) since 四 2021-04-08 14:14:27 CST; 7min ago
  Process: 1131 ExecStartPost=/usr/libexec/mariadb-wait-ready $MAINPID (code=exited, status=0/SUCCESS)
  Process: 1066 ExecStartPre=/usr/libexec/mariadb-prepare-db-dir %n (code=exited, status=0/SUCCESS)
 Main PID: 1130 (mysqld_safe)
   CGroup: /system.slice/mariadb.service
           ├─1130 /bin/sh /usr/bin/mysqld_safe --basedir=/usr
           └─1360 /usr/libexec/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib64/mysql/plugin --log-error=/var/log/mariadb/mariadb.log --pid-file=/var/run/mariadb/mari...

4月 08 14:14:21 localhost.localdomain systemd[1]: Starting MariaDB database server...
4月 08 14:14:23 localhost.localdomain mariadb-prepare-db-dir[1066]: Database MariaDB is probably initialized in /var/lib/mysql already, nothing is done.
4月 08 14:14:23 localhost.localdomain mariadb-prepare-db-dir[1066]: If this is not the case, make sure the /var/lib/mysql is empty before running mariadb-prepare-db-dir.
4月 08 14:14:23 localhost.localdomain mysqld_safe[1130]: 210408 14:14:23 mysqld_safe Logging to '/var/log/mariadb/mariadb.log'.
4月 08 14:14:24 localhost.localdomain mysqld_safe[1130]: 210408 14:14:24 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql
4月 08 14:14:27 localhost.localdomain systemd[1]: Started MariaDB database server.
停止 mariadb 服务
[root@localhost ~]# systemctl stop mariadb

2.然后跳过授权表启动MySQL服务程序

启动mysql跳过授权表
[root@localhost ~]# /usr/bin/mysqld_safe
210408 14:23:16 mysqld_safe Logging to '/var/log/mariadb/mariadb.log'.
210408 14:23:16 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql
开启一个新终端执行mysql进入mysql命令行
[root@localhost ~]# mysql
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 5.5.68-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)]> 
查看mysql用户列表
MariaDB [(none)]> select user,password,host from mysql.user;
+------+-------------------------------------------+-----------+
| user | password                                  | host      |
+------+-------------------------------------------+-----------+
| root | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 | 127.0.0.1 |
| root |                                           | ::1       |
+------+-------------------------------------------+-----------+
忘记root用户密码,修改root用户的密码为rootPwd1234
MariaDB [(none)]> update mysql.user set password=password('rootPwd1234') where User="root" and Host="127.0.0.1";
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

MariaDB [(none)]> select user,password,host from mysql.user;
+------+-------------------------------------------+-----------+
| user | password                                  | host      |
+------+-------------------------------------------+-----------+
| root | *9A8DADA52F3C4C5613A4074064974949033E3C62 | 127.0.0.1 |
| root |                                           | ::1       |
+------+-------------------------------------------+-----------+
2 rows in set (0.00 sec)
MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.00 sec)

3.重新启动mysql

ctrl-C 结束上面启动的mysqld_safe进行

Ctrl-c 结束不了进程,可以Ctrl-z退出,再执行kill

[root@localhost ~]# /usr/bin/mysqld_safe
210409 12:09:58 mysqld_safe Logging to '/var/log/mariadb/mariadb.log'.
210409 12:09:58 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql
^C^C^Z
[1]+  已停止               /usr/bin/mysqld_safe
[root@localhost ~]# ps -ef |grep mysql
root       2586   2069  0 12:09 pts/0    00:00:00 /bin/sh /usr/bin/mysqld_safe
mysql      2722   2586  0 12:09 pts/0    00:00:00 /usr/libexec/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib64/mysql/plugin --user=mysql --log-error=/var/log/mariadb/mariadb.log --pid-file=/var/run/mariadb/mariadb.pid --socket=/var/lib/mysql/mysql.sock
root       2746   2069  0 12:10 pts/0    00:00:00 grep --color=auto mysql
[root@localhost ~]# kill -9 2586 2722
[root@localhost ~]# 
[1]+  已杀死               /usr/bin/mysqld_safe
[root@localhost ~]# ps -ef |grep mysql
root       2748   2069  0 12:10 pts/0    00:00:00 grep --color=auto mysql
[root@localhost ~]# 
启动mariadb服务
[root@localhost ~]# systemctl start mariadb
[root@localhost ~]# systemctl status mariadb
● mariadb.service - MariaDB database server
   Loaded: loaded (/usr/lib/systemd/system/mariadb.service; enabled; vendor preset: disabled)
   Active: active (running) since 五 2021-04-09 12:12:22 CST; 4s ago
  Process: 2790 ExecStartPost=/usr/libexec/mariadb-wait-ready $MAINPID (code=exited, status=0/SUCCESS)
  Process: 2755 ExecStartPre=/usr/libexec/mariadb-prepare-db-dir %n (code=exited, status=0/SUCCESS)
 Main PID: 2789 (mysqld_safe)
   CGroup: /system.slice/mariadb.service
           ├─2789 /bin/sh /usr/bin/mysqld_safe --basedir=/usr
           └─2959 /usr/libexec/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib64/mysql/plugin --log-error=/var/log/mariadb/mariadb.log --pid-file=/var/run/mariadb/mari...

4月 09 12:12:20 localhost.localdomain systemd[1]: Starting MariaDB database server...
4月 09 12:12:20 localhost.localdomain mariadb-prepare-db-dir[2755]: Database MariaDB is probably initialized in /var/lib/mysql already, nothing is done.
4月 09 12:12:20 localhost.localdomain mysqld_safe[2789]: 210409 12:12:20 mysqld_safe Logging to '/var/log/mariadb/mariadb.log'.
4月 09 12:12:20 localhost.localdomain mysqld_safe[2789]: 210409 12:12:20 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql
4月 09 12:12:22 localhost.localdomain systemd[1]: Started MariaDB database server.
[root@localhost ~]# 
验证
[root@localhost ~]# mysql -h127.0.0.1 -prootPwd1234
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 5
Server version: 5.5.68-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)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
+--------------------+
5 rows in set (0.00 sec)

MariaDB [(none)]> 

已上就是mysql重置密码过程,觉得有用就给个赞吧

版权声明:本文为作者原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。

原创文章,作者:老C,如若转载,请注明出处:https://www.code404.icu/225.html

发表评论

登录后才能评论