Access denied for user 'mysql'@'localhost' (using password: NO)

本文详细记录了解决MySQL ERROR1045(28000):Access denied for user 'mysql' @ 'localhost' (using password: NO) 的全过程。首先需要修改 PID 文件路径,接着重置默认密码,最后实现成功登录。

完整过程解决 ERROR 1045 (28000): Access denied for user 'mysql'@'localhost' (using password: NO)

分类: mysql   21791人阅读  评论(1)  收藏  举报
 ERROR 1045 (28000): Access denied for user 'mysql'@'localhost' (using password: NO)
此问题网上大部分都是围绕下面的第二步(修改密码)展开的,很是坑爹的是我怎么都登陆不进去(各种模式登陆均失败),何谈修改密码呢?
本人分心mysql日志文件总结此问题的整体步骤如下:


第一步:修改pid路径
查看日志文件:
 cat /var/log/mysqld.log


2013-10-26 16:39:34 3712 [ERROR] /usr/sbin/mysqld: Can't create/write to file '/var/run/mysqld/mysqld.pid' (Errcode: 2 - No such file or directory)
2013-10-26 16:39:34 3712 [ERROR] Can't start server: can't create PID file: No such file or directory


原因:
mysql 用户没有操作/var/run目录的权限,所以pid文件无法创建,导致登陆时无法建立 进程信息文件,登陆进程就无法开启,自然无法登陆。


解决:
修改 /etc/my.conf
原来的
 #pid-file=/var/run/mysqld/mysqld.pid
修改为
pid-file=/var/lib/mysql/mysqlid.pid


检查发现,mysql用户根本无法 cd /var/run/。修改为mysql可以有权限的目录后再执行mysql就进入数据库了。


第二步:修改数据库默认密码
/etc/init.d/mysql stop   (service mysqld stop )
/usr/bin/mysqld_safe --skip-grant-tables
另外开个SSH连接
[root@localhost ~]# mysql
mysql>use mysql
mysql>update user set password=password("123456") where user="root";
mysql>flush privileges;
mysql>exit


然后
[mysql@localhost etc]$ ps -A | grep mysql
4532 pts/0    00:00:00 mysqld_safe
5542 pts/0    00:00:00 mysqld
[mysql@localhost etc]$ kill -9 4532 5542 
正常启动 MySQL:/etc/init.d/mysql start   (service mysqld start)


第三步:
登陆ok。 mysql -uroot -p


/*********************下面为本人的完整修复过程信息,含分析过程:*******************************/
myslq status SUCCESS 但是各种尝试登陆均不起效
[mysql@localhost etc]$ service mysql start
Starting MySQL. SUCCESS!
[mysql@localhost etc]$ service mysql status
SUCCESS! MySQL running (4463)
[mysql@localhost etc]$ mysql -uroot
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
[mysql@localhost etc]$ mysql -umysql
ERROR 1045 (28000): Access denied for user 'mysql'@'localhost' (using password: NO)
[mysql@localhost etc]$ mysqladmin -uroot -p password
Enter password:
mysqladmin: connect to server at 'localhost' failed
error: 'Access denied for user 'root'@'localhost' (using password: YES)'
想死有木有!!!
冷静查看日志吧-->
[mysql@localhost ~]$ cat /var/log/mysqld.log 
2013-10-26 16:39:34 3712 [ERROR] /usr/sbin/mysqld: Can't create/write to file '/var/run/mysqld/mysqld.pid' (Errcode: 2 - No such file or directory)
2013-10-26 16:39:34 3712 [ERROR] Can't start server: can't create PID file: No such file or directory
尼玛!!!
[mysql@localhost ~]$ cd /var/run/
-bash: cd: /var/run/: 权限不够
[mysql@localhost ~]$ 
就这样吧!!!
[mysql@localhost ~]$ cat /etc/my.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0


[mysqld_safe]
log-error=/var/log/mysqld.log
#pid-file=/var/run/mysqld/mysqld.pid
pid-file=/var/lib/mysql/mysqlid.pid
重启mysql
[mysql@localhost etc]$ service mysql stop
Shutting down MySQL.. SUCCESS!
[mysql@localhost etc]$ service mysql start
Starting MySQL.. SUCCESS!
尼玛啊!!!
[mysql@localhost etc]$ mysql
ERROR 1045 (28000): Access denied for user 'mysql'@'localhost' (using password: NO)
到这一步,如果想执行mysql直接登陆,需要如下操作
[mysql@localhost etc]$ service mysql stop
Shutting down MySQL.. SUCCESS! 
[mysql@localhost etc]$ mysqld_safe --user=mysql --skip-grant-tables --skip-networking
[mysql@localhost ~]$ mysql
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.6.14


Copyright (c) 2000, 2013, 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> 


但是要想在service mysql start方式下登陆继续往下看吧。
尼玛!!!
[mysql@localhost etc]$ mysql -uroot -p
Enter password:
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
[mysql@localhost ~]$ mysql
ERROR 1045 (28000): Access denied for user 'mysql'@'localhost' (using password: NO)
解释:这里是因为数据库默认的root密码没有设置导致的。
(下面这个是网络上搜索ERROR 1045 (28000): Access denied for user 'mysql'@'localhost' (using password: NO)  大家的解决方法,但是如果
没有修改pid的路径,这里根本登陆不进去,何谈修改密码。因为mysql始终无法创建pid文件才是问题根源)
这种问题需要强行重新修改密码,方法如下:
/etc/init.d/mysql stop   (service mysqld stop )
/usr/bin/mysqld_safe --skip-grant-tables
另外开个SSH连接
[root@localhost ~]# mysql
mysql>use mysql
mysql>update user set password=password("123456") where user="root";
mysql>flush privileges;
mysql>exit


然后
[mysql@localhost etc]$ ps -A | grep mysql
4532 pts/0    00:00:00 mysqld_safe
5542 pts/0    00:00:00 mysqld
[mysql@localhost etc]$ kill -9 4532 5542 
正常启动 MySQL:/etc/init.d/mysql start   (service mysqld start)


终于!!!
[mysql@localhost ~]$ mysql -uroot -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.6.14


Copyright (c) 2000, 2013, 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> 
### 解决方案 当遇到 `Access denied for user 'gjx'@'localhost'` 的问题时,通常是因为用户名、密码不匹配或者权限不足引起的。以下是具体的解决方案: #### 方法一:验证用户名和密码 确认当前使用的用户名 `'gjx'` 和对应的密码是否正确。如果不确定密码,可以通过重置密码的方式解决问题。 ```sql use mysql; alter user 'gjx'@'localhost' identified by '新密码'; flush privileges; ``` 上述命令用于更改指定用户的密码并刷新权限表[^1]。 #### 方法二:跳过授权表登录 MySQL 并重置密码 如果无法通过正常方式登录到 MySQL 数据库,则可以尝试以下步骤来绕过认证机制以重新设置密码。 1. MySQL 服务: ```bash sudo systemctl stop mysqld ``` 2. 编辑 MySQL 配置文件(通常是 `/etc/my.cnf` 或者安装路径下的 `my.ini`),添加以下内容: ``` skip-grant-tables ``` 3. 启动 MySQL 服务: ```bash sudo systemctl start mysqld ``` 4. 登录 MySQL 而无需提供密码: ```bash mysql -u gjx -p ``` 5. 更改用户密码: ```sql use mysql; update user set authentication_string=PASSWORD('新密码') where user='gjx'; flush privileges; exit; ``` 6. 删除配置中的 `skip-grant-tables` 行,并重启 MySQL 服务: ```bash sudo systemctl restart mysqld ``` 此过程允许管理员在未记住原始密码的情况下恢复访问权[^3]。 #### 方法三:检查主机连接限制 有时错误可能源于客户端 IP 地址未被授予访问许可。可通过下面语句查看是否有针对特定 host 的约束条件存在: ```sql select Host, User from mysql.user where User = 'gjx'; ``` 假如结果显示只有某些固定的 hosts 可用而本地地址不在其中的话,就需要扩展其范围至 `%`(代表任意位置),即执行如下操作: ```sql grant all on *.* to 'gjx'@'%' with grant option; flush privileges; ``` 以上措施能够有效应对大多数因账户凭证丢失所引发的拒绝接入状况[^2]。 ### 注意事项 - 执行任何涉及敏感数据的操作前,请务必做好备份工作以防万一发生意外情况。 - 如果仍然存在问题,建议核查防火墙设定以及端口监听状态等因素是否存在干扰因素。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值