douduan7295 2016-03-05 12:48
浏览 84

无法在Go中连接到远程MySql

I need to connect to a remote MySql server using Go. I'm using following code to connect to MySql via gorm.

 type DBController struct {
    DB gorm.DB
}

func (dc *DBController) InitDB() {
    var err error

  host := v.GetString("db.mysql.host")
  port := v.GetString("db.mysql.port")

  user := v.GetString("db.mysql.user")
  pass := v.GetString("db.mysql.pass")

  db := v.GetString("db.mysql.db")

  //user:password@tcp(localhost:5555)/dbname
  conn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s", user, pass, host, port, db)
  //conn := v.GetString($user+":"$pass+"@tcp("+$host+":"+$port+")/"+$db)
  log.Debug(conn)

    dc.DB, err = gorm.Open("mysql", conn)
    if err != nil {
        log.Fatalf("Error when connect database, the error is '%v'", err)
    }
    dc.DB.LogMode(true)
}

func (dc *DBController) GetDB() gorm.DB {
  return dc.DB
}

When I run the Go server I'm getting following errors

No configuration file loaded - using defaults
:@tcp(:)/
Error when connect database, the error is 'dial tcp: unknown port tcp/'
exit status 1

How can fix this error?

  • 写回答

1条回答 默认 最新

  • doumeikuan6834 2016-03-15 00:20
    关注

    I'm guessing "v" is your config variable… Which contains no data, this is not a problem with Gorm, it's because dc.Open() is not receiving the config values.

    If you try to run it with static data, it should work.

    评论

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作