skip to Main Content

I trying to connect db I have set no password for the db I am leaving blank in the password field. But it’s not connecting and showing error connector.go:95: could not use requested auth plugin 'mysql_native_password': this user requires mysql native password authentication.. Also I am using phpmyadmin as db so how to connect here is my code

package main

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

var db *sql.DB

func main() {
    // Capture connection properties.
    cfg := mysql.Config{
        User:   "root",
        Passwd: "",
        Net:    "tcp",
        Addr:   "127.0.0.1:3306",
        DBName: "recordings",
    }
    // Get a database handle.
    var err error
    db, err = sql.Open("mysql", cfg.FormatDSN())
    if err != nil {
        log.Fatal(err)
    }

    pingErr := db.Ping()
    if pingErr != nil {
        log.Fatal(pingErr)
    }
    fmt.Println("Connected!")
}

3

Answers


  1. Try to change authentication plugin of root user to mysql_native_password in your database. More information about here

    UPDATE mysql.user SET plugin = 'mysql_native_password' WHERE user = 'root' AND plugin = 'unix_socket';
    
    Login or Signup to reply.
  2. That looks a lot like the code from the Tutorial: Accessing a relational database which unfortunately does not work. You need to specify AllowNativePasswords: true for it to work. That value is true by default, but the defaults are only applied if you use NewConfig() and not create the Config struct yourself. But this will work as well:

        cfg := mysql.Config{
            User:                 "root",
            Passwd:               "",
            Net:                  "tcp",
            Addr:                 "127.0.0.1:3306",
            DBName:               "recordings",
            AllowNativePasswords: true,
        }
    
    Login or Signup to reply.
  3. This code is from Go tutorial. It doesn’t work for default code. You have to add one more value in config which is AllowNativePasswords: true,

    cfg := mysql.Config{
        User:                 "root",
        Passwd:               "test",
        Net:                  "tcp",
        Addr:                 "127.0.0.1:3306",
        DBName:               "recordings",
        AllowNativePasswords: true,
    }
    

    And you’re good go go for connected!

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search