天天看点

Idea 连接数据库出错Connection to xxx@localhost failed. [08001] Could not create connection to database问题原因解决其它解决办法

问题

使用Idea自带的Database工具连接MySQL数据库出错:Connection to aplanmis@localhost failed.

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

Idea 连接数据库出错Connection to xxx@localhost failed. [08001] Could not create connection to database问题原因解决其它解决办法

原因

系统默认的时区与IDEA连接数据库的时区 (MySQL默认时区为UTC) 不一致,

按照格林威治的时间,中国属于东八区时间,领先UTC八个小时,所以说存在8小时的时差。

解决

在URL后面加上 ?serverTimezone=GMT 即可

Idea 连接数据库出错Connection to xxx@localhost failed. [08001] Could not create connection to database问题原因解决其它解决办法

其它解决办法

上面的解决办法每次连接url都得加上?serverTimezone=GMT,不太方便,这里还查到了一些其它的解决办法,一并列出。

  • 命令行设置time_zone

    在mysql的命令模式下,输入:

set global time_zone='+8:00'      
  • 配置文件设置time_zone

    修改配置文件,在[mysqld]下添加或修改一行:

default-time-zone='+08:00'      

重启数据库。

  • Idea Database管理工具中,修改 Advanced 中 serverTimezone 的属性值为 Asia/Shanghai