英文:
Go - Connecting to a external mysql database
问题
我对使用go语言还比较新,遇到了连接外部mysql数据库的问题。
我正在使用go-sql-driver,它看起来相当不错。欢迎提供其他驱动程序的建议!
这是整个程序:
import (
"database/sql"
_ "github.com/go-sql-driver/mysql"
"fmt"
)
const (
DB_HOST = "tcp(http://thedburl.com)"
DB_NAME = "nameofdatabase"
DB_USER = "username"
DB_PW = "password"
)
func main() {
dsn := DB_USER + ":" + DB_PW + "@" + DB_HOST + "/" + DB_NAME + "?charset=uf8"
db, err := sql.Open("mysql", dsn)
if err != nil {
fmt.Println("shiiet didn't work yo! Initialization failed")
}
defer db.Close() // go's purty cool
var str string
q := "SELECT * FROM forums"
err = db.QueryRow(q).Scan(&str)
if err != nil {
fmt.Println(err)
}
fmt.Println(str)
}
在我收到的请求中,出现了以下错误
"GetAddrInfoW: The specified class was not found."
有什么想法吗?我在网上搜索了几个小时,似乎无法解决这个问题。
值得注意的是,我以前在Java中多次使用过相同的数据库服务。
英文:
I'm rather new to use go and am having issues connecting to an external mysql database.
I'm using the go-sql-driver which seams rather nice. Suggestions to other drivers are welcomed!
this is the whole program:
import (
"database/sql"
_ "github.com/go-sql-driver/mysql"
"fmt"
)
const (
DB_HOST = "tcp(http://thedburl.com)"
DB_NAME = "nameofdatabase"
DB_USER = "username"
DB_PW = "password"
)
func main() {
dsn := DB_USER + ":" + DB_PW + "@" + DB_HOST + "/" + DB_NAME + "?charset=uf8"
db, err := sql.Open("mysql", dsn)
if err != nil {
fmt.Println("shiiet didn't work yo! Initialization failed")
}
defer db.Close() // go's purty cool
var str string
q := "SELECT * FROM forums"
err = db.QueryRow(q).Scan(&str)
if err != nil {
fmt.Println(err)
}
fmt.Println(str)
}
On the request I'm recieving the following error
"GetAddrInfoW: The specified class was not found."
Any ideas? I've siting for hours on the webs, and can't seem to solve the problem.
It might be worth noting that I have used the same database service many times in java.
答案1
得分: 1
有趣的是,这个错误也可以由服务器端口中的空格引起,例如 server.Run(":8080 ")
和 server.Run(":8080")
。
英文:
Interestingly, this error can be also caused by spaces in the server port as in server.Run(":8080 ")
vs server.Run(":8080")
答案2
得分: 0
感谢大家的回答。
Go语言的mysql驱动在处理旧版本的mysql时遇到了困难。具体来说,这个问题是由于mysql的old_password(2006年的版本)不兼容导致的。因此,与旧数据库的工作非常麻烦。(来源:https://github.com/go-sql-driver/mysql/wiki/old_passwords)- 我的猜测也适用于mymysql驱动程序,它会抛出“bad connection”错误。基本上,在多次尝试连接后会超时。
特别感谢@Lepidosteus的答案让我发现了真正的问题。
英文:
Thanks for everyones answers.
The mysql drivers for Go are at the current time having a hard time dealing with older mysql versions. Specifically this issue happened due to the incompatibility of mysql's old_password from 2006. Therefore working with older databases is a pain. (source: https://github.com/go-sql-driver/mysql/wiki/old_passwords) - My educated guess also applies to the mymysql driver whom throws the "bad connection" error. Which basicly times out after repetitive attempts of connection.
Special thanks to @Lepidosteus who's answer made me discover the real problem.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论