步骤 3:登录 MySQL
mysql -u root
class="hljs-button signin active" data-title="登录复制" data-report-click="{"spm":"1001.2101.3001.4334"}">
步骤 4:重置密码
ALTER USER 'root' @'localhost' IDENTIFIED BY 'newpassword' ;
FLUSH PRIVILEGES ;
class="hljs-button signin active" data-title="登录复制" data-report-click="{"spm":"1001.2101.3001.4334"}">
步骤 5:重启 MySQL
sudo service mysql restart
sudo systemctl restart mysqld
class="hljs-button signin active" data-title="登录复制" data-report-click="{"spm":"1001.2101.3001.4334"}">
3. 确保用户有足够的权限
确保用户有访问指定数据库的权限。
查看用户权限:
SHOW GRANTS FOR 'root' @'localhost' ;
class="hljs-button signin active" data-title="登录复制" data-report-click="{"spm":"1001.2101.3001.4334"}">
如果需要,授予权限:
GRANT ALL PRIVILEGES ON * . * TO 'root' @'localhost' WITH GRANT OPTION ;
FLUSH PRIVILEGES ;
class="hljs-button signin active" data-title="登录复制" data-report-click="{"spm":"1001.2101.3001.4334"}">
4. 检查 MySQL 配置文件
检查 MySQL 配置文件(通常是 my.cnf
或 my.ini
),确保 skip-networking
没有被启用,并且 bind-address
设置正确。
配置示例:
# my.cnf 或 my.ini
[mysqld]
# 确保 skip-networking 被注释掉
# skip-networking
# 将 bind-address 设置为 127.0.0.1,允许本地连接
bind-address = 127.0.0.1
class="hljs-button signin active" data-title="登录复制" data-report-click="{"spm":"1001.2101.3001.4334"}">
重新启动 MySQL 服务
sudo service mysql restart
sudo systemctl restart mysqld
class="hljs-button signin active" data-title="登录复制" data-report-click="{"spm":"1001.2101.3001.4334"}">
5. 确保 MySQL 服务正在运行
确保 MySQL 服务是运行的。
检查 MySQL 服务状态:
sudo service mysql status
sudo systemctl status mysqld
class="hljs-button signin active" data-title="登录复制" data-report-click="{"spm":"1001.2101.3001.4334"}">
如果 MySQL 没有运行,启动它:
sudo service mysql start
sudo systemctl start mysqld
class="hljs-button signin active" data-title="登录复制" data-report-click="{"spm":"1001.2101.3001.4334"}">
示例与应用
示例 1:检查用户名和密码
错误示例:
mysql -u root -pwrongpassword
class="hljs-button signin active" data-title="登录复制" data-report-click="{"spm":"1001.2101.3001.4334"}">
正确示例:
mysql -u root -p
class="hljs-button signin active" data-title="登录复制" data-report-click="{"spm":"1001.2101.3001.4334"}">
示例 2:重置 root 密码
停止 MySQL 服务
sudo service mysql stop
class="hljs-button signin active" data-title="登录复制" data-report-click="{"spm":"1001.2101.3001.4334"}">
跳过权限表启动 MySQL
sudo mysqld_safe --skip-grant-tables &
class="hljs-button signin active" data-title="登录复制" data-report-click="{"spm":"1001.2101.3001.4334"}">
登录 MySQL 并重置密码
mysql - u root
ALTER USER 'root' @'localhost' IDENTIFIED BY 'newpassword' ;
FLUSH PRIVILEGES ;
class="hljs-button signin active" data-title="登录复制" data-report-click="{"spm":"1001.2101.3001.4334"}">
重启 MySQL 服务
sudo service mysql restart
class="hljs-button signin active" data-title="登录复制" data-report-click="{"spm":"1001.2101.3001.4334"}">
示例 3:授予权限
查看权限
SHOW GRANTS FOR 'root' @'localhost' ;
class="hljs-button signin active" data-title="登录复制" data-report-click="{"spm":"1001.2101.3001.4334"}">
如果需要,授予权限
GRANT ALL PRIVILEGES ON * . * TO 'root' @'localhost' WITH GRANT OPTION ;
FLUSH PRIVILEGES ;
class="hljs-button signin active" data-title="登录复制" data-report-click="{"spm":"1001.2101.3001.4334"}">
示例 4:检查 MySQL 配置文件
# my.cnf 或 my.ini
[mysqld]
# 确保 skip-networking 被注释掉
# skip-networking
# 将 bind-address 设置为 127.0.0.1,允许本地连接
bind-address = 127.0.0.1
class="hljs-button signin active" data-title="登录复制" data-report-click="{"spm":"1001.2101.3001.4334"}">
示例 5:确保 MySQL 服务正在运行
检查服务状态
sudo service mysql status
class="hljs-button signin active" data-title="登录复制" data-report-click="{"spm":"1001.2101.3001.4334"}">
启动服务
sudo service mysql start
class="hljs-button signin active" data-title="登录复制" data-report-click="{"spm":"1001.2101.3001.4334"}">
总结
“Access denied for user ‘root‘@‘localhost‘ (using password: YES)” 错误通常是由于用户名或密码错误、权限不足、MySQL 服务没有正确配置或运行状态异常导致的。通过检查用户名和密码、重置 root 密码、授予足够的权限、检查 MySQL 配置文件以及确保 MySQL 服务正在运行,可以有效解决这个问题。
希望本文对你理解和解决 Access denied for user ‘root‘@‘localhost‘ (using password: YES)
错误有所帮助。如果你有任何问题或建议,欢迎在评论区留言讨论!
id="blogExtensionBox" style="width:400px;margin:auto;margin-top:12px" class="blog-extension-box"> class="blog_extension blog_extension_type2" id="blog_extension">
class="extension_official" data-report-click="{"spm":"1001.2101.3001.6471"}">
class="blog_extension_card_left">
class="blog_extension_card_cont">
商务合作/咨询/联系我
class="blog_extension_card_cont_r">
微信名片
评论记录:
回复评论: