TL;DR
I had to learn MySQL for client project and here is the basics to get you started. This doesn't require too much of knowledge in SW development so you can be more junior or just figuring things out. You can find the five steps of setting up at the 5. steps to setup MySQL localy with Docker
Background
So recently i got work order from my client to build a NestJS back-end for their startup. NestJS it self is no challenge as i've been working with React and NextJS alot. BUT the client wanted to have MySQL database.. i never touched MySQL in any environment before, unless lectures in college counts. I didn't even know proper way to setup a local environment to try out different things. Setting up local LAMP environment didn't sound like something i'm interested to get into and i already had Docker running.
This article i will go thought shortly how to put up MySQL database localy, i wont go so deep, just few steps to get you started as i did when figuring out things, i'm sure there is more indepth MySQL experts around the internet.
First things first
Quick and simple recap for people who are completely new to MySQL.
MySQL is a open-source relational database management system and popular database choice for web applications.
It is storing the data in tables and tables are organized into databases. Each table is having rows and columns that are keeping your data. It uses Structured Query Language for managing the data in the tables and it is known for its speed, reliability, and flexibility.
5. steps to setup MySQL localy with Docker
-
Setup Docker or have it already up and running.
-
Open termianl and pull docker image from Docker hub following command:
docker pull mysql
- Create docker-compose.yaml (or docker-compose.yml) in your project folder (i will go through the other method of doing this later, i prefer having compose file)
version: '3.8' services: db: image: mysql:8.0 cap_add: - SYS_NICE restart: always environment: - MYSQL_DATABASE=yourdatabase - MYSQL_ROOT_PASSWORD=yourpassword volumes: - db:/var/lib/mysql - ./db/init.sql:/docker-entrypoint-initdb.d/init.sql volumes: db: driver: local 4. Run command docker-compose -f docker-compose.yml up
- You can verify if your docker is up and running with command prompt
docker ps
So thats basic dockercompose example for MySQL.
Alternative way to setup MySQL with Docker
I prefer docker-compose file, but here is alternative way to setup
-
Install Docker on your machine if not already installed.
-
Open a terminal and pull the MySQL image from the Docker Hub by running the command: docker pull mysql
-
Start a new container with the MySQL image by running the following command, replace the environment variables with your desired configuration:
docker run --name your-mysql-container -e MYSQL_ROOT_PASSWORD=rootpw -e MYSQL_DATABASE=dbname -e MYSQL_USER=user -e MYSQL_PASSWORD=pw -p 3306:3306 -d mysql
- You can verify that the container is running by using the following command:
docker ps
- Connect to the MySQL container by running the following command in your terminal:
docker exec -it mysql-container-name bash
This will open bash to run commands in container. Once you are in the container you can use the mysql command-line client to interact with the database using the following command:
mysql -u user -p
after this command your mysql instance will ask password, you defined it in third step.
So here it is, a basics of how to setup a MySQL in Docker.