当前位置:   article > 正文

golang适配GBase8s(南大通用)数据库

golang适配GBase8s(南大通用)数据库

本文主要讲解golang通过odbc连接GBase8s

环境配置

unixODBC安装

yum -y install unixODBC unixODBC-devel

配置 gbase8s-odbc-driver

wget https://gbasedbt.com/dl/odbc/GBase8s_3.0.0_1-Linux64-ODBC-Driver.tar.gz --no-check-certificate

tar -zxf GBase8s_3.0.0_1-Linux64-ODBC-Driver.tar.gz

配置 sqlhosts

gbase01  onsoctcp        ip  9088    g=db_group
cm_update       group   -       -       i=2,c=0
w1      onsoctcp        ip  18888   g=cm_update
cm_read         group   -       -       i=3,c=0
r1      onsoctcp        ip  19999   g=cm_read
  • 1
  • 2
  • 3
  • 4
  • 5

配置环境变量 (使用 root) vim /etc/profile

export GBASEDBTDIR=/home/gbase8s-odbc-driver
export GBASEDBTSERVER=gbase01
export PATH= G B A S E D B T D I R / b i n : GBASEDBTDIR/bin: GBASEDBTDIR/bin:PATH
export GBASEDBTSQLHOSTS= G B A S E D B T D I R / e t c / s q l h o s t s e x p o r t L D L I B R A R Y P A T H = GBASEDBTDIR/etc/sqlhosts export LD_LIBRARY_PATH= GBASEDBTDIR/etc/sqlhostsexportLDLIBRARYPATH=GBASEDBTDIR/lib: G B A S E D B T D I R / l i b / e s q l : GBASEDBTDIR/lib/esql: GBASEDBTDIR/lib/esql:GBASEDBTDIR/lib/cli:$LD_LIBRARY_PATH
export ODBCINI=/home/gbase8s-odbc-driver/odbc.ini
执行source /etc/profile生效

配置 驱动 (使用 root) vim /etc/odbcinst.ini

[com.gbasedbt.jdbc.Driver]
Driver=/home/gbase8s-odbc-driver/lib/cli/iclit09b.so
Setup=/home/gbase8s-odbc-driver/lib/cli/iclit09b.so
APILevel=1
ConnectFunctions=YYY
DriverODBCVer=03.51
FileUsage=0
SQLLevel=1
smProcessPerConnect=Y

配置 odbc.ini vim /home/linkdood/gbase8s-odbc-driver/odbc.ini

[ODBC]
UNICODE=UCS-2

测试驱动连接

isql -v -k “DRIVER={com.gbasedbt.jdbc.Driver};DB=testdataserver;UID=gbasedbt;pwd=GBase123;Server=gbase01;CLIENT_LOCALE=zh_CN.57372;DB_LOCALE=zh_CN.57372;”

golang通过odbc连接国产数据库

func init() {
	file, err := os.Open("config.xml")
	if err != nil {
		Log.Info("error:%v", err)
		return
	}
	defer file.Close()
	config, err := ioutil.ReadAll(file)
	if err != nil {
		Log.Info("error:%v", err)
		return
	}
	configValue := SConfig{}
	err = xml.Unmarshal(config, &configValue)
	if err != nil {
		Log.Error("error:%v", err)
		return
	}
	strKey := configValue.Server.Aid + configValue.Server.Elogo
	var strPassword string
	var strUsername string
	strPassword = configValue.DataBase.Password
	strUsername = configValue.DataBase.User
	fmt.Printf("strPassword:%v\n", strPassword)
	fmt.Printf("strUsername:%v\n", strUsername)
	ServerPort = configValue.Server.Port
	var dbDSN = ""      //dataSourceName
	var driverName = "" //驱动名
	var DbServer = configValue.DataBase.Host + ":" + configValue.DataBase.Port
	if configValue.DataBase.DbType == "mysql" {
		fmt.Printf("mysql database\n")
		driverName = "mysql"
		dbDSN = fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=%s", strUsername, strPassword, configValue.DataBase.Host, configValue.DataBase.Port, configValue.DataBase.DBName, configValue.DataBase.Charset)
	} else if configValue.DataBase.DbType == "shentong" {
		fmt.Printf("shentongsql database\n")
		/*driverName = "odbc"
		dbDSN = fmt.Sprintf("driver={%s};server=%s;port=%s;database=%s;uid=%s;pwd=%s;charset=%s", configValue.DataBase.Driver, configValue.DataBase.Host, configValue.DataBase.Port, configValue.DataBase.DBName, strUsername, strPassword, configValue.DataBase.Charset)*/
		driverName = "aci"
		dbDSN = fmt.Sprintf("%s/%s@%s:%s/%s", strUsername, strPassword, configValue.DataBase.Host, configValue.DataBase.Port, configValue.DataBase.DBName)
	} else if configValue.DataBase.DbType == "dameng" {
		strPassword = CheckAndModifyString(strPassword)
		fmt.Printf("dmsql database\n")
		driverName = "dm"
		//dbDSN = fmt.Sprintf("driver={%s};server=%s;database=%s;uid=%s;pwd=%s;charset=%s", configValue.DataBase.Driver, DbServer, configValue.DataBase.DBName, strUsername, strPassword, configValue.DataBase.Charset)
		dbDSN = fmt.Sprintf("%s://%s:%s@%s?schema=%s", driverName, strUsername, strPassword, DbServer, configValue.DataBase.DBName)
	} else if configValue.DataBase.DbType == "kingbase" {
		fmt.Printf("kingbase database\n")
		driverName = "odbc"
		dbDSN = fmt.Sprintf("driver={%s};server=%s;port=%s;database=%s;uid=%s;pwd=%s;charset=%s", configValue.DataBase.Driver, configValue.DataBase.Host, configValue.DataBase.Port, configValue.DataBase.DBName, strUsername, strPassword, configValue.DataBase.Charset)
	} else if configValue.DataBase.DbType == "GBase8s" {
		fmt.Printf("GBase8s database\n")
		driverName = "odbc"
		dbDSN = fmt.Sprintf("driver={%s};server=%s;port=%s;database=%s;uid=%s;pwd=%s;charset=%s", configValue.DataBase.Driver, configValue.DataBase.Host, configValue.DataBase.Port, configValue.DataBase.DBName, strUsername, strPassword, configValue.DataBase.Charset)
	}
	dbType = configValue.DataBase.DbType
	// 打开连接失败
	MysqlDb, MysqlDbErr = sql.Open(driverName, dbDSN)
	//defer MysqlDb.Close();
	if MysqlDbErr != nil {
		panic("数据源配置不正确: " + MysqlDbErr.Error())
	}
	// 最大连接数
	MysqlDb.SetMaxOpenConns(100)
	// 闲置连接数
	MysqlDb.SetMaxIdleConns(20)
	// 最大连接周期
	MysqlDb.SetConnMaxLifetime(100 * time.Second)
	if MysqlDbErr = MysqlDb.Ping(); nil != MysqlDbErr {
		panic("数据库链接失败: " + MysqlDbErr.Error())
	}
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72

Go测试程序运行测试

go run . -dsn="DRIVER={com.gbasedbt.jdbc.Driver};DB=testdataserver;UID=gbasedbt;pwd=GBase123;Server=gbase01;CLIENT_LOCALE=zh_CN.57372;DB_LOCALE=zh_CN.57372;"
  • 1
./gbase-test -dsn="DRIVER={com.gbasedbt.jdbc.Driver};DB=testdataserver;UID=gbasedbt;pwd=GBase123;Server=gbase01;CLIENT_LOCALE=zh_CN.57372;DB_LOCALE=zh_CN.57372;"
  • 1

问题&&解决方式

  • 异常:SQLDriverConnect: {01000} [unixODBC][Driver Manager]Can not open lib "com.gbasedbt.jdbc.Driver" : file not found

    • 解决方式

    • 使用

      ldd gbase-test
      
      • 1

      查看so依赖

      linux-vdso.so.1 =>  (0x00007fff083d1000)
      libodbc.so.2 => /data/libodbc.so.2 (0x00007f95d721c000)
      libpthread.so.0 => /lib64/libpthread.so.0 (0x00007f95d7000000)
      libc.so.6 => /lib64/libc.so.6 (0x00007f95d6c32000)
      libdl.so.2 => /lib64/libdl.so.2 (0x00007f95d6a2e000)
      libiconv.so.2 => /data/linkdood/im/cpp/libs/libiconv.so.2 (0x00007f95d6745000)
      /lib64/ld-linux-x86-64.so.2 (0x00007f95d7494000)
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 查看依赖的so是否存在
        • 如果 /data/linkdood/im/cpp/libs/libodbc.so.2版本较老,请联系运维替换
    • 使用 grep "cbuilder" /data/libs/* 如果匹配到则说明 odbc so库是老的,内部写死了odbcinst.ini地址

  • 异常 查询异常: SQLDriverConnect: {H} [ {0} [

    • 解决方式
    • 使用
      vim $ODBCINI
      
      • 1
      查看配置 是否配置一下参数
      [ODBC]
      UNICODE=UCS-2
      
      • 1
      • 2
  • 异常 出现 xxx.so不存在`

    • 解决方式
    • 确认so库是否存在,如果存在检测依赖是否存在
    • 使用 ldd xxx.so 查看so依赖是否存在

总结

国产化是大势所趋,目前主流的国产数据库有达梦,金仓,神通,优炫等,博主会在后续补充其他国产数据库的适配,敬请期待。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/615029
推荐阅读
相关标签
  

闽ICP备14008679号