GoLang, 原创,

解决golang xorm连接数据库 default addr for network localhost:3306 unknown

仅解决default addr for network unknown的错误,直接看代码

       var Orm *xorm.Engine
       var err error
       connection := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=%s",
		mysqlConfig["MysqlUser"].(string),
		mysqlConfig["MysqlPass"].(string),
		mysqlConfig["MysqlHost"].(string),
		mysqlConfig["MysqlPort"].(int),
		mysqlConfig["MysqlDb"].(string),
		mysqlConfig["MysqlCharSet"].(string),
	)
	fmt.Println(connection)
	Orm, err = xorm.NewEngine("mysql", connection)
	if err != nil {
		fmt.Println(err.Error())
	}
	Orm.ShowSQL(true)
	iris.RegisterOnInterrupt(func() {
		Orm.Close()
	})
即使用的字符串应该是
dbuser:dbpassword@tcp(dbhost:dbport)/databasename?charset=utf8

变量参考如下

  #Database
  #Mysql database
  MySql:
    MysqlUser: root
    MysqlPass: root
    MysqlHost: localhost
    MysqlPort: 3306
    MysqlDb: mydatabase
    MysqlCharSet: utf8mb4

以上解决

(782)

Related Post