S
S
Sergey2017-08-27 00:11:43
go
Sergey, 2017-08-27 00:11:43

No connection to mysql golang database?

Can you tell me there is no connection to the mysql database

package main

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

func main() {
  db, err := sql.Open("mysql", "root:[email protected](127.0.0.1:3306)/freeswitch")
  //db, err := sql.Open("mysql","root:[email protected](/var/run/mysqld/mysqld.sock)/freeswitch")
  if err != nil {
    panic(err)  ///если коннекта к БД нет кидаюю панику
  }
  if err != nil {fmt.Println("Коннект к БД есть ")} else {fmt.Println("Коннекта к БД нет")}
  db.Close()///закрываю коннект
}

But if you connect via console
[[email protected] scripts]# mysql -pqld501a87f54 -uroot -h127.0.0.1
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 3600
Server version: 5.5.52-MariaDB MariaDB Server

Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> Bye

There is a connection

Answer the question

In order to leave comments, you need to log in

[[+comments_count]] answer(s)
A
akzhan, 2017-08-27
@akzhan

and so?
and what error do we get?

// Open doesn't open a connection. Validate DSN data:
err = db.Ping()
if err != nil {
    panic(err.Error()) // proper error handling instead of panic in your app
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question