A MySQL plugin for the waitfor library that provides MySQL database readiness assertion capabilities. This library allows you to wait for MySQL databases to become available before proceeding with your application startup or tests.
- Simple Integration: Easy-to-use plugin for the waitfor framework
- Flexible URL Support: Supports various MySQL URL formats
- Configurable Timeouts: Customizable retry attempts and timeouts
- Context Awareness: Proper context handling for cancellation and timeouts
- Error Handling: Detailed error reporting for connection issues
go get github.com/go-waitfor/waitfor-mysqlpackage main
import (
"context"
"fmt"
"github.com/go-waitfor/waitfor"
"github.com/go-waitfor/waitfor-mysql"
"os"
)
func main() {
runner := waitfor.New(mysql.Use())
err := runner.Test(
context.Background(),
[]string{"mysql://user:password@localhost:3306/mydb"},
waitfor.WithAttempts(5),
)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}package main
import (
"context"
"log"
"time"
"github.com/go-waitfor/waitfor"
"github.com/go-waitfor/waitfor-mysql"
)
func main() {
// Create a new waitfor runner with MySQL support
runner := waitfor.New(mysql.Use())
// Test MySQL readiness
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
err := runner.Test(ctx, []string{"mysql://root:password@localhost:3306/myapp"})
if err != nil {
log.Fatalf("MySQL is not ready: %v", err)
}
log.Println("MySQL is ready!")
}package main
import (
"context"
"log"
"time"
"github.com/go-waitfor/waitfor"
"github.com/go-waitfor/waitfor-mysql"
)
func main() {
runner := waitfor.New(mysql.Use())
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute)
defer cancel()
err := runner.Test(
ctx,
[]string{
"mysql://user1:pass1@db1.example.com:3306/app1",
"mysql://user2:pass2@db2.example.com:3306/app2",
},
waitfor.WithAttempts(10),
waitfor.WithInterval(2*time.Second),
)
if err != nil {
log.Fatalf("One or more MySQL databases are not ready: %v", err)
}
log.Println("All MySQL databases are ready!")
}The library supports standard MySQL connection URLs with the following format:
mysql://[username[:password]@][host[:port]][/database][?parameters]
mysql://localhost:3306- Local MySQL on port 3306, no authenticationmysql://user:password@localhost/mydb- With authentication and databasemysql://user:password@mysql.example.com:3306/production- Remote MySQL with full URLmysql://root@localhost:3307- Custom port with username only
- username: MySQL username (optional)
- password: MySQL password (optional)
- host: MySQL server hostname or IP address (default: localhost)
- port: MySQL server port (default: 3306)
- database: Database name to connect to (optional)
- parameters: Additional MySQL driver parameters (optional)
The library supports all waitfor framework configuration options:
waitfor.WithAttempts(n): Set maximum number of connection attemptswaitfor.WithInterval(duration): Set interval between attemptswaitfor.WithTimeout(duration): Set timeout for individual attempts
The library provides detailed error information for troubleshooting:
import (
"context"
"log"
"strings"
)
err := runner.Test(ctx, []string{"mysql://invalid-host:3306/db"})
if err != nil {
// err will contain details about the connection failure
log.Printf("MySQL connection failed: %v", err)
// You can check for specific error types or patterns
if strings.Contains(err.Error(), "connection refused") {
log.Println("MySQL server is not running or not accessible")
}
}- Go 1.23 or later
- MySQL server (any version supported by go-sql-driver/mysql)
This library depends on:
- github.com/go-waitfor/waitfor - The core waitfor framework
- github.com/go-sql-driver/mysql - MySQL driver for Go
This project is licensed under the Apache License 2.0 - see the LICENSE file for details.
Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.
- waitfor - The main waitfor framework
- waitfor-postgresql - PostgreSQL plugin
- waitfor-redis - Redis plugin