Skip to content

Commit

Permalink
Fix problem of starting up mysql 5.7.33 version when user is root (#3953
Browse files Browse the repository at this point in the history
)

Co-authored-by: Richard North <rich.north@gmail.com>
Co-authored-by: Ivan Stanislavciuc <istanislavciuc@ebay.com>
  • Loading branch information
3 people committed Apr 11, 2021
1 parent 9183f8f commit f3ffed6
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,12 @@ protected Set<Integer> getLivenessCheckPorts() {
@Override
protected void configure() {
optionallyMapResourceParameterAsVolume(MY_CNF_CONFIG_OVERRIDE_PARAM_NAME, "/etc/mysql/conf.d",
"mysql-default-conf");
"mysql-default-conf");

addEnv("MYSQL_DATABASE", databaseName);
addEnv("MYSQL_USER", username);
if (!MYSQL_ROOT_USER.equalsIgnoreCase(username)) {
addEnv("MYSQL_USER", username);
}
if (password != null && !password.isEmpty()) {
addEnv("MYSQL_PASSWORD", password);
addEnv("MYSQL_ROOT_PASSWORD", password);
Expand Down Expand Up @@ -98,12 +100,12 @@ public String getJdbcUrl() {
protected String constructUrlForConnection(String queryString) {
String url = super.constructUrlForConnection(queryString);

if (! url.contains("useSSL=")) {
if (!url.contains("useSSL=")) {
String separator = url.contains("?") ? "&" : "?";
url = url + separator + "useSSL=false";
}

if (! url.contains("allowPublicKeyRetrieval=")) {
if (!url.contains("allowPublicKeyRetrieval=")) {
url = url + "&allowPublicKeyRetrieval=true";
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package org.testcontainers.containers;

import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.testcontainers.containers.output.Slf4jLogConsumer;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

@Slf4j
@RunWith(Parameterized.class)
public class MySQLRootAccountTest {

@Parameterized.Parameters(name = "{0}")
public static String[] params() {
return new String[]{
"mysql:8",
"mysql:5"
};
}

@Parameterized.Parameter()
public String image;

@Test
public void testRootAccountUsageWithDefaultPassword() throws SQLException {
testWithDB(new MySQLContainer<>(image).withUsername("root"));
}

@Test
public void testRootAccountUsageWithEmptyPassword() throws SQLException {
testWithDB(new MySQLContainer<>(image).withUsername("root").withPassword(""));
}

@Test
public void testRootAccountUsageWithCustomPassword() throws SQLException {
testWithDB(new MySQLContainer<>(image).withUsername("root").withPassword("not-default"));
}

private void testWithDB(MySQLContainer<?> db) throws SQLException {
try {
db.withLogConsumer(new Slf4jLogConsumer(log)).start();
Connection connection = DriverManager.getConnection(db.getJdbcUrl(), db.getUsername(), db.getPassword());
connection.createStatement().execute("SELECT 1");
connection.createStatement().execute("set sql_log_bin=0"); // requires root
} finally {
db.close();
}
}
}

0 comments on commit f3ffed6

Please sign in to comment.