Managing Multiple MySQL Instances on a Single Server
This guide explains how to set up and run several independent MySQL server instances on one Linux host by creating separate data directories, configuring individual my.cnf files, initializing each instance, managing them with systemd services, and verifying that each listens on its own port.
This article demonstrates how to run multiple MySQL instances on a single Linux server, each bound to a different port.
First, create separate data directories for the instances: mkdir -p /data/330{7,8,9}/data Then create a configuration file for each instance (e.g., /data/3307/my.cnf, /data/3308/my.cnf, /data/3309/my.cnf) with the following content, adjusting the paths and port numbers accordingly:
[mysqld]
basedir=/usr/local/mysql
datadir=/data/3307/data
socket=/data/3307/mysql.sock
log_error=/data/3307/mysql.log
port=3307
server_id=7
log_bin=/data/3307/mysql-binRepeat the same block for ports 3308 and 3309, changing the directory names, socket, log files, port, and server_id values.
Initialize each data directory (insecurely, for testing) with:
mysqld --initialize-insecure --user=mysql --basedir=/usr/local/mysql --datadir=/data/3307/data mysqld --initialize-insecure --user=mysql --basedir=/usr/local/mysql --datadir=/data/3308/data mysqld --initialize-insecure --user=mysql --basedir=/usr/local/mysql --datadir=/data/3309/dataIf the directory already contains files, MySQL will abort with an error similar to:
2020-10-30T10:29:36.959026Z 0 [ERROR] --initialize specified but the data directory has files in it. Aborting.After initialization, set proper ownership: chown -R mysql.mysql /data/* Manage the instances with systemd by copying the original service file and adjusting the ExecStart line to point to each instance's configuration:
cp mysqld.service mysqld3307.service
cp mysqld.service mysqld3308.service
cp mysqld.service mysqld3309.serviceVerify the ExecStart entries:
ExecStart=/usr/local/mysql/bin/mysqld --defaults-file=/data/3307/my.cnf ExecStart=/usr/local/mysql/bin/mysqld --defaults-file=/data/3308/my.cnf ExecStart=/usr/local/mysql/bin/mysqld --defaults-file=/data/3309/my.cnfStart each MySQL service:
systemctl start mysqld3307
systemctl start mysqld3308
systemctl start mysqld3309Finally, confirm that each instance is listening on its designated port using netstat:
netstat -nltp
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp6 0 0 :::3307 :::* LISTEN 10112/mysqld
tcp6 0 0 :::3308 :::* LISTEN 10149/mysqld
tcp6 0 0 :::3309 :::* LISTEN 10183/mysqldWith these steps, three independent MySQL servers run side‑by‑side on the same host.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Practical DevOps Architecture
Hands‑on DevOps operations using Docker, K8s, Jenkins, and Ansible—empowering ops professionals to grow together through sharing, discussion, knowledge consolidation, and continuous improvement.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
