Testcontainers MySQL module for creating throwaway MySQL database containers for Java integration testing
npx @tessl/cli install tessl/maven-org-testcontainers--mysql@1.21.00
# Testcontainers MySQL
1
2
Testcontainers MySQL provides a MySQL database container implementation for the Testcontainers framework, enabling developers to easily spin up throwaway MySQL database instances for integration testing in Java applications. The module offers comprehensive MySQL-specific configuration options, automatic JDBC URL construction, driver detection, and seamless integration with JUnit test frameworks.
3
4
## Package Information
5
6
- **Package Name**: org.testcontainers:mysql
7
- **Package Type**: Maven/Gradle dependency
8
- **Language**: Java
9
- **Installation**:
10
- Maven: `<dependency><groupId>org.testcontainers</groupId><artifactId>mysql</artifactId><version>1.21.3</version></dependency>`
11
- Gradle: `implementation 'org.testcontainers:mysql:1.21.3'`
12
13
## Core Imports
14
15
```java
16
import org.testcontainers.containers.MySQLContainer;
17
```
18
19
For provider-based usage:
20
21
```java
22
import org.testcontainers.containers.MySQLContainerProvider;
23
```
24
25
For R2DBC reactive database access:
26
27
```java
28
import org.testcontainers.containers.MySQLR2DBCDatabaseContainer;
29
import org.testcontainers.containers.MySQLR2DBCDatabaseContainerProvider;
30
```
31
32
## Basic Usage
33
34
```java
35
import org.testcontainers.containers.MySQLContainer;
36
import org.testcontainers.utility.DockerImageName;
37
38
// Create MySQL container with specific image
39
MySQLContainer<?> mysql = new MySQLContainer<>(DockerImageName.parse("mysql:8.0"))
40
.withDatabaseName("testdb")
41
.withUsername("testuser")
42
.withPassword("testpass");
43
44
// Start the container
45
mysql.start();
46
47
// Get JDBC connection details
48
String jdbcUrl = mysql.getJdbcUrl();
49
String username = mysql.getUsername();
50
String password = mysql.getPassword();
51
52
// Create database connection
53
Connection connection = mysql.createConnection("");
54
55
// Use with JUnit 5
56
@Testcontainers
57
class DatabaseTest {
58
@Container
59
static MySQLContainer<?> mysql = new MySQLContainer<>("mysql:8.0")
60
.withDatabaseName("test")
61
.withUsername("test")
62
.withPassword("test");
63
64
@Test
65
void testDatabaseOperation() {
66
String jdbcUrl = mysql.getJdbcUrl();
67
// Test database operations...
68
}
69
}
70
```
71
72
## Architecture
73
74
The Testcontainers MySQL module is built around several key components:
75
76
- **MySQLContainer**: Main container class extending JdbcDatabaseContainer with MySQL-specific features
77
- **Configuration Management**: Custom MySQL configuration file override support via .cnf files
78
- **Driver Detection**: Automatic JDBC driver detection with fallback between modern and legacy MySQL drivers
79
- **Connection Management**: JDBC URL construction with proper SSL and authentication settings
80
- **Provider Pattern**: Factory classes for creating containers from connection URLs or configuration
81
- **R2DBC Integration**: Reactive database access through R2DBC wrapper classes
82
- **Test Integration**: Seamless integration with JUnit and other testing frameworks
83
84
## Capabilities
85
86
### MySQL Container Management
87
88
Core MySQL container functionality including lifecycle management, configuration, and connection handling.
89
90
```java { .api }
91
public class MySQLContainer<SELF extends MySQLContainer<SELF>> extends JdbcDatabaseContainer<SELF> {
92
public MySQLContainer(DockerImageName dockerImageName);
93
public String getJdbcUrl();
94
public String getDriverClassName();
95
public Connection createConnection(String queryString) throws SQLException, NoDriverFoundException;
96
public SELF withDatabaseName(String databaseName);
97
public SELF withUsername(String username);
98
public SELF withPassword(String password);
99
public SELF withConfigurationOverride(String configPath);
100
}
101
```
102
103
[MySQL Container](./mysql-container.md)
104
105
### Database Configuration
106
107
MySQL-specific configuration options including custom configuration files, environment variables, and connection parameters.
108
109
```java { .api }
110
public SELF withConfigurationOverride(String configPath);
111
public SELF withUrlParam(String paramName, String paramValue);
112
public SELF withStartupTimeoutSeconds(int startupTimeoutSeconds);
113
public SELF withConnectTimeoutSeconds(int connectTimeoutSeconds);
114
```
115
116
[Database Configuration](./database-configuration.md)
117
118
### Initialization and Scripts
119
120
Database initialization with SQL scripts and setup procedures.
121
122
```java { .api }
123
public SELF withInitScript(String initScriptPath);
124
public SELF withInitScripts(String... initScriptPaths);
125
public SELF withInitScripts(Iterable<String> initScriptPaths);
126
```
127
128
[Database Initialization](./database-initialization.md)
129
130
### Provider Pattern Support
131
132
Factory pattern implementation for creating MySQL containers from connection URLs and configuration objects.
133
134
```java { .api }
135
public class MySQLContainerProvider extends JdbcDatabaseContainerProvider {
136
public boolean supports(String databaseType);
137
public JdbcDatabaseContainer newInstance();
138
public JdbcDatabaseContainer newInstance(String tag);
139
public JdbcDatabaseContainer newInstance(ConnectionUrl connectionUrl);
140
}
141
```
142
143
[Provider Pattern](./provider-pattern.md)
144
145
### R2DBC Reactive Database Access
146
147
Reactive database access using R2DBC for non-blocking database operations.
148
149
```java { .api }
150
public class MySQLR2DBCDatabaseContainer implements R2DBCDatabaseContainer {
151
public static ConnectionFactoryOptions getOptions(MySQLContainer<?> container);
152
public ConnectionFactoryOptions configure(ConnectionFactoryOptions options);
153
}
154
155
public class MySQLR2DBCDatabaseContainerProvider implements R2DBCDatabaseContainerProvider {
156
public boolean supports(ConnectionFactoryOptions options);
157
public R2DBCDatabaseContainer createContainer(ConnectionFactoryOptions options);
158
}
159
```
160
161
[R2DBC Integration](./r2dbc-integration.md)
162
163
## Types
164
165
```java { .api }
166
// Core container configuration
167
public static final String NAME = "mysql";
168
public static final Integer MYSQL_PORT = 3306;
169
170
// Default credentials (package-private but accessible)
171
static final String DEFAULT_USER = "test";
172
static final String DEFAULT_PASSWORD = "test";
173
174
// Deprecated constants (still available for compatibility)
175
@Deprecated
176
public static final String DEFAULT_TAG = "5.7.34";
177
@Deprecated
178
public static final String IMAGE = "mysql";
179
180
// Exception types
181
public class NoDriverFoundException extends Exception { }
182
public class ContainerLaunchException extends RuntimeException { }
183
```