解决Failed to connect to MySQL server :1184 (08S01): Aborted connection 2338416 to db: ‘unconnected‘

程序连库报错如下:

Failed to connect to MySQL server :1184 (08S01): Aborted connection 2341036 to db: 'unconnected' user: 'mm' host: '127.0.0.1' (init_connect command failed)

但是用mm用户直接登录数据库是没有问题的。
查看init_connect参数:

mysql> show variables like 'init_connect';
+---------------+-------------------+
| Variable_name | Value             |
+---------------+-------------------+
| init_connect  | setnames"utf8mb4" |
+---------------+-------------------+
1 row in set (0.00 sec)

可以看到init_connect的值 是 setnames"utf8mb4",这个内容是不能在mysql命令行里直接执行的(无空格问题),修改成 set names “utf8mb4” 之后,问题解决。

#用root用户连上数据库执行以下命令
mysql> set names "utf8mb4";
Query OK, 0 rows affected (0.00 sec)

mysql> set global init_connect='set names "utf8mb4"';
Query OK, 0 rows affected (0.00 sec)

mysql> show variables like 'init_connect';
+---------------+---------------------+
| Variable_name | Value               |
+---------------+---------------------+
| init_connect  | set names "utf8mb4" |
+---------------+---------------------+
1 row in set (0.00 sec)

程序再连数据库就成功了。

你可能感兴趣的:(数据库)