Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 315
awfun Avatar asked Jun 16 '17 13:06

awfun


People also ask

How do I connect to a MySQL docker database?

Here are the steps you can follow to install the Dockerhub MySQL Container: Step 1: Pull the Docker Image for MySQL. Step 2: Deploy and Start the MySQL Container. Step 3: Connect with the Docker MySQL Container.

Can I use MySQL with docker?

With Docker, you can run or scale your application in any environment. MySQL is one of the most popular SQL-compatible relational databases. Running MySQL inside a Docker container lets you separate your database from your code.

How do I change the root password in MySQL?

To change the root password, type the following at the MySQL/MariaDB command prompt: ALTER USER 'root'@'localhost' IDENTIFIED BY 'MyN3wP4ssw0rd'; flush privileges; exit; Store the new password in a secure location.

How do I remove all images from docker?

Remove all Containers: To remove all containers from the docker-machine, we need to get the ids of all the containers. We can simply get the ids of the containers with the command docker ps -aq, then by using the docker rm command, we can remove all the containers in the docker-machine.


1 Answers

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.

like image 151
Robert Avatar answered Oct 08 '22 08:10

Robert