MySQL is one of the most widely used relational databases. However installing and setting up MySQL locally on your system can be time-consuming and resource-intensive. But what if you could practice SQL queries and explore MySQL without needing to install it on your machine?
The answer is Docker.
In this post, I’ll show you how to use Docker to run an Ubuntu container with MySQL pre-configured, providing a lightweight environment for practicing SQL queries.
I will be using Play with Kubernetes - a free, web-based platform for experimenting with Docker and Kubernetes environments—making it even easier to run this setup without worrying about your system's resources.
Step 1: Set Up Play with Kubernetes
visit Play with Kubernetes.
If you don’t already have an account, sign up for free.
Start a New Session: Click on the ‘Start’ button to create a new Kubernetes session.
Step 2: Create a Docker Image with Ubuntu and MySQL
start by pulling the latest Ubuntu image from Docker Hub:
docker pull ubuntu:latest
After pulling the Ubuntu image, let’s create a new container and install MySQL. Run the following commands:
docker run -it ubuntu bash
this command starts a container in interactive with bash shell terminal.apt-get update
- updates the packages in ubuntuapt-get install -y mysql-server
- installs mysql server within the container\Step 3: Create a MySQL Database and Tables
Login to MySQL
mysql -u root -p
Create a Database:
CREATE DATABASE practice_db;
USE practice_db;
Create Sample Tables
CREATE TABLE users ( id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(100), email VARCHAR(100), age INT );
Insert Sample Data:
INSERT INTO users (name, email, age)
VALUES ('John Doe', '
john.doe@example.com
', 30),
('Jane Smith', '
jane.smith@example.com
', 25),
('Bob Johnson', '
bob.johnson@example.com
', 35);
Step 4: Practice SQL Queries
Now that the database is ready, you can start practicing your SQL queries. For example:
Select all users:
SELECT * FROM users;
Step 5: Clean Up
Once you’re done practicing, you can stop and remove the Docker container to free up resources:
exit
docker stop <container_id>
docker rm <container_id>
Using Docker to set up an Ubuntu container with MySQL is a powerful and lightweight way to practice SQL queries without the need to install MySQL on your local machine.