英文:
How to connect to mssql database in linux with GO
问题
我使用了这个教程在Linux上创建并连接了一个SQL Server。
我正在使用golang和这个驱动程序。(https://github.com/denisenkom/go-mssqldb)
我在使用golang连接数据库时遇到了问题。我指定了一个用户名为SA和一个密码。我还在终端中输入了"hostname"并得到了主机名xxx。当我使用这些信息进行连接时,我得到一个500错误。
conn, err := sql.Open("mssql", "server=xxx; id=SA; password=mypass; database=testdb)
这个看起来正确吗?作为一个新手,也许我漏掉了一些显而易见的东西?
英文:
I have created and connected to an sql server on linux using this tutorial.
I am using golang and this driver. (https://github.com/denisenkom/go-mssqldb)
I am having problems connecting to the database using golang. I have specified a user as SA and a password. I have also written in the terminal "hostname" and got the hostname xxx. When I use that info to connect I get a 500 error.
conn, err := sql.Open("mssql", "server=xxx; id=SA; password=mypass; database=testdb)
Does this seem correct? I am a newbie so maybe I am missing something obvious?
答案1
得分: 1
如果您在创建连接字符串时遇到问题,您可以始终让您的库为您格式化连接字符串。
import (
"database/sql"
_ "github.com/go-sql-driver/mysql"
// 其他代码
)
func main() {
cfg := mysql.Config{
User: "username",
Passwd: "password",
Net: "tcp",
Addr: "127.0.0.1:3306",
DBName: "database_name",
}
db, err := sql.Open("mysql", cfg.FormatDSN())
check(err)
// 使用 db 做一些操作
}
func check(e error) {
if e != nil {
panic(e)
}
}
以上是一个示例代码,您可以使用github.com/go-sql-driver/mysql
库来创建连接字符串。在示例中,您需要将username
、password
、127.0.0.1:3306
(数据库服务器地址和端口)以及database_name
(数据库名称)替换为实际的值。然后,您可以使用db
对象执行数据库操作。
英文:
If you are having troubles creating a connection string, then you can always get your library to format your connection string for you.
import (
"database/sql"
_ "github.com/go-sql-driver/mysql"
// OTHER STUFF
)
func main() {
cfg := mysql.Config{
User: "username",
Passwd: "password",
Net: "tcp",
Addr: "127.0.0.1:3306",
DBName: "database_name",
}
db, err := sql.Open("mysql", cfg.FormatDSN())
check(err)
// DO SOMETHING WITH db
}
func check(e error) {
if e != nil {
panic(e)
}
}
答案2
得分: 0
在Github示例中,你可以看到它是如何工作的。
你可以按照以下步骤进行操作:
var (
server string = "localhost" // 例如
user string = "userdb" // 数据库用户
password string = "userpwd" // 用户密码
port int = 1433 // 数据库端口
)
connString := fmt.Sprintf("server=%s;user id=%s;password=%s;port=%d", server, user, password, port)
conn, err := sql.Open("mssql", connString)
// 测试连接是否正常
if err != nil {
panic("无法连接到数据库")
} else {
fmt.Println("已连接!")
}
// 不要忘记关闭与数据库的连接
defer conn.Close()
以上是代码的翻译部分。
英文:
In the Github example you can see how it work.
You can follow those steps:
var (
server string = "localhost" // for example
user string = "userdb" // Database user
password string = "userpwd" // User Password
port int = 1433 // Database port
)
connString := fmt.Sprintf("server=%s;user id=%s;password=%s;port=%d", server, user, password, port)
conn, err := sql.Open("mssql", connString)
// Test if the connection is OK or not
if err != nil {
panic("Cannot connect to database")
} else {
fmt.Println("Connected!")
}
// Don't forget to close the connection to your database
defer conn.Close()
答案3
得分: 0
import "database/sql"
import _ "github.com/go-sql-driver/mysql"
db, err := sql.Open("mysql", "user:password@/dbname")
只需这样简单的代码,你就可以连接到你的数据库。
你需要在代码中添加未使用的驱动程序包,然后使用连接数据库的URI参数。
你可以像下面的链接中的示例一样进行查询:
https://golang.org/pkg/database/sql/#example_DB_Query
英文:
import "database/sql"
import _ "github.com/go-sql-driver/mysql"
db, err := sql.Open("mysql", "user:password@/dbname")
As simple as this you can connect to your DB
you should add driver in your code as unused package then it has parameters URI to connect to DB.
And you can query like this link blow
https://golang.org/pkg/database/sql/#example_DB_Query
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论