简体   繁体   English

从docker mysql容器切换到主机的mysql,而无需使用network-mode =“ host”

[英]Switch from docker mysql container to host's mysql without using network-mode=“host”

My company has a docker-container based website (Ubuntu containers), deployed via GitHub => CircleCI => AWS. 我公司有一个基于docker-container的网站(Ubuntu容器),通过GitHub => CircleCI => AWS进行部署。 (This was set up by a consultant, who we are not currently working with. I'm trying to make sense out of everything on my own.) (这是由我们目前不与之合作的顾问设立的。我试图自己理解所有事情。)
I've copied the source files to my Windows 10 development PC. 我已将源文件复制到Windows 10开发PC。
Locally, the website is running successfully, until it tries to access MySQL data. 网站在本地运行成功,直到尝试访问MySQL数据为止。

I've installed MySQL (8) locally, with default settings ( port 3306 ) and location ( C:\\ProgramData\\MySQL\\MySQL Server 8.0\\Data ), and imported the database. 我已经在本地安装了MySQL(8),具有默认设置( port 3306 )和位置( C:\\ProgramData\\MySQL\\MySQL Server 8.0\\Data ),并导入了数据库。 I've created root user. 我已经创建了root用户。 Running test queries as root from MySQL Workbench works. 从MySQL Workbench作为root运行测试查询是可行的。

I've read Q&A's such as How to connect locally hosted MySQL database with the docker container . 我已经阅读了问答,例如如何将本地托管的MySQL数据库与docker容器连接

I've successfully bridged to host's IP (on EthernetV; Docker running in Hyper-V) 172.26.92.81 for other purposes, specifically to have XDebug in my website container talk to phpstorm on my Windows 10 host. 我已经成功地桥接至主机的IP(上EthernetV;码头工人在Hyper-V上运行) 172.26.92.81用于其他用途,特别是有XDebug的在我的网站容器谈话phpstorm我的Windows 10的主机上。 (I could use host.docker.internal for IP; but I'm making everything as "concrete" as possible, until everything works.) (我可以将host.docker.internal用于IP;但是我将所有内容尽可能“具体”化,直到一切正常为止。)

I just don't understand what I have to change where, for redirecting MySQLi queries (in php as create web pages) to the (development pc) host. 我只是不明白为将MySQLi查询(以php作为创建网页)重定向到(开发pc)主机所必须更改的内容。 (Assume I'm clueless about both MySQL and Docker and Apache, until proven otherwise.) (假设我对MySQL,Docker和Apache都一无所知,除非另有说明。)

Relevant parts of (original) docker-compose.yml : (原始) docker-compose.yml相关部分:

mywebsite:
    build:
    context: ./mywebsite/
    dockerfile: Dockerfile
    ...
    depends_on:
    - mysql
    links:
    - mysql
    environment:
    MYSQL_HOST: mysql
    MYSQL_USER: root
    MYSQL_PASS: xxx
    MYSQL_SCHEMA: xxx
    MYSQL_PORT: 3306
    MYSQL_CHARSET: utf8

mysql:
    image: mysql:5.7
    ports:
    - 3305:3306
    command: mysqld --sql_mode=""
    environment:
    MYSQL_DATABASE: xxx
    MYSQL_ROOT_PASSWORD: xxx
    volumes:
    - data:/var/lib/mysql

NOTE: Not going for "best practices" above; 注意:请勿采用上述“最佳做法”; I need to see a simple approach working. 我需要查看一种简单的方法。 Security comes later. 安全性稍后出现。

I'd prefer a solution that doesn't involve "network_mode: "host" -- solving it that way would avoid details that I need to understand. [The linked Q&A shows that host mode is the simplest solution, but that is "too simple" - obscures some important considerations.] 我更喜欢不涉及“ network_mode:” host“的解决方案-以这种方式解决会避免我需要了解的细节。[链接的问与答表明,主机模式是最简单的解决方案,但“简单”-遮盖了一些重要的注意事项。]

Host has MySQL on its port 3306. The mysql container shown above: is it using its own copy of MySQL? 主机的端口3306上有MySQL。上面显示的mysql容器:它是否使用自己的MySQL副本? Or is that already attempting to connect to host's MySQL? 还是已经尝试连接到主机的MySQL? And why does it have the port mapping 3305:3306 ? 为什么它具有端口映射3305:3306 (I can't change that to 3306:3306 ; if do so, it is unable to assign the port number. I assume that is because host's MySQL already uses 3306, and that port line is exposing the mysql container's MySQL?) (我无法将其更改为3306:3306 ;如果这样做,它将无法分配端口号。我认为这是因为主机的MySQL已经使用3306,并且该端口行正在暴露mysql容器的MySQL?)

The volume mapping data:... is where I should move the data to, if I want to use the mysql container as is? 卷映射data:... ,如果我想按原样使用mysql容器,应该将数据移至其中? That's probably what I will do for now - which may make this SO question moot - but I'd still like to know how to do what I ask. 那可能就是我现在要做的事情-可能使这个SO问题变得毫无意义-但我仍然想知道如何去做我要问的事情。

I'm assuming it is its own database, because it has its own version number (5.7). 我假设它是自己的数据库,因为它有自己的版本号(5.7)。
From php inside mywebsite : mywebsite内部的php中:

$connection = new \mysqli(
            $_ENV["MYSQL_HOST"],
            $_ENV["MYSQL_USER"],
            $_ENV["MYSQL_PASS"],
            'INFORMATION_SCHEMA',
            $_ENV["MYSQL_PORT"]
        );
$result = $connection->query("SELECT VERSION();");
# breakpoint: $result > one row > ['VERSION()'] = 5.7.25

which is the version number of that mysql image, not the host's mysql. 这是该mysql映像的版本号,而不是主机的mysql。

similarly, list of databases doesn't include some databases I see on the host mysql from Workbench. 同样,数据库列表不包含我在Workbench的主机mysql上看到的某些数据库。

What changes do I need to make in docker-compose.yml ? 我需要在docker-compose.yml进行哪些更改?


Do I also need to make changes in mywebsite's Dockerfile? 我还需要在mywebsite的Dockerfile中进行更改吗?

mywebsite is based on apache2 + php 7.3 . mywebsite基于apache2 + php 7.3 Dockerfile: Dockerfile:

FROM php:7.3.3-apache-stretch
...
RUN apt-get update && ... docker-php-ext-install ... mysqli \
    && docker-php-ext-enable mysqli ...
...
COPY /php.ini /usr/local/etc/php/
COPY /src/ /var/www/html/

First of all, the declaration: 首先,声明:

links:
    - mysql

in the mywebiste service is not needed and actually stays in the way of you solving your issue. 不需要mywebiste服务,它实际上会妨碍您解决问题。 You should remove it. 您应该删除它。

After doing that, you can try 2 things (both of them worked for me): 完成此操作后,您可以尝试2件事(两者对我都有用):

  1. change MYSQL_HOST to point to your computer's IP (not 'localhost', not '127.0.0.1' but your local network IP address) and start only the mywebsite service. 更改MYSQL_HOST以指向您计算机的IP(不是“ localhost”,不是“ 127.0.0.1”,而是您的本地IP地址),然后仅启动mywebsite服务。
  2. in version 3.0 or higher of docker-compose file you can add extra hosts: 在docker-compose文件的3.0版或更高版本中,您可以添加额外的主机:
mywebsite:
...
  extra_hosts:
    - "mysql:<your ip>"

I hope this helps 我希望这有帮助

暂无
暂无

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

相关问题 可以从运行 network_mode: 'host' 的 docker 容器连接到在 localhost 上运行的 mysql - Can connect to mysql running on localhost from docker container running with network_mode: 'host' 从主机访问Docker MySQL服务器,而不需要容器的IP地址 - Accessing a Docker MySQL server from the host machine, without needing the container's IP address 如何将 docker 容器上的 php 脚本与网络主机连接,同时在网络主机上使用 mysql Z05B6053C41A2130AFD68BDAB3E 容器 - How connect a php script on docker container with network host, with a mysql docker container also on network host 无法从主机连接到Mysql docker容器 - Cant connect to Mysql docker container from host 从主机连接到 docker 容器中的 mysql - Connect to mysql in a docker container from the host 无法从主机连接到 Docker 容器中的 mysql - Cannot connect to mysql in Docker container from host 从Docker容器访问主机mysql - Accessing the host mysql from the docker Container 在主机模式下从 docker 容器连接到 MySQL 主机数据库不起作用 - Connecting to MySQL host DB from docker containers in host mode not work 无法连接到 Mysql 5.7 运行为 docker 容器从 docker 主机使用 Z81C3BE090DAD54BF527E1 - Unable to connect to Mysql 5.7 running as docker container from docker host using mysql client Docker + Airflow - 无法从 Z05B6053C41A21340AFD6FC3B234Z 容器连接到主机上的 MySQL - Docker + Airflow - Unable to connect to MySQL on host from docker container
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM