简体   繁体   中英

docker can't connect to mariadb from localhost with different user

Hi I have followed this docker tutorial for mariabdb and also checked this stackoverflow question as well. I can start to mariadb container without any issues and can connect with root and mypass password with command mysql -h0.0.0.0 -uroot -pmypass . However, after I create a new user and grant all on it, I am not able to connect with that specific username and password. I am getting access denied error.

Here is the docker ps command output;

CONTAINER ID        IMAGE                 COMMAND                  CREATED             STATUS              PORTS                    NAMES
268c36eac13a        mariadb/server:10.3   "docker-entrypoint.s…"   31 minutes ago      Up 31 minutes       0.0.0.0:3306->3306/tcp   mariadbtest

I connect to mariadb container with docker exec -it 268c36eac13a /bin/sh and run these commands;

mysql -uroot -pmypass
create database emails;
grant all on emails.* to 'testUser'@'localhost' identified by '123456';

Then, from localhost when I am run mysql -h0.0.0.0 -utestUser -123456 I am getting Access denied for user 'testUser'@'172.17.0.1' (using password: YES) error.

Thanks for help!

I think this is a problem with networking : you allow user testUser to connect to your database from localhost but this means he will be able to connect only from client running inside the container. As you can see when you are logging to your MariaDB container from host machine you get your host docker address 172.17.0.1 .

One solution is to allow your user to connect from any ip :

grant all on emails.* to 'testUser'@'%' identified by '123456';

or IP from given subnet. You can check how to do it in CREATE USER docs .

Another solution would be to add user for IP of host machine. You can get the host IP from within the container and add user for this IP. On how to get host IP from the container check this question .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM