英文:
Remote access OVH SQL database from Golang API
问题
我正在制作一个用于与我的Android应用程序中的SQL数据库进行交互的Golang API。我遇到的问题是:
我有一个托管在https://phpmyadmin.ovh.net/上的SQL数据库。
我正在尝试将我的本地API连接到这个数据库。根据文档:
func Open(driverName, dataSourceName string) (*DB, error)
driverName参数应该是"mysql",对吗?
但是,dataSourceName参数应该是什么?
我看到了很多关于Open的帖子,但是我不认为我理解如何组织关于dataSourceName参数的URL字符串。
谢谢阅读和帮助!
编辑:
根据你们两个的回答,我尝试了下面的代码:
package main
import (
"fmt"
"net/http"
"database/sql"
_ "github.com/go-sql-driver/mysql"
)
type databaseinfos struct {
user string
password string
name string
address string
port string
url string
}
var db *sql.DB // 全局变量,用于在main和HTTP处理程序之间共享
var dbi *databaseinfos
func main() {
dbi := databaseinfos{
user: "Emixam23",
password: "",
name: "mydb",
address: "127.0.0.1",
port: "3306",
url: ""}
dbi.url = (dbi.user + ":" + dbi.password + "@tcp(" + dbi.address + ":" + dbi.port + ")/" + dbi.name)
fmt.Println(dbi.url)
db, err := sql.Open("mysql", dbi.url)
if err != nil {
fmt.Println("Error on initializing database connection: %s", err.Error())
}
err = db.Ping()
if err != nil {
fmt.Println("Error on opening database connection: %s", err.Error())
}
/*http.HandleFunc("/", hello)*/
http.ListenAndServe(":8080", nil)
}
然而,我遇到了一个错误,错误信息是"Error on opening database connection: %s dial tcp 127.0.0.1:3306: connectex: No connection could be made because the target machine actively refused it."。
这个问题是由于我的计算机还是由于dataSourceName参数引起的?
英文:
I'm making a Golang API to be able to interact with a SQL database from my Android application. The problem I have is the following:
I have a sql database hosted on https://phpmyadmin.ovh.net/
I'm trying to connect my locale API to this one. According to the documentation:
func Open(driverName, dataSourceName string) (*DB, error)
The driverName params would be "mysql" isn't it?
But, what would be the dataSourceName params?
I saw lot of post about Open, however, I don't think I understand how to organize the url string about the dataSourceName params.
package main
import (
"net/http"
"database/sql"
"fmt"
)
var db *sql.DB // global variable to share it between main and the HTTP handler
func main() {
fmt.Println("starting up")
db, err := sql.Open("mysql", "????????????")
if err != nil {
fmt.Println("Error on initializing database connection: %s", err.Error())
}
err = db.Ping()
if err != nil {
fmt.Println("Error on opening database connection: %s", err.Error())
}
http.HandleFunc("/", hello)
http.ListenAndServe(":8080", nil)
}
Thank for read & help !
/!\ EDIT /!\
So, after your both answers, I tried the code below:
package main
import (
"fmt"
"net/http"
"database/sql"
_ "github.com/go-sql-driver/mysql"
)
type databaseinfos struct {
user string
password string
name string
address string
port string
url string
}
var db *sql.DB // global variable to share it between main and the HTTP handler
var dbi *databaseinfos
func main() {
dbi := databaseinfos{
user: "Emixam23",
password: "",
name: "mydb",
address: "127.0.0.1",
port: "3306",
url: ""}
dbi.url = (dbi.user + ":" + dbi.password + "@tcp(" + dbi.address + ":" + dbi.port + ")/" + dbi.name)
fmt.Println(dbi.url)
db, err := sql.Open("mysql", dbi.url)
if err != nil {
fmt.Println("Error on initializing database connection: %s", err.Error())
}
err = db.Ping()
if err != nil {
fmt.Println("Error on opening database connection: %s", err.Error())
}
/*http.HandleFunc("/", hello)*/
http.ListenAndServe(":8080", nil)
}
However, I got an error which is "Error on opening database connection: %s dial tcp 127.0.0.1:3306: connectex: No connection could be made because the target machine actively refused it.".
Does the problem comes because of my computer or because of the dataSourceName params?
答案1
得分: 1
dataSourceName 应该以 DSN 格式提供:
<username>:<password>@tcp(<host>:<port>)/<database>
例如:
db, err := sql.Open("mysql", "myuser:password@tcp(127.0.0.1:3306)/mydb")
英文:
dataSourceName should be provided in the DSN format:
<username>:<password>@tcp(<host>:<port>)/<database>
For example:
db, err := sql.Open("mysql", "myuser:password@tcp(127.0.0.1:3306)/mydb")
答案2
得分: 0
你的代码中有一个问题,就在这里:
dbi.url = (dbi.user + ":" + dbi.password + "@tcp(...
根据我看到的参数,用这种方式构建连接字符串会得到:
Emixam23:@tcp..
这是一个空密码,根据规范来看是错误的:
[username[:password]@][protocol[(address)]]/dbname[?param1=value1&...¶mN=valueN]
我们可以看到,如果不需要密码,就不要添加 ":"。
如果问题仍然存在,并且连接字符串实际上与正确的模式匹配,请测试是否可以访问你的数据库:
mysql -u Emixam23
或者使用密码:
mysql -u Emixam23 -p
然后尝试访问数据库,看看你的用户是否具有适当的权限:
> USE mydb; SELECT 1;
如果你甚至无法连接:
检查你的端口和IP绑定是否正确:
sudo netstat -antp | grep mysql
如果不正确,请相应地编辑 /etc/mysql/my.cnf
。
英文:
There's a problem in your code, here:
dbi.url = (dbi.user + ":" + dbi.password + "@tcp(...
This way of building the connection string with the parameters I see above will result in:
Emixam23:@tcp..
.
With an empty password, which is wrong if we look at the spec:
[username[:password]@][protocol[(address)]]/dbname[?param1=value1&...&paramN=valueN]
We see that if no password is required you must not add the ":"
If the problem persists and the connection string actually matches the correct pattern, test that you can access your DB:
mysql -u Emixam23
or with password:
mysql -u Emixam23 -p
Then try to access the database to see if your user has the proper pemissions:
> USE mydb; SELECT 1;
If you can't even connect:
Check that your port and IP binding are correct:
sudo netstat -antp | grep mysql
If not, edit your /etc/mysql/my.cnf
accordingly.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论