14

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!")
}

kostix
  • 51,517
  • 14
  • 93
  • 176
zircon
  • 742
  • 1
  • 10
  • 22
  • 1
    "But it's not connecting." What does that mean exactly? What is the error? – Volker Jan 18 '22 at 14:34
  • I have edited the question after your suggestion plz read it again. @Volker – zircon Jan 18 '22 at 14:57
  • I'd speculate "phpmyadmin" runs with the permission of the web server which executes its code while this program runs with the permissions of the user whose credentials the OP uses on their system. But we do not have enough information to make this guess educated. – kostix Jan 18 '22 at 15:15
  • Also [this](https://dba.stackexchange.com/questions/209514) might be a way simpler answer. – kostix Jan 18 '22 at 15:19
  • 1
    Wile we're on it—regarding that „But it's not connecting” statement—please read [this](https://www.chiark.greenend.org.uk/~sgtatham/bugs.html) carefully. – kostix Jan 18 '22 at 15:20

3 Answers3

41

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,
    }
Remon Huijts
  • 661
  • 6
  • 7
  • 1
    This works, I'd like to understand what it means the "native password" setting though – Aikanáro Dec 25 '22 at 14:38
  • In my case, the password is not empty, then I provided its value for the field 'Passwd' and the code works with "connected" output. – Doan Vu Feb 26 '23 at 14:26
0

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';
oyaro-tech
  • 13
  • 5
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jan 18 '22 at 17:37
-1

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!