Why does connecting to Oracle Autonomous Database from golang fail?

WBOY
Release: 2024-02-09 12:40:19
forward
458 people have browsed it

Why does connecting to Oracle Autonomous Database from golang fail?

php editor Baicao is here to answer a common question: "Why does the connection to the Oracle database from golang fail?" When using golang to connect to the Oracle database, sometimes the connection fails. question. This could be due to a variety of reasons, such as database misconfiguration, network connection issues, or driver incompatibility. This article will analyze the possible causes in detail and provide solutions to help readers solve this problem and make the process of connecting to the Oracle database smoother.

Question content

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()
Copy after login

I created an autonomous database, I tried to connect with the above code in golang, but could not connect, the following error occurred, how to connect, I can't 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 '='
Copy after login

Solution

I am a new user of oracle and golang, everything is new and I am having difficulty finding the documentation for connecting oracle and golang. There is a manual but I don't quite understand it.

If you post a question on stackoverflow, the answer is just a link to the manual. For beginners, it is really difficult to experience a new environment

I finally found the solution, my code is as follows. Even if you can't read the code, it's good to think about it one by one as you look 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())
}
Copy after login

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

The above is the detailed content of Why does connecting to Oracle Autonomous Database from golang fail?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!