连接MySQL出现错误:ERROR 1045 (28000): Access denied for user ‘root‘@‘localhost‘ (using password: YES)

这里写目录标题

  • 1、错误代码
  • 2、解决方法
    • 2.1、重置密码
    • 2.2、重新启动MySQL
    • 2.3、进入mysql中
    • 2.4、重新编辑`my.cnf`去除刚才的跳过密码校验段

1、错误代码

[root@iZuf6el9zfn76jfyw0aoi9Z /]# mysql -uroot -p
Enter password: 
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
  • 一般这个错误是由密码错误引起的,解决的办法自然就是重置密码!
  • 如果当前不是root用户,使用命令sudo su root切换至root用户,然后操作!

2、解决方法

  • 执行如下命令查看MySQL数据库实例在启动时,会在哪些位置查找配置文件
    [root@iZuf6el9zfn76jfyw0aoi9Z /]# mysql --help | grep my.cnf
    order of preference, my.cnf, $MYSQL_TCP_PORT,
    /etc/mysql/my.cnf /etc/my.cnf ~/.my.cnf 
    
  • 如上可知,是按照这个顺序去查找的配置文件
    /etc/mysql/my.cnf -> /etc/my.cnf -> ~/.my.cnf
  • 并且MySQL数据库会以读取到的最后一个配置文件中的参数为准
  • 在Linux环境下,配置文件一般放在/etc/my.cnf,并且是以.cnf为后缀;
  • 在Windows环境下,配置文件一般在/mysql/my.ini,后缀名可能是.cnf,也可能是.ini

2.1、重置密码

  • 重置密码就要跳过密码校验(按照上述查找配置文件的顺序,编辑最后一个文件)

    [root@iZuf6el9zfn76jfyw0aoi9Z etc]# vim my.cnf
    	
    [mysqld]
    port            = 3306
    socket          = /tmp/mysql.sock
    datadir = /www/server/data
    default_storage_engine = InnoDB
    performance_schema_max_table_instances = 400
    table_definition_cache = 400
    skip-external-locking
    key_buffer_size = 32M
    max_allowed_packet = 100G
    table_open_cache = 128
    sort_buffer_size = 768K
    net_buffer_length = 4K
    read_buffer_size = 768K
    read_rnd_buffer_size = 256K
    myisam_sort_buffer_size = 8M
    thread_cache_size = 16
    query_cache_size = 16M
    tmp_table_size = 32M
    sql-mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
    
    explicit_defaults_for_timestamp = true
    #skip-name-resolve
    max_connections = 500
    max_connect_errors = 100
    open_files_limit = 65535
    
  • 定位到[mysqld],在其下方添加skip-grant-tables跳过密码校验

    [mysqld]
    skip-grant-tables
    
  • :wq保存my.cnf文档,并退出

2.2、重新启动MySQL

  • 输入如下命令
    /etc/init.d/mysql restart
    #有的可能是需要是mysqld
    /etc/init.d/mysqld restart
    
  • 执行之后
    [root@iZuf6el9zfn76jfyw0aoi9Z mysql]# /etc/init.d/mysqld restart
    Shutting down MySQL..                                      [  OK  ]
    Starting MySQL.                                            [  OK  ]
    [root@iZuf6el9zfn76jfyw0aoi9Z mysql]# 
    

2.3、进入mysql中

  • 在mysql安装目录下,直接进入mysql
    [root@iZuf6el9zfn76jfyw0aoi9Z mysql]# mysql
    Welcome to the MySQL monitor.  Commands end with ; or \g.
    Your MySQL connection id is 1
    Server version: 5.6.50-log Source distribution
    
    Copyright (c) 2000, 2020, 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> 
    
  • 首先选中mysql数据库
    mysql> show databases;
    +--------------------+
    | Database           |
    +--------------------+
    | information_schema |
    | mysql              |
    | performance_schema |
    +--------------------+
    4 rows in set (0.00 sec)
    
    mysql> use mysql
    Database changed
    mysql> 
    
  • 执行sql修改密码
    mysql> update user set password=password("你的密码") where user="root";
    Query OK, 0 rows affected (0.00 sec)
    Rows matched: 4  Changed: 0  Warnings: 0
    
  • 修改host匹配,允许所有客户端ip地址访问链接数据库
    mysql> update user set host = '%' where user = 'root' and host = '*';
    	Query OK, 0 rows affected (0.00 sec)
    	Rows matched: 0  Changed: 0  Warnings: 0
    	
    	mysql> select user,host from user where user = 'root';
    	+------+-------------------------+
    	| user | host                    |
    	+------+-------------------------+
    	| root | %                       |
    	| root | 127.0.0.1               |
    	| root | ::1                     |
    	| root | izuf6el9zfn76jfyw0aoi9z |
    	| root | localhost               |
    	+------+-------------------------+
    	5 rows in set (0.00 sec)
    
  • 如果在修改上述host权限时,爆出如下错误
    #这说明已经修改为了%,无需再修改,直接进行下一步即可
    ERROR 1062 (23000): Duplicate entry '%-root' for key 'PRIMARY' 
    
  • 修改成功后,刷新,退出(如果在执行该步骤的时候,出现其他错误,再次执行flush privileges;刷新即可)
    mysql> flush privileges;
    Query OK, 0 rows affected (0.00 sec)
    
    mysql> quit
    Bye
    
  • 到这里root账户就已经重置成新的密码了

2.4、重新编辑my.cnf去除刚才的跳过密码校验段

  • 如果找不到my.cnf,在Linux环境下有快捷命令

    [root@iZuf6el9zfn76jfyw0aoi9Z etc]# whereis my
    my: /etc/my.cnf
    
  • 即可定位到my.cnf在/etc目录下,然后重新vim编辑去除skip-grant-tables

  • 再次重新启动MySQL

    /etc/init.d/mysql restart
    #有的可能是需要是mysqld	
    /etc/init.d/mysqld restart	
    
  • 再次链接数据库

    [root@iZuf6el9zfn76jfyw0aoi9Z mysql]# mysql -uroot -p
    Enter password: #此处输入你的密码
    Welcome to the MySQL monitor.  Commands end with ; or \g.
    Your MySQL connection id is 1
    Server version: 5.6.50-log Source distribution
    
    Copyright (c) 2000, 2020, 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> show databases;
    +--------------------+
    | Database           |
    +--------------------+
    | information_schema |
    | mysql              |
    | performance_schema |
    +--------------------+
    4 rows in set (0.00 sec)
    
  • 妥了,大功告成!!!

你可能感兴趣的:(数据库,踩坑,mysql,解决数据库忘记密码重连)