为什么无法从Golang连接到Oracle Autonomous Database?

huangapple go评论109阅读模式
英文:

Why is it failing to connect with oracle Autonomous Database from golang?

问题

dsn := user=ADMIN, password=temp1!a, (description= (retry_count=20)(retry_delay=3)(address=(protocol=tcps)(port=1522)(host=adb.ap-seoul-1.oraclecloud.com))(connect_data=(service_name=gee9edfb93f3cf6_redglqweyxqefhhf_high.adb.oraclecloud.com))(security=(ssl_server_cert_dn="CN=adb.ap-seoul-1.oraclecloud.com, OU=Oracle ADB SEOUL, O=Oracle Corporation, L=Redwood City, ST=California, C=US")))

db, err := sql.Open("godror", dsn)
if err != nil {
fmt.Println(err)
return
}
defer db.Close()

我创建了一个自主数据库,并尝试使用上述的Golang代码进行连接,但是无法连接,出现了以下错误。我找不到详细的手册,你能帮我吗?

在运行main.go时出现以下错误:
解析参数"user=ADMIN,\n\tpassword=temp1!a,\n\t(description= (retry_count=20)(retry_delay=3)(address=(protocol=tcps)(port=1522)(host=adb.ap-seoul-1.oraclecloud.com))(connect_data=(service_name=gee9edfb93f3cf6_redglqweyxqefhhf_high.adb.oraclecloud.com))(security=(ssl_server_cert_dn="CN=adb.ap-seoul-1.oraclecloud.com, OU=Oracle ADB SEOUL, O=Oracle Corporation, L=Redwood City, ST=California, C=US")))": 在第3行的第44个位置处出现logfmt语法错误:意外的'='。

英文:
	dsn := `
user=ADMIN,
password=temp1!a,
(description= (retry_count=20)(retry_delay=3)(address=(protocol=tcps)(port=1522)(host=adb.ap-seoul-1.oraclecloud.com))(connect_data=(service_name=gee9edfb93f3cf6_redglqweyxqefhhf_high.adb.oraclecloud.com))(security=(ssl_server_cert_dn="CN=adb.ap-seoul-1.oraclecloud.com, OU=Oracle ADB SEOUL, O=Oracle Corporation, L=Redwood City, ST=California, C=US")))
`

db, err := sql.Open("godror", dsn)
if err != nil {
	fmt.Println(err)
	return
}
defer db.Close()

I created an autonomous database, and I try to connect with the above code in golang, but it is impossible to connect with the following error, how to connect, I cannot find a detailed manual, can you help me?

go run main.go
parsing parameters "user=ADMIN,\n\tpassword=temp1!a,\n\t(description= (retry_count=20)(retry_delay=3)(address=(protocol=tcps)(port=1522)(host=adb.ap-seoul-1.oraclecloud.com))(connect_data=(service_name=gee9edfb93f3cf6_redglqweyxqefhhf_high.adb.oraclecloud.com))(security=(ssl_server_cert_dn=\"CN=adb.ap-seoul-1.oraclecloud.com, OU=Oracle ADB SEOUL, O=Oracle Corporation, L=Redwood City, ST=California, C=US\")))": logfmt syntax error at pos 44 on line 3: unexpected '='

答案1

得分: 0

我是你的中文翻译助手,以下是翻译好的内容:

我是一个使用Oracle和Golang的新用户,一切都很新,我在寻找连接Oracle和Golang的文档时遇到了困难。有一个手册,但我没有很好地理解它。

如果在stackoverflow上发布问题,答案只是指向手册的链接。对于初学者来说,体验一个新环境真的很困难。

最终我找到了解决方案,我的代码如下。即使你不理解代码,看着例子一步一步思考也是不错的。

package main

import (
	"database/sql"
	"fmt"

	go_ora "github.com/sijms/go-ora/v2"
)

type DbInfo struct {
	Username string
	Password string
}

func main() {
	serverInfo := DbInfo{
		Username: "databaseUser",
		Password: "databaseUserPassword",
	}
	urlOptions := map[string]string{
		"TRACE FILE": "trace.log",
		"SSL VERIFY": "FALSE",
	}
	connectString := "(description= (retry_count=20)(retry_delay=3)(address=(protocol=tcps)(port=1522)(host=adb.ap-seoul-1.oraclecloud.com))(connect_data=(service_name=gvsd1dfc9c3cdf6_racc7y8cxsvb16f0z_medium.adb.oraclecloud.com))(security=(ssl_server_dn_match=yes)))"

	db, err := sql.Open("oracle", go_ora.BuildJDBC(serverInfo.Username, serverInfo.Password, connectString, urlOptions))

	if err != nil {
		panic(err.Error())
	}
	defer db.Close()

	r, err := db.Exec(`create table users (
		nickname varchar(20)
	)`)
	if err != nil {
		panic(err.Error())
	}
	fmt.Println(r.LastInsertId())
}

上面的代码不需要使用TLS访问钱包。我还会上传如何通过钱包访问的方法,以供需要的初学者参考。

英文:

I am a new user of oracle and golang, everything is new, and I had difficulty finding documentation to connect oracle and golang. There is a manual, but I did not understand it well.

If you post a question on stackoverflow, the answers are only links to the manual. It was really hard for beginners to experience a new environment

I finally found a solution and my code is below. Even if you don't understand the code, it would be nice to think about it one by one while looking at the examples.

package main

import (
	"database/sql"
	"fmt"

	go_ora "github.com/sijms/go-ora/v2"
)

type DbInfo struct {
	Username string
	Password string
}

func main() {
	serverInfo := DbInfo{
		Username: "databaseUser",
		Password: "databaseUserPassword",
	}
	urlOptions := map[string]string{
		"TRACE FILE": "trace.log",
		"SSL VERIFY": "FALSE",
	}
	connectString := "(description= (retry_count=20)(retry_delay=3)(address=(protocol=tcps)(port=1522)(host=adb.ap-seoul-1.oraclecloud.com))(connect_data=(service_name=gvsd1dfc9c3cdf6_racc7y8cxsvb16f0z_medium.adb.oraclecloud.com))(security=(ssl_server_dn_match=yes)))"

	db, err := sql.Open("oracle", go_ora.BuildJDBC(serverInfo.Username, serverInfo.Password, connectString, urlOptions))

	if err != nil {
		panic(err.Error())
	}
	defer db.Close()

	r, err := db.Exec(`create table users (
		nickname varchar(20)
	)`)
	if err != nil {
		panic(err.Error())
	}
	fmt.Println(r.LastInsertId())
}

The code above does not require a wallet for tls access. I'll also upload how to access it through the wallet below, for beginners who need it.

huangapple
  • 本文由 发表于 2023年1月9日 18:09:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/75055727.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定