文档首页/
云数据库 GeminiDB/
GeminiDB Influx接口/
用户指南/
实例连接及管理/
程序代码连接GeminiDB Influx实例/
通过Go语言连接GeminiDB Influx实例
更新时间:2025-04-25 GMT+08:00
通过Go语言连接GeminiDB Influx实例
本章节介绍了通过Go语言连接GeminiDB Influx实例的方法。
前提条件
- 获取SSL默认证书,具体操作请参见下载SSL默认证书。
- 配置CCM私有证书,具体操作请参见CCM私有证书配置。
- 获取客户端相关代码,请自主从InfluxDB开源项目网站下载。
package main
import (
"fmt"
_ "github.com/influxdata/influxdb1-client" // this is important because of the bug in go mod
client "github.com/influxdata/influxdb1-client/v2"
"os"
)
func main(){
c, err := client.NewHTTPClient(client.HTTPConfig{
Addr: "http://ip:port",
// 认证用的用户名和密码直接写到代码中有很大的安全风险,建议在配置文件或者环境变量中存放(密码应密文存放、使用时解密),确保安全;
// 本示例以用户名和密码保存在环境变量中为例,运行本示例前请先在本地环境中设置环境变量(环境变量名称请根据自身情况进行设置)EXAMPLE_USERNAME_ENV和EXAMPLE_PASSWORD_ENV。
username = os.Getenv("EXAMPLE_USERNAME_ENV"),
password = os.Getenv("EXAMPLE_PASSWORD_ENV"),
Username: username,
Password: password,
})
if err != nil {
fmt.Println("Error creating InfluxDB Client: ", err.Error())
}
q := client.NewQuery("select * from cpu","db0","ns")
if response, err := c.Query(q); err == nil && response.Error() == nil {
fmt.Println("the result is: ",response.Results)
}
}
package main
import (
"fmt"
_ "github.com/influxdata/influxdb1-client" // this is important because of the bug in go mod
client "github.com/influxdata/influxdb1-client/v2"
"os"
)
func main(){
c, err := client.NewHTTPClient(client.HTTPConfig{
Addr: "https://ip:port",
// 认证用的用户名和密码直接写到代码中有很大的安全风险,建议在配置文件或者环境变量中存放(密码应密文存放、使用时解密),确保安全;
// 本示例以用户名和密码保存在环境变量中为例,运行本示例前请先在本地环境中设置环境变量(环境变量名称请根据自身情况进行设置)EXAMPLE_USERNAME_ENV和EXAMPLE_PASSWORD_ENV。
username = os.Getenv("EXAMPLE_USERNAME_ENV"),
password = os.Getenv("EXAMPLE_PASSWORD_ENV"),
Username:username,
Password:password,
InsecureSkipVerify: true, // true表示不验证服务端的信息,可能存在被攻击的风险,建议设为false,具体请参见使用CCM私有证书连接实例的示例代码。
})
if err != nil {
fmt.Println("Error creating InfluxDB Client: ", err.Error())
}
q := client.NewQuery("select * from cpu","databases","ns")
if response, err := c.Query(q); err == nil && response.Error() == nil {
fmt.Println(response.Results)
}
}
package main
import (
"fmt"
"io/ioutil"
"crypto/tls"
"crypto/x509"
_ "github.com/influxdata/influxdb1-client" // this is important because of the bug in go mod
client "github.com/influxdata/influxdb1-client/v2"
"os"
)
func main(){
pool := x509.NewCertPool()
caCertPath := "/data/CA/agent/ca.crt"
caCrt, err := ioutil.ReadFile(caCertPath)
if err != nil {
fmt.Println("ReadFile err:", err)
return
}
pool.AppendCertsFromPEM(caCrt) // 此处是将ca.crt证书内嵌到程序中,也可以使用sudo cp {client}/ca.crt /etc/ssl/certs命令将证书添加到本机上。
c, err := client.NewHTTPClient(client.HTTPConfig{
Addr: "https://ip:port",
// 认证用的用户名和密码直接写到代码中有很大的安全风险,建议在配置文件或者环境变量中存放(密码应密文存放、使用时解密),确保安全;
// 本示例以用户名和密码保存在环境变量中为例,运行本示例前请先在本地环境中设置环境变量(环境变量名称请根据自身情况进行设置)EXAMPLE_USERNAME_ENV和EXAMPLE_PASSWORD_ENV。
username = os.Getenv("EXAMPLE_USERNAME_ENV"),
password = os.Getenv("EXAMPLE_PASSWORD_ENV"),
Username: username,
Password: password,
TLSConfig: &tls.Config{
RootCAs: pool,
InsecureSkipVerify: false, // false表示需要校验服务端的证书。
},
})
if err != nil {
fmt.Println("Error creating InfluxDB Client: ", err.Error())
}
q := client.NewQuery("select * from cpu","database","ns")
if response, err := c.Query(q); err == nil && response.Error() == nil {
fmt.Println("the result is: ",response.Results)
}
}