or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

database-configuration.mddatabase-initialization.mdindex.mdmysql-container.mdprovider-pattern.mdr2dbc-integration.md

mysql-container.mddocs/

0

# MySQL Container

1

2

Core MySQL container functionality providing lifecycle management, configuration, and connection handling for MySQL database containers in Testcontainers.

3

4

## Capabilities

5

6

### Container Creation

7

8

Create MySQL containers with various configuration options.

9

10

```java { .api }

11

/**

12

* Creates a new MySQL container with specified Docker image

13

* @param dockerImageName The MySQL Docker image to use

14

*/

15

public MySQLContainer(DockerImageName dockerImageName);

16

17

/**

18

* Creates a new MySQL container with string image name

19

* @param dockerImageName String image name (e.g., "mysql:8.0")

20

*/

21

public MySQLContainer(String dockerImageName);

22

23

/**

24

* @deprecated Use MySQLContainer(DockerImageName) instead

25

* Creates a MySQL container with default image and tag

26

*/

27

@Deprecated

28

public MySQLContainer();

29

```

30

31

**Usage Examples:**

32

33

```java

34

import org.testcontainers.containers.MySQLContainer;

35

import org.testcontainers.utility.DockerImageName;

36

37

// Recommended: Use DockerImageName

38

MySQLContainer<?> mysql = new MySQLContainer<>(DockerImageName.parse("mysql:8.0"));

39

40

// Alternative: Use string image name

41

MySQLContainer<?> mysql2 = new MySQLContainer<>("mysql:8.0.33");

42

43

// Start the container

44

mysql.start();

45

```

46

47

### JDBC Connection Management

48

49

Get JDBC connection details and create database connections.

50

51

```java { .api }

52

/**

53

* Returns the JDBC URL for connecting to this MySQL container

54

* @return Complete JDBC URL with host, port, database, and connection parameters

55

*/

56

public String getJdbcUrl();

57

58

/**

59

* Returns the appropriate MySQL JDBC driver class name

60

* Automatically detects and returns modern or legacy driver

61

* @return "com.mysql.cj.jdbc.Driver" or "com.mysql.jdbc.Driver" based on availability

62

*/

63

public String getDriverClassName();

64

65

/**

66

* Creates a database connection to the containerized MySQL instance

67

* @param queryString Additional query parameters to append to JDBC URL (must include '?')

68

* @return Active database connection

69

* @throws SQLException if connection fails

70

* @throws NoDriverFoundException if JDBC driver is not available

71

*/

72

public Connection createConnection(String queryString) throws SQLException, NoDriverFoundException;

73

74

/**

75

* Creates a database connection with additional properties

76

* @param queryString Additional query parameters to append to JDBC URL

77

* @param info Additional JDBC properties

78

* @return Active database connection

79

* @throws SQLException if connection fails

80

* @throws NoDriverFoundException if JDBC driver is not available

81

*/

82

public Connection createConnection(String queryString, Properties info)

83

throws SQLException, NoDriverFoundException;

84

```

85

86

**Usage Examples:**

87

88

```java

89

MySQLContainer<?> mysql = new MySQLContainer<>("mysql:8.0");

90

mysql.start();

91

92

// Get connection details

93

String jdbcUrl = mysql.getJdbcUrl();

94

String driverClass = mysql.getDriverClassName();

95

96

// Create basic connection

97

try (Connection conn = mysql.createConnection("")) {

98

// Use connection...

99

}

100

101

// Create connection with additional parameters

102

try (Connection conn = mysql.createConnection("?useUnicode=true")) {

103

Statement stmt = conn.createStatement();

104

ResultSet rs = stmt.executeQuery("SELECT 1");

105

// Process results...

106

}

107

108

// Create connection with properties

109

Properties props = new Properties();

110

props.setProperty("useSSL", "false");

111

try (Connection conn = mysql.createConnection("", props)) {

112

// Use connection...

113

}

114

```

115

116

### Database Configuration

117

118

Configure database name, credentials, and connection parameters.

119

120

```java { .api }

121

/**

122

* Sets the database name to create and connect to

123

* @param databaseName Name of the database to create

124

* @return Container instance for method chaining

125

*/

126

public SELF withDatabaseName(String databaseName);

127

128

/**

129

* Sets the username for database connections

130

* @param username Database username

131

* @return Container instance for method chaining

132

*/

133

public SELF withUsername(String username);

134

135

/**

136

* Sets the password for database connections

137

* @param password Database password

138

* @return Container instance for method chaining

139

*/

140

public SELF withPassword(String password);

141

142

/**

143

* Returns the configured database name

144

* @return Database name

145

*/

146

public String getDatabaseName();

147

148

/**

149

* Returns the configured username

150

* @return Database username

151

*/

152

public String getUsername();

153

154

/**

155

* Returns the configured password

156

* @return Database password

157

*/

158

public String getPassword();

159

160

/**

161

* Returns the test query used for health checks

162

* @return "SELECT 1" test query

163

*/

164

public String getTestQueryString();

165

```

166

167

**Usage Examples:**

168

169

```java

170

MySQLContainer<?> mysql = new MySQLContainer<>("mysql:8.0")

171

.withDatabaseName("myapp_test")

172

.withUsername("app_user")

173

.withPassword("secure_password");

174

175

mysql.start();

176

177

// Retrieve configuration

178

String dbName = mysql.getDatabaseName(); // "myapp_test"

179

String user = mysql.getUsername(); // "app_user"

180

String pass = mysql.getPassword(); // "secure_password"

181

182

// Connection URL includes the configuration

183

String url = mysql.getJdbcUrl();

184

// jdbc:mysql://localhost:32768/myapp_test?useSSL=false&allowPublicKeyRetrieval=true

185

```

186

187

### MySQL Configuration Override

188

189

Override MySQL configuration using custom .cnf files.

190

191

```java { .api }

192

/**

193

* Provides a custom MySQL configuration file override

194

* @param configPath Path to custom MySQL .cnf configuration file

195

* @return Container instance for method chaining

196

*/

197

public SELF withConfigurationOverride(String configPath);

198

```

199

200

**Usage Examples:**

201

202

```java

203

// Create custom MySQL configuration

204

MySQLContainer<?> mysql = new MySQLContainer<>("mysql:8.0")

205

.withConfigurationOverride("mysql-config/custom.cnf");

206

207

mysql.start();

208

```

209

210

The custom configuration file at `mysql-config/custom.cnf` might contain:

211

212

```ini

213

[mysqld]

214

innodb_buffer_pool_size=256M

215

max_connections=500

216

slow_query_log=1

217

slow_query_log_file=/var/log/mysql/slow.log

218

```

219

220

### Driver Instance Management

221

222

Get JDBC driver instances for direct database operations.

223

224

```java { .api }

225

/**

226

* Obtain an instance of the correct JDBC driver for MySQL

227

* @return MySQL JDBC Driver instance

228

* @throws NoDriverFoundException if MySQL JDBC driver is not available on classpath

229

*/

230

public Driver getJdbcDriverInstance() throws NoDriverFoundException;

231

```

232

233

**Usage Examples:**

234

235

```java

236

MySQLContainer<?> mysql = new MySQLContainer<>("mysql:8.0");

237

mysql.start();

238

239

try {

240

Driver driver = mysql.getJdbcDriverInstance();

241

242

// Use driver directly

243

Properties props = new Properties();

244

props.setProperty("user", mysql.getUsername());

245

props.setProperty("password", mysql.getPassword());

246

247

Connection conn = driver.connect(mysql.getJdbcUrl(), props);

248

// Use connection...

249

} catch (NoDriverFoundException e) {

250

// Handle missing MySQL JDBC driver

251

System.err.println("MySQL JDBC driver not found: " + e.getMessage());

252

}

253

```

254

255

### Liveness Check Configuration

256

257

Configure container health check behavior.

258

259

```java { .api }

260

/**

261

* @deprecated Use getLivenessCheckPortNumbers() instead

262

* Returns the ports on which to check if the container is ready

263

* @return Set of port numbers for liveness checks

264

*/

265

@Deprecated

266

protected Set<Integer> getLivenessCheckPorts();

267

```

268

269

## Constants and Default Values

270

271

```java { .api }

272

// Container identification

273

public static final String NAME = "mysql";

274

275

// Network configuration

276

public static final Integer MYSQL_PORT = 3306;

277

278

// Default image configuration (deprecated)

279

@Deprecated

280

public static final String DEFAULT_TAG = "5.7.34";

281

@Deprecated

282

public static final String IMAGE = "mysql";

283

284

// Default credentials (package-private)

285

static final String DEFAULT_USER = "test";

286

static final String DEFAULT_PASSWORD = "test";

287

```