diff --git a/mysql/resource_user_password.go b/mysql/resource_user_password.go index d1a4ce39..3e00fbdf 100644 --- a/mysql/resource_user_password.go +++ b/mysql/resource_user_password.go @@ -2,6 +2,7 @@ package mysql import ( "fmt" + "log" "github.com/gofrs/uuid" "github.com/hashicorp/go-version" @@ -64,23 +65,27 @@ func SetUserPassword(d *schema.ResourceData, meta interface{}) error { d.Set("key_fingerprint", fingerprint) d.Set("encrypted_password", encrypted) - requiredVersion, _ := version.NewVersion("8.0.0") - currentVersion, err := serverVersion(db) + /* ALTER USER syntax introduced in MySQL 5.7.6 deprecates SET PASSWORD (GH-8230) */ + serverVersion, err := serverVersion(db) if err != nil { - return err + return fmt.Errorf("Could not determine server version: %s", err) } - - passSQL := fmt.Sprintf("'%s'", password) - if currentVersion.LessThan(requiredVersion) { - passSQL = fmt.Sprintf("PASSWORD(%s)", passSQL) + ver, _ := version.NewVersion("5.7.6") + var stmtSQL string + if serverVersion.LessThan(ver) { + stmtSQL = fmt.Sprintf("SET PASSWORD FOR '%s'@'%s' = PASSWORD('%s')", + d.Get("user").(string), + d.Get("host").(string), + password) + } else { + stmtSQL = fmt.Sprintf("ALTER USER '%s'@'%s' IDENTIFIED BY '%s'", + d.Get("user").(string), + d.Get("host").(string), + password) } - sql := fmt.Sprintf("SET PASSWORD FOR '%s'@'%s' = %s", - d.Get("user").(string), - d.Get("host").(string), - passSQL) - - _, err = db.Exec(sql) + log.Println("Executing query:", stmtSQL) + _, err = db.Exec(stmtSQL) if err != nil { return err }