cann't connect to the mysql docker container following the official instructures - mysql

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

Related

Docker compose and mysql issues connecting to local db

Even though I googled as much as I could there's not really anything that worked out for me.
Hence my question:
I am learning docker and docker compose.
I made a dockerfile:
# base image
FROM mysql:8.0-debian
# creating user to access db
RUN groupadd -r group-mysql && useradd -r -g user-mysql group-mysql
# required env variable
ENV MYSQL_ROOT_PASSWORD ale123
ENV MYSQL_USER user-mysql
ENV MYSQL_PASSWORD pass123
# expose the default port
EXPOSE 3306
When I docker run the equivalent container and execute a command from the inside of the container the mysql part works perfectly and it allows me to create tables, users dbs and such. There is no connection error.
Now the issue, I created an (in my opinion) equivalent docker compose file
services:
mysql:
image: mysql:8.0-debian
command: sh -c "groupadd -r group-mysql &&
useradd -r -g user-mysql group-mysql &&
tail -f /dev/null"
environment:
MYSQL_ROOT_PASSWORD: root123
MYSQL_USER: user-mysql
MYSQL_PASSWORD: pass123
ports:
- 3306:3306
When I run the docker compose up of the aforementioned .yml file and I try to run docker exec -it containername bash and then mysql -u root -p I get this connection error: ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
What am I doing wrong? Could someone point me in the right direction?
Thank you very much
Delete the command: line.
If you provide a Compose command: override, that runs instead of the normal main container command. In your case the command: override runs a couple of commands, then does tail -f /dev/null; that "keep the container running" command is running instead of the actual database server.
You don't actually need the additional user inside the container. Nothing in this setup makes use of that user. The default mysql image setup will run the MySQL server as a foreground process, and that's the behavior you need.

how two link two docker containers, and how to use the mysql client on a mysql docker container

I'd like to test webtrees PHP docker. They suggest connecting to a mysql docker using --link mysql:db, as follows:
docker run -d -p 80:80 --name webtrees --link mysql:db -v /webtrees/data:/var/www/html/data -v /webtrees/media:/var/www/html/media -e DISABLE_SSL=TRUE -e PORT=80 --restart always dtjs48jkt/webtrees
Their README says:
The image does not contain a MySQL database. Instead you have to use a separate MySQL instance. For example you could use the
MySQL Docker Image. Using the --link parameter a direct connection to
the database in an other container could be established. If you use
the --link parameter it is sufficient to set as database hostname db
and port 3306. The database user must have all access rights to
create the necessary database and tables.
However the webtrees container cannot access the mysql server. How I correctly link these two docker containers?
I tried using the official mysql docker image as follows:
docker run --name some-mysql -e MYSQL_ROOT_PASSWORD=my-secret-pw -e MYSQL_DATABASE=webtrees -e MYSQL_USER=my_user -e MYSQL_PASSWORD=my_pwd -d mysql:5.5
but then I don't know how to link webtrees docker container with the mysql container.
Also, how can I use the mysql client? the documentation gives this example, but I don't understand what are the correct parameters for netowrk and -h:
$ docker run -it --network some-network --rm mysql mysql -hsome-mysql -uexample-user -p
Regarding your first question, the --link option for docker run is deprecated according to the documentation, so I wouldn't recommend using it.
With the amount of configuration required, I'd recommend setting up a docker-compose.yml instead. I set up the configuration you require like this:
version: '3.0'
services:
webtrees:
image: dtjs48jkt/webtrees
restart: always
ports:
- 80:80
environment:
- DISABLE_SSL=TRUE
- PORT=80
volumes:
- /webtrees/data:/var/www/html/data
- /webtrees/media:/var/www/html/media
networks:
- my-network
mysql:
image: mysql:5.5
environment:
- MYSQL_ROOT_PASSWORD=my-secret-pw
- MYSQL_DATABASE=webtrees
- MYSQL_USER=my_user
- MYSQL_PASSWORD=my_pwd
networks:
- my-network
networks:
my-network:
To run the containers, use:
docker-compose up --detach
What this will do is spin up a mysql container and a webtrees container according to the configuration you specified in your question with a network called my-network.
In the web interface of web trees on http://localhost/ you can make it connect to the mysql container with the following configuration, so it will connect to it through the docker network:
Since the service name in the docker-compose.yml is mysql, the required hostname is mysql.
Basically you need to have all the containers (mysql DB server, mysql client and application) in the same Docker network. By default they are not. Alternatively, --link can be used to link them (as shown in webtrees run example), but it's considred as legacy feature and network should be used instead of that.
So what you need to do:
Create custom Docker network:
docker network create user-network
Run mysql server in that network. Name should be db, because webtrees relies on that hostname for DB:
docker run --name db --network user-network -e MYSQL_ROOT_PASSWORD=my-secret-pw -e MYSQL_DATABASE=webtrees -e MYSQL_USER=my_user -e MYSQL_PASSWORD=my_pwd -d mysql:5.5
Run mysql client in the same network:
docker run -it --network user-network --rm mysql mysql -hdb -umy_user -p
Finally you can run an app in the same network:
docker run -d -p 80:80 --name webtrees --network user-network -v /webtrees/data:/var/www/html/data -v /webtrees/media:/var/www/html/media -e DISABLE_SSL=TRUE -e PORT=80 --restart always dtjs48jkt/webtrees
After that web app should be accessible from your browser under http://localhost/

Cannot connect to mysql in Docker container from host

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

Docker Wordpress connection to the database server on the localhost

I run docker wordpress image with command
docker run --name test-wordpress -p 8081:80 -d wordpress
MySQL 8 is on localhost on Windows 10. Database credentials are valid.
In wordpress setup I use this configuration
Database name: wordpress (not exist yet)
Username: root
Password: ***
Database host: localhost || 127.0.0.1 || host.docker.internal
I get error
Error establishing a database connection
What is correct database host?
By default, docker will attach your new container to a bridged network. This means that addresses such as: localhost and 127.0.0.1 only refers to the container itself. Not the host machine.
The easy was to solve this, is to wrap the MySQL database in a container of it's own. This way your containers can address eachother without issues.
Connect to MySQL database on the host
If you really want to connect the service in the container with a service on the host, you will need to connect the container to the appropriate network.
First, you will need to create a network. Assuming that your local machine has a fixed IP of 192.168.0.1, you should be able to do this with:
docker network create -d bridge --subnet 192.168.0.0/24 --gateway 192.168.0.1 dockernet
You can then:
docker run --name test-wordpress --net=dockernet -p 8081:80 -d wordpress
And you should then be able to refer to the host from inside the container by the IP: 192.168.0.1.
Create stack with wordpress and MySQL
The better alternative here though, is to create an application stack definition with docker-compose, that includes both the database and the wordpress application.
You can create a docker-compose.yml file like this:
docker-compose.yml
version: '3.3'
services:
db:
image: mysql:5.7
volumes:
- db_data:/var/lib/mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: somewordpress
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD: wordpress
wordpress:
depends_on:
- db
image: wordpress:latest
ports:
- "8000:80"
restart: always
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: wordpress
WORDPRESS_DB_NAME: wordpress
volumes:
db_data: {}
And the start the stack with this:
docker-compose up
Then visit: http://localhost:8000
Notice that the database data will be stored in the docker managed volume called db_data.
Details on installing docker-compose can be found here: https://docs.docker.com/compose/install/
Start both containers with just docker run
The same can be achieved with just docker run, like this:
docker volume create db_data
docker network create mysqlnet
docker run --name test-mysql -e MYSQL_ROOT_PASSWORD=somewordpress -e MYSQL_DATABASE=wordpress -e MYSQL_USER=wordpress -e MYSQL_PASSWORD=wordpress -v db_data:/var/lib/mysql --net=mysqlnet -d mysql:5.7
docker run --name test-wordpress -e WORDPRESS_DB_HOST=test-mysql:3306 -e WORDPRESS_DB_USER=wordpress -e WORDPRESS_DB_PASSWORD=wordpress -e WORDPRESS_DB_NAME=wordpress --net=mysqlnet -p 8081:80 -d wordpress:latest
You can change the mapping of the mysql datafiles to a local directory instead, and just ommit the docker volume create statement.
Found a great article explaining How to setup Wordpress using Docker.
The issue is MySQL container is not accessible from the wordpress container, so to fix this we will have to create docker network and connect both the containers.
Lets create a docker network -
> docker network create --attachable wordpress-network
Now attach both the containers with following command -
> docker network connect wordpress-network mysql-container
> docker network connect wordpress-network wordpress-container
Now open the wordpress in browser and set mysql-container as Database Host
The accepted answer is incomplete and inaccurate. You can very easily connect to MySQL running in host machine with wordpress running inside container by providing --network=host option during docker run.
Issue With Wordpress:
The only caveat is wordpress can't connect with MySQL with default setup.
In Ubuntu systems running MySQL 5.7 (and later versions), any MySQL user is set to authenticate using the auth_socket plugin by default rather than with a password. This allows for some greater security and usability in many cases, but it can also complicate things when you need to allow an external incompatible program like wordpress to access the user.
In order to use a password to connect to MySQL as user, you will need to switch its authentication method from auth_socket to mysql_native_password.
Solution:
Create a dedicated mysql user for wordpress database in legacy mysql_native_password mode.
$> sudo mysql
mysql> CREATE USER 'wpuser'#'localhost' IDENTIFIED WITH mysql_native_password BY 'wp_password';
mysql> CREATE DATABASE wordpress_db CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
mysql> GRANT ALL ON wordpress_db.* TO 'wpuser'#'localhost';
mysql> FLUSH PRIVILEGES;
Run Wordpress container:
docker run --rm --name my_wordpress --network=host -e WORDPRESS_DB_USER=wpuser -e WORDPRESS_DB_PASSWORD=wp_password -e WORDPRESS_DB_NAME=wordpress_db -e WORDPRESS_DB_HOST=127.0.0.1 wordpress

TeamCity failed to connect MySQL which is run in Docker

I start MySQL in docker by using below command:
docker run --name mysql-for-teamcity \
-e MYSQL_ROOT_PASSWORD=FAKE-ROOT-PW\
-v ~/MySQL/var_lib_mysql:/var/lib/mysql \
-p 3306:3306 \
-p 33060:33060 \
-it mysql
But TeamCity failed to connect the MySQL, the error message is :
I can connect to the MySQL in Terminal using below command:
mysql -u root --protocol=tcp -p
And database "teamcity" has also been created.
My Environment:
Mac OS X 10.14.1
Docker Desktop 2.0.0.0-mac81(29211)
TeamCity and MySQL are running in seperated Docker containers
Both Docker images tag is latest
The reason is TeamCity and MySQL are running in the separate containers, so when I specified "127.0.0.1" for TeamCity, it is not possible for it to connect to MySQL. Because they are simply not running in the same host.
The solution is using Docker Compose which set up a local network for containers by default.
Step 1: create a docker-compose.yml in an empty directory you want to place your TeamCity:
version: '3'
services:
TeamCity:
image: jetbrains/teamcity-server
ports:
- "8111:8111"
volumes:
- <your TeamCity dir>/data:/data/teamcity_server/datadir
- <your TeamCity dir>log:/opt/teamcity/logs
MySQL:
image: mysql
ports:
- "3306:3306"
volumes:
- <your TeamCity dir>/mysql:/var/lib/mysql
env_file:
- mysql.env
Step 2: create a mysql.env in the same directory:
MYSQL_ROOT_PASSWORD=YOUR-MYSQL-PASSWD
Step 3: run docker-compose up -d in the terminal in
Step 4: open "http://127.0.0.1:8111" in browser
Step 5: input "MySQL:3306" in the DataBase Host field.