doutuohan6606 2018-10-26 00:47
浏览 247

尝试使用docker将Golang连接到mssql服务器时拒绝连接

I am trying to connect my golang program to a mssql server that I run on a container using docker. the golang is also running on a docker container. the go script seems to be able to successfully connect to the database, but can't perform any operation on it. So,

db, err := sql.Open("mysql","SA:YourStrong!Passw0rd@tcp(127.0.0.1:1433)/")

if err != nil {
    panic(err)
}

defer db.Close()
fmt.Println("Success open database")

This prints out the "success open database", however the next portion of the code throws a panic,

_,err = db.Exec("CREATE DATABASE currency1")
if err != nil {
    panic(err)
}

fmt.Println("Success CREATE database")

After using docker to build and run it, this is what I got

GO MYSQL START
Success open database
panic: dial tcp 127.0.0.1:1433: getsockopt: connection refused

This is the command I used to run the mssql database:

docker run -e 'ACCEPT_EULA=Y' -e 'SA_PASSWORD=YourStrong!Passw0rd' -e 'MSSQL_PID=Express' -p 1433:1433 --name sql1 -d microsoft/mssql-server-linux:latest
  • 写回答

3条回答 默认 最新

  • douliaodun9153 2018-10-26 01:14
    关注

    First thing first you should understand how networking in docker works. Every running container will treated as single virtual node. They have their own IP to connect to each other. In your case, there will 2 running container, 1 for Go and 1 for SQLServer. So there is Go container wanted to connect to SQLServer container. The address 127.0.0.1 or localhost means it's for it self(a loopback address). To connect it you can use the container name as the address (because IP is randomly assigned). Actually it converted to IP, docker doing this for you. Based on your docker run above, it should be sql1.

    Hope it helps.

    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog