Coder Social home page Coder Social logo

Comments (5)

gongna-au avatar gongna-au commented on May 24, 2024 1

@SergeTupchiy
Regarding what you said, use the docker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' mysql command to get the IP address of Mysql, and then use this in the connector configuration It is feasible to connect to Mysql using IP+Port👍
image
The address of the configured Mysql server is the IP obtained by docker inspect + the port 3306 inside the container
Thank you for your patient reply

from emqx.

SergeTupchiy avatar SergeTupchiy commented on May 24, 2024

Hi @gongna-au,
Docker containers are isolated, EMQX running in a docker container can't reach MySql running in another container at 127.0.0.1:3307 even though you have bound Mysql container port to host port with -p 3307:3306.
You need to create a Docker network so that containers can communicate with each other, though it would be easier to use docker-compose and define EMQX and MySql services.
Another quick work-around is to use Docker container IP address, but this can be changed after each container restart:

# Set this IP address to the bridge config: 
docker inspect  -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' mysql

from emqx.

gongna-au avatar gongna-au commented on May 24, 2024

非常感谢@SergeTupchiy 的回复😘

创建Docker网络

docker network create my-network

运行emqx

docker run -d --name emqx-enterprise --network my-network -p 1883:1883 -p 8083:8083 -p 8084:8084 -p 8883:8883 -p 18083:18083 emqx/emqx-enterprise:5.6.0

运行Mysql

docker run --name mysql --network my-network -p 3307:3306 -e MYSQL_ROOT_PASSWORD=public -d mysql

创建数据库和表

docker exec -it mysql bash
mysql -u root -ppublic
CREATE DATABASE emqx_data CHARACTER SET utf8mb4;
use emqx_data;
CREATE TABLE emqx_messages (id INT AUTO_INCREMENT PRIMARY KEY,clientid VARCHAR(255),topic VARCHAR(255),payload BLOB,created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP);
CREATE TABLE emqx_client_events (id INT AUTO_INCREMENT PRIMARY KEY,clientid VARCHAR(255), event VARCHAR(255),created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP);

仪表盘连接

image

输入的Mysql地址为mysql而不是127.0.0.1:3307
至此访问http://localhost:18083/#/connector/create
可以成功创建Mysql连接器

from emqx.

gongna-au avatar gongna-au commented on May 24, 2024

非常感谢@SergeTupchiy 的回复
我又尝试让emqx 和Mysql都运行在宿主机--network host,而不是创建Docker网络,也成功解决了我之前的疑惑。

运行emqx

docker run -d --name emqx-enterprise --network host emqx/emqx-enterprise:5.6.0

这条命令会启动一个名为emqx-enterprise的容器,该容器直接使用宿主机的网络。这意味着,任何EMQX企业版在容器内监听的端口(例如1883, 8083, 8084, 8883, 18083)都会直接在宿主机上开放,无需进行端口映射。

注意事项:
确保宿主机的这些端口(1883, 8083, 8084, 8883, 18083)未被其他服务占用。

运行Mysql

docker run --name mysql --network host -e MYSQL_ROOT_PASSWORD=public -d mysql

这条命令将启动一个名为mysql的容器,容器直接使用宿主机的网络。MySQL服务将在其默认端口3306上监听,而不是通过Docker在宿主机上映射到3307端口。

注意事项:
由于容器使用宿主机网络,MySQL服务将在宿主机的3306端口上直接对外开放。如果宿主机上已经有服务监听3306端口(例如另一个MySQL实例),则可能会导致端口冲突。

创建数据库和表

docker exec -it mysql bash
mysql -u root -ppublic
CREATE DATABASE emqx_data CHARACTER SET utf8mb4;
use emqx_data;
CREATE TABLE emqx_messages (id INT AUTO_INCREMENT PRIMARY KEY,clientid VARCHAR(255),topic VARCHAR(255),payload BLOB,created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP);
CREATE TABLE emqx_client_events (id INT AUTO_INCREMENT PRIMARY KEY,clientid VARCHAR(255), event VARCHAR(255),created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP);

当使用--network host参数运行Docker容器时,容器会直接使用host的网络命名空间。这意味着容器中的应用程序将直接在后续机的网络上运行,而不是在Docker自己的虚拟网络中因此,使用--network host时指定的任何如图所示的端口映射(-p或--publish参数)都将被忽略,正如 Docker 运行时给出的警告:

WARNING: Published ports are discarded when using host network mode

这是因为在host网络模式下,容器与一个宿主机共享网络堆栈,容器内的服务可以直接监听对应的端口,而不是需要通过Docker进行端口映射。,比如EMQX或MySQL,它们将直接在对应机的对应端口上可用,而不需要Docker来管理端口映射。

创建Mysql连接器

image
输入的Mysql地址为127.0.0.1:3306 而不是127.0.0.1:3307或者直接是mysql 🤔

from emqx.

SergeTupchiy avatar SergeTupchiy commented on May 24, 2024

Happy to help, @gongna-au!

from emqx.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.