Quantcast
Channel: Oracle
Viewing all articles
Browse latest Browse all 1814

Wiki Page: Using MySQL Database with Docker Engine

$
0
0
Written by Deepak Vohra If MySQL database is to be installed on Linux it involves creating a mysql user and group, downloading and un-packaging the binary distribution tar.gz file, running the mysql_install_db script, and setting owner of installation and data directories to root and mysql respectively. Subsequently, the bin/mysqld_safe command would need to be run to start the MySQL server. The installation and configuration requires several commands and scripts to be run. Using a Docker image the installation and configuration is simplified to running one or two Docker Engine commands. In this tutorial we shall install MySQL Database on Docker Engine. Setting the Environment Pulling the Docker Image Running the Docker Image Listing the Docker Containers Listing the Docker Images Starting an Interactive Terminal Starting MySQL CLI Setting the Database Creating a MySQL Database Table Stopping the Docker Container Running a Variation of the docker run Command Deleting the Docker Container and Docker Image Setting the Environment The following software is required for this tutorial. Docker Engine Docker Image for MySQL Database Create an Amazon EC2 instance from AMI Ubuntu Server 14.04 LTS (HVM), SSD Volume Type - ami-d05e75b8. Obtain the IP Address of the Ubuntu instance from the Amazon EC2 instance and SSH login to the Ubuntu instance from a local Linux machine. ssh -i “docker.pem” ubuntu@52.23.240.180 Install, start and verify status for Docker Engine. sudo service docker start sudo service docker status Docker should be running as in the following output. Initially, the Ubuntu instance does not have any Docker containers running or Docker images installed. Run the following command to list the running Docker containers and Docker images installed. sudo docker ps sudo docker images No Docker containers get listed as running, and no Docker images get listed as downloaded. Pulling the Docker Image Though several Docker images are available on the Docker hub we shall use the official Docker image called “mysql” ( https://hub.docker.com/_/mysql/ ) . Run the following command to pull the Docker image mysql . sudo docker pull mysql Docker image mysql:latest gets downloaded. The Docker image mysql is not running yet. Running the Docker Image Run the mysql Docker image with the following command in which the –name parameter specifies the Docker container name as mysql . The –d parameter makes the Docker container run in detached mode. The mysql Docker images requires a mandatory environment variable to be set, the MYSQL_ROOT_PASSWORD variable for the root password. An environment variable is set with the –e command argument. sudo docker run --name mysql -e MYSQL_ROOT_PASSWORD='mysql' -d mysql A Docker container gets started to run MySQL Database. Listing the Docker Containers List the Docker running containers with the following command. sudo docker ps The Docker container mysql should get listed including the container id, port/s, command used to start the container, and status. Port 3306 is exposed by the Docker container. For subsequent Docker Engine commands either the container id “77fbe3dc4f6e” may be used or the container name “mysql” may be used. Listing the Docker Images List the Docker image/s downloaded sudo docker images The Docker image mysql gets listed. Starting an Interactive Terminal As indicated in the Dockerfile for the "mysql" Docker image the image is based on the Docker image "debian", which implies that an interactive terminal may be run on the Docker container. Run the following command to start an interactive terminal on the mysql container. sudo docker exec -it mysql bash Alternatively, run the same command using the container id. sudo docker exec –it 77fbe3dc4f6e bash A Docker container gets started and the root@77fbe3dc4f6e command prompt gets displayed. The MySQL Database software installed in the Docker container may be accessed from the bash shell. Listing the Docker container Logs To list the Docker container logs run the following command. sudo docker logs -f mysql The Docker container logs get listed. If the MySQL server got started without error the message “mysqld: ready for connections” gets displayed. Starting MySQL CLI Start the MySQL command line interface (CLI) with the following command run in the bash shell. mysql –u root -p When prompted to specify the password specify “mysql”, which is the password set in the environment variable MYSQL_ROOT_PASSWORD when the Docker image is run. Click Enter. The MySQL CLI gets started and the mysql> command prompt gets displayed. Setting the Database Before any command may be run in the MySQL CLI a database must be set as the current database. List the databases with the following command. show databases; In addition to the system, information and performance databases, a user database “mysql” also gets listed. Set the current database as the “mysql” database. Creating a MySQL Database Table Create a MySQL Database table “wlslog” with the following SQL statement. First, drop the “mysql” table if it already exists. CREATE TABLE wlslog(time_stamp VARCHAR(45) PRIMARY KEY,category VARCHAR(25),type VARCHAR(25),servername VARCHAR(25),code VARCHAR(25),msg VARCHAR(45)); The wlslog table gets created. Describe the wlslog table. desc wlslog; The table description gets listed. Add data to the wlslog table with the following SQL statements. INSERT INTO wlslog VALUES('Apr-8-2014-7:06:16-PM-PDT','Notice','WebLogicServer','AdminServer','BEA-000365','Server state changed to STANDBY'); INSERT INTO wlslog VALUES('Apr-8-2014-7:06:17-PM-PDT','Notice','WebLogicServer','AdminServer','BEA-000365','Server state changed to STARTING'); INSERT INTO wlslog VALUES('Apr-8-2014-7:06:18-PM-PDT','Notice','WebLogicServer','AdminServer','BEA-000365','Server state changed to ADMIN'); INSERT INTO wlslog VALUES('Apr-8-2014-7:06:19-PM-PDT','Notice','WebLogicServer','AdminServer','BEA-000365','Server state changed to RESUMING'); INSERT INTO wlslog VALUES('Apr-8-2014-7:06:20-PM-PDT','Notice','WebLogicServer','AdminServer','BEA-000331','Started WebLogic AdminServer'); INSERT INTO wlslog VALUES('Apr-8-2014-7:06:21-PM-PDT','Notice','WebLogicServer','AdminServer','BEA-000365','Server state changed to RUNNING'); INSERT INTO wlslog VALUES('Apr-8-2014-7:06:22-PM-PDT','Notice','WebLogicServer','AdminServer','BEA-000360','Server started in RUNNING mode'); Data gets added to the wlslog table. Run the following SQL statement to query the wlslog table. SELECT * FROM wlslog; The SQL query result is shown in MySQL CLI. The 7 rows of data added get listed. To exit MySQL CLI and the interactive bash shell run “exit” for each. Stopping the Docker Container Stop the Docker container mysql with container id 77fbe3dc4f6e with the following command. sudo docker stop 77fbe3dc4f6e Subsequently list the running Docker containers. sudo docker ps In contrast to before running the docker stop command no Docker container gets listed. Running a Variation of the docker run Command When the docker run command was run earlier only one mandatory environment variable MYSQL_ROOT_PASSWORD was specified. The “mysql” image also supports the following environment variables. Environment Variable Description MYSQL_DATABASE Creates a MySQL database MYSQL_USER, MYSQL_PASSWORD Creates a MySQL user and password MYSQL_ALLOW_EMPTY_PASSWORD Allows empty password The –v command argument mounts a directory from the host on the data directory /var/lib/mysql in the Docker container, in which MySQL stores data by default. Run the following docker run command in which the /mysql/data directory on the host mounts the /var/lib/mysql data directory in the Docker container. The MYSQL_DATABASE environment variable is set to “mysqldb”, which creates a new database. The MYSQL_USER , MYSQL_PASSWORD environment variables are set to create a new user called “mysql” with password “mysql”. The MYSQL_ALLOW_EMPTY_PASSWORD environment variable is set to “no”, which disallows empty password for the root user. sudo docker run -v /mysql/data:/var/lib/mysql --name mysqldb -e MYSQL_DATABASE='mysqldb' -e MYSQL_USER='mysql' -e MYSQL_PASSWORD='mysql' -e MYSQL_ALLOW_EMPTY_PASSWORD='no' -e MYSQL_ROOT_PASSWORD='mysql' -d mysql If the command runs successfully a hash is returned, the first 12 characters of which form the container id of the Docker container created. The running Docker containers may be listed with the following command. sudo docker ps The Docker container mysqldb gets listed. The container id is also listed. Start an interactive shell. sudo docker exec –it mysqldb bash An interactive bash shell gets started. Start the MySQL CLI as user “mysql”, which was created by setting the MYSQL_USER environment variable, with the following command. mysql –u mysql –p Specify the password as set in the MYSQL_PASSWORD environment variable and click Enter. The mysql> command prompt gets displayed as before. SQL statements may be run in the MySQL CLI as discussed before. To exit the MySQL CLI and the bash shell run the “exit” command. Deleting the Docker Container and Docker Image A Docker container cannot be removed before it is stopped and a Docker image cannot be removed untill all Docker containers running or stopped based on the Docker image are removed. To delete a Docker image the following sequence must be followed. Stop the Docker container Remove the Docker container Remove the Docker image Delete the Docker container mysqldb with the docker rm command. sudo docker rm mysqldb If the mysqldb container is still running the message “You cannot remove a running container” gets displayed. Run the following command to stop Docker container mysqldb . sudo docker stop mysqldb List all Docker containers, running or stopped, with the following command. sudo docker ps –a Two containers get listed, mysql and mysqldb . Remove both containers with the following command. sudo docker rm mysql mysqldb Both containers mysql and mysqldb get removed. Delete the Docker image mysql with the docker rmi command. sudo docker rmi mysql The Docker image gets deleted. In this tutorial we discussed running MySQL Database on Docker Engine. We downloaded and ran the Docker image mysql to create a Docker container. Subsequently, we started a bash shell on the Docker container and started MySQL CLI to create a MySQL table.

Viewing all articles
Browse latest Browse all 1814

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>