问题:
C:\Users\ASDS>mysql -uroot -p
Enter password: **********
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
出现这种情况的原因:
- 密码输入错误
- Mysql服务未开启
解决办法:
进入mysql安装目录:找到my.ini
Win10系统的是在 C:\ProgramData\MySQL\MySQL Server 5.7目录下
编辑my.ini 添加一行代码:skip-grant-tables
如图:
进入mysql数据库
mysql -uroot -p
密码不用填,直接回车
进入数据库
use mysql #进入mysql数据库进行设置
desc user #查看user表结构
| plugin | char(64) | NO | | mysql_native_password | |
| authentication_string | text | YES | | NULL | |
| password_expired | enum('N','Y') | NO | | N | |
| password_last_changed | timestamp | YES | | NULL | |
| password_lifetime | smallint(5) unsigned | YES | | NULL | |
| account_locked | enum('N','Y') | NO | | N | |
+------------------------+-----------------------------------+------+-----+-----------------------+-------+
45 rows in set (0.01 sec)
mysql> select user,authentication_string 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 'where user="root"' at line 1
mysql> select user,authentication_string from user where user="root";
+------+-------------------------------------------+
| user | authentication_string |
+------+-------------------------------------------+
| root | *33E5325E7E3283C80E0C495434954FD74919C12D |
+------+-------------------------------------------+
1 row in set (0.01 sec)
#修改root用户的密码:
mysql> update user set authentication_string = password('root'), password_expired = 'N', password_last_changed = now() where user = 'root';
Query OK, 1 row affected, 1 warning (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 1
mysql> select user,authentication_string from user where user="root";
+------+-------------------------------------------+
| user | authentication_string |
+------+-------------------------------------------+
| root | *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B |
+------+-------------------------------------------+
1 row in set (0.00 sec)
#修改成功!
回到my.ini文件,将刚刚添加的代码删除,保存
重新启动mysql服务,我的mysql版本是5.7,win10系统
以管理员身份运行命令行窗口关闭或开启mysql服务:
net start 服务名称
举例:
net stop mysql57
net start mysql57
Mysql服务启动成功,就可以用刚刚设置的用户用户密码进入mysql数据库了