MySQL忘记密码

1. 忘记mysql密码后很苦恼

2. 修改mysql 配置文件

root@localhost:/etc/mysql/mysql.conf.d# cd /etc/mysql/mysql.conf.d
root@localhost:/etc/mysql/mysql.conf.d# vim mysql.cnf
# 在[mysqld]下增加
skip-grant-tables
# 就是在启动mysql时不启动grant-tables,授权表

3. 重启mysql服务

service mysql restart

4. mysql直接进入命令行

root@localhost:/etc/mysql/mysql.conf.d# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.23-0ubuntu0.16.04.1 (Ubuntu)

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>

5. 修改密码

use mysql;
update mysql.user set authentication_string=password('你想输入的密码') where user='root';
--不要忘记(或删掉第二步添加的skip-grant-tables后,重启mysql服务)
flush privileges;
  • flush privileges;
    • 如:在新创建用户后,不使用该命令刷新user_privileges表则无法登陆
    • 一是重启mysql服务,二是使用flush privileges在插入之后刷新系统权限相关表

6. 删除第二步中添加的mysql配置文件中的授权表配置

7. 重启mysql服务

service mysql restart

ps: root权限丢了之后数据库分分钟丢...嗯...

你可能感兴趣的:(MySQL忘记密码)