Help us shape the future of Go! Take the 2026 Go Developer Survey

tidb

package module
v0.44.0 Latest Latest
Warning

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

Go to latest
Published: Aug 7, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Container

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

Container represents the TiDB container type used in the module

func Run

Run creates an instance of the TiDB container type

Example
package main

import (
	"context"
	"fmt"
	"log"

	_ "github.com/go-sql-driver/mysql"
	"github.com/testcontainers/testcontainers-go"
	"github.com/testcontainers/testcontainers-go/modules/tidb"
)

func main() {
	// runTiDBContainer {
	ctx := context.Background()

	ctr, err := tidb.Run(ctx,
		"pingcap/tidb:v8.4.0",
	)
	defer func() {
		if err := testcontainers.TerminateContainer(ctr); err != nil {
			log.Printf("failed to terminate container: %s", err)
		}
	}()
	if err != nil {
		log.Printf("failed to start container: %s", err)
		return
	}
	// }

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

	fmt.Println(state.Running)

}
Output:
true
Example (Connect)
package main

import (
	"context"
	"database/sql"
	"fmt"
	"log"

	_ "github.com/go-sql-driver/mysql"
	"github.com/testcontainers/testcontainers-go"
	"github.com/testcontainers/testcontainers-go/modules/tidb"
)

func main() {
	ctx := context.Background()

	ctr, err := tidb.Run(ctx,
		"pingcap/tidb:v8.4.0",
	)
	defer func() {
		if err := testcontainers.TerminateContainer(ctr); err != nil {
			log.Printf("failed to terminate container: %s", err)
		}
	}()
	if err != nil {
		log.Printf("failed to start container: %s", err)
		return
	}

	connectionString, err := ctr.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 TiDB: %s", err)
		return
	}
	defer db.Close()

	if err = db.Ping(); err != nil {
		log.Printf("failed to ping TiDB: %s", err)
		return
	}

	var result int
	row := db.QueryRow("SELECT 1")
	if err = row.Scan(&result); err != nil {
		log.Printf("failed to scan row: %s", err)
		return
	}

	fmt.Println(result)

}
Output:
1

func (*Container) ConnectionString

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

ConnectionString returns a DSN connection string for the TiDB container, using the MySQL driver format. It is possible to pass extra parameters to the connection string, e.g. "tls=skip-verify".

func (*Container) MustConnectionString

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

MustConnectionString panics if the connection string cannot be determined.

Jump to

Keyboard shortcuts

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