简体   繁体   English

连接到Microsoft Azure VM中Docker容器内的MySQL Server

[英]Connect to MySQL Server inside Docker container in an Microsoft Azure VM

I created an Ubuntu 15.10 VM on Microsoft Azure. 我在Microsoft Azure上创建了Ubuntu 15.10 VM。 On the Server i created a docker container with MySQL running (IP: 172.17.0.2) 在服务器上,我创建了一个运行MySQL的docker容器(IP:172.17.0.2)

CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
4255233555db        mysql               "/entrypoint.sh mysql"   16 hours ago        Up 16 hours         0.0.0.0:3306->3306/tcp   compose_mysql_1

I can connect over ssh to the VM and then access the MySQL-Server running in the docker container. 我可以通过ssh连接到VM,然后访问在docker容器中运行的MySQL-Server。

I now wanna have access to the MySQL-Server from the outside so that my WebApp on Microsoft Azure (same Ressource Group) can connect to the database. 现在,我想从外部访问MySQL-Server,以便我在Microsoft Azure(相同的资源组)上的WebApp可以连接到数据库。

I already forwarded the port 3306 of the VM to 172.17.0.2:3306. 我已经将虚拟机的端口3306转发到了172.17.0.2:3306。

With MySQL-Workbench from my local PC i can't connect to the MySQL-Server. 使用本地计算机上的MySQL-Workbench,我无法连接到MySQL-Server。 In fact i am not sure which credentials i need to provide to connect. 实际上,我不确定需要提供哪些凭据才能进行连接。 I tried it with the public ip of the VM and the root password of the MySQL-Server. 我尝试使用VM的公共IP和MySQL-Server的root密码进行尝试。 But shouldn't i somewhere also provide the password and user for the VM itself? 但是我不应该在某处也为虚拟机本身提供密码和用户吗?

Other than the firewall on the actual VM, Azure manage ports security using a separate VPC Network security group. 除了实际VM上的防火墙之外,Azure使用单独的VPC网络安全组管理端口安全。 By default, the Network group associated with your VM does not allow inbound TCP connections on port 3306. You have to add that manually: 默认情况下,与您的VM关联的Network组不允许端口3306上的入站TCP连接。您必须手动添加该端口:

  1. Go to Network interfaces 转到Network interfaces

图片1

  1. Select your network interface 选择您的网络接口

在此处输入图片说明

  1. Select Network security group 选择Network security group

在此处输入图片说明

  1. Select your group (usually only one here) 选择您的组(这里通常只有一个)

在此处输入图片说明

  1. Select Inbound security rules 选择Inbound security rules

在此处输入图片说明

  1. Click Add and Fill as follow 单击Add并填写,如下所示

在此处输入图片说明

  1. Finally apply the changes and wait for 1-2 minutes (time for the new settings to propagate correctly, although it should be instantly) 最后,应用更改并等待1-2分钟(新设置可以正确传播的时间,尽管应该立即生效)

在此处输入图片说明

Magic! 魔法!

Alternative to directly using official MySQL images 直接使用官方MySQL映像的替代方法

I also had troubles getting the official MySQL image running and accessible. 我也很难运行和访问官方的MySQL映像。 sameersbn/docker-mysql provides an nice and easy wrapper that lets you specify user and database to be set up. sameersbn / docker-mysql提供了一个简单易用的包装程序,可让您指定要设置的用户和数据库。

My docker-compose file has the following section: 我的docker-compose文件包含以下部分:

db:
  image: sameersbn/mysql:latest
  volumes:
    - /opt/mysql/data:/var/lib/mysql
  environment:
    - DB_USER=${DB_USER}
    - DB_PASS=${DB_PASS}
    - DB_NAME=theDatabaseName
  ports:
    - "3306:3306"

The details: 细节:

  • image - The above mentioned wrapper. image -上述包装器。
  • volumes - Mapping the data on the persistent host. volumes -在持久主机上映射数据。
  • environment - Variables that define the database setup. environment -定义数据库设置的变量。 Either give plain text as with DB_NAME or access environment variables through the ${...} syntax. 可以像使用DB_NAME一样提供纯文本,也可以通过${...}语法访问环境变量。
  • ports - Map out the port 3306 on the host so it may be accessed remotely. ports映射主机上的端口3306,以便可以对其进行远程访问。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法连接到在Vagrant VM中作为Docker容器安装的MySql - Can't Connect To MySql Which is Installed As Docker Container Inside Vagrant VM 如何连接远程docker容器mysql服务器 - how to connect remote docker container mysql server 如何在docker容器中连接外部mysql服务器 - How to connect external mysql server in docker container 无法连接在 docker 容器上运行的 mysql 服务器 - Not Able to connect mysql server running on docker container 如何从 Windows 机器中的 Docker 容器内部连接到本地主机上的 MySQL 服务器? - How to connect to MySQL server on localhost from inside of a Docker container in a windows machine? 当 Docker 容器内的 connect.Net 应用程序连接到远程 Mysql 服务器时出现问题 - Issue when connect .Net app inside a Docker container to remote Mysql Server 无法连接到Docker内的MySQL服务器 - Cannot connect to MySQL server inside Docker docker 容器内的 mysql 说“无法通过套接字 '/var/run/mysqld/mysqld.sock' 连接到本地 MySQL 服务器” - mysql inside a docker container says “Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' ” 在 Docker 容器内运行 Spring Boot 应用程序,无法连接 MySQL - Running Spring Boot app inside Docker container, unable to connect MySQL NodeJS无法连接到Docker容器内的MYSQL最新版本 - NodeJS could not connect to MYSQL latest version inside Docker Container
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM