Skip to content

Commit f3ffed6

Browse files
sevenevesrnorthIvan Stanislavciuc
authored
Fix problem of starting up mysql 5.7.33 version when user is root (#3953)
Co-authored-by: Richard North <rich.north@gmail.com> Co-authored-by: Ivan Stanislavciuc <istanislavciuc@ebay.com>
1 parent 9183f8f commit f3ffed6

File tree

2 files changed

+59
-4
lines changed

2 files changed

+59
-4
lines changed

modules/mysql/src/main/java/org/testcontainers/containers/MySQLContainer.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,12 @@ protected Set<Integer> getLivenessCheckPorts() {
6262
@Override
6363
protected void configure() {
6464
optionallyMapResourceParameterAsVolume(MY_CNF_CONFIG_OVERRIDE_PARAM_NAME, "/etc/mysql/conf.d",
65-
"mysql-default-conf");
65+
"mysql-default-conf");
6666

6767
addEnv("MYSQL_DATABASE", databaseName);
68-
addEnv("MYSQL_USER", username);
68+
if (!MYSQL_ROOT_USER.equalsIgnoreCase(username)) {
69+
addEnv("MYSQL_USER", username);
70+
}
6971
if (password != null && !password.isEmpty()) {
7072
addEnv("MYSQL_PASSWORD", password);
7173
addEnv("MYSQL_ROOT_PASSWORD", password);
@@ -98,12 +100,12 @@ public String getJdbcUrl() {
98100
protected String constructUrlForConnection(String queryString) {
99101
String url = super.constructUrlForConnection(queryString);
100102

101-
if (! url.contains("useSSL=")) {
103+
if (!url.contains("useSSL=")) {
102104
String separator = url.contains("?") ? "&" : "?";
103105
url = url + separator + "useSSL=false";
104106
}
105107

106-
if (! url.contains("allowPublicKeyRetrieval=")) {
108+
if (!url.contains("allowPublicKeyRetrieval=")) {
107109
url = url + "&allowPublicKeyRetrieval=true";
108110
}
109111

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package org.testcontainers.containers;
2+
3+
import lombok.extern.slf4j.Slf4j;
4+
import org.junit.Test;
5+
import org.junit.runner.RunWith;
6+
import org.junit.runners.Parameterized;
7+
import org.testcontainers.containers.output.Slf4jLogConsumer;
8+
9+
import java.sql.Connection;
10+
import java.sql.DriverManager;
11+
import java.sql.SQLException;
12+
13+
@Slf4j
14+
@RunWith(Parameterized.class)
15+
public class MySQLRootAccountTest {
16+
17+
@Parameterized.Parameters(name = "{0}")
18+
public static String[] params() {
19+
return new String[]{
20+
"mysql:8",
21+
"mysql:5"
22+
};
23+
}
24+
25+
@Parameterized.Parameter()
26+
public String image;
27+
28+
@Test
29+
public void testRootAccountUsageWithDefaultPassword() throws SQLException {
30+
testWithDB(new MySQLContainer<>(image).withUsername("root"));
31+
}
32+
33+
@Test
34+
public void testRootAccountUsageWithEmptyPassword() throws SQLException {
35+
testWithDB(new MySQLContainer<>(image).withUsername("root").withPassword(""));
36+
}
37+
38+
@Test
39+
public void testRootAccountUsageWithCustomPassword() throws SQLException {
40+
testWithDB(new MySQLContainer<>(image).withUsername("root").withPassword("not-default"));
41+
}
42+
43+
private void testWithDB(MySQLContainer<?> db) throws SQLException {
44+
try {
45+
db.withLogConsumer(new Slf4jLogConsumer(log)).start();
46+
Connection connection = DriverManager.getConnection(db.getJdbcUrl(), db.getUsername(), db.getPassword());
47+
connection.createStatement().execute("SELECT 1");
48+
connection.createStatement().execute("set sql_log_bin=0"); // requires root
49+
} finally {
50+
db.close();
51+
}
52+
}
53+
}

0 commit comments

Comments
 (0)