windows server上 gorm 连接DB提示 the system cannot find the path specified

友情提示:window server上很容易就踩坑

1. 背景

  • 语言:golang
  • ORM: gorm
  • 运行环境: Windows server

1. 起因

因为外在因素,不得不在Windows server上跑程序,然后就直接在在初始化DB连接时失败了,返回的error是the system cannot find the path specified。这里其实是有点奇怪的,这提示似乎指的是本地文件路径问题,但DB配置是已经读取成功的,连接也是连接远端DB,并没有读取本地文件操作?

2. 查

  1. 顺着链路往下查,在gorm.open报错,查到是parseDsn失败,但同样的程序和dsn在本地电脑运行并没有问题。
  2. 继续往下,看到parseDsn里的parseDSNParams里其中有个地方调用time.LoadLocation函数。
  3. 知道了。LoadLocation 有个问题,它依赖于 IANA Time Zone Database 这个数据库,一般linux系统都带了,但是windows系统就没带。没有 tzdata 就会从$GOROOT/中找。但很明显,Windows server并没有装golang,所以肯定会找不到指定路径,因此会提示这个看似搭不上边的错误。(Linux就不用管)

3.解决

把tzdata文件放到程序目录,我直接指定ZONEINFO环境变量就行了,os.Setenv("ZONEINFO", '.\zoneinfo.zip')。 另外Windows我一般都用filepath.Join去处理一遍路径,免得斜杠反斜杠烦恼。

你可能感兴趣的:(windows server上 gorm 连接DB提示 the system cannot find the path specified)