Go MySQL driver working with new default authentication

After the official release of MySQL 8.0, a few were complaining that the Go driver for MySQL did not have support for new default authentication method. It did indeed miss support, but this was already noticed and bug reported when moving to MySQL 8.0.4-rc. But, there was a valid workaround: you could (re-)create the users with a different authentication plugin, or set it globally in the configuration file using default_authenication_plugin.

A few days a ago, 1 June 2018, the good folks working on Go MySQL driver pushed a new feature to support the caching_sha2_password plugin (1 June 2018), pushed to master (so not yet released). Result: users with native passwords can login, as well as users using the new default. Nice work!

Code Example

It is also possible to add a connectoin paramter to disallow native MySQL passwords. The following example uses commit d743639f9 (merged into master) of go-sql-driver/mysql:

package main

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

func main() {
	dsn := "scott:tiger@tcp(127.0.0.1)/employees"
	log.Println("using", dsn)

	db, _ := sql.Open("mysql", dsn)
	if err := db.Ping(); err != nil {
		log.Fatal(err)
	} else {
		log.Println("successfully connected using native password")
	}

	dsn = dsn + "?allowNativePasswords=false"
	log.Println("using", dsn)

	db, _ = sql.Open("mysql", dsn)
	if err := db.Ping(); err != nil {
		log.Fatal(err)
	}
}

Output:

2018/06/03 11:08:49 using scott:tiger@tcp(127.0.0.1)/employees
2018/06/03 11:08:49 successfully connected using native password
2018/06/03 11:08:49 using scott:tiger@tcp(127.0.0.1)/employees?allowNativePasswords=false
2018/06/03 11:08:49 this user requires mysql native password authentication.