dopuz8728 2018-09-25 13:25
浏览 88
已采纳

Ubuntu升级后的Golang MySQL连接超时

I am having a problem with a golang project fetching data from MySQL database. This project has been working without issue until I upgraded from Ubuntu 16.04 to Ubuntu 18.04.01. The application now times out when connecting to the database.

My first thought was that something broke in the 16.04 to 18.04 upgrade. To prove this I spun up a new VM running 16.04, performed a "do-release-upgrade" and brought it up to 18.04. However on this VM, my application works just fine without problems.

I'm at a loss where to go from here to troubleshoot what is happening. Here is a summary of the two configurations. Both the VM (working) and the server (not working) are the following;

Ubuntu 18.04.01 LTS x86_64
Kernal 4.15.0-34-generic
Go 1.11
MySQL 5.7.23-0ubuntu

Here is my test program:
package main

import (
        "fmt"
        "database/sql"
        _"github.com/go-sql-driver/mysql"
)

func main() {
        fmt.Printf("Connecting to db
")
        db,err := sql.Open("mysql","Test:@/Test")
        if err != nil {
                panic(err)
        }
        fmt.Printf("Trying query Row
")
        var Data int 
        err = db.QueryRow("SELECT Data FROM Test WHERE ID=2").Scan(&Data)
        fmt.Printf("After Query
")
        if err != nil {
                panic(err)
        }
        fmt.Printf("Got Data=%d
",Data)
        db.Close()
}

On the VM the program executes without problem and returns some valid data. The server hangs on the QueryRow line. After 2 minutes I get this return

$ time ./test
Connecting to db
Trying query Row
After Query
panic: dial tcp 127.0.0.1:3306: connect: connection timed out

goroutine 1 [running]:
main.main()
    /home/daedalus/test/sqltest.go:20 +0x263

real    2m10.874s
user    0m0.001s
sys 0m0.009s

I realize the problem is the program cannot connect to the database, or that the database does not respond, but I cannot see why. There is nothing being logged either in syslog, mysql's log files. There is nothing wrong with MySQL that I can see. I can connect and run queries as the Test user just fine.

I'm not sure how to proceed with tracking down why the server is failing but the VM is working when both configurations are the same.

Any help would be appreciated.

展开全部

  • 写回答

2条回答 默认 最新

  • dpde7365 2018-10-10 08:30
    关注

    Answering my question just to close it off but thank you Peter for your comments that led to my solution.

    The problem was specifically my firewall rules. I have some strict rules around country of origin but inadvertently blocked traffic from 127.0.0.1. Once I made an exception for this, everything works fine.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)
编辑
预览

报告相同问题?

悬赏问题

  • ¥15 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部