glide icon indicating copy to clipboard operation
glide copied to clipboard

dial tcp 127.0.0.1:3306: connectex: No connection could be made because the target machine actively refused it. exit status 1

Open dipikantadutta opened this issue 7 years ago • 10 comments

I'm trying to connect to sql db but it is throwing the following error: Command: go run main.go Output: dial tcp 127.0.0.1:3306: connectex: No connection could be made because the target machine actively refused it. exit status 1

Code Snippet: db,err := sql.Open("mysql", "prxqamydb:dbnms#666@tcp(fcdb1198)/IPCC") if err != nil { fmt.Println(err) }

e := db.Ping()
if e != nil {
	log.Fatal(e)
}else{
fmt.Println("Ping to database successful, connection is still alive")}


var vcCode string
 rows, err:= db.Query("SELECT vcCode FROM dbo.CodeMaster")
 if err!=nil{
 log.Fatal("Not working ")
 }else{
 fmt.Println("working")
 }
 defer rows.Close()
 
 for rows.Next(){
 err:=rows.Scan(&vcCode)
 if err != nil {
 log.Fatal("Not again")
 }
 fmt.Println(vcCode)
 }

N.B: I have imported both "database/sql" _ "github.com/go-sql-driver/mysql"

I do not know how to proceed from here.

dipikantadutta avatar Jun 15 '18 05:06 dipikantadutta

The dial tcp 127.0.0.1:3306: connectex: No connection could be made because the target machine actively refused it. exit status 1 error indicates that your machine is hitting localhost and possibly localhost isn't listening for TCP connections. If you are trying to connect to the cloud and not locally consider using this code below.

fmt.Println("Connecting to database...")
connString := "username:password@tcp(sql.hosting.com:3306)/databaseName"
db, err := sql.Open("mysql", connString)
if err != nil {
	panic(err.Error())
}

jimfilippou avatar Jul 29 '18 17:07 jimfilippou

What if we are trying to connect locally? @jimfilippou

VVScript avatar Jun 17 '19 17:06 VVScript

What if we are trying to connect locally? @jimfilippou

Well I'm not sure but you could do something like this

connString := "username:password@tcp(127.0.0.1:3306)/databaseName"

or

connString := "username:password@tcp(localhost:3306)/databaseName"

jimfilippou avatar Jun 17 '19 20:06 jimfilippou

I have the same problem but I have "exit status 2"

DubBro avatar May 01 '20 20:05 DubBro

I have the same problem but I have "exit status 2"

hey bro, you solved it? i have the same error

myomyintaung1411 avatar May 08 '20 08:05 myomyintaung1411

@myomyintaung1411 Yep, I just enable the "TCP/IP Connection" in configurations of my server. But I use the MS SQL Server, not MySQL, so I don't know how to enable TCP/IP there.

DubBro avatar May 08 '20 18:05 DubBro

I was having the same error because I forgot to start MySQL service in xampp xD;

Rakesh6841 avatar Dec 17 '22 07:12 Rakesh6841

for ubuntu

edit /etc/mysql/mysql.conf.d/mysqld.cnf then comment these lines

#bind-address           = 127.0.0.1
#mysqlx-bind-address    = 127.0.0.1

and restart

Deepanshusuman avatar Jan 18 '23 05:01 Deepanshusuman

ok,亲~~~

fengkongling avatar Jan 18 '23 05:01 fengkongling

I am trying to connect planetscale sql giving same issue

dEvAshirvad avatar Mar 25 '23 15:03 dEvAshirvad