从主机连接到 docker 容器中的 mysql

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33001750/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 21:18:57  来源:igfitidea点击:

Connect to mysql in a docker container from the host

mysqldockerdockerfile

提问by gturri

(It's probably a dumb question due to my limited knowledge with Docker or mysql administration, but since I spent a whole evening on this issue, I dare to ask it.)

(由于我对 Docker 或 mysql 管理的了解有限,这可能是一个愚蠢的问题,但由于我在这个问题上花了整整一个晚上,所以我才敢问。)

In a nutshell

简而言之

I want to run mysql in a docker container and connect to it from my host. So far, the best I have achieved is:

我想在 docker 容器中运行 mysql 并从我的主机连接到它。到目前为止,我取得的最好成绩是:

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

More details

更多细节

I'm using the following Dockerfile:

我正在使用以下内容Dockerfile

FROM ubuntu:14.04.3
RUN apt-get update && apt-get install -y mysql-server

# Ensure we won't bind to localhost only
RUN grep -v bind-address /etc/mysql/my.cnf > temp.txt \
  && mv temp.txt /etc/mysql/my.cnf

# It doesn't seem needed since I'll use -p, but it can't hurt
EXPOSE 3306

CMD /etc/init.d/mysql start && tail -F /var/log/mysql.log

In the directory where there is this file, I can succesfully build the image and run it with:

在有此文件的目录中,我可以成功构建映像并使用以下命令运行它:

> docker build -t my-image .
> docker run -d -p 12345:3306 my-image

When I attach to the image, it seems to work just fine:

当我附加到图像时,它似乎工作得很好:

# from the host
> docker exec -it <my_image_name> bash

#inside of the container now
$ mysql -u root
Welcome to the MySQL monitor.  Commands end with ; or \g.
[...]

However I don't have that much success from the host:

但是我没有从主持人那里获得那么多成功:

> mysql -P 12345 -uroot
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)

Even more details

更多细节

  • I've seen that there's a question which looks like mine. However, it isn't the same (and it doesn't have any answers anyway)
    • I've seen that there are images dedicated to mysql, but I didn't have more success with them
    • My grep -vmay feel weird. Admittedly, there may be cleaner way to do it. But when I attach my image, I can observe it actually worked as expected (ie: removed the bind-address). And I can see in the container /var/log/mysql/error.log:
  • 我已经看到有一个看起来像我的问题。但是,它不一样(无论如何它都没有任何答案)
    • 我已经看到有专门用于 mysql 的图像,但我并没有取得更大的成功
    • 我的grep -v可能觉得很奇怪。诚然,可能有更清洁的方法来做到这一点。但是当我附加我的图像时,我可以观察到它实际上按预期工作(即:删除了bind-address)。我可以在容器中看到/var/log/mysql/error.log

Server hostname (bind-address): '0.0.0.0'; port: 3306 - '0.0.0.0' resolves to '0.0.0.0'; Server socket created on IP: '0.0.0.0'.

服务器主机名(绑定地址):'0.0.0.0';端口:3306 - '0.0.0.0' 解析为 '0.0.0.0';在 IP 上创建的服务器套接字:'0.0.0.0'。

回答by maniekq

If your Docker MySQL host is running correctly you can connect to it from local machine, but you should specify host, port and protocol like this:

如果你的 Docker MySQL 主机运行正常,你可以从本地机器连接到它,但你应该像这样指定主机、端口和协议:

mysql -h localhost -P 3306 --protocol=tcp -u root

Change 3306 to port number you have forwarded from Docker container (in your case it will be 12345).

将 3306 更改为您从 Docker 容器转发的端口号(在您的情况下,它将是 12345)。

Because you are running MySQL inside Docker container, socket is not available and you need to connect through TCP. Setting "--protocol" in the mysql command will change that.

因为你在 Docker 容器中运行 MySQL,所以 socket 不可用,你需要通过 TCP 连接。在 mysql 命令中设置“--protocol”将改变它。

回答by Vasili Pascal

If you use "127.0.0.1" instead of localhost mysql will use tcp method and you should be able to connect container with:

如果您使用“127.0.0.1”而不是 localhost mysql 将使用 tcp 方法,您应该能够通过以下方式连接容器:

mysql -h 127.0.0.1 -P 3306 -u root

回答by l3x

I recommend checking out docker-compose. Here's how that would work:

我建议查看 docker-compose。这是如何工作的:

Create a file named, docker-compose.yml that looks like this:

创建一个名为 docker-compose.yml 的文件,如下所示:

version: '2'

services:

  mysql:
    image: mariadb:10.1.19
    ports:
      - 8083:3306
    volumes:
      - ./mysql:/var/lib/mysql
    environment:
      MYSQL_ROOT_PASSWORD: wp

Next, run:

接下来,运行:

$ docker-compose up

$ docker-compose up

Notes:

笔记:

Now, you can access the mysql console thusly:

现在,您可以这样访问 mysql 控制台:

$ mysql -P 8083 --protocol=tcp -u root -p

$ mysql -P 8083 --protocol=tcp -u root -p

Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 5.5.5-10.1.19-MariaDB-1~jessie mariadb.org binary distribution

Copyright (c) 2000, 2016, 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>

Notes:

笔记:

  • You can pass the -d flag to run the mysql/mariadb container in detached/background mode.

  • The password is "wp" which is defined in the docker-compose.yml file.

  • Same advice as maniekq but full example with docker-compose.

  • 您可以传递 -d 标志以在分离/后台模式下运行 mysql/mariadb 容器。

  • 密码是在 docker-compose.yml 文件中定义的“wp”。

  • 与 maniekq 相同的建议,但使用 docker-compose 的完整示例。

回答by Jobin

The simple method is to share the mysql unix socket to host machine. Then connect through the socket

简单的方法是将mysql unix socket共享给主机。然后通过socket连接

Steps:

脚步:

  • Create shared folder for host machine eg: mkdir /host
  • Run docker container with volume mount option docker run -it -v /host:/shared <mysql image>.
  • Then change mysql configuration file /etc/my.cnfand change socket entry in the file to socket=/shared/mysql.sock
  • Restart MySQL service service mysql restartin docker
  • Finally Connect to MySQL servver from host through the socket mysql -u root --socket=/host/mysql.sock. If password use -p option
  • 为主机创建共享文件夹,例如: mkdir /host
  • 使用卷挂载选项运行 docker 容器docker run -it -v /host:/shared <mysql image>
  • 然后更改mysql配置文件/etc/my.cnf,将文件中的socket入口改成socket=/shared/mysql.sock
  • service mysql restart在 docker 中重启 MySQL 服务
  • 最后通过套接字从主机连接到 MySQL 服务器mysql -u root --socket=/host/mysql.sock。如果密码使用 -p 选项

回答by Gerardo Rochín

if you running docker under docker-machine?

如果你在 docker-machine 下运行 docker?

execute to get ip:

执行获取ip:

docker-machine ip <machine>

returns the ip for the machine and try connect mysql:

返回机器的 ip 并尝试连接 mysql:

mysql -h<docker-machine-ip>

回答by COwnby

I do this by running a temporary docker container against my server so I don't have to worry about what is installed on my host. First, I define what I need (which you should modify for your purposes):

我通过在我的服务器上运行一个临时的 docker 容器来做到这一点,这样我就不必担心我的主机上安装了什么。首先,我定义了我需要的内容(您应该根据自己的目的进行修改):

export MYSQL_SERVER_CONTAINER=mysql-db
export MYSQL_ROOT_PASSWORD=pswd 
export DB_DOCKER_NETWORK=db-net
export MYSQL_PORT=6604

I always create a new docker network which any other containers will need:

我总是创建一个任何其他容器都需要的新 docker 网络:

docker network create --driver bridge $DB_DOCKER_NETWORK

Start a mySQL database server:

启动一个 mySQL 数据库服务器:

docker run --detach --name=$MYSQL_SERVER_CONTAINER --net=$DB_DOCKER_NETWORK --env="MYSQL_ROOT_PASSWORD=$MYSQL_ROOT_PASSWORD" -p ${MYSQL_PORT}:3306 mysql

Capture IP address of the new server container

捕获新服务器容器的 IP 地址

export DBIP="$(docker inspect ${MYSQL_SERVER_CONTAINER} | grep -i 'ipaddress' | grep -oE '((1?[0-9][0-9]?|2[0-4][0-9]|25[0-5])\.){3}(1?[0-9][0-9]?|2[0-4][0-9]|25[0-5])')"

Open a command line interface to the server:

打开服务器的命令行界面:

docker run -it -v ${HOST_DATA}:/data --net=$DB_DOCKER_NETWORK --link ${MYSQL_SERVER_CONTAINER}:mysql --rm mysql sh -c "exec mysql -h${DBIP} -uroot -p"

This last container will remove itself when you exit the mySQL interface, while the server will continue running. You can also share a volume between the server and host to make it easier to import data or scripts. Hope this helps!

当您退出 mySQL 界面时,最后一个容器将自行删除,而服务器将继续运行。您还可以在服务器和主机之间共享一个卷,以便更轻松地导入数据或脚本。希望这可以帮助!

回答by Hamfri

mysql -u root -P 4406 -h localhost --protocol=tcp -p

Remember to change the user, port and host so that it matches your configurations. The -p flag is required if your database user is configured with a password

请记住更改用户、端口和主机,使其与您的配置相匹配。如果您的数据库用户配置了密码,则需要 -p 标志

回答by lupguo

For conversion,you can create ~/.my.cnffile in host:

对于转换,您可以~/.my.cnf在主机中创建 文件:

[Mysql]
user=root
password=yourpass
host=127.0.0.1
port=3306

Then next time just run mysqlfor mysql client to open connection.

然后下次只需运行mysqlmysql客户端即可打开连接。

回答by Rajesh k

I was able to connect my sql server5.7 running on my host using the below command : mysql -h 10.10.1.7 -P 3307 --protocol=tcp -u root -p where the ip given is my host ip and 3307 is the port forwaded in mysql docker .After entering the command type the password for myql.that is it.Now you are connected the mysql docker container from the you hostmachine

我能够使用以下命令连接在我的主机上运行的 sql server5.7:mysql -h 10.10.1.7 -P 3307 --protocol=tcp -u root -p 其中给定的 ip 是我的主机 ip,而 3307 是在 mysql docker 中转发端口。输入命令后键入 myql 的密码。就是这样。现在您已从主机连接了 mysql docker 容器

回答by Denis Prescornic

In your terminal run: docker exec -it container_name /bin/bashThen: mysql

在您的终端中运行:docker exec -it container_name /bin/bash然后:mysql