go 连接 mysql: could not use requested auth plugin ‘mysql_native_password‘
2021/12/18 19:19:49
本文主要是介绍go 连接 mysql: could not use requested auth plugin ‘mysql_native_password‘,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
文章目录
- go 连接 mysql 问题
go 连接 mysql 问题
参考官方文档
https://go.dev/doc/tutorial/database-access
var db *sql.DB func main() { // Capture connection properties. cfg := mysql.Config{ User: os.Getenv("DBUSER"), Passwd: os.Getenv("DBPASS"), 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!") }
报错:
zego@zegodeMacBook-Pro-53 data-access % go run main.go [mysql] 2021/12/16 11:23:02 connector.go:95: could not use requested auth plugin 'mysql_native_password': this user requires mysql native password authentication. 2021/12/16 11:23:02 this user requires mysql native password authentication. exit status 1
解决方法:添加 AllowNativePasswords: true,
cfg := mysql.Config{ User: os.Getenv("DBUSER"), Passwd: "123456", Net: "tcp", Addr: "127.0.0.1:3306", DBName: "recordings", AllowNativePasswords: true, }
参考文档中说明该参数默认为 true,我这边不知道何原因为 false
当设为 false 表示不允许使用 MySQL 原生密码方法
参数文档
https://github.com/go-sql-driver/mysql#dsn-data-source-name
解决方案参考链接
https://github.com/go-sql-driver/mysql/issues/815
这篇关于go 连接 mysql: could not use requested auth plugin ‘mysql_native_password‘的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-04部署MySQL集群项目实战:新手入门教程
- 2024-11-04如何部署MySQL集群资料:新手入门指南
- 2024-11-02MySQL集群项目实战:新手入门指南
- 2024-11-02初学者指南:部署MySQL集群资料
- 2024-11-01部署MySQL集群教程:新手入门指南
- 2024-11-01如何部署MySQL集群:新手入门教程
- 2024-11-01部署MySQL集群学习:新手入门教程
- 2024-11-01部署MySQL集群入门:新手必读指南
- 2024-10-23BinLog入门:新手必读的MySQL二进制日志指南
- 2024-10-23Binlog入门:MySQL数据库的日志管理指南