繁体   English   中英

如何从外部访问 docker mariadb 容器?

[英]how to access docker mariadb container from outside?

我遵循了官方指南: https://mariadb.com/kb/en/installing-and-using-mariadb-via-docker/但是,我没有在我的 my.cnf 文件中找到任何带有 bind-address 的条目,它看起来像这样:

# The MariaDB configuration file
#
# The MariaDB/MySQL tools read configuration files in the following order:
# 0. "/etc/mysql/my.cnf" symlinks to this file, reason why all the rest is read.
# 1. "/etc/mysql/mariadb.cnf" (this file) to set global defaults,
# 2. "/etc/mysql/conf.d/*.cnf" to set global options.
# 3. "/etc/mysql/mariadb.conf.d/*.cnf" to set MariaDB-only options.
# 4. "~/.my.cnf" to set user-specific options.
#
# If the same option is defined multiple times, the last one will apply.
#
# One can use all long options that the program supports.
# Run program with --help to get a list of available options and with
# --print-defaults to see which it would actually understand and use.
#
# If you are new to MariaDB, check out https://mariadb.com/kb/en/basic-mariadb-articles/

#
# This group is read both by the client and the server
# use it for options that affect everything
#
[client-server]
# Port or socket location where to connect
# port = 3306
socket = /run/mysqld/mysqld.sock

# Import all .cnf files from configuration directory
!includedir /etc/mysql/conf.d/
!includedir /etc/mysql/mariadb.conf.d/

当我尝试从外部(即主机)连接到它时,我得到以下信息:

Creating a session to 'root@172.17.0.2'
MySQL Error 2003 (HY000): Can't connect to MySQL server on '172.17.0.2' (60)

我应该怎么做才能从外部连接到服务器? 它确实运行,因为我可以从 docker 容器内连接。

我正在使用 macOS。

您不能在 Mac 上执行此技巧mysql -h 172.17.0.2 -u root -p

There is no docker0 bridge on macOS🔗
Because of the way networking is implemented in Docker Desktop for Mac, you cannot see a docker0 interface on the host. This interface is actually within the virtual machine.

I cannot ping my containers
Docker Desktop for Mac can’t route traffic to containers.

请参阅官方 docker Mac 文档

我建议您将容器端口公开给主机-p 127.0.0.1:3306:3306然后连接到您的数据库作为本地主机mysql -h 127.0.0.1 -p -uroot

docker run --name mariadbtest \
 -p 127.0.0.1:3306:3306\
 -e MYSQL_ROOT_PASSWORD=mypass \
 -d mariadb/server:10.3 \
 --log-bin \
 --binlog-format=MIXED

您的配置使用socket进行连接,因为您已注释掉port

# port = 3306
socket = /run/mysqld/mysqld.sock

所以你应该取消上面的port注释(并删除/注释掉socket配置)。 这将导致数据库侦听端口 3306。

对于本地使用,您需要将该端口映射到本地主机,例如使用-p运行您的容器,以便您可以通过localhost:3306连接:

docker -d -p 127.0.0.1:3306:3306 [..] example/mariadb

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM