mysql8之前的修改方式:
1.管理员身份打开cmd:然后关闭mysql,停止MySQL服务,输入 net stop mysql 停止服务
2.切换到MySQL的bin文件下,输入mysqld --console --skip-grant-tables --shared-memory。
3上个窗口保留不要关闭,保证MySQL可以免密码登录,
新开管理员命令行,切换到MySQL的bin文件下,
输入mysql -uroot -p回车,不用输入密码,直接按回车跳过,
输入use mysql,进入数据库成功,
输入update user set password=PASSWORD('123456') where USER='root';
更新数据库成功。
mysql8版本之后修改方式:
mysql8之前用上面的方式修改会报语法错误:
mysql> UPDATE user SET password=PASSWORD(123456) WHERE user='root';
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '(123456) WHERE user='root'' at line 1
查看mysql user 表发现表中没有password 字段,所以update user set password 会出错,查找资料发现mysql 8.0.2之后数据库,密码规则发生变化,改变了user表。
正确更新root 密码的方法。
1.打开cmd 启动 mysql 跳过 权限验证
mysqld --skip-grant-tables --shared-memory --console
2.打开cmd
C:\Users\gongcm>mysql
mysql>
mysql> flush privileges;
Query OK, 0 rows affected (1.86 sec)
mysql> alter user 'root'@'localhost' IDENTIFIED BY '123456'; # 123456 为你要修改的密码
Query OK, 0 rows affected (0.42 sec)
mysql> exit;
3.关闭 mysqld 启动 cmd 窗口
重新打开cmd,输入命令
# 启动 mysql
net start mysql
#登录
C:\Users\gongcm>mysql -u root -p
Enter password: ******
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 8.0.18 MySQL Community Server - GPL
Copyright (c) 2000, 2019, 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>