英文:
Connecting SQL Server to Golang gorm
问题
我是新手,正在尝试将gorm连接到远程SQL Server数据库时感到困惑。
在.NET中,我通常在web.config中使用以下代码:
<add name="DevSaveLog" connectionString="Data Source=11.111.1.111;Network Library=DBMSSOCN;Initial Catalog=Database_Log;User ID=user_log;Password=dhhdf127ihd" providerName="System.Data.SqlClient" />
当我尝试连接gorm时,我尝试了以下代码:
func Init() {
dsn := url.QueryEscape("sqlserver://user_log:dhhdf127ihd@http://11.111.1.111?database=Database_Log")
db, err := gorm.Open(sqlserver.Open(dsn), &gorm.Config{})
if err != nil {
panic("error connecting to database")
}
db.AutoMigrate()
}
但是我遇到了错误:
[error] failed to initialize database, got error unable to open tcp connection with host 'localhost:1433': dial tcp 127.0.0.1:1433: connectex: No connection could be made because the target machine actively refused it.
panic: error connecting to database
顺便说一下,我在这个问题中没有使用真实的URL。即使我已经声明了sqlserver数据库的URL服务器,它仍然在读取localhost。
有什么解决办法吗?
谢谢
英文:
I am new in Golang btw. I got confused when I try to connect gorm to remote SQL Server database.
In .NET I usually use this for web.config
<add name="DevSaveLog" connectionString="Data Source=11.111.1.111;Network Library=DBMSSOCN;Initial Catalog=Database_Log;User ID=user_log;Password=dhhdf127ihd" providerName="System.Data.SqlClient" />
And when I try to connect to gorm I tried like this
func Init() {
dsn := url.QueryEscape("sqlserver://user_log:dhhdf127ihd@http://11.111.1.111?database=Database_Log")
db, err := gorm.Open(sqlserver.Open(dsn), &gorm.Config{})
if err != nil {
panic("error connecting to database")
}
db.AutoMigrate()
}
And I got errors :
[error] failed to initialize database, got error unable to open tcp connection with host 'localhost:1433': dial tcp 127.0.0.1:1433: connectex: No connection could be made because the target machine actively refused it.
panic: error connecting to database
BTW I don't use the real URL in this question .Even I have declared the url server of sqlserver database it just keep reading localhost.
Any solutions?
Thanks
答案1
得分: 1
我可以看到一些问题(文档是一个很好的起点):
DSN字符串
sqlserver://user_log:dhhdf127ihd@http://11.111.1.111?database=Database_Log
不是一个有效的DSN(删除 http://
)。
URL编码
url.QueryEscape("sqlserver://user_log:dhhdf127ihd@http://11.111.1.111?database=Database_Log")
返回 sqlserver%3A%2F%2Fuser_log%3Adhhdf127ihd%40http%3A%2F%2F11.111.1.111%3Fdatabase%3DDatabase_Log
。
这不是一个有效的URL,github.com/denisenkom/go-mssqldb
不会正确处理它,因为它会查找 sqlserver://
前缀(参考 这里)。删除 url.QueryEscape
。
总结一下,尝试像这样的代码:
dsn := "sqlserver://user_log:dhhdf127ihd@11.111.1.111?database=Database_Log"
db, err := gorm.Open(sqlserver.Open(dsn), &gorm.Config{})
英文:
I can see a few issues (the docs are a good place to start):
DSN String
sqlserver://user_log:dhhdf127ihd@http://11.111.1.111?database=Database_Log
is not a valid DSN (remove the http://
).
URL Encoding
url.QueryEscape("sqlserver://user_log:dhhdf127ihd@http://11.111.1.111?database=Database_Log")
returns sqlserver%3A%2F%2Fuser_log%3Adhhdf127ihd%40http%3A%2F%2F11.111.1.111%3Fdatabase%3DDatabase_Log
.
This is not a valid URL and will not be correctly processed by github.com/denisenkom/go-mssqldb
which looks for a prefix of sqlserver://
. Remove the url.QueryEscape
.
In summary try something like:
dsn := "sqlserver://user_log:dhhdf127ihd@11.111.1.111?database=Database_Log"
db, err := gorm.Open(sqlserver.Open(dsn), &gorm.Config{})
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论