centos7忘记MySQL密码

1、错误提示如下:

[root@localhost ~]# mysql -u root -p 
Enter password: 
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)

2、查看一下mysql的进程,是否处于启动状态

[root@localhost ~]# ps -ef | grep -i mysql
mysql      1983      1  0 10:24 ?        00:00:02 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid
root       3225   2935  0 10:33 pts/0    00:00:00 grep --color=auto -i mysql

或者

[root@localhost ~]# systemctl status mysqld.service
● mysqld.service - MySQL Server
   Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
   Active: inactive (dead) since 三 2020-08-26 10:34:53 CST; 16s ago
     Docs: man:mysqld(8)
           http://dev.mysql.com/doc/refman/en/using-systemd.html
  Process: 1980 ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid $MYSQLD_OPTS (code=exited, status=0/SUCCESS)
  Process: 1223 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
 Main PID: 1983 (code=exited, status=0/SUCCESS)

3、如果处于启动状态则关闭服务

[root@localhost ~]# /bin/systemctl stop mysqld.service 

4、修改MySQL的配置文件my.cnf,查找一下文件的位置

[root@localhost etc]# whereis my.cnf
my: /etc/my.cnf

5、打开 my.cnf文件,加入如下代码

#登录是跳过密码验证
skip-grant-tables

6、重启mysql服务,同时查看状态,看是否启动成功

[root@localhost etc]# systemctl start mysqld.service
[root@localhost etc]# systemctl status mysqld.service
● mysqld.service - MySQL Server
   Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
   Active: active (running) since 三 2020-08-26 10:37:48 CST; 4s ago
     Docs: man:mysqld(8)
           http://dev.mysql.com/doc/refman/en/using-systemd.html
  Process: 3402 ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid $MYSQLD_OPTS (code=exited, status=0/SUCCESS)
  Process: 3380 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
 Main PID: 3405 (mysqld)
    Tasks: 27
   CGroup: /system.slice/mysqld.service
           └─3405 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid

7、进入MySQL,提示让输入密码,直接 回车即可。

[root@localhost etc]# mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.30 MySQL Community Server (GPL)

Copyright (c) 2000, 2020, 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> 

8、修改密码,并刷新特权,最后退出。

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> update mysql.user set authentication_string=password('ryUl1_33au_n0krQ') where user='root';
Query OK, 1 row affected, 1 warning (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 1

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

mysql> exit;

注意:刚改完密码,我想查看一下MySQL端口号,结果提示错误

[root@localhost etc]# mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.7.30

Copyright (c) 2000, 2020, 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> show variables like 'port';
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
mysql> use mysql;
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.

如上图,根据错误提示,意思就是让执行一下 ALTER USER ,就是在执行(use mysql;)此语句之前,必须使用ALTER USER语句重置密码

mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'ryUl1_33au_n0krQ';
Query OK, 0 rows affected (0.00 sec)

mysql> show variables like 'port';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| port          | 3306  |
+---------------+-------+
1 row in set (0.00 sec)

如上图,重置一下密码就可以了。最后重新启动一下MySQL服务即可。

你可能感兴趣的:(Linux,PHP,Mysql/Sql,php,mysql,linux)