Thursday, May 2, 2019

Starting a MySQL Server and PhpMyAdmin Docker containers

MacOS Mojave 10.14.4
Docker Engine: 18.09.0


Goal:
  • To have a docker container running a MySQL Server instance and another running the PhpMyAdmin.

Install:
  • Download the MySQL Community Edition image:
    • docker pull mysql:5.7.26
  • Start a new Docker container for the MySQL Community Server:
    • docker run --name=mysql1 -p 3306:3306 -e MYSQL_RANDOM_ROOT_PASSWORD=yes -d mysql:5.7.26
  • Or create and start the container:
    • docker container create --name=mysql1 -p 3306:3306 -e MYSQL_RANDOM_ROOT_PASSWORD=yes mysql:5.7.26
    • docker container start mysql1
  • The container appears in the list of running containers when you run the docker ps command:
    • docker ps
    • ============================================
    • CONTAINER ID        IMAGE                COMMAND                  CREATED             STATUS                   PORTS                 NAMES
    • 188d9a1f7d0d        mysql:5.7.26            "docker-entrypoint.s…"   25 minutes ago      Up 25 minutes       0.0.0.0:3306->3306/tcp, 33060/tcp   mysql1
    • ============================================
  • The -d option used in the docker run command above makes the container run in detached mode. These are other options available:
    • -d, --detach - Detached mode: run command in the background
    • -i, --interactive - Keep STDIN open even if not attached
    • -t, --tty - Allocate a pseudo-TTY
    • -u, --user string  -  Username or UID (format: <name|uid>[:<group|gid>])
    • -w, --workdir string  -  Working directory inside the container
  • Use this command to monitor the output from the any container:
    • docker logs <Container-ID>
  • Once initialization is finished, the command's output is going to contain the random password generated for the root user; check the password with, for example, this command:
    • docker logs mysql1 2>&1 | grep GENERATED
    • GENERATED ROOT PASSWORD: aSS3sNArecAMEsH3w=Os.it0N.i
  • Once the server is ready, you can run the mysql client within the MySQL Server container you just started and connect it to the MySQL Server using the password generated:
    • docker exec -it mysql1 mysql -u root -p
  • You can access the server from a client running on another computer via network, or from the computer were docker is running, using the command:
    • mysql -u root -p -h <IP-OF-THE-COMPUTER-WHERE-DOCKER-IS-RUNNING>
  • Because we used MYSQL_RANDOM_ROOT_PASSWORD=yes, after you have connected a mysql client to the server you must reset the server root password by running these commands:
    • ALTER USER 'root'@'localhost' IDENTIFIED BY '<password>';
    • ALTER USER 'root'@'%' IDENTIFIED BY '<password>';
    • FLUSH PRIVILEGES;
  • To run MySql Server again, after close Docker, you need to start Docker and run:
    • docker container start mysql1

Additional Info:
  • Docker requires your command (e.g. CMD) to keep running in the foreground. Otherwise, it thinks that your applications stops and shutdown the container. The problem is that some application does not run in the foreground. In this situation, you can add tail -f /dev/null to your command. Even if your main command runs in the background, your container doesn’t stop because tail is keep running in the foreground.
  • You can run the container that doesn't have a command running in foreground and open a terminal inside the container by using:
    • docker run --entrypoint "/bin/sh" -it alpine/git
    • docker run --entrypoint "/bin/bash" -it alpine/git
  • You can stop the container using the command:
    • docker stop mysql1
  • You can also remove the container using the command:
    • docker rm mysql1
  • To connect to MySQL server remotely using MySQL client, edit the /etc/mysql/mysql.conf.d/mysqld.cnf configuration file:
    • sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf
  • And change the "bind-address" from  127.0.0.1  to:
    • bind-address = 0.0.0.0

Install PhPMyAdmin:
  • Download and run PhpMyAdmin docker image:
    • docker run --name myadmin -d -e PMA_ARBITRARY=1 -p 8080:80 phpmyadmin/phpmyadmin
    • docker ps
    • ============================================
    • CONTAINER ID        IMAGE                   COMMAND                  CREATED             STATUS              PORTS                               NAMES
    • 188d9a1f7d0d        mysql:5.7.26            "docker-entrypoint.s…"   25 minutes ago      Up 25 minutes       0.0.0.0:3306->3306/tcp, 33060/tcp   mysql1
    • 56e041e28d47        phpmyadmin/phpmyadmin   "/run.sh supervisord…"   20 minutes ago         Up 20 minutes          9000/tcp, 0.0.0.0:8080->80/tcp      myadmin
    • ============================================
  • Access the address:
    • http://localhost:8080
      • Server: localhost
      • Username: root
      • Password: aSS3sNArecAMEsH3w=Os.it0N.i
  • To run PhpMyAdmin again, after close Docker, you need to start Docker and run:
    • docker container start myadmin

Backup and Restore MySQL databases:

  • Backup with data:
    • mysqldump -u root -p iotdb > iotdb_bkp20190627.sql
  • Backup without data, only structure:
    • mysqldump -u root -p -d iotdb > iotdb_bkp20190627.sql
  • Backup compatible with: (ansi, mysql323, mysql40, postgresql, oracle, mssql, db2, maxdb, no_key_options, no_table_options, or no_field_options)
    • mysqldump --compatible=ansi -u root -p iotdb > iotdb_bkp20190627.sql
  • Restore:
    • mysqldump -u root -p iotdb < iotdb_bkp20190627.sql

References: