MySQL5.6初始化后,root用户无法免密登录

在搭建一套测试环境时,发现MySQL5.6初始化完成后,root用户无法免密登录,被拒绝访问数据库。故障现象:

--初始化命令:

/usr/local/mysql/scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql/data

 

--启动MySQL实例:

[root@mysqldb01 mysql]# systemctl start mysqld

[root@mysqldb01 mysql]#

[root@mysqldb01 mysql]# ps -ef |grep mysql

mysql 18360 1 0 06:30 ? 00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --defaults-file=/etc/my.cnf

mysql 19159 18360 4 06:30 ? 00:00:00 /usr/local/mysql/bin/mysqld --defaults-file=/etc/my.cnf --basedir=/usr/local/mysql --datadir=/data/mysql/data --plugin-dir=/usr/local/mysql/lib/plugin --log-error=/data/mysql/mysqld.error.log --pid-file=/data/mysql/mysqld.pid --socket=/data/mysql/mysql.sock --port=3306

root 19182 6662 0 06:30 pts/2 00:00:00 grep --color=auto mysql

 

--登录失败:

[root@mysqldb01 mysql]# mysql

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

[root@mysqldb01 mysql]#

这是非常不正常的,理论上5.6版本,实例初始化后,可以免密登录。

 

处理过程:

1、既然不输入密码不让登录,那我们可以尝试给root@localhost设置一个密码,然后输入密码登录。将MySQL启动到单用户模式。

[root@mysqldb01 mysql]# /usr/local/mysql/bin/mysqld_safe --defaults-file=/etc/my.cnf --skip-grant-tables --skip-networking &

[1] 20922

[root@mysqldb01 mysql]# ps -ef |grep mysql

root 20922 6662 0 06:32 pts/2 00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --defaults-file=/etc/my.cnf --skip-grant-tables --skip-networking

mysql 21748 20922 0 06:32 pts/2 00:00:00 /usr/local/mysql/bin/mysqld --defaults-file=/etc/my.cnf --basedir=/usr/local/mysql --datadir=/data/mysql/data --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --skip-grant-tables --skip-networking --log-error=/data/mysql/mysqld.error.log --pid-file=/data/mysql/mysqld.pid --socket=/data/mysql/mysql.sock --port=3306

root 21775 6662 0 06:34 pts/2 00:00:00 grep --color=auto mysql

[root@mysqldb01 mysql]#

单用户模式启动成功。

2、登录MySQL实例,给root@localhost设置一个密码。

[root@mysqldb01 mysql]# mysql

Welcome to the MySQL monitor. Commands end with ; or g.

Your MySQL connection id is 1

Server version: 5.6.50-log MySQL Community Server (GPL)

 

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.

 

root@localhost [(none)]>select user,host from mysql.user;

Empty set (0.00 sec)

 

root@localhost [(none)]>

是不是很神奇,竟然什么帐号也没有。

3、事出反常必有妖。此时需要仔细检查初始化过程中的所有error日志,终于在MySQL的日志文件中发现了这段错误提示。

ERROR: 1787 When @@GLOBAL.ENFORCE_GTID_CONSISTENCY = 1, the statements CREATE TEMPORARY TABLE and DROP TEMPORARY TABLE can be executed in a non-transactional context only, and require that AUTOCOMMIT = 1. These statements are also not allowed in a function or trigger because functions and triggers are also considered to be multi-statement transactions.

2021-08-24 06:28:53 18302 [ERROR] Aborting

从这个错误日志提示可以看出,当设置了ENFORCE_GTID_CONSISTENCY = 1时,一些临时表的创建和删除语句想成功执行,则要求AUTOCOMMIT参数必须开启。

4、检查我的my.cnf配置文件。

[root@mysqldb01 mysql]# cat /etc/my.cnf

[mysqld]

略......

gtid_mode = ON

enforce_gtid_consistency = 1

binlog_checksum = NONE

log_bin_trust_function_creators = ON

 

略......

#-- Trasaction parameters --#

transaction_isolation = READ-COMMITTED

autocommit = off

 

[mysql]

socket = /data/mysql/mysql.sock

prompt = "\u@\h [\d]>"

default_character_set = utf8mb4

[root@mysqldb01 mysql]#

从配置文件可以看出,我当前的环境正是开启了GTID,但同时关闭了autocommit。

5、怀疑正是由于我的这个配置,导致了MySQL实例初始化不完成成功,一些临时性的操作语句未执行。

6、修改/etc/my.cnf配置文件,删除autocommit = off这行,(autocommit参数的默认值为 on),清空环境,重新初始化后,一切正常。终于可以免密码登录数据库。

原文地址:https://www.cnblogs.com/missyou-shiyh/p/15178537.html