DEV Community

loading...

Run MySQL on Port 3307 Using Docker Compose

Philip Mutua
Software Engineer
Updated on ・1 min read

Sometimes you would like to run MySQL image on different port other than the default one 3306. Suppose you have deployed your back-end application on a server and you find out that there is another service running on port 3306. That was my situation a while back and to solve this I just added the variable MYSQL_TCP_PORT: 3307 under the MYSQL image environments: section as shown below on docker-compose.yml file:


  db:
    restart: always
    image: mysql:5.7

    command: --default-authentication-plugin=mysql_native_password

    environment:
      MYSQL_ROOT_PASSWORD: test#$!
      MYSQL_DATABASE: default_schema
      MYSQL_USER: test
      MYSQL_PASSWORD: test
      MYSQL_TCP_PORT: 3307

    ports:
      - "3307:3307"

Enter fullscreen mode Exit fullscreen mode

Make sure in your application's DB settings you change the database port settings in my case I was developing the back-end service with Django framework here are the settings below:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql', 
        'NAME': 'default_schema',
        'USER': 'root',
        'PASSWORD': 'test#$!',
        'HOST': 'db',
        'PORT': '3307',
    }
}

Enter fullscreen mode Exit fullscreen mode

Discussion (4)

Collapse
adahyto profile image
Adam

Hero without a cape. Thank You.

Collapse
medkhabt profile image
LOUKHNATI Mohamed Khalil

Thank you !

Collapse
quanty007 profile image
Tshegofatso Legwale

It worked, Thanks

Collapse
tahmidrana profile image
Tahmidur Rahman

You saved my day. Thank you