解决MariaDB或MySQL登录时忘记密码或出现Access denied for user

博客介绍了MariaDB和MySQL遇到问题时的解决方案。通过修改my.cnf配置文件,在文件末尾加入配置,重启服务后可免密登录数据库,修改密码后,再重新修改配置文件,删除或注释配置并重启服务,最后用新密码登录测试。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

遇到问题

ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)

解决方案

  1. 修改 my.cnf 配置文件
    在 /etc/my.cnf 文件末尾加入 skip-grant-tables配置

    [mysqld]
    datadir=/var/lib/mysql
    socket=/var/lib/mysql/mysql.sock
    # Disabling symbolic-links is recommended to prevent assorted security risks
    symbolic-links=0
    # Settings user and group are ignored when systemd is used.
    # If you need to run mysqld under a different user or group,
    # customize your systemd unit file for mariadb according to the
    # instructions in http://fedoraproject.org/wiki/Systemd
    
    [mysqld_safe]
    log-error=/var/log/mariadb/mariadb.log
    pid-file=/var/run/mariadb/mariadb.pid
    
    #
    # include all files from the config directory
    #
    !includedir /etc/my.cnf.d
    #增加下面配置使无密码登录
    skip-grant-tables       
    
  2. 重启MariaDB服务

    [root@cdh01 ~]# systemctl restart mariadb
    
  3. 登录MariaDB

    在命令行中,输入 mysql -u root -p命令然后回车,当需要输入密码时,直接按enter键,便可以不用密码登录到数据库当中。
    修改密码后,执行flush privileges后退出。

    [root@cdh01 yum.repos.d]# mysql -uroot -p
    Enter password: 
    Welcome to the MariaDB monitor.  Commands end with ; or \g.
    Your MariaDB connection id is 3
    Server version: 5.5.60-MariaDB MariaDB Server
    
    Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
    
    Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
    
    MariaDB [(none)]> update mysql.user set password=password("123456") where user="root";
    Query OK, 0 rows affected (0.00 sec)
    
    MariaDB [(none)]> flush privileges;
    Query OK, 0 rows affected (0.00 sec)
    
    MariaDB [(none)]> quit;
    Bye
    
  4. 重新修改 my.cnf 配置文件

    skip-grant-tables配置删除或注释掉后,重启mysql服务

    [root@cdh01 ~]# systemctl restart mariadb
    
  5. 利用修改后的密码登录 MariaDB 进行测试

    [root@cdh01 ~]# mysql -uroot -p123456
    Welcome to the MariaDB monitor.  Commands end with ; or \g.
    Your MariaDB connection id is 3
    Server version: 5.5.60-MariaDB MariaDB Server
    
    Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
    
    Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
    
    MariaDB [(none)]> 
    

    MySQL解决方法同上。

当您遇到 "Access denied for user 'root'@'localhost' (using password: YES)" 这样的错误,通常这意味着MySQL/MariaDB数据库试图连接,作为 'root' 用户从本地主机('localhost')登录,但提供的密码不被接受。这可能是由于以下几个原因: 1. **密码错误**:您可能已经改变了 root 用户的密码,而尝试登录输入的不是新的密码。 2. **权限问题**:'root' 用户可能没有在当前的数据库上设置为可从本地主机访问,者权限已被限制。 3. **安全配置**:MySQL/MariaDB的安全组设置可能阻止了从 'localhost' 的连接,者防火墙规则未正确配置。 4. **账户锁定**:如果登录尝试过于频繁违反了某种安全策略,root 账户可能会被锁定。 要解决这个问题,请按照以下步骤操作: 1. **确认密码**:确保您输入的密码是正确的,者使用 `mysql` `mysql -u root -p` 命令以提示输入密码。 2. **检查权限**:检查 `mysql` `my.cnf` 文件中的 `bind-address` 配置,确保允许从 'localhost' 连接。 3. **解锁账户**:如果账户被锁定,运行 `mysqladmin -u root unlock` 来解锁 root 账户。 4. **修改安全配置**:如果需要,更新安全组规则调整防火墙设置。 5. **重新启动服务**:有候重启 MariaDB 服务器可以清除临的连接问题。 如果你是第一次安装配置 MariaDB,也请确保你已经在安装过程中设置了 root 密码,并且正确地设置了权限。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值