Tuesday, 14 April 2026

MySQL docker container in a host machine which has MySQL server running

Map container to another host port other than 3306

version: '3'
services:

  mysql:
    container_name: docker_mysql_1
    ports:
      - 3307:3306/tcp
    privileged: true
    restart: on-failure
    image: mysql:8.4
    volumes:
      - mysql84:/var/lib/mysql
    network_mode: bridge
    environment:
      MYSQL_ROOT_PASSWORD: test1234
    command:
      - --innodb_file_per_table=0
      - --innodb_checksum_algorithm=INNODB
      - --binlog_checksum=NONE
      - --sql_mode=NO_ENGINE_SUBSTITUTION
      - --slow-query-log=1
      - --slow-query-log-file=/var/log/mysql/my-slow.log
      - --long_query_time=1
      - --log-queries-not-using-indexes
      - --restrict_fk_on_non_standard_key=0
    security_opt:
      - seccomp=unconfined

volumes:
  mysql84:

After container is up, connect to container Mysql Server

#when ask password, type: test1234
mysql -h 127.0.0.1 -P 3307 -u root -p 

#another way using container name
docker exec -it docker_mysql_1 mysql -uroot -p

To connect MySQL running on the host machine. Assume there is MySQL user tester

mysql -u tester -p

How to check who owns 3306 now

#On host:
ss -ltnp | grep 3306
# or
lsof -i :3306

No comments:

Post a Comment