()
| 556 | } |
| 557 | |
| 558 | func startMySQL() error { |
| 559 | log.Println("--- Starting MySQL ---") |
| 560 | |
| 561 | // Check if MySQL is already running and accessible with the expected password |
| 562 | if mysqlReady() { |
| 563 | log.Println("mysql is already running and accepting connections") |
| 564 | return verifyMySQL() |
| 565 | } |
| 566 | |
| 567 | // Stop any existing MySQL service that might be running (e.g. pre-installed |
| 568 | // on GitHub Actions runners) to avoid port conflicts. |
| 569 | log.Println("stopping any existing mysql service") |
| 570 | _ = exec.Command("sudo", "service", "mysql", "stop").Run() |
| 571 | _ = exec.Command("sudo", "mysqladmin", "shutdown").Run() |
| 572 | // Give MySQL time to fully shut down |
| 573 | time.Sleep(2 * time.Second) |
| 574 | |
| 575 | if err := ensureMySQLDirs(); err != nil { |
| 576 | return err |
| 577 | } |
| 578 | |
| 579 | // Check if data directory already exists and has been initialized |
| 580 | needsPasswordReset := false |
| 581 | if mysqlInitialized() { |
| 582 | log.Println("mysql data directory already initialized, skipping initialization") |
| 583 | // Existing data dir may have an unknown root password (e.g. pre-installed |
| 584 | // MySQL on GitHub Actions). We'll need to use --skip-grant-tables to reset it. |
| 585 | needsPasswordReset = true |
| 586 | } else { |
| 587 | log.Println("initializing mysql data directory") |
| 588 | if err := run("sudo", "mysqld", "--initialize-insecure", "--user=mysql"); err != nil { |
| 589 | return fmt.Errorf("mysqld --initialize-insecure: %w", err) |
| 590 | } |
| 591 | } |
| 592 | |
| 593 | if needsPasswordReset { |
| 594 | // Start with --skip-grant-tables to reset the unknown root password. |
| 595 | if err := startMySQLDaemon("--skip-grant-tables"); err != nil { |
| 596 | return err |
| 597 | } |
| 598 | |
| 599 | log.Println("resetting root password via --skip-grant-tables") |
| 600 | resetSQL := "FLUSH PRIVILEGES; ALTER USER 'root'@'localhost' IDENTIFIED WITH caching_sha2_password BY 'mysecretpassword';" |
| 601 | if err := run("mysql", "-u", "root", "-e", resetSQL); err != nil { |
| 602 | return fmt.Errorf("resetting mysql root password: %w", err) |
| 603 | } |
| 604 | |
| 605 | // Restart without --skip-grant-tables |
| 606 | log.Println("restarting mysql normally") |
| 607 | if err := run("sudo", "mysqladmin", "-u", "root", "-pmysecretpassword", "shutdown"); err != nil { |
| 608 | // If mysqladmin fails, try killing the process directly |
| 609 | _ = run("sudo", "pkill", "-f", "mysqld") |
| 610 | } |
| 611 | time.Sleep(2 * time.Second) |
| 612 | |
| 613 | if err := startMySQLDaemon(); err != nil { |
| 614 | return err |
| 615 | } |
no test coverage detected