Prerequisites #
Our guides are written with the expectation that the following requirements are met:
- Server: VPS/Dedicated Vultr / Contabo
- Ubuntu operating system 22.04 configured
- Docker/Docker Compose and Nginx installed
- SSH client with SFTP capabilities
What is MySQL #
MySQL is an open-source relational database management system (RDBMS) that uses Structured Query Language (SQL) to manage and manipulate data. It is widely used for web applications and as a backend database in a variety of software environments. MySQL is known for its reliability, scalability, and ease of use, making it a popular choice for developers and organizations of all sizes.
Setup Docker-compose #
Create a new folder, for your database:
mkdir database && cd database
Create a docker-compose.yml file:
nano docker-compose.yml
Paste the following code edit your credentials and save:
version: "3.8"
services:
mysql:
image: mysql:9
container_name: mysql_container
restart: always
environment:
MYSQL_ROOT_PASSWORD: your_root_password
MYSQL_DATABASE: your_database_name
MYSQL_USER: your_user_name
MYSQL_PASSWORD: your_user_password
volumes:
- ./mysql_data:/var/lib/mysql
ports:
- "3306:3306"
Start / Stop / Restart your Database #
Start your database with:
sudo docker compose up
Start as Daemon(with auto restart) database with:
sudo docker compose up -d
Restart database:
sudo docker restart mysql_container
Start database:
sudo docker start mysql_container
Stop database:
sudo docker stop mysql_container
Add Custom config for MySQL #
Once your database is running, you might want to tweak the Database settings:
This is how your folder structure should looks like:
Create file called my_conf.cnf
:
nano my_conf.cnf
Add your Custom configs like:
[mysqld]
max_connections = 500
Edit your docker-compose.yml file:
nano docker-compose.yml
Your docker-compose.yml
file:
services:
mysql:
image: mysql:9
container_name: mysql_container
restart: always
environment:
MYSQL_ROOT_PASSWORD: your_root_password
MYSQL_DATABASE: your_database_name
MYSQL_USER: your_user_name
MYSQL_PASSWORD: your_user_password
volumes:
- ./mysql_data:/var/lib/mysql
- ./my_conf.cnf:/etc/mysql/conf.d/my_conf.cnf #ADD THIS LINE TO LINK YOUR CONF!
ports:
- "3306:3306"
Apply with Restart database:
sudo docker restart mysql_container
Test your config with SQL Query e.g.:
SHOW VARIABLES LIKE 'max_connections';
Tips and #
For protecting your database either only allow access on localhost:
ports:
- "127.0.0.1:3306:3306"
Use non-default port which help to avoid port scanners:
ports:
- "32121:3306"
In case you are experiencing performance issues, use MySQLTuner before start tweaking the configuration: