<bdo id='SN5OT'></bdo><ul id='SN5OT'></ul>

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

    1. <legend id='SN5OT'><style id='SN5OT'><dir id='SN5OT'><q id='SN5OT'></q></dir></style></legend>

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

      从本地主机连接到 Docker MySQL 容器?

      时间:2023-06-02

          <tbody id='YjXOb'></tbody>

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

          <bdo id='YjXOb'></bdo><ul id='YjXOb'></ul>

              • <tfoot id='YjXOb'></tfoot>
              • <legend id='YjXOb'><style id='YjXOb'><dir id='YjXOb'><q id='YjXOb'></q></dir></style></legend>
                <i id='YjXOb'><tr id='YjXOb'><dt id='YjXOb'><q id='YjXOb'><span id='YjXOb'><b id='YjXOb'><form id='YjXOb'><ins id='YjXOb'></ins><ul id='YjXOb'></ul><sub id='YjXOb'></sub></form><legend id='YjXOb'></legend><bdo id='YjXOb'><pre id='YjXOb'><center id='YjXOb'></center></pre></bdo></b><th id='YjXOb'></th></span></q></dt></tr></i><div id='YjXOb'><tfoot id='YjXOb'></tfoot><dl id='YjXOb'><fieldset id='YjXOb'></fieldset></dl></div>
                本文介绍了从本地主机连接到 Docker MySQL 容器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我有一个 docker mysql 镜像正在运行,以下是 docker-compose.yml 文件的样子:

                db:图片:mysql环境:MYSQL_ROOT_PASSWORD: ""MYSQL_ALLOW_EMPTY_PASSWORD:是端口:- 3306:3306"

                这很好用.

                我的问题是:如何从主机(我的 macbook)上的命令行 mysql 客户端连接到在该容器上运行的 MySQL 实例?

                澄清:

                • 我有一台安装了 Docker 的 macbook
                • 我有一个带有 mysql 的 docker 容器
                • 我想从我的 macbook 上的终端连接到在上述容器上运行的 mysql 实例
                • 我不想使用 docker 命令来实现这一点.相反,我想直接从终端使用 mysql 客户端(不通过 docker 容器进入).

                我没有在本地运行 MySQL,所以端口 3306 应该是开放的并且可以使用.

                我用来启动容器的命令是:docker-compose run

                解决方案

                使用 docker-compose up

                由于您在 docker 主机 上发布了端口 3306,因此您将从该主机本身连接到 127.0.0.1:3306.

                使用docker-compose run

                在这种情况下,docker-compose.yml 文件的端口映射部分将被忽略.要考虑端口映射部分,您必须添加 --service-ports 选项:

                docker-compose run --service-ports db

                附加说明

                请注意,默认情况下,当您告诉 mysql 客户端连接到 localhost 时,它会尝试使用 unix 套接字进行连接.所以一定要使用 127.0.0.1 而不是 localhost:

                 $ mysql -h 127.0.0.1 -P 3306 -u root

                <块引用>

                欢迎使用 MySQL 监视器.命令以 ; 结尾或\g.您的 MySQL 连接 ID 为 1服务器版本:5.6.26 MySQL Community Server (GPL)

                版权 (c) 2000、2015,Oracle 和/或其附属公司.保留所有权利.

                Oracle 是 Oracle Corporation 和/或其附属公司.其他名称可能是其各自的商标业主.

                输入帮助";或 '\h' 寻求帮助.输入 '\c' 清除当前输入语句.

                mysql>

                $ mysql -h localhost -P 3306 -u root

                <块引用>

                ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)

                I have a docker mysql image running, following is what the docker-compose.yml file looks like:

                db:
                  image: mysql
                  environment:
                    MYSQL_ROOT_PASSWORD: ""
                    MYSQL_ALLOW_EMPTY_PASSWORD: yes
                  ports:
                    - "3306:3306"
                

                This works fine.

                My question is: How can I connect to the MySQL instance running on that container from the command line mysql client on my the host (my macbook)?

                To clarify:

                • I have a macbook with Docker installed
                • I have a docker container with mysql
                • I want to connect to the mysql instance running on the aforementioned container from the Terminal on my macbook
                • I do NOT want to user a docker command to make this possible. Rather, I want to use the mysql client directly from the Terminal (without tunneling in through a docker container).

                I don't have MySQL running locally, so port 3306 should be open and ready to use.

                The command I am using to start the container is: docker-compose run

                解决方案

                Using docker-compose up

                Since you published port 3306 on your docker host, from that host itself you would connect to 127.0.0.1:3306.

                Using docker-compose run

                In that case the port mapping section of the docker-compose.yml file is ignored. To have the port mapping section considered, you have to add the --service-ports option:

                docker-compose run --service-ports db
                

                Additional note

                Beware that by default, the mysql client tries to connect using a unix socket when you tell it to connect to localhost. So do use 127.0.0.1 and not localhost:

                 $ mysql -h 127.0.0.1 -P 3306 -u root
                

                Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 1 Server version: 5.6.26 MySQL Community Server (GPL)

                Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.

                Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners.

                Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

                mysql>

                $ mysql -h localhost -P 3306 -u root
                

                ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)

                这篇关于从本地主机连接到 Docker MySQL 容器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:“子查询返回多于1行"的解决方案错误 下一篇:如何让 MySQL 使用 INDEX 进行视图查询?

                相关文章

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

                <i id='Ape2n'><tr id='Ape2n'><dt id='Ape2n'><q id='Ape2n'><span id='Ape2n'><b id='Ape2n'><form id='Ape2n'><ins id='Ape2n'></ins><ul id='Ape2n'></ul><sub id='Ape2n'></sub></form><legend id='Ape2n'></legend><bdo id='Ape2n'><pre id='Ape2n'><center id='Ape2n'></center></pre></bdo></b><th id='Ape2n'></th></span></q></dt></tr></i><div id='Ape2n'><tfoot id='Ape2n'></tfoot><dl id='Ape2n'><fieldset id='Ape2n'></fieldset></dl></div>
              • <tfoot id='Ape2n'></tfoot>

                  <legend id='Ape2n'><style id='Ape2n'><dir id='Ape2n'><q id='Ape2n'></q></dir></style></legend>
                  • <bdo id='Ape2n'></bdo><ul id='Ape2n'></ul>