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.
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
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/
I'm trying to connect to MySQL (inside a Docker container), through MySQL Workbench and PHPMyAdmin with no luck. I keep receiving the same error everytime I try to log in with both applications.
PhpMyAdmin error.png
I've also tried to deploy both containers with this docker-compose.yml:
version: '2.0'
services:
db:
image: mysql
environment:
- MYSQL_DATABASE=mysqltest
- MYSQL_USER=user
- MYSQL_PASSWORD=userpassword
- MYSQL_ROOT_PASSWORD=rootpassword
ports:
- "127.0.0.1:3306:3306"
phpmyadmin:
image: phpmyadmin/phpmyadmin
depends_on:
- db
ports:
- "127.0.0.1:8080:80"
...and again, no luck!
Do you have any suggestion,please?
Solved.
Basically, it was a version problem. With the latest version of MySQL (I think it's 8) there are some problem if I try to connect through Workbench or PHPMyAdmin.
So, I've tried to use an older version of MySQL (version 5) and I've created two containers with these commands:
docker run --name mysql_test -e MYSQL_DATABASE=dbtest -e MYSQL_USER=user -e MYSQL_PASSWORD=userpassword -e MYSQL_ROOT_PASSWORD=rootpassword -p 127.0.0.1:3306:3306 -d mysql:5
docker run --name myadmin -d --link mysql_test:db -p 127.0.0.1:8080:80 phpmyadmin/phpmyadmin
So I managed to log in successfully, using both credentials that I've created as environment variables.
Hope this can help somebody!
I have problem with mysql-server container.
Post mysql with Exited(1) from docker has no solution for me included.
Here my workflow on windows 10.
0. Docker version:
Docker version 17.12.0-ce, build c97c6d6
1. My Dockerfile:
FROM mysql/mysql-server
ENV MYSQL_ROOT_PASSWORD root
ENV MYSQL_DATABASE blockchain
ENV MYSQL_USER block
ENV MYSQL_PASSWORD blockchain
COPY create_schema.sql /docker-entrypoint-initdb.d/create_schema.sql
EXPOSE 3306
Build command:
docker build -t mysqlserver .
Run command (option -d is used):
docker run -ti -p 3306:3306 --name mysqlserver1 -v C:/Users/user/sandbox/mysql:/var/lib/mysql -d --net testnetwork mysqlserver --innodb_use_native_aio=0
But result after start is: Exited (1) 11 minutes ago, declared path folder for database is initialized.
Where is my error ?
Thx for help
For mysql docker container, you need to specify the following parameters in your Dockerfile;
MYSQL_ROOT_PASSWORD
Initial Database to be created
Expose Port from the docker container to the host machine
For instance if you're creating the running mysql docker image using terminal, it could be: sudo docker run -e MYSQL_ROOT_PASSWORD=dev --name testdb -p 3800:3600 -d mysql:8.0.17
If I have a data.sql, how I can import database to my mysql docker container? How I can import database data. In a dockerised world this adds a layer of complexity. some methods please.
Here my docker-compose.yml:
nginx:
build: ./nginx/
container_name: nginx-container
ports:
- 80:80
links:
- php
volumes_from:
- app-data
php:
build: ./php/
container_name: php-container
expose:
- 9000
links:
- mysql
volumes_from:
- app-data
app-data:
image: php:7.0-fpm
container_name: app-data-container
volumes:
- ./www/html/:/var/www/html/
command: "true"
mysql:
image: mysql:latest
container_name: mysql-container
ports:
- 3306:3306
volumes_from:
- mysql-data
environment:
MYSQL_ROOT_PASSWORD: secret
MYSQL_DATABASE: name_db
MYSQL_USER: user
MYSQL_PASSWORD: password
mysql-data:
image: mysql:latest
container_name: mysql-data-container
volumes:
- /var/lib/mysql
command: "true"
You can import database afterwards:
docker exec -i mysql-container mysql -uuser -ppassword name_db < data.sql
Mount your sql-dump under/docker-entrypoint-initdb.d/yourdump.sql utilizing a volume mount
mysql:
image: mysql:latest
container_name: mysql-container
ports:
- 3306:3306
volumes:
- ./dump.sql:/docker-entrypoint-initdb.d/dump.sql
environment:
MYSQL_ROOT_PASSWORD: secret
MYSQL_DATABASE: name_db
MYSQL_USER: user
MYSQL_PASSWORD: password
This will trigger an import of the sql-dump during the start of the container, see
https://hub.docker.com/_/mysql/ under "Initializing a fresh instance"
I can't seem to make this work with the latest mysql or mysql:5.7. So I use mariaDB instead. Here is my docker-compose.yaml code.
version: '3'
services:
mysql:
image: mariadb:10.3
container_name: mariadb
volumes:
- container-volume:/var/lib/mysql
- ./dump.sql:/docker-entrypoint-initdb.d/dump.sql
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: name_db
ports:
- "3306:3306"
volumes:
container-volume:
Another option if you don't wanna mount a volume, but wanna dump a file from your local machine, is to pipe cat yourdump.sql. Like so:
cat dump.sql | docker exec -i mysql-container mysql -uuser -ppassword db_name
See:
https://gist.github.com/spalladino/6d981f7b33f6e0afe6bb
Just write docker ps and get the container id and then write the following;
docker exec -i your_container_id mysql -u root -p123456 your_db_name < /Users/your_pc/your_project_folder/backup.sql
Import using docker-compose
cat dump.sql | docker-compose exec -T <mysql_container> mysql -u <db-username> -p<db-password> <db-name>
combine https://stackoverflow.com/a/51837876/1078784
and answers in this question, I think the best answer is:
cat {SQL FILE NAME} | docker exec -i {MYSQL CONTAINER NAME} {MYSQL PATH IN CONTAINER} --init-command="SET autocommit=0;"
for example in my system this command should look like:
cat temp.sql | docker exec -i mysql.master /bin/mysql --init-command="SET autocommit=0;"
also you can use pv to moniter progress:
cat temp.sql | pv | docker exec -i mysql.master /bin/mysql --init-command="SET autocommit=0;"
And the most important thing here is "--init-command" which will speed up the import progress 10 times fast.
I can import with this command
docker-compose exec -T mysql mysql -uroot -proot mydatabase < ~/Desktop/mydatabase_2019-10-05.sql
you can follow these simple steps:
FIRST WAY :
first copy the SQL dump file from your local directory to the mysql container. use docker cp command
docker cp [SRC-Local path to sql file] [container-name or container-id]:[DEST-path to copy to]
docker cp ./data.sql mysql-container:/home
and then execute the mysql-container using (NOTE: in case you are using alpine version you need to replace bash with sh in the given below command.)
docker exec -it -u root mysql-container bash
and then you can simply import this SQL dump file.
mysql [DB_NAME] < [SQL dump file path]
mysql movie_db < /home/data.sql
SECOND WAY : SIMPLE
docker cp ./data.sql mysql-container:/docker-entrypoint-initdb.d/
As mentioned in the mysql Docker hub official page.
Whenever a container starts for the first time, a new database is created with the specified name in MYSQL_DATABASE variable - which you can pass by setting up the environment variable see here how to set environment variables
By default container will execute files with extensions .sh, .sql and .sql.gz that are found in /docker-entrypoint-initdb.d folder. Files will be executed in alphabetical order. this way your SQL files will be imported by default to the database specified by the MYSQL_DATABASE variable.
for more details you can always visit the official page
do docker cp file.sql <CONTAINER NAME>:/file.sql first
then docker exec -it <CONTAINER NAME> mysql -u user -p
then inside mysql container execute source \file.sql
Trying "docker exec ... < data.sql" in Window PowerShell responses with:
The '<' operator is reserved for future use.
But one can wrap it out with cmd /c to eliminate the issue:
cmd /c "docker exec -i mysql-container mysql -uuser -ppassword name_db < data.sql"
This one work for me
$ docker exec -i NAME_CONTAINER_MYSQL mysql -u DB_USER -pPASSWORD DATABASE < /path/to/your/file.sql
First if do you want to know what is the NAME_CONTAINER_MYSQL, you should use
this command below :
$ docker ps
In the output column NAME you will see the NAME_CONTAINER_MYSQL that do you need to replace in the command above.
You can run a container setting a shared directory (-v volume), and then run bash in that container. After this, you can interactively use mysql-client to execute the .sql file, from inside the container. obs: /my-host-dir/shared-dir is the .sql location in the host system.
docker run --detach --name=test-mysql -p host-port:container-port --env="MYSQL_ROOT_PASSWORD=my-root-pswd" -v /my-host-dir/shared-dir:/container-dir mysql:latest
docker exec -it test-mysql bash
Inside the container...
mysql -p < /container-dir/file.sql
Custom parameters:
test-mysql (container name)
host-port and container-port
my-root-pswd (mysql root password)
/my-host-dir/shared-dir and /container-dir (the host directory that will be mounted in the container, and the container location of the shared directory)
you can copy the export file for e.g dump.sql using docker cp into the container and then import the db. if you need full instructions, let me know and I will provide