DEV Community

Philip Mutua
Philip Mutua

Posted on • Updated on

Run MySQL on Port 3307 Using Docker Compose

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

Top comments (7)

Collapse
 
medkhabt profile image
LOUKHNATI Mohamed Khalil

Thank you !

Collapse
 
mirzasetiyono profile image
Mirza Setiyono

thanks bud!

Collapse
 
quanty007 profile image
Tshegofatso Legwale

It worked, Thanks

Collapse
 
adahyto profile image
Adam

Hero without a cape. Thank You.

Collapse
 
yassansplus profile image
Yassine Bousaidi

I love you thank you

Collapse
 
tineoc profile image
ChrisTineo

worked <3

Collapse
 
tahmidrana profile image
Tahmidur Rahman

You saved my day. Thank you