2024-07-12
한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina
In Linux systems, by default MySQL (or MariaDB, a popular MySQL fork) does not directly support running multiple MySQL instances on the same machine, each using a different port and independent password system. However, you can achieve this requirement by configuring multiple MySQL instances (also known as multi-instance). This usually involves configuring different ports, data directories, configuration files, and possibly user permissions for each instance.
Here are the basic steps to configure multiple MySQL instances:
Make sure you have a working MySQL installation. This guide assumes you are using MySQL, but if you are using MariaDB, the steps are very similar.
Create a new data directory for each MySQL instance:
sudo mkdir -p /data/mysql1 /data/mysql2 | |
sudo chown -R mysql:mysql /data/mysql1 /data/mysql2 |
here/data/mysql1
and/data/mysql2
These are the data directories of two different instances.
The default configuration file for MySQL is usually/etc/my.cnf
(or/etc/mysql/my.cnf
, depending on your distribution). You will need to create a new configuration file for each instance.
sudo cp /etc/my.cnf /etc/mysql1.cnf | |
sudo cp /etc/my.cnf /etc/mysql2.cnf |
Modify the following section of each configuration file:
[mysqld]
Section Settingsdatadir
For newly created data directories (such as/data/mysql1
or/data/mysql2
)。port
(For example, the first instance uses 3306 and the second instance uses 3307).socket
paths, making sure they are unique.user
andlog_error
and other options to ensure that the instance runs independently.For new MySQL instances, you need to initialize the data directory. This usually involves runningmysqld --initialize
ormysql_install_db
(Depending on your MySQL version.) Note that this may overwrite any existing data.
sudo mysqld --initialize --user=mysql --datadir=/data/mysql1 | |
sudo mysqld --initialize --user=mysql --datadir=/data/mysql2 |
You need to write a startup script for each instance or usesystemd
Unit files to manage them. This is usually complex and needs to be customized according to your specific needs and system environment.
A simplified approach is to usemysqld_safe
Or run directly in the command linemysqld
, but this is not suitable for production environments.
Each instance has a default root password after initialization (MySQL 5.7.6 and higher). You need to set a separate password and user permissions for each instance.
--socket
and--port
options).ALTER USER
Statement to set the root password.Make sure you have monitoring and backup strategies in place to maintain your multiple MySQL instances.