Fix: MySQL/MariaDB socket authentication - remove hardcoded -h flag for localhost

Issue: MySQL/MariaDB functions always used '-h hostname' flag, which can cause
issues with Unix socket authentication when connecting to localhost.

Similar to PostgreSQL peer authentication, MySQL prefers Unix socket connections
for localhost rather than TCP connections. Using '-h localhost' forces TCP which
may fail with socket-based authentication configurations.

Fixed locations:
1. internal/restore/safety.go:
   - checkMySQLDatabaseExists() - now conditionally adds -h flag
   - listMySQLUserDatabases() - now conditionally adds -h flag

2. cmd/placeholder.go:
   - mysqlRestoreCommand() - now conditionally adds -h flag

Pattern applied (consistent with PostgreSQL fixes):
- Skip -h flag when host is localhost, 127.0.0.1, or empty
- Only add -h flag for actual remote hosts
- Allows mysql client to use Unix socket connection for local access

This ensures MySQL/MariaDB operations work correctly with both:
- Socket authentication (localhost via Unix socket)
- Password authentication (remote hosts via TCP)
This commit is contained in:
2025-11-12 08:55:06 +00:00
parent 98f483ae11
commit eb3e5c0135
2 changed files with 27 additions and 12 deletions

View File

@@ -730,12 +730,17 @@ func containsSQLKeywords(content string) bool {
}
func mysqlRestoreCommand(archivePath string, compressed bool) string {
parts := []string{
"mysql",
"-h", cfg.Host,
parts := []string{"mysql"}
// Only add -h flag if host is not localhost (to use Unix socket)
if cfg.Host != "localhost" && cfg.Host != "127.0.0.1" && cfg.Host != "" {
parts = append(parts, "-h", cfg.Host)
}
parts = append(parts,
"-P", fmt.Sprintf("%d", cfg.Port),
"-u", cfg.User,
}
)
if cfg.Password != "" {
parts = append(parts, fmt.Sprintf("-p'%s'", cfg.Password))