- 默认mysql配置文件在/etc/my.cnf,sudo root账号找到[mysqld]添加skip-grant-tables(Mac安装Mysql5.7请戳 //www.greatytc.com/p/1fdfba9173f8)
cat /etc/my.cnf
[mysqld]
skip-grant-tables
- 重启mysql
(base) tpt@localhost:/usr/local/etc $ sudo mysql.server restart
Password:
Shutting down MySQL
. SUCCESS!
Starting MySQL
. SUCCESS!
- 直接登录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
- 注释掉/etc/my.cnf中的skip-grant-tables
vim /etc/my.cnf
[mysqld]
#skip-grant-tables
- 重启mysql
(base) tpt@localhost:/usr/local/etc $ sudo mysql.server restart
Password:
Shutting down MySQL
. SUCCESS!
Starting MySQL
. SUCCESS!
- 用户名密码登录成功!