簡體   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