无法从MySQL Workbench连接到dockerized MySQL服务器

我正在尝试从MySQL Workbench连接到我的dockerized mysql服务器。 我正在使用Windows 10.这是我的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"] 

我正在使用这个库来创build,运行Java等容器:

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

我在Java中这样做的实际方法是:

  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从之前创build的Docker镜像构build容器。 当我查看实际运行的Docker容器时,它看起来端口绑定工作。 当我用docker exec -it SQLContainer bash访问我的容器时,它工作正常,我可以访问我的SQL服务器。

当我尝试连接MySQL Workbench时,出现如下图所示的错误消息: MySQL Workbench Error Message

有人知道我在做什么错吗?

谢谢,

我想你需要为你的mysql服务器创build一个新的用户,并指定用户可以使用的地方。 这是因为只有通过本地主机才能访问根访问权限。

所以要创build一个新的用户,你需要在启动你的服务器的地方给RUN命令添加一些东西:

 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" 

这样你就可以创build一个名为newUser的新用户,可以从任何地方访问。