dopuz8728
dopuz8728
2018-09-25 21:25
浏览 82

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
    dpde7365 2018-10-10 16: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.

    点赞 评论
  • dongtuhe0506
    dongtuhe0506 2018-09-25 23:01

    I can think about 2 possibility:

    • MySQL server lacks user "Test@127.0.0.1" but has user "Test@localhost"
    • server firewall is blocking access to port 3306 on 127.0.0.1
    点赞 评论

相关推荐