Connecting to MySQL container from Mysqlworkbench
You need to use localhost
(127.0.0.1) instead the container IP (in your case 172.17.0.2) at hostname
Connect to mysql container using Mysql Workbench
I found the solution, and I hope it will help someone:
It that MySQL changing their authentication style after relese 8.0
Running older versions solve the problem:
docker run --name my-db -p 3306:3306 -v
/e/DockerData/mysql:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=admin -e
MYSQL_DATABASE=testDB -d mysql:5.7.22
How to access mysql running in docker container from host machine?
It's because you've changed your username password. First you set it during the creation, as:
CREATE USER 'demo_java' IDENTIFIED BY 'java';
But then you override it:
grant all on *.* to 'demo_java'@'%' identified by '1234';
Please, see this SO entry for more details.
Simply remove password change from your scripts. You would have something like this:
CREATE USER 'demo_java' IDENTIFIED BY 'java';
grant all on *.* to 'demo_java'@'%';
FLUSH PRIVILEGES;
CREATE DATABASE hello_java CHARACTER SET utf8 COLLATE utf8_general_ci;
Then
DriverManager.getConnection("jdbc:mysql://localhost:3306/hello_java", "demo_java", "java");
should work fine.
Regarding connecting as the root, please see another SO entry, after starting up new container, there are some connection restrictions by default in MySQL. If you want to be able to connect as the root, you'll have to change its priviliges like this:
update mysql.user set host = '%' where user='root';
then you'll have to restart your MySQL container and you should be able to connect as the root as well.
Docker Container cant connect to MySQL database
No information does not help solving your problem but a wild guess:
By default docker containers are joining a virtual network separate from host named bridge
.
You can't reach host by localhost
or 127.0.0.1
, because this is pointing to your docker container itself. To reach host directly either let container use hosts IP by --network=host
(with some disadvantages) or use host.docker.internal
as DNS-Name instead of an IP.
BUT you should not take the way over host, connect directly to the mySQL-container by using the alias or IP or the container. You'll get that by docker inspect <containername>
. No need to map ports then..
How to connect MySQL with Docker using Navicat or MySQL Workbench?
Do try host as mysql
instead of localhost from the connected container. If you are on windows try 192.168.99.100
as host.
If still having issue try retest with mysql-compose.yml
version: "3.1"
services:
mysql:
image: mysql:5.7.22
command: "--innodb_use_native_aio=0"
restart: always
environment:
MYSQL_ROOT_PASSWORD: root
env_file:
- .env
ports:
- "3306:3306"
volumes:
- /var/database/docker/common:/var/lib/mysql
networks:
- mysqlnet
- mysqlnetfpm
networks:
mysqlnet:
driver: bridge
mysqlnetfpm:
driver: bridge
Note: Before running new mysql container don't forgot to delete the older mysqldata files.
Now you can use the username as root
, password as root
and for the internal containers(attached containers) use the host as mysql.
If you are try to connect from outside the docker do try with host as localhost
or 127.0.0.1
or your public docker ip
.
How to connect to MySQL database cluster
In your docker run command, you can use -p 3306:3306
(or any available port). Then you can use <host>:<port>
from Workbench or Dbeaver connection URL.
Related Topics
Checking If a SQL Server Login Already Exists
Sql Server-How to Replace a Date Column With a Current Date When the Column Has a Null Value
How to Subtract One Month from a Date Column
Sql Server Function to Return Minimum Date (January 1, 1753)
Using Node.Js (Express) and MySQL to Insert for a Timestamp
Mysql Convert Date from Mm-Dd-Yyyy to Yyyy-Mm-Dd Format
Laravel Update Multiple Records At Once
Select Distinct Records on a Join
Subtract Values from Two Columns in SQL Query
Deleting a Specific Character in a String from a Pattern
How to Modify Query to Remove Double-Quotes from All Columns
Insert Multiple Rows Without Repeating the "Insert into ..." Part of the Statement
Sql Take Just the Numeric Values from a Varchar
Wamp Server Error [Local Server - 2 of 3 Services Running]
How to Get Calendar Quarter from a Date in Tsql
Convert the Students Marks into Respective Grades