mysql

package module
v0.37.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 25, 2025 License: MIT Imports: 7 Imported by: 33

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func WithConfigFile

func WithConfigFile(configFile string) testcontainers.CustomizeRequestOption

func WithDatabase

func WithDatabase(database string) testcontainers.CustomizeRequestOption

func WithDefaultCredentials

func WithDefaultCredentials() testcontainers.CustomizeRequestOption

func WithPassword

func WithPassword(password string) testcontainers.CustomizeRequestOption

func WithScripts

func WithScripts(scripts ...string) testcontainers.CustomizeRequestOption

func WithUsername

func WithUsername(username string) testcontainers.CustomizeRequestOption

Types

type MySQLContainer

type MySQLContainer struct {
	testcontainers.Container
	// contains filtered or unexported fields
}

MySQLContainer represents the MySQL container type used in the module

func Run added in v0.32.0

func Run(ctx context.Context, img string, opts ...testcontainers.ContainerCustomizer) (*MySQLContainer, error)

Run creates an instance of the MySQL container type

Example
// runMySQLContainer {
ctx := context.Background()

mysqlContainer, err := mysql.Run(ctx,
	"mysql:8.0.36",
	mysql.WithConfigFile(filepath.Join("testdata", "my_8.cnf")),
	mysql.WithDatabase("foo"),
	mysql.WithUsername("root"),
	mysql.WithPassword("password"),
	mysql.WithScripts(filepath.Join("testdata", "schema.sql")),
)
defer func() {
	if err := testcontainers.TerminateContainer(mysqlContainer); err != nil {
		log.Printf("failed to terminate container: %s", err)
	}
}()
if err != nil {
	log.Printf("failed to start container: %s", err)
	return
}
// }

state, err := mysqlContainer.State(ctx)
if err != nil {
	log.Printf("failed to get container state: %s", err)
	return
}

fmt.Println(state.Running)
Output:

true
Example (Connect)
ctx := context.Background()

mysqlContainer, err := mysql.Run(ctx,
	"mysql:8.0.36",
	mysql.WithConfigFile(filepath.Join("testdata", "my_8.cnf")),
	mysql.WithDatabase("foo"),
	mysql.WithUsername("root"),
	mysql.WithPassword("password"),
	mysql.WithScripts(filepath.Join("testdata", "schema.sql")),
)
defer func() {
	if err := testcontainers.TerminateContainer(mysqlContainer); err != nil {
		log.Printf("failed to terminate container: %s", err)
	}
}()
if err != nil {
	log.Printf("failed to start container: %s", err)
	return
}

connectionString, err := mysqlContainer.ConnectionString(ctx)
if err != nil {
	log.Printf("failed to get connection string: %s", err)
	return
}

db, err := sql.Open("mysql", connectionString)
if err != nil {
	log.Printf("failed to connect to MySQL: %s", err)
	return
}
defer db.Close()

if err = db.Ping(); err != nil {
	log.Printf("failed to ping MySQL: %s", err)
	return
}
stmt, err := db.Prepare("SELECT @@GLOBAL.tmpdir")
if err != nil {
	log.Printf("failed to prepare statement: %s", err)
	return
}
defer stmt.Close()
row := stmt.QueryRow()
tmpDir := ""
err = row.Scan(&tmpDir)
if err != nil {
	log.Printf("failed to scan row: %s", err)
	return
}

fmt.Println(tmpDir)
Output:

/tmp

func RunContainer deprecated

func RunContainer(ctx context.Context, opts ...testcontainers.ContainerCustomizer) (*MySQLContainer, error)

Deprecated: use Run instead RunContainer creates an instance of the MySQL container type

func (*MySQLContainer) ConnectionString

func (c *MySQLContainer) ConnectionString(ctx context.Context, args ...string) (string, error)

func (*MySQLContainer) MustConnectionString added in v0.30.0

func (c *MySQLContainer) MustConnectionString(ctx context.Context, args ...string) string

MustConnectionString panics if the address cannot be determined.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL