MySQL 如何访问我的 docker maria db?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33170489/
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:20:03  来源:igfitidea点击:

How can I access my docker maria db?

mysqldockermariadb

提问by MorRich

My main question is that after I have created a docker container for my mariadb with the command docker run --name db -e MYSQL_ROOT_PASSWORD=test -d -p 3306:3306 mariadbhow can I access the sql db?

我的主要问题是,在使用命令为 mariadb 创建 docker 容器后,docker run --name db -e MYSQL_ROOT_PASSWORD=test -d -p 3306:3306 mariadb如何访问 sql db?

Somewhere I have seen a solution using a temporal (after exit the container is deleted) container, but cannot find it anymore.

在某处我看到了使用临时(退出容器后删除)容器的解决方案,但再也找不到它了。

I am searching for a command like: sudo docker exec -it [other flags] [command] db.

我正在寻找像这样的命令:sudo docker exec -it [other flags] [command] db

采纳答案by BMW

Connect to MariaDB from the MySQL command line client The following command starts another mariadb container instance and runs the mysql command line client against your original mariadb container, allowing you to execute SQL statements against your database instance:

从 MySQL 命令行客户端连接到 MariaDB 以下命令启动另一个 mariadb 容器实例并针对您的原始 mariadb 容器运行 mysql 命令行客户端,从而允许您针对您的数据库实例执行 SQL 语句:

$ docker run -it --link some-mariadb:mysql --rm mariadb sh -c 'exec mysql -h"$MYSQL_PORT_3306_TCP_ADDR" -P"$MYSQL_PORT_3306_TCP_PORT" -uroot -p"$MYSQL_ENV_MYSQL_ROOT_PASSWORD"'

... where some-mariadb is the name of your original mariadb container.

...其中 some-mariadb 是原始 mariadb 容器的名称。

More information about the MySQL command line client can be found in the MySQL documentation

有关 MySQL 命令行客户端的更多信息可以在 MySQL 文档中找到

Refer: https://hub.docker.com/_/mariadb/

参考:https: //hub.docker.com/_/mariadb/

回答by michaelbahr

Just mysql-client, no extra docker container

只是 mysql-client,没有额外的 docker 容器

Install the mysql client on your host,

在你的主机上安装 mysql 客户端,

apt-get install mysql-client

then use the following command to access your database container.

然后使用以下命令访问您的数据库容器。

mysql -u<user> -p<pass> -h $(docker inspect --format '{{ .NetworkSettings.IPAddress }}' <db-container>)

The command will automatically get the IP of your docker container.

该命令将自动获取您的 docker 容器的 IP。

Make sure to replace <user>, <pass>and <db-container>with your respective values. In your case:

确保将<user>,<pass>和替换<db-container>为您各自的值。在你的情况下:

mysql -uroot -ptest -h $(docker inspect --format '{{ .NetworkSettings.IPAddress }}' db)

Your command lets mariadb run at the standard port 3306. If not, you have to tell the mysql command the new port.

您的命令让 mariadb 在标准端口 3306 上运行。如果没有,您必须告诉 mysql 命令新端口。

回答by Sunfloro

from Official Mariadb website:

来自官方 Mariadb 网站

Connecting to MariaDB from Outside the Container

从容器外部连接到 MariaDB

If we try to connect to the MariaDB server on localhost, the client will bypass networking and attempt to connect to the server using a socket file in the local filesystem. However, this doesn't work when MariaDB is running inside a container because the server's filesystem is isolated from the host. The client can't access the socket file which is inside the container, so it fails to connect.

如果我们尝试连接到本地主机上的 MariaDB 服务器,客户端将绕过网络并尝试使用本地文件系统中的套接字文件连接到服务器。但是,当 MariaDB 在容器内运行时,这不起作用,因为服务器的文件系统与主机隔离。客户端无法访问容器内的套接字文件,因此无法连接。

Therefore connections to the MariaDB server must be made using TCP, even when the client is running on the same machine as the server container.

因此,必须使用 TCP 连接到 MariaDB 服务器,即使客户端与服务器容器在同一台机器上运行。

Most MariaDB images, including the official one, have external TCP connections disabled using the bind-address option in their #my.cnf# file. The docker image used in this guide is based on Ubuntu, so the file is located at /etc/mysql/my.cnf.

大多数 MariaDB 镜像,包括官方镜像,都使用 #my.cnf# 文件中的 bind-address 选项禁用了外部 TCP 连接。本指南中使用的 docker 镜像基于 Ubuntu,因此该文件位于 /etc/mysql/my.cnf。

To use MariaDB we will need to edit the configuration file to change the appropriate option, and then restart the container.

要使用 MariaDB,我们需要编辑配置文件以更改相应的选项,然后重新启动容器。

Inside the container, edit the file my.cnf and check for the line that begins bind-address. Put a hash at the start of the line to comment it out:

在容器内,编辑文件 my.cnf 并检查以 bind-address 开头的行。在行首放置一个散列以将其注释掉:

#bind-address            = 127.0.0.1

Save the file.

保存文件。

While still inside the container, send the shutdown command to MariaDB. This will shut down the server and also exit back out to the host:

仍在容器内时,将关闭命令发送到 MariaDB。这将关闭服务器并退出到主机:

mysqladmin -u root -p shutdown

Start the container again. This time the MariaDB server will have networking enabled:

再次启动容器。这次 MariaDB 服务器将启用网络:

docker start mariadbtest

Find the IP address that has been assigned to the container:

找到已经分配给容器的IP地址:

docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' mariadbtest

You can now connect to the MariaDB server using a TCP connection to that IP address.

您现在可以使用到该 IP 地址的 TCP 连接来连接到 MariaDB 服务器。

Forcing a TCP Connection

强制 TCP 连接

After enabling network connections in MariaDB as described above, we will be able to connect to the server from outside the container.

如上所述在 MariaDB 中启用网络连接后,我们将能够从容器外部连接到服务器。

On the host, run the client and set the server address ("-h") to the container's IP address that you found in the previous step:

在主机上,运行客户端并将服务器地址(“-h”)设置为您在上一步中找到的容器 IP 地址:

mysql -h 172.17.0.2 -u root -p

This simple form of the connection should work in most situations. Depending on your configuration, it may also be necessary to specify the port for the server or to force TCP mode:

这种简单的连接形式应该适用于大多数情况。根据您的配置,可能还需要为服务器指定端口或强制 TCP 模式:

mysql -h 172.17.0.2 -P 3306 --protocol=TCP -u root -p

Port Configuration for Clustered Containers and Replication

集群容器和复制的端口配置

Multiple MariaDB servers running in separate Docker containers can connect to each other using TCP. This is useful for forming a Galera cluster or for replication.

在单独的 Docker 容器中运行的多个 MariaDB 服务器可以使用 TCP 相互连接。这对于形成 Galera 集群或复制很有用。

When running a cluster or a replication setup via Docker, we will want the containers to use different ports. The fastest way to achieve this is mapping the containers ports to different port on our system. We can do this when creating the containers (docker run command), by using the -p option, several times if necessary. For example, for Galera nodes we will use a mapping similar to this one:

当通过 Docker 运行集群或复制设置时,我们希望容器使用不同的端口。实现这一点的最快方法是将容器端口映射到我们系统上的不同端口。我们可以在创建容器时执行此操作(docker run 命令),通过使用 -p 选项,如有必要,多次。例如,对于 Galera 节点,我们将使用与此类似的映射:

-p 4306:3306 -p 5567:5567 -p 5444:5444 -p 5568:5568

回答by Gianluca Mattiroli

Slightly different syntax, docker 18.05.0-ce on ubuntu 18.04:

稍微不同的语法,ubuntu 18.04 上的 docker 18.05.0-ce:

sudo docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' db