• <tfoot id='EPxKd'></tfoot>

  • <small id='EPxKd'></small><noframes id='EPxKd'>

      <legend id='EPxKd'><style id='EPxKd'><dir id='EPxKd'><q id='EPxKd'></q></dir></style></legend>

          <bdo id='EPxKd'></bdo><ul id='EPxKd'></ul>
        <i id='EPxKd'><tr id='EPxKd'><dt id='EPxKd'><q id='EPxKd'><span id='EPxKd'><b id='EPxKd'><form id='EPxKd'><ins id='EPxKd'></ins><ul id='EPxKd'></ul><sub id='EPxKd'></sub></form><legend id='EPxKd'></legend><bdo id='EPxKd'><pre id='EPxKd'><center id='EPxKd'></center></pre></bdo></b><th id='EPxKd'></th></span></q></dt></tr></i><div id='EPxKd'><tfoot id='EPxKd'></tfoot><dl id='EPxKd'><fieldset id='EPxKd'></fieldset></dl></div>
      1. “无法连接到本地 MySQL 服务器"在 docker-compose 中

        时间:2024-04-16

              <small id='msJJF'></small><noframes id='msJJF'>

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

                  <legend id='msJJF'><style id='msJJF'><dir id='msJJF'><q id='msJJF'></q></dir></style></legend>

                  <i id='msJJF'><tr id='msJJF'><dt id='msJJF'><q id='msJJF'><span id='msJJF'><b id='msJJF'><form id='msJJF'><ins id='msJJF'></ins><ul id='msJJF'></ul><sub id='msJJF'></sub></form><legend id='msJJF'></legend><bdo id='msJJF'><pre id='msJJF'><center id='msJJF'></center></pre></bdo></b><th id='msJJF'></th></span></q></dt></tr></i><div id='msJJF'><tfoot id='msJJF'></tfoot><dl id='msJJF'><fieldset id='msJJF'></fieldset></dl></div>
                  <tfoot id='msJJF'></tfoot>
                • 本文介绍了“无法连接到本地 MySQL 服务器"在 docker-compose 中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有带有 MySQL 的 Laravel 应用程序,我正在尝试使用 docker-compose 在 Docker 中运行它.但是当应用程序尝试连接数据库时,它会抛出:

                  I have Laravel application with MySQL and I'm trying to run it in Docker using docker-compose. But when app trying to connect DB, it throws:

                  PDOException in Connector.php line 55: SQLSTATE[HY000] [2002] Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)

                  PDOException in Connector.php line 55: SQLSTATE[HY000] [2002] Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)

                  我的 docker-compose.yml:

                  My docker-compose.yml:

                  load_balancer:
                  image: tutum/haproxy
                  links:
                      - web
                  ports:
                      - "80:80"
                  
                  cache:
                      image: redis
                  
                  db:
                      image: mysql
                      environment:
                          MYSQL_ROOT_PASSWORD: Q1w2e3r4t5
                          MYSQL_DATABASE: regappbase
                  
                  web:
                      image: andrewmclagan/nginx-hhvm
                      links:
                          - db
                          - cache
                      volumes: 
                          - ./:/var/www
                      environment:
                          - APP_ENV=local
                          - DB_DATABASE=regappbase
                          - DB_PASSWORD=Q1w2e3r4t5
                          - VIRTUAL_HOST=laravel.local
                  

                  我的 .env:

                  APP_ENV=production
                  APP_DEBUG=true
                  APP_KEY=SomeRandomString
                  APP_URL=http://laravel.local
                  
                  DB_CONNECTION=mysql
                  DB_HOST=localhost
                  DB_PORT=3306
                  DB_DATABASE=regappbase
                  DB_USERNAME=root
                  DB_PASSWORD=Q1w2e3r4t5
                  
                  CACHE_DRIVER=file
                  SESSION_DRIVER=file
                  QUEUE_DRIVER=sync
                  
                  REDIS_HOST=laravel.local
                  REDIS_PASSWORD=null
                  REDIS_PORT=6379
                  
                  MAIL_DRIVER=smtp
                  MAIL_HOST=mailtrap.io
                  MAIL_PORT=2525
                  

                  推荐答案

                  默认情况下,Mysql 连接到localhost"会尝试通过本地计算机上的套接字进行连接.您的 DB_HOST 应该是链接的容器名称,db".

                  Mysql connections to "localhost" by default attempt to connect over a socket present on the local machine. Your DB_HOST should be the linked container name, "db".

                  来自 MYSQL 文档:

                  在 Unix 上,MySQL 程序特别对待主机名 localhost,与其他基于网络的程序相比,其方式可能与您期望的不同.对于到本地主机的连接,MySQL 程序尝试使用 Unix 套接字文件连接到本地服务器.即使提供了 --port 或 -P 选项来指定端口号,也会发生这种情况.要确保客户端与本地服务器建立 TCP/IP 连接,请使用 --host 或 -h 指定主机名值 127.0.0.1,或本地服务器的 IP 地址或名称.您还可以使用 --protocol=TCP 选项显式指定连接协议,即使对于本地主机也是如此.例如:

                  On Unix, MySQL programs treat the host name localhost specially, in a way that is likely different from what you expect compared to other network-based programs. For connections to localhost, MySQL programs attempt to connect to the local server by using a Unix socket file. This occurs even if a --port or -P option is given to specify a port number. To ensure that the client makes a TCP/IP connection to the local server, use --host or -h to specify a host name value of 127.0.0.1, or the IP address or name of the local server. You can also specify the connection protocol explicitly, even for localhost, by using the --protocol=TCP option. For example:

                  这篇关于“无法连接到本地 MySQL 服务器"在 docker-compose 中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:mySQL init 脚本未与 docker-compose 一起运行 下一篇:Docker 中的 Mysql 连接错误与 Spriing 启动

                  相关文章

                    • <bdo id='WqflV'></bdo><ul id='WqflV'></ul>

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

                      <small id='WqflV'></small><noframes id='WqflV'>

                      <tfoot id='WqflV'></tfoot>