Install MySQL for your Home Server
Since we are developing out own personal home server, We need a centralized database server that can be accessed by the services that will be running on our home server. If you want to get started developing your personal home server. Here is the guide you can refer to.
For this installation we are going to need docker and docker compose installed on your home server. Here is how you can install docker and docker compose on your server here.
If you're looking to set up MySQL quickly and efficiently, Docker Compose is the way to go. Docker Compose simplifies the process of managing multi-container applications, and MySQL is no exception. In this blog, We'll walk you through the steps to install MySQL using Docker Compose. By the end, you'll have a fully functional MySQL instance running in a Docker container.
First, create a new directory to hold your Docker Compose configuration and MySQL data. Open your terminal and run:
mkdir mysql-docker && cd mysql-docker
*Note: you might want to organise your docker compose file in a folder*
This directory will contain the docker-compose.yml
file, which defines the MySQL service and its configuration.
Inside the mysql-docker
directory, create a docker-compose.yml
file:
nano docker-compose.yml
services:
mysql:
image: mysql:latest
container_name: mysql_db
environment:
MYSQL_ROOT_PASSWORD: super_secure_password
MYSQL_USER: your_new_user
MYSQL_PASSWORD: your_new_user_secure_password
ports:
- "3306:3306"
volumes:
- /your-folder/mysql_data:/var/lib/mysql
volumes:
mysql_data:
Here is the explanation of how this code works
image: mysql:latest
: This pulls the latest MySQL image from Docker Hub.container_name: mysql_db
: Names the containermysql_db
for easy reference.environment
: Sets up environment variables for MySQL.MYSQL_ROOT_PASSWORD
: The root password for MySQL.MYSQL_DATABASE
: Creates a default database with the specified name.MYSQL_USER
andMYSQL_PASSWORD
: Creates a new user with the specified username and password.ports
: Maps port3306
on your host machine to port3306
in the container, allowing you to connect to MySQL from your local machine.volumes
: Persists MySQL data in a Docker volume namedmysql_data
, ensuring your data isn’t lost if the container is removed.
Save this file with name docker-compose.yml or download the docker compose file and run the file using the command:
docker compose up -d
To confirm that your MySQL container is up and running, use the following command:
docker ps
*You should see the mysql_db
container listed with a status of "Up."*
Now we can access the mysql server using docker exec -it mysql_db mysql -u your_username -p
We will use this mysql container as a database service for our service that will be running on the home server in the future.