简体   繁体   中英

Why I can't connect to mysql docker container through JDBC with another database user than root?

I am using official docker container from mysql .

I've run docker container with this commands]:

sudo docker run -d --name desarrollo -p 3306:3306 -h localhost -e MYSQL_ROOT_PASSWORD=admin --mount src=mysql-desarrollo,dst=/var/lib/mysql mysq

Then I connected:

sudo docker exec -it desarrollo mysql -u root -p

It worked. Althougth I don't know why I have a root user with host '%'

mysql> select user, host from mysql.user;
+------------------+-----------+
| user             | host      |
+------------------+-----------+
| root             | %         |
| mysql.infoschema | localhost |
| mysql.session    | localhost |
| mysql.sys        | localhost |
| root             | localhost |
+------------------+-----------+
5 rows in set (0.00 sec)

Then I performed these sentences for create a database and its user:

create database my_database CHARACTER SET utf8 COLLATE utf8_bin;
create user 'my_user'@'localhost' identified by 'my_password';
grant ALL PRIVILEGES ON *.* TO 'my_user'@'localhost';

I asignned all privileges because when I only asignned privileges over my created database it didn't work.

Now, When I try to connect with my_user user through JDB it doesn't work but with root user it works (It was tested with internal client on Intellij IDE). This the JDBC url:

jdbc:mysql://localhost:3306/my_database

Thanks in advance for your help!

This might be because your my_user is allowed to access from localhost . But this is referred to localhost inside the docker container. But you're trying to access from the host PC. So replacing it with your PC IP address or using wild card would work.

create user 'my_user'@'%' identified by 'my_password';

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