Configuring and Running MySQL in Docker: A Step-by-Step Guide


Hello, tech enthusiasts! Today, we're diving into the world of Docker and MySQL. Whether you're a seasoned developer or just dipping your toes into containerisation, this guide will walk you through configuring and running a MySQL database using Docker. So, let's roll up our sleeves and get started!

Step 1: Pulling the MySQL Docker Image:

To begin our journey, let's pull the MySQL Docker image from the official repository. Open your terminal and enter the following command:

bash
docker pull mysql

This command fetches the latest MySQL image, allowing us to create and run a MySQL container effortlessly.

Step 2: Running MySQL Container with Environment Variables:

Now that we have the MySQL image, let's run a container with customised environment variables. These variables include the MySQL root password and the desired database name. Run the following command:

bash
docker run --name mysqldb -e MYSQL_ROOT_PASSWORD={YOUR_MYSQL_PASSWORD_HERE} -e MYSQL_DATABASE={YOUR_DB_NAME_HERE} -p 3306:3306 -d mysql:8

In this command:

  • --name mysqldb assigns the name "mysqldb" to our container.
  • -e MYSQL_ROOT_PASSWORD={YOUR_MYSQL_PASSWORD_HERE} sets your MySQL root password.
  • -e MYSQL_DATABASE={YOUR_DB_NAME_HERE} specifies the name of the database you want to create.
  • -p 3306:3306 maps the container's port 3306 (MySQL's default port) to the host's port 3306.
  • -d runs the container in detached mode, allowing you to work in the terminal without being tied to the container.

Step 3: Checking if MySQL Container is Running:

To verify that the MySQL container is up and running, use the following command:

bash
docker ps

You should see the container named "mysqldb" listed among the active containers.

Step 4: Accessing MySQL Console via Docker Bash:

Now, let's get inside the MySQL container's shell using Docker Bash:

bash
docker exec -it mysqldb bash

Step 5: Logging into MySQL Console:

Within the Docker Bash shell, we'll access the MySQL console with this command:

bash
mysql -p {YOUR_DB_NAME_HERE}

You'll be prompted to enter the MySQL root password you set earlier.

Step 6: Exploring the MySQL Console:

Upon successful login, you'll find yourself in the MySQL console. Here are a few useful commands to explore your database:

  • To view available databases:

    sql
    show databases;
  • To select a specific database:

    sql
    use {YOUR_DB_NAME_HERE};
  • To list tables in the selected database:

    sql
    show tables;
  • To describe the structure of a table:

    sql
    describe {YOUR_TABLE_NAME};

Step 7: Exiting MySQL and Docker Bash:

To exit the MySQL console, press Ctrl + C. Similarly, use Ctrl + C to exit the Docker Bash shell.

Step 8: Exposing Ports through Security Groups:

To access the MySQL instance from your local machine, you need to modify the security group associated with your EC2 instance:

  • Go to the AWS Management Console.
  • Navigate to "EC2" and then "Instances."
  • Select your instance and look for the "Security groups" section.
  • Click on the security group linked to your instance.
  • In the "Inbound rules" tab, add a rule to allow incoming traffic on port 3306 (MySQL's default port) from your IP address.

Step 6: Connecting to MySQL from Your Local Machine:

With the exposed port and public IP in hand, you can now connect to the MySQL instance using a MySQL client on your local machine. Use the following command, replacing placeholders with actual values:

bash
mysql -h {YOUR_EC2_PUBLIC_IP} -P 3306 -u root -p

Enter the MySQL root password when prompted.

Step 8: Configure Docker in Instance:

If you haven't set up Docker on your EC2 Instance yet, I encourage you to check out my previous blog post, which provides a comprehensive guide on configuring Docker and Docker Compose on an AWS EC2 Instance.


Conclusion:

Congratulations! You've successfully configured and run a MySQL database in a Docker container. This step-by-step guide should help you get started on your journey into containerisation and database management. Remember, practice makes perfect, so feel free to experiment and explore further. Until next time, happy coding and containerising!

About Me:

Hello, coding enthusiasts! I'm Prince K Sharma, a software developer with a passion for sharing knowledge and simplifying complex tech concepts. Join me on my blog for more guides, tutorials, and insights into the world of programming and technology. Follow me on Linked In to stay updated on my latest posts and tech explorations.

Comments

Popular posts from this blog

A Beginner's Guide to Installing and Setting Up Docker on AWS EC2

Deploying a React Application on AWS EC2 with CI/CD using AWS CodeDeploy