Skip to content

Commit ce30b9a

Browse files
committed
Fix MySQL root password setup for auth_socket plugin
On GitHub Actions ubuntu-24.04, the pre-installed MySQL 8.0 uses the auth_socket plugin for root, which requires the OS user to match the MySQL user. Since the runner runs as "runner" (not "root"), plain `mysql -u root` fails with access denied. Fix by falling back to `sudo mysql -u root` when the non-sudo attempt fails, which satisfies auth_socket's OS user check. https://claude.ai/code/session_01CsyRwSkRxBcQoaQFVkMQsJ
1 parent 822c758 commit ce30b9a

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

cmd/sqlc-test-setup/main.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -333,10 +333,15 @@ func startMySQL() error {
333333
log.Println("mysql root password already set to expected value, skipping")
334334
} else {
335335
log.Println("setting mysql root password with caching_sha2_password plugin")
336-
// Try via socket (works when auth_socket is the plugin or password is blank)
337-
if err := run("mysql", "-u", "root", "-e",
338-
"ALTER USER 'root'@'localhost' IDENTIFIED WITH caching_sha2_password BY 'mysecretpassword'; FLUSH PRIVILEGES;"); err != nil {
339-
return fmt.Errorf("setting mysql root password: %w", err)
336+
alterSQL := "ALTER USER 'root'@'localhost' IDENTIFIED WITH caching_sha2_password BY 'mysecretpassword'; FLUSH PRIVILEGES;"
337+
// Try via socket without sudo (works when password is blank)
338+
if err := run("mysql", "-u", "root", "-e", alterSQL); err != nil {
339+
// Try via socket with sudo (needed when auth_socket plugin is
340+
// active, since it requires the OS user to match the MySQL user)
341+
log.Println("retrying with sudo (auth_socket requires OS root user)")
342+
if err := run("sudo", "mysql", "-u", "root", "-e", alterSQL); err != nil {
343+
return fmt.Errorf("setting mysql root password: %w", err)
344+
}
340345
}
341346
}
342347

0 commit comments

Comments
 (0)