I dockerized a MySQL database by doing:
docker pull mysql
docker run --name=container_name -p 3307:3307 -e MYSQL_ROOT_PASSWORD=password -d mysql
Now the container <container_name> is the only one I have in Docker, and its network is set by default to "bridge".
Using: docker exec -it container_name mysql -u root -p (then typing the password) I created a brand new database.
I tried reaching that database from my SQL client, using the specified credentials and database url:
server: localhost or container_name, port: 3307, user: root, password: password.
The client doesn't reach the database and I get the following errors:
Connection refused: connect
Communications link failure
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.
Obviously I get the same error while running my spring boot application (not dockerized yet), but the problem is something in MySQL container since the SQL client isn't working as well.
I've been reading countless questions same as mine, but the solutions that they offer, never work for me.
What's weird is the fact that in the past projects it worked fine.
I'm running Docker on Windows 10 Pro with Hyper-V.
If you're trying to connect from outside of Docker, your docker run -p option is wrong: the second port number always needs to be the standard port number of its service. For MySQL, that second port number needs to be 3306, always. The two port numbers need to match.
docker run -d -p 3307:3306 ... mysql
# ^^^^
# must be the standard port number
If you're trying to connect from inside Docker, then
You must docker network create a network, and docker run --net all involved containers on that network
You must use the other container's name as a host name, not localhost
You must use the standard port number, again 3306; docker run -p options are ignored (and aren't required)
docker network create some-net
docker run -d --net some-net --name database \
-p ... -e ... -v ... \
mysql
docker run -d --net some-net --name application \
-e MYSQL_HOST=database \
-e MYSQL_PORT=3306 \
my/application
If you're running this under Docker Compose, it automatically creates the network for you, and you can use the Compose service name as the host name. You do not need to manually set up networks: or override the container_name:.
version: '3.8'
services:
database:
image: mysql
# ports: [...]
# environment: [...]
# volumes: [...]
application:
build: .
environment:
MYSQL_HOST: database
MYSQL_PORT: '3306'
Related
Following steps in https://hub.docker.com/_/mysql/:
Start a mysql server instance
Starting a MySQL instance is simple:
$ docker run --name some-mysql -e MYSQL_ROOT_PASSWORD=my-secret-pw -d
mysql:tag
... where some-mysql is the name you want to assign to your container,
my-secret-pw is the password to be set for the MySQL root user and tag
is the tag specifying the MySQL version you want. See the list above
for relevant tags. Connect to MySQL from the MySQL command line client
The following command starts another mysql container instance and runs
the mysql command line client against your original mysql container,
allowing you to execute SQL statements against your database instance:
$ docker run -it --network some-network --rm mysql mysql -hsome-mysql
-uexample-user -p
... where some-mysql is the name of your original mysql container
(connected to the some-network Docker network).
I started a mysql docker container, and then I tried to run another as the mysql client, but I don't know how to specific the --network parameter:
What should I input instead of some-network? I am newbie to Docker, and have no idea of Docker network. If I ommit this parameter, Unknown MySQL server host error is given.
Before you start the first container, you need to create a Docker network
docker network create some-network
You can use any name you want here, but I will use some-network for consistency with the question.
When you start the database container, it also needs to be attached to the same network
docker volume create mysql-data # this is essentially required too
docker run \
--name some-mysql \
-e MYSQL_ROOT_PASSWORD=my-secret-pw \
-d \
--net some-network \ # matches `docker network create`
-v mysql-data:/var/lib/mysql/data \ # don't lose data on restart
mysql:tag
(There is also a docker network connect command, but recreating containers to change settings is a pretty normal practice.)
You also don't need a second container to run a MySQL client: you can connect with the ordinary mysql command-line tool from the host. You need to publish a port out of the container
docker run \
-p 12345:3306 \
...
The first port number can be anything you want that doesn't conflict with another process on the host; the second number must be the standard MySQL port number 3306. You can then connect to that database with
mysql -h 127.0.0.1 -p 12345 -u example-user -p
Other answers to this question have endorsed Docker Compose as a setup. Compose will docker network create a network for you; Networking in Compose describes this setup in more detail. However, it's not great at running interactive terminal applications, and you might need to do something like docker-compose run db mysql -h db ... to get access to it this way. The published ports: approach will work too.
If you have more than one container which work together, you should read about docker-compose in order to config network, host, env var and so on...
// docker-compose.yml
version: "3.2"
services:
mysql_client:
depends_on:
- mysql_database
mysql_database:
image: mysql:5.7
environment:
MYSQL_ROOT_PASSWORD: test
MYSQL_DATABASE: test
MYSQL_USER: test
MYSQL_PASSWORD: test
# exec this command to up your containers
docker-compose up
By default container are on the same network, in your mysql_client use mysql_database as hostname for mysql connection.
Via DockerHub I found this docker compose script to have Adminer and MySQL running in harmony.
# Use root/example as user/password credentials
version: '3.1'
services:
adminer:
image: adminer
restart: always
ports:
- 8080:8080
db:
image: mysql:5.6
restart: always
environment:
MYSQL_ROOT_PASSWORD: example
Save it ti a file called docker-compose.yml and run it using docker-compose.
In cmd promt navigate to the directory containing the file and run the following:
docker-compose up
docker-compose reference
Let's say I have the following compose file :
networks:
my_network:
services:
...
mysql:
container_name: "mysql"
image: "mysql:5.7"
volumes:
- ./mysql.cnf:/etc/mysql/conf.d/mysql.cnf
environment:
MYSQL_ROOT_PASSWORD: "password"
MYSQL_USER: "user"
MYSQL_PASSWORD: "password"
MYSQL_DATABASE: "test-db"
ports:
- "3306:3306"
restart: always
networks:
- my_network
Once I run docker-compose up, my services get started and I can see that my MySQL server is ready to accept connections. Now what do I need to do to access the mysql terminal prompt from "outside" the container ? I remember seeing a teacher run another docker container (from a new terminal), and access the MySQL command prompt, enabling him to manage the database by hand, from another terminal window, but I can't remember the command exactly.
I tried running a new Docker container like this :
docker run -it --rm --name mysqlterm \
--network="compose_project_my_network" \
--link mysql mysql:5.7 \
sh -c 'exec mysql \
-h "localhost" -P 3306" \
-uroot \
-ppassword'
But unfortunatly, the container can't connect to the MySQL server, giving me the following error : ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2).
I guess the "localhost" or 3306 port are wrong ? What should I put in these parameters ?
Thanks in advance for your answers :)
You need an ordinary MySQL client; if your host's package manager (Debian/Ubuntu APT, MacOS Homebrew, ...) has a packaged named something like mysql-client, that's it.
When you run mysql, that tool does not interpret localhost as a host name but rather as a signal to use a Unix socket, so you need to use the corresponding IP address 127.0.0.1 instead. (If the database is running on a different system, use its DNS name or IP address: this is indistinguishable from running the database directly on the host outside of Docker.)
So from the host run:
mysql -h 127.0.0.1 -u root -p
The default port 3306 matches the published ports:, and you'll be prompted to interactively enter the password.
Iam beginner to docker and iam working on mysql and node.js I run mysql docker container as
docker run --name docker-mysql -e MYSQL_ROOT_PASSWORD=abc123 -d mysql:latest
and result of docker ps is showing mysql container is running
and docker logs says
MySQL init process done. Ready for start up.
how to connect with this container in workbench or in my application
Try this:
docker run -p 3306:3306 --name docker-mysql -e MYSQL_ROOT_PASSWORD=abc123 -d mysql:latest
This will bind the port 3306 on your local machine to the docker image. You should be able to connect to the database with localhost & port 3306 with username root and password abc123.
I just tested it and it works like a charm.
If you are struggling with the error:
failed to connect to localhost at 33016" details = Authentication
plugin 'caching_sha2_password' cannot be loaded:
dlopen(/usr/local/mysql/lib/plugin/caching_sha2_password.so, 2): image
not found
Update your MySQL-workbench.
If that doesn't work you will need to add a native password to the root user. Here is how:
Connect to your docker image via bash:
docker exec -it docker-mysql bash
Log into mysql as root
mysql --user=root --password
Enter the password for root (Default is 'root', but 'abc123' in this example)
Finally Run:
ALTER USER 'root' IDENTIFIED WITH mysql_native_password BY 'abc123';
You need to expose the port
Use -p, or -P
-p is bound to a custom port, -P will randomly assign a port to you.
:latest does not need to add, docker will help you add.
The final command should look like this:
docker run -dit -P --name docker-mysql -e MYSQL_ROOT_PASSWORD=abc123 mysql:latest
Then use xx to see the exposed ports:
docker port docker-mysql
Check which port of the machine is mapped to port 3306 of the container, My result is:
33060/tcp -> 0.0.0.0:32818
3306/tcp -> 0.0.0.0:32819
Now you can connect to this port via software or code.
For development best way is to connect the container to "host" network
docker run --name docker-mysql --network host -e MYSQL_ROOT_PASSWORD=abc123 -d mysql:latest
When you will move to staging/prod environment consider to use some orchestration solution.
Run docker inspect container_id its will show container port but will not show host port bind. By Default running without -p flag it will assign port to container but will not expose host port , so to achieve this you shout try -p flag, -p 3306:3306 in docker run command.
I have a mysql container like this :
I want to open the mysql using mysql workbench, does anyone know how ??
I am still confused how to fill this data ..
I hope there is a solution to my problem
tl;dr
You have to publish MySQL's port 3306 to the "outside" using the -p switch.
Container ports are not accessible by default to the "outside" and are only open to other containers in the same network.
Run your MySQL image with -p 15000:3306 (map local port 15000 to container's port 3306) then connect in the Workbench to localhost at port 15000. You can choose any port you want, it can be 3306 too: -p 3306:3306.
Example docker run command:
docker run -it --rm -v mysql:/var/lib/mysql -p 3306:3306 mysql
In case of docker-compose:
services:
# …
mysql:
# …
ports:
- 3306:3306
Here is the picture of what I am doing:
I have local Go API code, I have built it into a docker image.
I have pulled MySQL docker image from docker.
I have DB in MySQL and Go API is accessing mysql.
Everything worked fine till my Go API was local and mysql was docker container. Now I have built local Go code as docker image and when I try to run this image using docker run --name mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=password -d mysql:5.5 , Docker container starts and exits immediately.
I tried Docker Start -a Container-ID to start container again, I get this error 'dial tcp 127.0.0.1:3306: getsockopt: connection refused'.
When I searched about this error I got this input - "After setting bind-address: 127.0.0.1 in mysql server config I was able to get the installation working with host localhost:3306."
But I am not aware how to set bind-address.
Any inputs regarding this will be helpful.
Thanks.
You should use Docker's network service discovery feature.
Put both your containers onto the same network and then they can discover eachother via DNS.
For example:
docker network create mynet
docker run --net mynet --name mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=password -d mysql:5.5
docker run --net mynet --name app -p 80:80 -d myappimage:latest
Both of these containers will be able to resolve 'mysql' or 'app' to the ip that each respective container has on the 'mynet' network. Configure your application to connect to mysql at 'mysql:3306'.