Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can’t connect to mysql docker image

I have issue using mysql/mysql-server docker image. I want to connect to mysql server, create db, user with all PRIVILEGES to have ability to create new schema and run liquibase in future.

I run container like this:

sudo docker run --name db -e MYSQL_ROOT_PASSWORD=qwe123 -e MYSQL_DATABASE=test \
  -d -p 3306:3306 mysql/mysql-server:5.6 --lower-case-table-names=1

when I do docker ps I see that it's running but when I'm trying to run liquibase (using root/qwe123) it failed with:

[ERROR] Failed to execute goal org.codehaus.mojo:sql-maven-plugin:1.5:execute (default-cli) on project db: null, message from server: "Host '172.17.0.1' is not allowed to connect to this MySQL server" -> [Help 1]

tried with user:

sudo docker run --name db -e MYSQL_ROOT_PASSWORD=qwe123 -e MYSQL_DATABASE=test \
  -e MYSQL_USER=admin -e MYSQL_PASSWORD=qwe123 -d -p 3306:3306 \
  mysql/mysql-server:5.6 --lower_case_table_names=1

with admin user have error creating new schema, it looks like admin has only access to test database:

[ERROR] Failed to execute goal org.codehaus.mojo:sql-maven-plugin:1.5:execute (default-cli) on project db: Access denied for user 'admin'@'%' to database 'test2' -> [Help 1]

with root the following error:

[ERROR] The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server. Can not read response from server. Expected to read 4 bytes, read 0 bytes before connection was unexpectedly lost.

Also tried to set privileges for admin in init command:

docker run --name db -e MYSQL_ROOT_PASSWORD=qwe123 -e MYSQL_DATABASE=test -d \
  -p 3306:3306 mysql/mysql-server:5.6 --lower_case_table_names=1 \
 '--init-connect=CREATE USER `admin`@`localhost` IDENTIFIED BY "qwe123"; GRANT ALL PRIVILEGES ON *.* TO `admin`@`localhost` WITH GRANT OPTION; CREATE USER `admin`@`%` IDENTIFIED BY "qwe123";GRANT ALL PRIVILEGES ON *.* TO `admin`@`%` WITH GRANT OPTION;FLUSH PRIVILEGES;'

In this case for root user have error:

[ERROR] Failed to execute goal org.codehaus.mojo:sql-maven-plugin:1.5:execute (default-cli) on project db: null, message from server: "Host '172.17.0.1' is not allowed to connect to this MySQL server" -> [Help 1]

for admin - the following:

12:13:57 [ERROR] The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server. Can not read response from server. Expected to read 4 bytes, read 0 bytes before connection was unexpectedly lost.

Also tried to use localhost and 127.0.0.1 as host to DB, always have error with connection. Any ideas?

like image 387
Oksana Vikul Avatar asked Oct 30 '22 13:10

Oksana Vikul


1 Answers

You should allow the user within MySQL to connect from the IP of your host machine. Do not forget to flush privileges after changing user in MySQL

GRANT CREATE USER ON *.* TO 'root'@'%';

And restart the mysql instance

Also there is a chance to have mysql bind to 127.0.0.1 via my.cnf. If it is so - change the bind to 0.0.0.0 to get it accessible via any IP

like image 174
podarok Avatar answered Nov 13 '22 00:11

podarok