Skip to content

Commit

Permalink
Merge pull request #99 from cybozu-go/issue-98
Browse files Browse the repository at this point in the history
issue-98: Fixed determination of whether root user exists or not
  • Loading branch information
shunki-fujita authored Jun 20, 2024
2 parents 3580f25 + a8b42fa commit 759ab97
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
2 changes: 1 addition & 1 deletion cmd/moco-agent/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ func initializeMySQLForMOCO(ctx context.Context, socketPath string, logger logr.
if err == nil {
break
}
if server.IsAccessDenied(err) {
if server.UserNotExists(err) {
// There is no passwordless 'root'@'localhost' account.
// It means the initialization has been completed.
return nil
Expand Down
9 changes: 7 additions & 2 deletions server/connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,14 @@ func GetMySQLConnLocalSocket(user, password, socket string) (*sqlx.DB, error) {
return db, nil
}

func IsAccessDenied(err error) bool {
func UserNotExists(err error) bool {
// For security reason, error messages are randomly output when a user does not exist.
// https://github.com/mysql/mysql-server/commit/b40001faf6229dca668c9d03ba75c451f999c9f5
// This function assumes the user does not exist when the following message is output:
// ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
// ERROR 1524 (HY000): Plugin 'mysql_native_password' is not loaded
var merr *mysql.MySQLError
if errors.As(err, &merr) && merr.Number == 1045 {
if errors.As(err, &merr) && (merr.Number == 1045 || merr.Number == 1524) {
return true
}

Expand Down

0 comments on commit 759ab97

Please sign in to comment.