前言
有不少程序员大佬在日常开发中肯定遇到过这样的问题,登陆MySql
时忘记了root账号的密码,下面将以MacOS系统下重置一下MySql
的root账号的密码。
- 打开终端修改 /etc/my.cnf 文件
输入密码后,发现我的mac上并没有这个文件,这里提供一个默认的my.cnf文件如下所示:
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html
# *** DO NOT EDIT THIS FILE. It's a template which will be copied to the
# *** default location during install, and will be replaced if you
# *** upgrade to a newer version of MySQL.
[mysqld]
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
# These are commonly set, remove the # and set as required.
# basedir = .....
# datadir = .....
# port = .....
# server_id = .....
# socket = .....
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
然后在 [mysqld]的段中加上一句: skip-grant-tables
保存并且退出。
- 重新启动MySQL服务
在系统偏好设置中找到MySQL图表如下所示:
然后重启一下Mysql实例如下:
- 设置root账户和密码
## 直接登陆MySQL然后回车(无需输入密码)
mysql -uroot -p
然后使用mysql库修改root账户密码为123456如下:
mysql> update user set password=password("123456")where user="root";
ERROR 1054 (42S22): Unknown column 'password' in 'field list'
mysql> use mysql;
很遗憾我们发现并没有修改成功,我们查看一下user这张表如下:
我们发现了一个列 authentication_string
,这个列是最新版MySQL的列而不是之前的password。所以我们重新修改SQL语句如下:
我们发现执行成功了。
- 刷新权限
mysql> flush privileges ;
Query OK, 0 rows affected (0.00 sec)
- 还原my.cnf配置
我们将 [mysqld] 的段中** skip-grant-tables
** 配置删除保存并退出然后重启一下MySQL。