mysql 5.7 ERROR 1045 (28000): Access denied for user 'root'@'localhost'

时间:2022-01-24 03:13:22

mysql 安装了最新版,但是初始化登陆不了,网上找了很多资料,但是都没用,后来在网上看到说是新版本的mysql初始化安装会随机生成一个root密码,如果你不知道这个密码,或者没有留意到这个密码,则会出现我这样的情况,安装了登不了,总是报错:

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

或者

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


mysql 版本:

[root@master make]# mysql -V

mysql  Ver 14.14 Distrib 5.7.14, for Linux (x86_64) using  EditLine wrapper


[root@master make]# mysql -uroot
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)

[root@master make]# mysql -uroot -p
Enter password: [直接回车]
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)

以上咋都登陆不了。


下面就是找这个随机生成的密码:

[root@master make]# cat /etc/my.cnf 

# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html
[client]
[mysqld]
#skip-grant-tables
#
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
#
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
#
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock


# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0


log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid


注意到这一行:

log-error=/var/log/mysqld.log


[root@master make]# head -100 /var/log/mysqld.log 
2016-08-16T15:26:28.672682Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2016-08-16T15:26:30.315620Z 0 [Warning] InnoDB: New log files created, LSN=45790
2016-08-16T15:26:30.613729Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2016-08-16T15:26:30.766670Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: ce9fd1a4-63c5-11e6-91e7-14dae97164e1.
2016-08-16T15:26:30.791134Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2016-08-16T15:26:30.791808Z 1 [Note] A temporary password is generated for root@localhost: q+673+g>+=lW
2016-08-16T15:26:44.292088Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2016-08-16T15:26:44.294757Z 0 [Note] /usr/sbin/mysqld (mysqld 5.7.14) starting as process 16748 ...
2016-08-16T15:26:44.298157Z 0 [Note] InnoDB: PUNCH HOLE support available
2016-08-16T15:26:44.298195Z 0 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
2016-08-16T15:26:44.298202Z 0 [Note] InnoDB: Uses event mutexes
2016-08-16T15:26:44.298207Z 0 [Note] InnoDB: GCC builtin __atomic_thread_fence() is used for memory barrier
2016-08-16T15:26:44.298211Z 0 [Note] InnoDB: Compressed tables use zlib 1.2.3
2016-08-16T15:26:44.298215Z 0 [Note] InnoDB: Using Linux native AIO
2016-08-16T15:26:44.298434Z 0 [Note] InnoDB: Number of pools: 1
2016-08-16T15:26:44.298517Z 0 [Note] InnoDB: Using CPU crc32 instructions
2016-08-16T15:26:44.300427Z 0 [Note] InnoDB: Initializing buffer pool, total size = 128M, instances = 1, chunk size = 128M
2016-08-16T15:26:44.310554Z 0 [Note] InnoDB: Completed initialization of buffer pool
2016-08-16T15:26:44.313035Z 0 [Note] InnoDB: If the mysqld execution user is authorized, page cleaner thread priority can be changed. See the man page of setpriority().
2016-08-16T15:26:44.326783Z 0 [Note] InnoDB: Highest supported file format is Barracuda.
2016-08-16T15:26:44.432774Z 0 [Note] InnoDB: Creating shared tablespace for temporary tables
2016-08-16T15:26:44.432989Z 0 [Note] InnoDB: Setting file './ibtmp1' size to 12 MB. Physically writing the file full; Please wait ...
2016-08-16T15:26:44.916314Z 0 [Note] InnoDB: File './ibtmp1' size is now 12 MB.
2016-08-16T15:26:44.917405Z 0 [Note] InnoDB: 96 redo rollback segment(s) found. 96 redo rollback segment(s) are active.
2016-08-16T15:26:44.917423Z 0 [Note] InnoDB: 32 non-redo rollback segment(s) are active.
2016-08-16T15:26:44.917756Z 0 [Note] InnoDB: Waiting for purge to start
2016-08-16T15:26:44.967968Z 0 [Note] InnoDB: 5.7.14 started; log sequence number 2525737
2016-08-16T15:26:44.968208Z 0 [Note] InnoDB: Loading buffer pool(s) from /var/lib/mysql/ib_buffer_pool
2016-08-16T15:26:44.968362Z 0 [Note] Plugin 'FEDERATED' is disabled.
2016-08-16T15:26:44.970151Z 0 [Note] InnoDB: Buffer pool(s) load completed at 160816 23:26:44
2016-08-16T15:26:44.973702Z 0 [Note] Found ca.pem, server-cert.pem and server-key.pem in data directory. Trying to enable SSL support using them.
2016-08-16T15:26:44.973914Z 0 [Warning] CA certificate ca.pem is self signed.
2016-08-16T15:26:44.975822Z 0 [Note] Server hostname (bind-address): '*'; port: 3306
2016-08-16T15:26:44.975922Z 0 [Note] IPv6 is available.
2016-08-16T15:26:44.975934Z 0 [Note]   - '::' resolves to '::';
2016-08-16T15:26:44.975957Z 0 [Note] Server socket created on IP: '::'.
2016-08-16T15:26:44.998808Z 0 [Note] Event Scheduler: Loaded 0 events
2016-08-16T15:26:44.998999Z 0 [Note] /usr/sbin/mysqld: ready for connections.
Version: '5.7.14'  socket: '/var/lib/mysql/mysql.sock'  port: 3306  MySQL Community Server (GPL)
2016-08-16T15:26:54.409735Z 2 [Note] Access denied for user 'root'@'localhost' (using password: NO)
2016-08-16T15:27:01.473344Z 3 [Note] Access denied for user 'root'@'localhost' (using password: NO)
2016-08-16T15:27:12.888399Z 4 [Note] Access denied for user 'root'@'localhost' (using password: YES)
2016-08-16T15:27:28.039559Z 5 [Note] Access denied for user 'root'@'localhost' (using password: YES)


其中能够找到这样一行:

2016-08-16T15:26:30.791808Z 1 [Note] A temporary password is generated for root@localhost: q+673+g>+=lW

q+673+g>+=lW  这个就是随机生成的密码啊。

下面测试:

[root@master make]# mysql -uroot -p'q+673+g>+=lW'
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 16
Server version: 5.7.14


Copyright (c) 2000, 2016, 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;
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.

这里是因为密码不符合规则,这个与validate_password_policy的值有关。

validate_password_policy有以下取值:

Policy Tests Performed
0 or LOW Length
1 or MEDIUM Length; numeric, lowercase/uppercase, and special characters
2 or STRONG Length; numeric, lowercase/uppercase, and special characters; dictionary file

默认是1,即MEDIUM,所以刚开始设置的密码必须符合长度,且必须含有数字,小写或大写字母,特殊字符。

有时候,只是为了自己测试,不想密码设置得那么复杂,譬如说,我只想设置root的密码为123456。

必须修改两个全局参数:

首先,修改validate_password_policy参数的值

mysql> select @@validate_password_length;
+----------------------------+
| @@validate_password_length |
+----------------------------+
|                          8 |
+----------------------------+
1 row in set (0.03 sec)

mysql> alter user 'root'@'localhost' identified by 'root123456';
Query OK, 0 rows affected (0.00 sec)


至此问题已经解决。