如何在不使用密码的情况下连接到MySQL实例?

huangapple go评论77阅读模式
英文:

How do I connect to a MySQL instance without using the password?

问题

我正在尝试连接数据库,我已经为数据库设置了无密码,所以在密码字段中留空。但是它无法连接,并显示错误信息 connector.go:95: could not use requested auth plugin 'mysql_native_password': this user requires mysql native password authentication.。另外,我正在使用phpmyadmin作为数据库,以下是我的代码:

package main

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

var db *sql.DB

func main() {
    // 捕获连接属性。
    cfg := mysql.Config{
        User:   "root",
        Passwd: "",
        Net:    "tcp",
        Addr:   "127.0.0.1:3306",
        DBName: "recordings",
    }
    // 获取数据库句柄。
    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("已连接!")
}

希望这可以帮助你解决问题。

英文:

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

答案1

得分: 41

这看起来很像Tutorial: Accessing a relational database中的代码,但不幸的是它不起作用。你需要指定AllowNativePasswords: true才能使其工作。这个值默认为true,但默认值只有在使用NewConfig()时才会应用,而不是自己创建Config结构体。但这种方式也可以工作:

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

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,
    }

答案2

得分: 0

尝试将数据库中的root用户的身份验证插件更改为mysql_native_password。有关更多信息,请参阅此处

UPDATE mysql.user SET plugin = 'mysql_native_password' WHERE user = 'root' AND plugin = 'unix_socket';
英文:

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';

答案3

得分: -1

这段代码来自Go教程。默认代码无法正常工作,你需要在配置中添加一个额外的值,即AllowNativePasswords: true,

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

现在你可以连接了
英文:

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!

huangapple
  • 本文由 发表于 2022年1月18日 22:27:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/70757210.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定