MySQL 5.5 正确配置 ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: Y ES)

第一步:C:\Program Files (x86)\MySQL\bin>net stop mysql
mysql 服务正在停止.
mysql 服务已成功停止。


第二步:

mysql>C:\Program Files (x86)\MySQL\bin>mysqld --skip-grant-tables


第三步:

C:\Program Files (x86)\MySQL\bin>net start mysql
mysql 服务正在启动 ..

mysql 服务已经启动成功。


第四步:

C:\Program Files (x86)\MySQL\bin>mysql -u root mysql

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.5.38 MySQL Community Server (GPL)


Copyright (c) 2000, 2014, 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> UPDATE user SET Password=PASSWORD('root') where USER='root';
Query OK, 3 rows affected (0.07 sec)
Rows matched: 3  Changed: 3  Warnings: 0

第六步:
mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.01 sec)

第七步:
mysql> quit
Bye

第八步:
C:\Program Files (x86)\MySQL\bin> net start mysql

请求的服务已经启动。


第九步:
C:\Program Files (x86)\\MySQL\bin> mysql -u root -p
Enter password: ****
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.5.38 MySQL Community Server (GPL)


Copyright (c) 2000, 2014, 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>
### 解决方案 在 Windows 上遇到 MySQL 登录错误 `ERROR 1045` 的情况通常是因为用户名、密码不匹配或者权限设置不当引起的。以下是详细的解决方案: #### 方法一:重置 root 密码 如果忘记了 root 用户的密码,可以通过以下方法重新设置[^5]。 1. **停止 MySQL 服务** 打开命令提示符(以管理员身份运行),输入以下命令来停止 MySQL 服务: ```cmd net stop mysql ``` 2. **跳过权限表启动 MySQL** 使用 `--skip-grant-tables` 参数启动 MySQL 服务,这样可以绕过权限验证: ```cmd mysqld --shared-memory --skip-grant-tables ``` 3. **登录到 MySQL 并修改密码** 打开另一个命令窗口,执行以下命令进入 MySQL 命令行工具: ```cmd mysql -u root ``` 修改 root 用户的密码(假设新密码为 `new_password`): ```sql ALTER USER 'root'@'localhost' IDENTIFIED BY 'new_password'; FLUSH PRIVILEGES; EXIT; ``` 4. **正常重启 MySQL 服务** 关闭之前使用 `--skip-grant-tables` 启动的服务,并通过常规方式启动 MySQL 服务: ```cmd net start mysql ``` #### 方法二:检查用户权限 有时即使提供了正确的用户名和密码,仍然会因为权限不足而无法登录。此时需要确认用户的权限设置是否正确[^4]。 1. 进入 MySQL 数据库并查看当前用户权限: ```sql SHOW GRANTS FOR 'root'@'localhost'; ``` 2. 如果发现权限受限,则可以通过以下语句授予完全权限: ```sql GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' WITH GRANT OPTION; FLUSH PRIVILEGES; ``` 3. 若希望允许从其他主机访问该数据库实例,可将 `'localhost'` 替换为 `'%'` 或指定的具体 IP 地址: ```sql GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'your_password' WITH GRANT OPTION; FLUSH PRIVILEGES; ``` #### 方法三:检查配置文件 某些情况下可能是由于 MySQL 配置文件中的绑定地址或插件冲突导致的问题[^2]。 1. 编辑 MySQL 配置文件(通常是 `my.ini` 文件),找到 `[mysqld]` 节下的 `bind-address` 设置,默认值可能为 `127.0.0.1`。将其更改为 `0.0.0.0` 来支持外部连接: ```ini bind-address=0.0.0.0 ``` 2. 确认使用的认证插件兼容性。对于较新的版本(如 MySQL 8.x),默认采用 `caching_sha2_password` 插件,这可能导致旧客户端无法连接。可通过更改插件解决此问题: ```sql ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'your_password'; FLUSH PRIVILEGES; ``` --- ### 总结 上述三种方法分别针对忘记密码、权限不足以及配置文件异常的情况提供了解决思路。具体操作时需根据实际情况选择合适的方法处理。 ```python import mysql.connector try: connection = mysql.connector.connect( host='localhost', user='root', password='your_new_password', database='test' ) cursor = connection.cursor() cursor.execute("SELECT VERSION()") result = cursor.fetchone() print(f"MySQL Version: {result[0]}") except Exception as e: print(e) finally: if connection.is_connected(): cursor.close() connection.close() ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值