Where is the mysql database saved when using docker? - mysql

Where is my databse saved when I create it in MYSQL through docker?
I created a SQL server with docker by running the following command:
sudo docker run --name toms-sql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=password mysql
However

Your data is stored in the container. You can mount the mysql data folder to a local folder like this
docker run --name some-mysql -v /my/own/datadir:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=my-secret-pw mysql

Related

I am unable to find all the files from my docker container volume

inside the volume of the docker container
I have been trying to export all my data from my local database to my docker container.
I created a volume "mydb" and I copy pasted the whole directory for the database "BookLook" from my local mysql server to the docker volume directory.
I am running my docker container with this command in manjaro linux:
docker run --name mysql-container -e MYSQL_ROOT_PASSWORD=password -v mydb:/var/lib/mysql -p 3306:3306 -d mysql:latest
still I am not being able to get the database when I am opening the mysql in the docker container
I am executing the docker container with the following command:
docker exec -it mysql-container bash

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/

How to connect with MySQL Docker image from host?

I am Running a MySQL Docker image and creating database and table. The container is lauched with the command:
docker run --network host -p 3306:3306 --name mysql -e MYSQL_ROOT_PASSWORD=root -d mysql/mysql-server
Spark is running on my host machine, So i want to write data from Spark into the database running in the container.
But the connection does not succeed and I am getting an Exception:
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
You can try this 2 solutions:
Check if your connection URL is not wrong in your java file.
Check if your container accept connections that is not localhost of the container:
Go to your cmd and enter in the container:
docker exec -it containerID /bin/bash
install vim inside container apt install vim
vim /etc/mysql/mysql.conf.d/mysqld.cnf
comment bind-adress or make it listen to 0.0.0.0
exit from container
restart
Hope this help.
As Schwarz54 mentioned in his answer, it could be host issue.
I usually pass it at the time of initialising the container using the MYSQL_HOST parameter.
docker run --name mysql_5.7 -d -p 3306:3306 -e MYSQL_ROOT_PASSWORD=root1234 -e MYSQL_HOST=localhost mysql:5.7
I think you don't have to map the ports if you are running it on the host network.
docker run --name mysql_5.7 -d --network host -e MYSQL_ROOT_PASSWORD=root1234 -e MYSQL_HOST=localhost mysql:5.7
You can mount a local directory to the local container if you want the data to be persisted across container restarts using -v flag:
mkdir -p /Users/projects/data/mysql5.7
And mount this directory in the container at /var/lib/mysql
docker run --name mysql_5.7 -d --rm -p 3306:3306 -e MYSQL_ROOT_PASSWORD=root1234 -e MYSQL_HOST=localhost -v /Users/projects/data/mysql5.7:/var/lib/mysql mysql:5.7

Basic MySQL Docker Container

I just created a mysql docker container using
docker run -p 3310:3310 --restart always --env MYSQL_ROOT_HOST=% --name=ipca-mysql -e MYSQL_ROOT_PASSWORD=admin -d mysql/mysql-server:5.7
I am able to connect to my database using docker exec -ti ipca-mysql bash and mysql -u root -p
But when I tried to connect to my database at localhost:3310 using mysql workbench I get:
lost connection to mysql server at 'reading initial communication
packet' system error 0
Any idea what is this issue?
MySQL container start using this command it's worked.
docker run -p 3310:3306 --restart always --env MYSQL_ROOT_HOST=% --name=ipca-mysql -e MYSQL_ROOT_PASSWORD=admin -d mysql/mysql-server:5.7
You could use this docker command:
docker run --name mysql-tacs -e MYSQL_ROOT_PASSWORD=root -d -p 3310:3306 mysql
In the link below there is a video which shows step by step the creation of mysql server with docker until the connection in its server using MySQL Workbench.
https://www.youtube.com/watch?v=ZZwvhMvUJiw

how to extend docker mysql image

I pull the mysql/mysql-server image, then I execute the following commands below:
docker run --name myapp -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql/mysql-server
docker exec -it myapp bash
After this, I install jdk and tomcat in the newly created myapp container, then I exit the shell and run:
docker commit myapp myappwithjdk
Then I run the following, but the container exits immediately:
docker run -e MYSQL_ROOT_PASSWORD=my-secret-pw -d myappwithjdk
I don't know why it does this.
It could be leftovers from running mysql (like pid file). It is bad approach to use exec & commit for creating own container based on different one. Much better to create own image via Dockerfile:
FROM mysql/mysql-server
RUN <your commands here>
and then
docker build -t myappwithjdk .
The mysql image have the way to execute init scripts (both bash and sql) once on container creation. Simply put them to /docker-entrypoint-initdb.d/ folder:
FROM mysql:latest
ENV MYSQL_ROOT_PASSWORD=root
COPY 00-extra_env_setup.sh /docker-entrypoint-initdb.d/00-extra_env_setup.sh
COPY 01-user_setup.sql /docker-entrypoint-initdb.d/01-user_setup.sql
build:
docker build -t mymysql .
and run(note port exposing):
docker run -p 3306:3306 mymysql
now you able connect to it outside(change the ip to the ip of your docker server):
mysql --host 127.0.0.1 --port 3306 --user root --password
That's it!