Home > database >  How to expose mysql port?
How to expose mysql port?

Time:02-13

I'm trying to expose the port 3310 to allow remote MySQL connection. So far, I have this configuration:

  database:
    container_name: sfapi_db
    restart: always
    ports:
      - 3310:3306
    build:
      context: ./docker/database
    environment:
      - MYSQL_DATABASE=${DB_NAME}
      - MYSQL_USER=${DB_USER}
      - MYSQL_PASSWORD=${DB_PWD}
      - MYSQL_ROOT_PASSWORD=${DB_PWD_ROOT}
    volumes:
      - dbdata:/var/lib/mysql
      - type: bind
        source: ./docker/database/my.cnf
        target: /etc/mysql/my.cnf

and this is the Dockerfile:

FROM mariadb:latest

CMD ["mysqld"]

EXPOSE 3310

here the my.cnf:

[client-server]
# Port or socket location where to connect
port = 3310
socket = /run/mysqld/mysqld.sock

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

when I run docker-compose up --build -d and access to the container list, I get:

enter image description here

I also opened the port on the server using sudo ufw allow 3310. Also, I cannot use the port 3306 'cause it's used by another container, that's why I'm using 3310.

For some reason, when I go there and check if the port 3310 is opened, I always get that is closed.

How can I fix this?

CodePudding user response:

If the my.cnf resides in the container. Then the port used should be 3306

[client-server]
# Port or socket location where to connect
port = 3306
socket = /run/mysqld/mysqld.sock

Since in the context of the container, this is the port that exposes mysql.

So what probably happens is that you expose 3306 but the container has assigned 3310 for the mysql server. Thus 3306 is indeed closed.

  • Related