1. <tfoot id='3TkVx'></tfoot>
    2. <i id='3TkVx'><tr id='3TkVx'><dt id='3TkVx'><q id='3TkVx'><span id='3TkVx'><b id='3TkVx'><form id='3TkVx'><ins id='3TkVx'></ins><ul id='3TkVx'></ul><sub id='3TkVx'></sub></form><legend id='3TkVx'></legend><bdo id='3TkVx'><pre id='3TkVx'><center id='3TkVx'></center></pre></bdo></b><th id='3TkVx'></th></span></q></dt></tr></i><div id='3TkVx'><tfoot id='3TkVx'></tfoot><dl id='3TkVx'><fieldset id='3TkVx'></fieldset></dl></div>
      <legend id='3TkVx'><style id='3TkVx'><dir id='3TkVx'><q id='3TkVx'></q></dir></style></legend>

      <small id='3TkVx'></small><noframes id='3TkVx'>

          <bdo id='3TkVx'></bdo><ul id='3TkVx'></ul>

        如何将本地托管的 MySQL 数据库与 docker 容器连接

        时间:2023-09-18
        <legend id='gIyyv'><style id='gIyyv'><dir id='gIyyv'><q id='gIyyv'></q></dir></style></legend>
        <i id='gIyyv'><tr id='gIyyv'><dt id='gIyyv'><q id='gIyyv'><span id='gIyyv'><b id='gIyyv'><form id='gIyyv'><ins id='gIyyv'></ins><ul id='gIyyv'></ul><sub id='gIyyv'></sub></form><legend id='gIyyv'></legend><bdo id='gIyyv'><pre id='gIyyv'><center id='gIyyv'></center></pre></bdo></b><th id='gIyyv'></th></span></q></dt></tr></i><div id='gIyyv'><tfoot id='gIyyv'></tfoot><dl id='gIyyv'><fieldset id='gIyyv'></fieldset></dl></div>
          • <tfoot id='gIyyv'></tfoot>

            1. <small id='gIyyv'></small><noframes id='gIyyv'>

                <bdo id='gIyyv'></bdo><ul id='gIyyv'></ul>
                  <tbody id='gIyyv'></tbody>

                  本文介绍了如何将本地托管的 MySQL 数据库与 docker 容器连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  通过 docker-compose.yml 我能够运行该应用程序.现在我们想将应用程序移至生产环境,但我们不想使用容器数据库.那么有什么方法可以让我使用 docker-compose 将我的本地 MySQL 数据库与应用程序连接起来?

                  Through docker-compose.yml I am able to run the application. Now we want to move the application to production, But we don't want to use the container database. So is there any way so that I can connect my local MySQL database with the application using docker-compose?

                  我的 docker-compose.yml 看起来像:

                  My docker-compose.yml looks like:

                  version: '3'
                  services:
                    web-app:
                      build:
                        context: .
                        dockerfile: web-app/Dockerfile
                      ports:
                        - 8080:8080
                      links:
                        - app-db
                  
                    app-db:
                      build:
                        context: .
                        dockerfile: app-db/Dockerfile
                  
                      environment:
                      - MYSQL_ROOT_PASSWORD=password
                      - MYSQL_DATABASE=Optimize
                      ports:
                        - 3306:3306
                  

                  我只想连接到本地托管的 mysql 数据库,而不是 app-db 部分.

                  Instead of app-db part I just want to connect to my locally hosted mysql database.

                  推荐答案

                  在docker网络中查找宿主机ip.如果你使用 docker-compose.yml version: "3" 那很可能是那个 IP 是:172.18.0.1但是确认它搜索您的容器(您的主机)的网关":

                  Find the host machine ip in the docker network. If you use docker-compose.yml version: "3" it's probably that that IP is: 172.18.0.1, but confirm it searching for the "Gateway" of your container (your host):

                  docker inspect <container-id-or-name> | grep Gateway
                  "Gateway": "",
                              "IPv6Gateway": "",
                              "Gateway": "172.18.0.1",
                              "IPv6Gateway": "",
                  

                  因此在您的 docker 应用程序中指向 MySQL 如下:172.18.0.1:3306(可能在配置文件中).考虑到该 IP 是固定的,只要 docker 网络仍然相同(网络是由 docker-compose 创建的,除非您执行 docker-compose down),否则不会删除它

                  So inside your docker application point to MySQL as this: 172.18.0.1:3306 (maybe in a configuration file). Take into account that that IP is fixed as long as the docker network still the same (the network is created by docker-compose, and it is not removed unless you do docker-compose down)

                  另外,检查您的 MySQL 是否正在侦听其所有接口.在您的 my.cnf 中搜索 bind-address 应该是 0.0.0.0(如果您的服务器有公共 IP,请考虑安全问题).

                  Also, check that your MySQL is listening to all of its interfaces. In your my.cnf search for bind-address that should be 0.0.0.0 (consider security issues if your server has public IP).

                  作为替代方案,您可以将与主机相同的网络连接到容器中,以便共享本地主机,因此容器将在那里找到 mysql.使用网络模式作为主机":

                  As an alternative you can bring to the container the same networking as your host, in order to share the localhost, so the container will find mysql there. Use network mode as "host":

                  version: '3'
                  services:
                    web-app:
                      build:
                        context: .
                        dockerfile: web-app/Dockerfile
                      ports:
                         - 8080:8080
                      network_mode: "host"
                  

                  然后,将您的 hibernate.properties 指向 mysql,如下所示:localhost:3306

                  Then, point in your hibernate.properties to mysql as this: localhost:3306

                  这篇关于如何将本地托管的 MySQL 数据库与 docker 容器连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:Docker (Apple Silicon/M1 Preview) MySQL“在清单列表条目中没有与 linux/ar 下一篇:无法从本地连接到 mysql docker

                  相关文章

                  <legend id='c3nRS'><style id='c3nRS'><dir id='c3nRS'><q id='c3nRS'></q></dir></style></legend>
                  <tfoot id='c3nRS'></tfoot>

                    <bdo id='c3nRS'></bdo><ul id='c3nRS'></ul>
                  1. <i id='c3nRS'><tr id='c3nRS'><dt id='c3nRS'><q id='c3nRS'><span id='c3nRS'><b id='c3nRS'><form id='c3nRS'><ins id='c3nRS'></ins><ul id='c3nRS'></ul><sub id='c3nRS'></sub></form><legend id='c3nRS'></legend><bdo id='c3nRS'><pre id='c3nRS'><center id='c3nRS'></center></pre></bdo></b><th id='c3nRS'></th></span></q></dt></tr></i><div id='c3nRS'><tfoot id='c3nRS'></tfoot><dl id='c3nRS'><fieldset id='c3nRS'></fieldset></dl></div>
                  2. <small id='c3nRS'></small><noframes id='c3nRS'>