Skip to content

Checking your reading list…

Retry

Stay curious. Build better.

Appearance

Choose how BestHub looks on this device.

Changes apply immediately.

Search articles

Open Discover with these filters

Suggested searches

Databases 5 min read

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-bin

Repeat 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/data

If 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.service

Verify 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.cnf

Start each MySQL service:

systemctl start mysqld3307
systemctl start mysqld3308
systemctl start mysqld3309

Finally, 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/mysqld

With these steps, three independent MySQL servers run side‑by‑side on the same host.

About this article

For questions about attribution, corrections or content rights, please contactadmin@besthub.devand we will review it promptly.

ConfigurationLinuxMySQLmultiple instances

Discussion 0

Questions, practical examples and thoughtful responses.

Open full discussion