dial tcp 127.0.0.1:3306: connectex: No connection could be made because the target machine actively refused it. exit status 1
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.
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())
}
What if we are trying to connect locally? @jimfilippou
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"
I have the same problem but I have "exit status 2"
I have the same problem but I have "exit status 2"
hey bro, you solved it? i have the same error
@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.
I was having the same error because I forgot to start MySQL service in xampp xD;
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
ok,亲~~~
I am trying to connect planetscale sql giving same issue