欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

[08001] Could not create connection to database server. Attempted reconnect 3 times. Giving up.

程序员文章站 2022-03-08 15:56:11
...

今天用IDEA链接Mysql时,出现了问题:如图所示:

​​​​[08001] Could not create connection to database server. Attempted reconnect 3 times. Giving up.
错误信息:

Connection to aaa@qq.com failed.

[08001] Could not create connection to database server. Attempted reconnect 3 times. Giving up.

先放上我的解决方案:

解决方案

1、查看自己的serverTimezone大小写是否正确

s是小写。

2、时区的选择

UTC代表的是全球标准时间 ,但是我们使用的时间是北京时区也就是东八区,领先UTC八个小时。

1. 常用设置
//北京时间东八区即GMT+8
serverTimezone=GMT%2B8 
//或者使用上海时间
serverTimezone=Asia/Shanghai

2. 设置为UTC

上边说过UTC代表的是全球标准时间,如果设置为UTC会出现什么问题:

我们在Java中给user表添加一条数据,

[08001] Could not create connection to database server. Attempted reconnect 3 times. Giving up.

gmt_create字段是通过user.setGmtCreate(new Date());赋值的。

gmt_modify字段使用默认值CURRENT_TIMESTAMP

也就是说,在Java中赋值的时间和我们在数据库中查到的时间不一致。

gmt_create应该是13点,但是数据库中却是5点,也就相差8个小时。

[08001] Could not create connection to database server. Attempted reconnect 3 times. Giving up.

[08001] Could not create connection to database server. Attempted reconnect 3 times. Giving up.

插入数据(指定gmt_create的值)

结论: 若配置serverTimezone=UTC,则会造成两端时间相差8个小时。