24

I am trying to connect the MySql DB using Go Language and gives me following error.

sql: unknown driver "mysql" (forgotten import?)

My Code

package main

    import (
        "database/sql"
        "fmt"
    )

func main() {
    db, err := sql.Open("mysql", "astaxie:astaxie@/test?charset=utf8")
    checkErr(err);
    err=db.Ping();
}

Also when I import

        _ "github.com/go-sql-driver/mysql"

I am getting error of

imported and not used
1
  • 3
    what does your code look like when you add _ "github.com/go-sql-driver/mysql"? It shouldn't give imported and not used when you put underscore before package name. Commented Mar 28, 2016 at 8:03

4 Answers 4

48

For others coming to this page as a result of the error sql: unknown driver "mysql" (forgotten import?), the database/sql package must be used in conjunction with a database driver. That means in addition to importing the database/sql package, you need to import a database driver.

For example, for mysql, you could use the package go-sql-driver. Typically, you import this package using the underscore _ notation, meaning it is imported for its side effects only:

import _ "github.com/go-sql-driver/mysql"

You can read more about this and find a list of SQL drivers below:

Sign up to request clarification or add additional context in comments.

2 Comments

Why do we need this driver?
Because database/sql does not provide an implementation for specific sql databases, only a generic interface -- the driver implements the logic that interfaces with the specific database, in this case mysql (handling connections to the database, executing queries, preparing statements, etc)
11

Try it again, but look for my NOTEs:

package main

import (
    "database/sql"

    _ "github.com/go-sql-driver/mysql"
)
// NOTE - I removed the import for "fmt" because it was unused.

func main() {
    db, err := sql.Open("mysql", "astaxie:astaxie@/test?charset=utf8")
    checkErr(err);
    err=db.Ping();
    // NOTE - the above line will trigger an error because err is unused.
}

I added the import for the MySQL driver and removed "fmt" because it was unused. This may be the cause of your "imported and not used" error.

Comments

0

Try to recheck the location of the packages. I made such a mistake when I manually added this package to the project. It is best to clean GOROOT and GOPATH from this package and reinstall/reconnect it as indicated in the source: https://github.com/go-sql-driver/mysql

Comments

0

A more modern implementation shouldn't need to use unused imports like the other answers have been suggesting. According to https://cloud.google.com/sql/docs/mysql/connect-app-engine-standard#go_1, you should be using something like this:

import (
    "cloud.google.com/go/cloudsqlconn"
    "github.com/go-sql-driver/mysql"
)

func connectWithConnector() (*sql.DB, error) {
    d, err := cloudsqlconn.NewDialer(context.Background(), cloudsqlconn.WithLazyRefresh())
    if err != nil {
        return nil, fmt.Errorf("cloudsqlconn.NewDialer: %w", err)
    }
    var opts []cloudsqlconn.DialOption
    if env.usePrivate != "" {
        opts = append(opts, cloudsqlconn.WithPrivateIP())
    }
    mysql.RegisterDialContext("cloudsqlconn",
        func(ctx context.Context, addr string) (net.Conn, error) {
            return d.Dial(ctx, env.instanceConnectionName, opts...)
        })

    dbPool, err := sql.Open("mysql", dbConnString)
    if err != nil {
        return nil, fmt.Errorf("sql.Open: %w", err)
    }
    return dbPool, nil
}

Make sure that your network name (in the connection string) matches the one passed to mysql.RegisterDialContext. Additionally, your instance name should be the one listed on https://console.cloud.google.com/sql (of the form project:region:instance-id).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.