mysql icon indicating copy to clipboard operation
mysql copied to clipboard

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

Open davejonesbkk opened this issue 8 years ago • 10 comments

Im trying to move some Wordpress sites into Docker running locally on a Centos7 machine.

I can bring the WP & MYSQL container up fine but then I get nothing in the browser and if I inspect the logs I get errors about root being denied.

This is the output from the Mysql container logs and the WP container logs:

Version: '5.7.23' socket: '/var/run/mysqld/mysqld.sock' port: 3306 MySQL Community Server (GPL) 2018-09-07T06:26:23.117937Z 2 [Note] Access denied for user 'root'@'172.18.0.3' (using password: YES)

Warning: mysqli::__construct(): (HY000/1045): Access denied for user 'root'@'172.18.0.3' (using password: YES) in Standard input code on line 22 MySQL Connection Error: (1045) Access denied for user 'root'@'172.18.0.3' (using password: YES)

The mysql root pw is correct and I can exec into the mysql container and login using that pw just fine, I can also see that my production sql file from ./data/mysite.sql is loaded there too.

This is my compose yml file:

`version: '2'

services: db: image: mysql:5.7 volumes: - ./data:/docker-entrypoint-initdb.d - ./db:/var/lib/mysql restart: always environment: - MYSQL_ROOT_HOST=% - MYSQL_ROOT_PASSWORD=wordpress - MYSQL_DATABASE=mysite_wp - MYSQL_USER=mysite_user - MYSQL_PASSWORD=mypass

wordpress: depends_on: - db image: wordpress:4.9.8-fpm-alpine ports: - "8000:80" restart: always environment: - WORDPRESS_DB_HOST=db:3306 - WORDPRESS_DB_PASSWORD=mypass volumes: - ./src:/var/www/html/ `

I have been trying this for almost 2 days now with a mixture of different mysql & WP versions but still no luck. Im deleting my ./db folder after I do docker-compose down each time to make sure everything starts correctly each time also.

I have also tried other suggestions from related issues such as this and this but still no luck.

davejonesbkk avatar Sep 07 '18 06:09 davejonesbkk

If you exec into the container you're logging in as root@localhost, so maybe a) root@'%' does not exist, or it has a different password? Note that if ./db/ was already populated with a database when you start the container, the environment variables won't have any effect, as they are only used for init of a fresh database. When you exec in, maybe try setting the password for the root@% user manually and see if that resolves it.

ltangvald avatar Sep 07 '18 06:09 ltangvald

I was mostly trying it without that 'MYSQL_ROOT_HOST' and just added that as saw someone else had success with it but it didnt work for me sadly. I was able to exec in and list all the users and saw host '%' for root which is correct I believe. I will try to exec in again and manually reset the pw as you suggested, thanks.

davejonesbkk avatar Sep 07 '18 09:09 davejonesbkk

MYSQL_ROOT_HOST="%" is the default on the mysql image, but on the mysql/mysql-server image(which is maintained by Oracle), it defaults to empty, meaning only the root@localhost user is created, so for that image it might have helped. Similarly, if your database was created with standard 5.7+ packages, either native or from upstream, only root@localhost is created by default.

ltangvald avatar Sep 07 '18 10:09 ltangvald

Issue seems resolved so going to close

wglambert avatar Sep 19 '18 17:09 wglambert

I had same issue, turns out my password contained comma, which in powershell is array separator. If that is the issue put your pwd in quotes, or check the actual password in Kitematic

mikeTWC1984 avatar May 16 '19 16:05 mikeTWC1984

Note that if ./db/ was already populated with a database when you start the container, the environment variables won't have any effect, as they are only used for init of a fresh database.

This was my problem, my db was pre-populated but i kept creating new env variables.

Thanks @ltangvald

jomadoye avatar Sep 10 '19 02:09 jomadoye

When you exec in, maybe try setting the password for the root@% user manually and see if that resolves it.

how to setting the pw user manually?

dhodyrahmad avatar Oct 14 '19 04:10 dhodyrahmad

Hi @everyone, any solution for this issue yet?

0xcodecrafterx avatar May 24 '21 22:05 0xcodecrafterx

For me this worked: change DB_PASSWORD=password -> DB_PASSWORD= In your .env file. Also make sure that you copied the .env-.example into the .env :-) Else you need to login to the sql container and set a password for the root user: Something like this:

$ sudo mysql -u root -p
Now you can add a new MySQL user with the username of your choice.

mysql> CREATE USER 'root'@'localhost' IDENTIFIED BY 'password';
And finally grant superuser privileges to the user you just created.

mysql> GRANT ALL PRIVILEGES ON * . * TO 'root'@'localhost';

after that go and edit .env DB_USERNAME=root
DB_PASSWORD=password

MartinDKnudsen avatar May 25 '22 11:05 MartinDKnudsen

It worked for me. Thanks @MartinDKnudsen I want to understand , why we have to do this?

ryemugodu avatar Jan 04 '23 22:01 ryemugodu