Mac忘记mysql登录密码

  1. 默认mysql配置文件在/etc/my.cnf,sudo root账号找到[mysqld]添加skip-grant-tables(Mac安装Mysql5.7请戳 https://www.jianshu.com/p/1fdfba9173f8
cat /etc/my.cnf
[mysqld]
skip-grant-tables
  1. 重启mysql
(base)  tpt@localhost:/usr/local/etc $ sudo mysql.server restart
Password:
Shutting down MySQL
. SUCCESS!
Starting MySQL
. SUCCESS!
  1. 直接登录mysql(无需输入账号密码),更改root密码
(base)  tpt@localhost:/usr/local/etc $ mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.29 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> 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 user set authentication_string = password('你的密码'), password_expired = 'N', password_last_changed = now() where user = 'root';
Query OK, 1 row affected, 1 warning (0.03 sec)
Rows matched: 1  Changed: 1  Warnings: 1

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

mysql> quit;
Bye
  1. 注释掉/etc/my.cnf中的skip-grant-tables
vim /etc/my.cnf
[mysqld]
#skip-grant-tables
  1. 重启mysql
(base)  tpt@localhost:/usr/local/etc $ sudo mysql.server restart
Password:
Shutting down MySQL
. SUCCESS!
Starting MySQL
. SUCCESS!
  1. 用户名密码登录成功!
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1.A simple master-to-slave replication is currently being...
    Kevin关大大阅读 6,024评论 0 3
  • 下载安装好 MySQL 以后就会出现在系统偏好设置里, 双击进入设置界面,此时 MySQL 还未启动 点击 Sta...
    顾顾314阅读 810评论 0 0
  • 1. 启动与关闭MySQL 1.1 单实例MySQL启动与关闭 正确启动单实例MySQL数据库 这条命令是把数据库...
    阿丧小威阅读 481评论 0 0
  • 二十年没在出生的家过年了。今年回家和爸爸、哥哥、两个侄女一起过的年。我腊月二十四到家,第二天开始由我唱主角和大...
    梅花雪儿阅读 190评论 0 1
  • 春啊春, 我等你来。 春有泥土气息, 春给世界带了美好! 带给我们幸福和享乐! 春天啊春天! 我为你感到骄傲, 春...
    n王若萱阅读 148评论 0 0