Month: June 2017

Docker MySQL 5.7 example

Example running MySql 5.7 inside docker container with data stored outside and access from remote computers.

Init container for the first time

docker run --name mysql-container -e MYSQL_ROOT_PASSWORD=123456 -e MYSQL_ROOT_HOST=172.17.0.1 -p 3306:3306 -v /home/user/mysql:/var/lib/mysql -d mysql/mysql-server:5.7 --character-set-server=utf8 --collation-server=utf8_general_ci

  • --name mysql-container – Container name
  • -e MYSQL_ROOT_PASSWORD=123456 – Password for root
  • -e MYSQL_ROOT_HOST=172.17.0.1 – Virtual host
  • -p 3306:3306 – Port forwarding. Real:Virtual
  • -v /home/user/mysql:/var/lib/mysql – Data directory assignment
  • -d mysql/mysql-server:5.7 – MySQL 5.7 image
  • --character-set-server=utf8 --collation-server=utf8_general_ci – Settings

Run mysql command line client

docker exec -it mysql-container mysql -uroot -p

Enter command line inside the docker & view mysql log

docker exec -it mysql-container bash
cat /var/log/mysqld.log

Connect on hosting machine or remotely

mysql -u root -p -h 172.17.0.1

Move config outside from docker

  1. Copy config from docker to outside
    docker exec -it mysql-container cat /etc/my.cnf > /home/user/mysql/my.cnf
  2. Delete container
    docker rm mysql-container
  3. Init container and reassign config file path
    docker run --name mysql-container -v /home/user/mysql/my.cnf:/etc/my.cnf -e MYSQL_ROOT_PASSWORD=123456 -e MYSQL_ROOT_HOST=172.17.0.1 -p 3306:3306 -v /home/user/mysql:/var/lib/mysql -d mysql/mysql-server:5.7 --character-set-server=utf8 --collation-server=utf8_general_ci

Enable remote connection

docker exec -it mysql-container mysql -uroot -p

  • Enable remote connection from any host
    grant all privileges on *.* to root@'%' identified by '123456';
  • Or enable remote connection from specific host to specific databese
    grant all privileges on mydb.* to root@'10.0.0.100' identified by '123456';

Full tutorial: https://hub.docker.com/r/mysql/mysql-server/