打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
jdbc连接mysq之serverTimezone设定

mysql返回的时间总是有问题,比实际时间要早8小时。检查是jdbc连接的url中配置的时区有问题,原先是

jdbc:mysql://172.xx.xx.xx:3306/test?useUnicode=true&characterEncoding=gbk&useSSL=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC

尝试了GMT、GMT8、UTC+8,UTC0800等可能的参数均报错。后来在mysql文档中发现可用的时区都在/usr/share/zoneinfo目录下,ll下发现可用的时区信息如下
  1. lewis@lewis-dzwww:/usr/share/zoneinfo$ ll
  2. 总用量 308
  3. drwxr-xr-x 21 root root 4096 5月 27 12:54 ./
  4. drwxr-xr-x 334 root root 12288 6月 21 15:11 ../
  5. drwxr-xr-x 2 root root 4096 5月 27 12:56 Africa/
  6. drwxr-xr-x 6 root root 4096 5月 27 12:56 America/
  7. drwxr-xr-x 2 root root 4096 5月 27 12:56 Antarctica/
  8. drwxr-xr-x 2 root root 4096 5月 27 12:56 Arctic/
  9. drwxr-xr-x 2 root root 4096 5月 27 12:56 Asia/
  10. drwxr-xr-x 2 root root 4096 5月 27 12:56 Atlantic/
  11. drwxr-xr-x 2 root root 4096 5月 27 12:56 Australia/
  12. drwxr-xr-x 2 root root 4096 5月 27 12:56 Brazil/
  13. drwxr-xr-x 2 root root 4096 5月 27 12:56 Canada/
  14. -rw-r--r-- 1 root root 2102 4月 21 02:09 CET
  15. drwxr-xr-x 2 root root 4096 5月 27 12:56 Chile/
  16. -rw-r--r-- 1 root root 2294 4月 21 02:09 CST6CDT
  17. -rw-r--r-- 1 root root 2437 4月 21 02:09 Cuba
  18. -rw-r--r-- 1 root root 1876 4月 21 02:09 EET
  19. -rw-r--r-- 1 root root 1972 4月 21 02:09 Egypt
  20. -rw-r--r-- 1 root root 3559 4月 21 02:09 Eire
  21. -rw-r--r-- 1 root root 127 4月 21 02:09 EST
  22. -rw-r--r-- 1 root root 2294 4月 21 02:09 EST5EDT
  23. drwxr-xr-x 2 root root 4096 5月 27 12:56 Etc/
  24. drwxr-xr-x 2 root root 4096 5月 27 12:56 Europe/
  25. -rw-r--r-- 1 root root 264 4月 21 02:09 Factory
  26. -rw-r--r-- 1 root root 3687 4月 21 02:09 GB
  27. lrwxrwxrwx 1 root root 2 5月 27 09:07 GB-Eire -> GB
  28. -rw-r--r-- 1 root root 127 4月 21 02:09 GMT
  29. lrwxrwxrwx 1 root root 3 5月 27 09:07 GMT0 -> GMT
  30. lrwxrwxrwx 1 root root 3 5月 27 09:07 GMT-0 -> GMT
  31. lrwxrwxrwx 1 root root 3 5月 27 09:07 GMT+0 -> GMT
  32. lrwxrwxrwx 1 root root 3 5月 27 09:07 Greenwich -> GMT
  33. -rw-r--r-- 1 root root 1189 4月 21 02:09 Hongkong
  34. -rw-r--r-- 1 root root 128 4月 21 02:09 HST
  35. -rw-r--r-- 1 root root 1190 4月 21 02:09 Iceland
  36. drwxr-xr-x 2 root root 4096 5月 27 12:56 Indian/
  37. -rw-r--r-- 1 root root 1678 4月 21 02:09 Iran
  38. -rw-r--r-- 1 root root 4475 2月 24 17:01 iso3166.tab
  39. -rw-r--r-- 1 root root 2265 4月 21 02:09 Israel
  40. -rw-r--r-- 1 root root 507 4月 21 02:09 Jamaica
  41. -rw-r--r-- 1 root root 355 4月 21 02:09 Japan
  42. -rw-r--r-- 1 root root 237 4月 21 02:09 Kwajalein
  43. -rw-r--r-- 1 root root 10384 2月 22 23:58 leap-seconds.list
  44. -rw-r--r-- 1 root root 655 4月 21 02:09 Libya
  45. lrwxrwxrwx 1 root root 14 5月 27 09:07 localtime -> /etc/localtime
  46. -rw-r--r-- 1 root root 2102 4月 21 02:09 MET
  47. drwxr-xr-x 2 root root 4096 5月 27 12:56 Mexico/
  48. -rw-r--r-- 1 root root 127 4月 21 02:09 MST
  49. -rw-r--r-- 1 root root 2294 4月 21 02:09 MST7MDT
  50. -rw-r--r-- 1 root root 2453 4月 21 02:09 Navajo
  51. -rw-r--r-- 1 root root 2460 4月 21 02:09 NZ
  52. -rw-r--r-- 1 root root 2057 4月 21 02:09 NZ-CHAT
  53. drwxr-xr-x 2 root root 4096 5月 27 12:56 Pacific/
  54. -rw-r--r-- 1 root root 2705 4月 21 02:09 Poland
  55. -rw-r--r-- 1 root root 3453 4月 21 02:09 Portugal
  56. drwxr-xr-x 19 root root 4096 5月 27 12:56 posix/
  57. -rw-r--r-- 1 root root 3545 4月 21 02:09 posixrules
  58. -rw-r--r-- 1 root root 414 4月 21 02:09 PRC
  59. -rw-r--r-- 1 root root 2294 4月 21 02:09 PST8PDT
  60. drwxr-xr-x 19 root root 4096 5月 27 12:56 right/
  61. -rw-r--r-- 1 root root 800 4月 21 02:09 ROC
  62. -rw-r--r-- 1 root root 571 4月 21 02:09 ROK
  63. -rw-r--r-- 1 root root 428 4月 21 02:09 Singapore
  64. drwxr-xr-x 2 root root 4096 5月 27 12:56 SystemV/
  65. -rw-r--r-- 1 root root 2747 4月 21 02:09 Turkey
  66. -rw-r--r-- 1 root root 127 4月 21 02:09 UCT
  67. lrwxrwxrwx 1 root root 4 5月 27 09:07 Universal -> Zulu
  68. drwxr-xr-x 2 root root 4096 5月 27 12:56 US/
  69. lrwxrwxrwx 1 root root 4 5月 27 09:07 UTC -> Zulu
  70. -rw-r--r-- 1 root root 1873 4月 21 02:09 WET
  71. -rw-r--r-- 1 root root 1528 4月 21 02:09 W-SU
  72. -rw-r--r-- 1 root root 17533 4月 13 16:01 zone1970.tab
  73. -rw-r--r-- 1 root root 19014 4月 13 16:01 zone.tab
  74. -rw-r--r-- 1 root root 127 4月 21 02:09 Zulu

为解决此问题,可以选择东8区的Hongkong、Asia/Shanghai或者Asia/Hongkong作为参数,修改后url为

jdbc:mysql://172.xx.xx.xx:3306/test?useUnicode=true&characterEncoding=gbk&useSSL=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=Hongkong

时间返回就没问题了                                      
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
quickStart是遇到问题
Linux 下 Vi 配置文件 .vimrc 文件
Linux进程的基础 - 海阔&任我行的日志 - 网易博客
6.3 init进程
linux中/etc/rc.d/rc与/etc/rc.d/init.d的关系介绍
Linux删除乱码文件或者目录
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服