0

I'm trying to connect from the MySQL Workbench to my dockerized mysql server. I'm using Windows 10. Here is my Dockerfile:

FROM ubuntu:latest

# package updates & install mysql
RUN apt-get update && apt-get install -y mysql-server
RUN apt-get -y install supervisor

ADD supervisord.conf /etc/supervisor/conf.d/supervisord.conf

# bind sql script
ADD musicdb.sql /tmp/musicdb.sql

RUN sed -i -e"s/^bind-address\s*=\s*127.0.0.1/bind-address = 0.0.0.0/" /etc/mysql/my.cnf

EXPOSE 3306

RUN /bin/bash -c "/usr/bin/mysqld_safe &" && \
sleep 5 && \
mysql -u root -e "CREATE DATABASE musicdb" && \
mysql -u root musicdb < /tmp/musicdb.sql

CMD ["/usr/bin/supervisord", "-n"]

I'm using this library to create, run, etc. the container from Java:

https://github.com/spotify/docker-client

My actual method to do this in Java is:

    public static void createContainerWithPorts(){
        DockerClient docker;


        try {
            docker = DefaultDockerClient.fromEnv().build();

//PortBindings
            Map<String, List<PortBinding>> portBindings = new HashMap<>();

            List<PortBinding> hostPorts = new ArrayList<>();

            hostPorts.add(PortBinding.of("0.0.0.0", "3306"));
            portBindings.put("3306", hostPorts);

            HostConfig hostConfig = HostConfig.builder().portBindings(portBindings).build();
            ContainerConfig containerConfig = ContainerConfig.builder().hostConfig(hostConfig).image("mysql-container").exposedPorts("3306").build();

//create container
            ContainerCreation contaierCreation = docker.createContainer(containerConfig);
            String id = contaierCreation.id();

//start container
            docker.startContainer(id);

            ContainerInfo containerInfo = docker.inspectContainer(id);
            System.out.println(containerInfo.toString());

            Scanner scanner = new Scanner(System.in);
            scanner.next();
            docker.killContainer(id);
            docker.removeContainer(id);
            docker.close();
        } catch (DockerCertificateException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (DockerException e) {
            e.printStackTrace();
        }
    }

Java builds the Container fine from the before created Docker Image. When I'm looking on actual running Docker Container it looks that the port binding works. When I access my container with docker exec -it SQLContainer bash it works fine and I can access my SQL server.

When i try to connect with MySQL Workbench, I got the error message shown in the picture below: MySQL Workbench Error Message

Does someone know what I'm doing wrong?

Thanks,

1 Answer 1

1

I think you need to create a new user for your mysql server and specify from where the user can be used. This is because root access is only available through localhost I believe.

So to create a new user, you need to add a little something to the RUN command where you start your server:

RUN /bin/bash -c "/usr/bin/mysqld_safe &" && \
sleep 5 && \
mysql -u root -e "CREATE DATABASE musicdb" && \
mysql -u root musicdb < /tmp/musicdb.sql && \
mysql -u root -e "CREATE USER 'newUser'@'%' IDENTIFIED BY 'somePassword'" && \
mysql -u root -e "GRANT ALL PRIVILEGES ON *.* TO 'newUser'@'%' WITH GRANT OPTION"

That way you create a new user with the name newUser that can access from anywhere.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.