the following file is a docker-compose file. If I execute it via docker-compose up the container create itselfs but is impossible to connect to server, via terminal as via database visual editor. And, if I check the container via docker inspect by terminal, some vaule (i.e. IPaddress) are empty.
If I try to create the same container but manually via docker run command, passing the same parameters via command line, all works perfectly and if I check the container via docker inspect via terminal, all values are correct (also, in particular, IP address) and I can connect to the database so via terminal as via db visual editor.
Why it happens, and why in particular creating the mySql container via this docker-compose file the ipaddress seems empty? Is my docker compose file not correct? I checked several times with
version: '3.6'
services:
mysql:
image: mysql:5.7
restart: unless-stopped
environment:
- MYSQL_ROOT_PASSWORD=xyz
ports:
- 127.0.0.1:port_number:port_number
volumes:
- mysql:/var/lib/mysql
- ./mysql-init:/docker-entrypoint-initdb.d
command:
- --max-allowed-packet=64M
volumes:
mysql: {}
EDIT: to reply to 2 users,
1) Port_number was exactly 3306 in the orginal file;
2) The full run command is
sudo docker run --name my_mysql -e MYSQL_ROOT_PASSWORD=my_password -p 3306:3306 mysql:5.7
My guess is the issue is you're listening on IP 127.0.0.1, which is local to the container and therefore can't be accessed remotely. You should listen on 0.0.0.0.
Long version: https://pythonspeed.com/articles/docker-connection-refused/
Related
I am running a mysql server inside a docker container using docker-compose, here is my yaml file:
version: '3'
services:
mysqltest:
image: mysql
network_mode: host
ports:
- "3306:3306"
volumes:
- "/home/myuser/bds/mysql/:/var/lib/mysql"
user: "1000:1000"
environment:
- "MYSQL_ROOT_PASSWORD=mysecret"
The container loads file with the docker-compose up command, but when i try to connect from the host machine to the mysql server with user root, it fails:
mysql -h 127.0.0.1 -u root -pmysecret
ERROR 1045 (28000): Access denied for user 'root'#'127.0.0.1' (using password: YES)
However, i can connect to the server inside the container using the same command, if i start the container using docker command line:
docker run -it --rm --name mysqltest --user 1000:1000 -v /home/myuser/bds/mysql/:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=mysecret -p 3306:3306 -d mysql
I tried setting network_mode to host, none and also without specifying it, but the result is the same using docker-compose.
What could be wrong with my YAML file so that i cannot connect as when i use docker command ?
Thanks in advance
I assume you didn't change your default Docker configuration. By default Docker will run in the network mode: 'bridge' and not host. See the difference here.
You can check using docker inspect container.
When you just start the container using the command you will start your container in bridge mode and the container port 3306 will be mapped on 3306. This will not happen when you try host. Again see the link for the difference.
So update your docker-compose.yaml and define bridge as network mode:
version: '3'
services:
mysqltest:
image: mysql
network_mode: bridge
ports:
- "3306:3306"
volumes:
- "/home/myuser/bds/mysql/:/var/lib/mysql"
user: "1000:1000"
environment:
- "MYSQL_ROOT_PASSWORD=mysecret"
SOLVED !! The issue was originated from setting different mysql root passwords with the different docker commands:
The first time i was testing the container i user std docker command and the environment MYSQL_ROOT_PASSWORD=othersecret. Everything worked fine. i connected from host using mysql command and restored a database
Once i finished testing, i wrote yaml file, but with the root password i wanted to use for producction:
environment:
MYSQL_ROOT_PASSWORD: "mysecret"
But then, when i started the container with docker-compose and the yaml file, i could not connect from host using mysql command. It seems the original root password ("othersecret") was written to the user table inside the mysql schema and that is why i could not connect using the password set in the yaml file. I cleaned up the mysql directory and started the container using docker-compose (without specifying a network mode) and finally i was able to connect from host.
My company has been working with local setups of our mysql database for years. We have recently decided to adopting a containerized approach to local development, and we want to add the database into being run in a container. The issue is, because all of our data is already set up locally, we want to be able to just use the same data in the mysql container. I have tried using volumes to mount the directory storing all the mysql data into the container to no avail. Has anyone had success with doing this?
db part of docker-compose.yml:
db:
image: mysql:5.6
container_name: mysql
ports:
- "3306:3306"
environment:
MYSQL_ALLOW_EMPTY_PASSWORD: 1
volumes:
- /usr/local/mysql/data:/var/lib/mysql
I am able to get mysql running fine and am able to connect to it easily from my local machine, but when I connect, none of the local databases that already exist are there. Is there something that I'm overlooking?
#yourknightmares,
So I just ran a test and it worked for me. Here is what I did:
docker-compose.yml
version: "3.9"
services:
nginx:
image: nginx:latest
ports:
- "9999:9999"
command: tail -f /dev/null
volumes:
- "/etc/nginx/nginx.conf:/opt/nginx/nginx.conf"
In my host machine, I have the file at /etc/nginx/nginx.conf
, then:
$ docker-compose up -d
$ docker exec -it 02ba7032d699 bash
$ root#02ba7032d699:/# cat /opt/nginx/nginx.conf
#hello
The file was mounted just fine from the host to the container. I would suggest you to do the same exercise just for troubleshooting purposes. Also, have you looked at the container logs with docker logs container_id?
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
I'm running a Docker mysql container on my Mac laptop. Previously I was able to connect to it from the host OS with the mysql client. However, somehow it got deleted, and now after I re-created it, I can no longer to connect to it. I've searched dozens of similar questions, but am completely stumped.
Here's how I created it:
docker container run --name mysql-zhxw-dev -p 3306:3306 --expose=3306 -v zhxw-local-db-:/var/lib/mysql -e MYSQL_ALLOW_EMPTY_PASSWORD=yes -d mysql:5.7.30
Every time I run mysql -u root -h 127.0.0.1 the following from my host OS, I get:
ERROR 2003 (HY000): Can't connect to MySQL server on '127.0.0.1' (61)
I can login to the container, and connect to mysql from within:
docker container exec -it mysql-zhxw-dev bash
mysql -u root <-- connects fine
I've tried:
Omitting the named volume
Specifying a password
Various versions of mysql, including 5.6 and 5.7
Logging in to the container with docker container exec, installing vi, editing /etc/mysql/mysql.conf.d/mysqld.cnf and uncommenting the line that contains bind-address. I tried it with both bind-address = 0.0.0.0 and bind-address = 127.0.0.1, then obviously exiting and running docker container restart mysql-zhxw-dev.
Specifying port to connect to with -P 3306
Connecting with -h localhost, -h 127.0.0.1, -h 0.0.0.0, and omitting the -h
Specifying --protocol=TCP when connecting
I'm at a loss as to what else to try.
i have a template in docker-compose with mysql, maybe it can help you.
docker-compose.yml
version: "3.2"
services:
mysql:
image: mysql:latest
ports:
- "3306:3306"
volumes:
- /path-persistent-volumen:/var/lib/mysql/
command: --default-authentication-plugin=mysql_native_password
restart: always
environment:
- MYSQL_ROOT_PASSWORD=password
It turns out docker must have been in a strange state. Rebooting my laptop solved the problem.
Before rebooting, I tried restarting Docker Desktop, and that did not fix it. Only a full reboot resolved it.
One thing that I did notice was before the reboot, when I ran docker container ls -a, there were no containers, apart from the one mysql one I was trying to get working. I thought I had perhaps pruned them from some cleanup command. After the reboot, all my containers came back.
I did recently upgrade docker using Homebrew, so perhaps that put it in a weird state.
This error generally occurs due to problems related to port on the host.
Try these things:
Check logs of the container
Start the container in attached mode using -a flag
Run docker inspect mysql-zhxw-dev and check HostPort and HostIp and try to find something anomalous.
You can also change the host port in port mapping to something else like -v 3308:3306.
Also, this might help https://stackoverflow.com/a/32361238/9586997
P.S. I have copied and run the same command you have given, and it is running fine.
I had this issue after modifying the docker images and container configuration. Turns out the local copy of the MySQL data instance was corrupt.
Removing the ./data directory noted in this compose file and rebuilding worked.
The compose file
# /docker/docker-compose.yml
---
services:
db:
container_name: 'wp-mysql'
image: 'mysql:5.7.37'
platform: linux/amd64
volumes:
- './data/mysql:/var/lib/mysql'
ports:
- "18766:3306"
environment:
MYSQL_ROOT_PASSWORD: somewordpress
MYSQL_DATABASE: wordpress_db
MYSQL_USER: wordpress_user
MYSQL_PASSWORD: wordpress_password
The rebuild command for docker
docker-compose -f "docker-compose.yml" up -d --build
I have the following yml file, the services are created correctly, but when installing wordpress I cannot logon to mysql and I need to understand why.
I'm totally new to docker, I'd need to see all the services together from command line (bash), now I'm running a command like
$ sudo docker exec -ti 4295b34c014a /bin/bash
but I get a login to a specific service, how can I view wordpress and mysql together from cli?
yml file (from here):
version: '3.1'
services:
adminer:
image: adminer
ports:
- '8080:8080'
db:
image: mysql
volumes:
- 'wptut:/var/lib/mysql'
environment:
MYSQL_ROOT_PASSWORD: mysqlpassword
wordpress:
image: wordpress
ports:
- '81:80'
volumes:
wptut: null
I'm not sure what you mean by viewing them together, but in order to check if they are running you can use docker ps and if you want to see the logs after you docker-compose up -d use docker-compose logs -f. You should also make sure in WordPress you are referencing your MySQL database properly. For hostname, you should probably use db instead of localhost
Each service is running in a separate container. If you want log access, docker-compose up should stream logs from all three by default. If you detached from the docker-compose up session I think docker-compose logs -f should also combine log output of all services. docker-compose exec attaches to a running container, you can only do that to one container at a time. At the very least you can run docker-compose exec wordpress or another service name as a convenience over the direct docker command you have above. docker-compose logs -f wordpress also works for a one-off.