很久之前在阿里云上装的mysql,最近想用的时候发现忘记密码了,
这时候可以用下面的方法解决
1、停止mysql,可以直接杀进程
root@iZ286yyopd3Z ~]# netstat -tlnp | grep mysql
tcp6 0 0 :::3306 :::* LISTEN 21763/mysqld
[root@iZ286yyopd3Z ~]# kill -9 21763
2、跳过授权方式启动
[root@iZ286yyopd3Z ~]# /bin/mysqld_safe --defaults-file=my.cnf --skip-grant-tables --skip-networking &
[1] 10981
[root@iZ286yyopd3Z ~]# Could not open required defaults file: /root/my.cnf
Fatal error in defaults handling. Program aborted
WARNING: Defaults file 'my.cnf' not found!
Could not open required defaults file: /root/my.cnf
Fatal error in defaults handling. Program aborted
WARNING: Defaults file 'my.cnf' not found!
Logging to '/var/lib/mysql/iZ286yyopd3Z.err'.
190510 15:11:55 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql
190510 15:11:55 mysqld_safe mysqld from pid file /var/lib/mysql/iZ286yyopd3Z.pid ended
[1]+ Done /bin/mysqld_safe --defaults-file=my.cnf --skip-grant-tables --skip-networking
[root@iZ286yyopd3Z ~]#
3、进入mysql,输密码的时候直接敲回车即可
[root@iZ286yyopd3Z ~]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.40 MySQL Community Server (GPL)
Copyright (c) 2000, 2018, 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>
4、重置密码,并使其生效
注意,如果是5.6的版本,重置密码的命令为:
update mysql.user set password=password(‘123456’) where host=‘localhost’ and user=‘root’;
如果是5.7以及以上版本,则
mysql> update mysql.user set authentication_string=password('123456') where user='root';
Query OK, 5 rows affected (0.14 sec)
Rows matched: 5 Changed: 5 Warnings: 0
mysql> flush privileges;
Query OK, 0 rows affected (0.07 sec)