MySQL 5.7 修改 / 设置密码
1、修改配置,终端执行 sudo vim /etc/mysql/my.cnf
或者 sudo vim /usr/local/etc/my.cnf
:
# 在 [mysqld] 这个位置增加一行 skip-grant-tables ,保存并退出
[mysqld]
skip-grant-tables
2、终端执行 sudo service mysql restart
重启 MySQL 服务。
3、终端执行 mysql -u root
进入 MySQL 客户端修改密码:
# 执行这两步即可修改密码
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
mysql> update user set authentication_string=password('新密码') where user='root';
Query OK, 1 row affected, 1 warning (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 1
4、修改后退出 shell,再次修改配置文件,删掉前面添加的那一行
5、然后重启 MySQL 服务即可~
MySQL 8.0 修改 / 设置密码
1、修改配置,终端执行 sudo vim /etc/mysql/my.cnf
或者 sudo vim /usr/local/etc/my.cnf
:
# 在 [mysqld] 这个位置增加一行 skip-grant-tables ,保存并退出
[mysqld]
skip-grant-tables
2、终端执行 sudo service mysql restart
或 brew services stop mysql
+ brew services start mysql
重启 MySQL 服务。
3、终端执行 mysql -u root
进入 MySQL shell 修改密码:
# 将密码置空
mysql> UPDATE mysql.user SET authentication_string='' WHERE user="root";
# 刷新权限
mysql> FLUSH PRIVILEGES;
# 创建新密码
mysql> UPDATE mysql.user SET authentication_string='xxxxx' WHERE user="root";
# 退出客户端
mysql> exit
4、再次修改配置文件,删掉前面添加的那一行
5、然后重启 MySQL 服务即可~
网友评论