欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 汽车 > 维修 > golang中连接达梦数据库使用域名来代替IP时会出现解析问题

golang中连接达梦数据库使用域名来代替IP时会出现解析问题

2025/9/22 4:16:25 来源:https://blog.csdn.net/dl425134845/article/details/142213798  浏览:    关键词:golang中连接达梦数据库使用域名来代替IP时会出现解析问题

中间件使用gorm

driverName := "dm"
dataSourceName := fmt.Sprintf("dm://%s:%s@%s:%s/SYSDBA?charset=utf8&parseTime=true", config.Database.Username, config.Database.Password, config.Database.Address, config.Database.Port)config.Database.Address   这里使用了比如"dm8"这样的字符串,而不是ip

执行程序会遇到Error 6001: Communication  error dial address: localhost:5236的报错

这个是因为dm的驱动包中有问题,我这里使用的是V1.0.0版本的驱动
在n.go文件中,
func (c *DmConnector) mergeConfigs(dsn string) error函数中有这样的逻辑

host, port, err := net.SplitHostPort(host)
if err != nil || net.ParseIP(host) == nil {c.host = hostDef
} else {c.host = host
}

这里可以看到,会对域名进行格式校验,如果不是合法IP,则将其替换成了hostDef,也就是localhost。
吐槽一下,两个host明明不是同一个东西,为啥不能使用2个名称呢

所以将其修改
 

if err != nil {c.host = hostDef
} else if net.ParseIP(host) == nil {if ip, err := net.LookupHost(host); err != nil {c.host = hostDef} else {if len(ip) == 1 {c.host = ip[0]} else {c.host = hostDef}}
} else {c.host = host
}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com