awfun
awfun

Reputation: 2498

MySQL in docker-compose -- access denied

I try to start MySQL server with docker-compose. Here is docker-compose.yaml part:

  mysql:
    restart: always
    image: mysql:latest
    ports:
    - "3306:3306"
    volumes:
    - /Users/user/Documents/.docker/mysql/config:/etc/mysql/
    - /Users/user/Documents/.docker/mysql/data:/var/lib/mysql
    environment:
    - MYSQL_ROOT_PASSWORD='123'
    - MYSQL_ROOT_HOST='172.18.0.1'

You see I've specified root password and host as it is said here. Then I try to connect to db (using Intellij Idea if that matters):

jdbc:mysql://localhost:3306/?user=root&password=123&ssl=false

But it doesn't accept the credentials and writes to log:

Access denied for user 'root'@'172.18.0.1' (using password: YES)

Please advise on how to fix it. Thanks.

Upvotes: 15

Views: 40743

Answers (4)

amangupta
amangupta

Reputation: 142

I was having same issue while connecting with Django. Later after following with few answers, able to solve by deleting the "Volume" created by the images. It was picking the old username and password from the volume, that is why it was not working even after rebuilding the image.

Upvotes: 0

Michael Pap
Michael Pap

Reputation: 1

if not in production you can also use the below with docker run -e MYSQL_ROOT_HOST=% Also it will be better to create your own docker network

Upvotes: 0

Nick Minchev
Nick Minchev

Reputation: 39

In my case

dataSource.setCatalog(...)

helped.

Upvotes: -1

Robert
Robert

Reputation: 36853

Most likely you have initialized the mysql data directory when these were different:

environment:
- MYSQL_ROOT_PASSWORD='123'
- MYSQL_ROOT_HOST='172.18.0.1'

MySQL image only honors those vars when the /var/lib/mysql directory is created.

So if you don't care about the data, empty your volume: /Users/user/Documents/.docker/mysql/data, or change the credentials manually from mysql terminal.

Upvotes: 21

Related Questions