How to Install manual Multiple MySQL Instances on a Single Server.

Gerenciar múltiplas instâncias de MySQL em um único servidor é uma prática útil para diversos cenários, maximização do uso de recursos, criação de ambientes com desenvolvimento e homologação. Hoje, vamos explorar o processo de instalação e configuração de múltiplas instâncias de MySQL em um único servidor.

Versão do SO:

[root@mysql01 ~]# cat /etc/*-release | grep PRETTY
PRETTY_NAME="Oracle Linux Server 9.5"

Instâncias a serem criadas com portas diferentes:

mysql1 - 3307
mysql2 - 3308
mysql3 - 3309
mysql4 - 3310

Instalar o MySQL:

[root@mysql01 /]# dnf install -y  https://dev.mysql.com/get/mysql84-community-release-el9-1.noarch.rpm
Last metadata expiration check: 0:09:28 ago on Sat 04 Jan 2025 09:47:01 PM -03.
mysql84-community-release-el9-1.noarch.rpm                                                              13 kB/s |  13 kB     00:01
Dependencies resolved.
===================================================================================================================================
 Package                                 Architecture         Version              Repository                   Size
===================================================================================================================================
Installing:
 mysql84-community-release               noarch               el9-1                @commandline                 13 k

Transaction Summary
==================================================================================================================================
Install  1 Package

Total size: 13 k
Installed size: 14 k
Downloading Packages:
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
  Preparing        :                                                                1/1
  Installing       : mysql84-community-release-el9-1.noarch                         1/1
  Verifying        : mysql84-community-release-el9-1.noarch                         1/1

Installed:
  mysql84-community-release-el9-1.noarch

Complete!


[root@mysql01 /]# yum install -y mysql-server
Last metadata expiration check: 0:00:09 ago on Sat 04 Jan 2025 10:01:17 PM -03.
Dependencies resolved.
==================================================================================================================================================
 Package                                       Architecture               Version                  Repository                               Size
==================================================================================================================================================
Installing:
 mysql-community-server                        x86_64                     8.4.3-1.el9              mysql-8.4-lts-community                  50 M
Installing dependencies:
 mysql-community-client                        x86_64                     8.4.3-1.el9              mysql-8.4-lts-community                  3.1 M
 mysql-community-client-plugins                x86_64                     8.4.3-1.el9              mysql-8.4-lts-community                  1.5 M
 mysql-community-common                        x86_64                     8.4.3-1.el9              mysql-8.4-lts-community                  575 k
 mysql-community-icu-data-files                x86_64                     8.4.3-1.el9              mysql-8.4-lts-community                  2.3 M
 mysql-community-libs                          x86_64                     8.4.3-1.el9              mysql-8.4-lts-community                  1.5 M

Transaction Summary
==================================================================================================================================================
Install  6 Packages

Total download size: 59 M
Installed size: 330 M
Downloading Packages:
(1/6): mysql-community-common-8.4.3-1.el9.x86_64.rpm                             845 kB/s | 575 kB     00:00
(2/6): mysql-community-client-plugins-8.4.3-1.el9.x86_64.rpm                     2.0 MB/s | 1.5 MB     00:00
(3/6): mysql-community-client-8.4.3-1.el9.x86_64.rpm                             2.8 MB/s | 3.1 MB     00:01
(4/6): mysql-community-libs-8.4.3-1.el9.x86_64.rpm                               3.6 MB/s | 1.5 MB     00:00
(5/6): mysql-community-icu-data-files-8.4.3-1.el9.x86_64.rpm                     4.1 MB/s | 2.3 MB     00:00
(6/6): mysql-community-server-8.4.3-1.el9.x86_64.rpm                             7.3 MB/s |  50 MB     00:06
--------------------------------------------------------------------------------------------------------------------------------------------------
Total                                                                            7.4 MB/s |  59 MB     00:07
MySQL 8.4 LTS Community Server                                                   3.0 MB/s | 3.1 kB     00:00
Importing GPG key 0xA8D3785C:
 Userid     : "MySQL Release Engineering <[email protected]>"
 Fingerprint: BCA4 3417 C3B4 85DD 128E C6D4 B7B3 B788 A8D3 785C
 From       : /etc/pki/rpm-gpg/RPM-GPG-KEY-mysql-2023
Key imported successfully
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
  Installing       : mysql-community-common-8.4.3-1.el9.x86_64                   1/6
  Installing       : mysql-community-client-plugins-8.4.3-1.el9.x86_64           2/6
  Preparing        :                                                             1/1
  Installing       : mysql-community-libs-8.4.3-1.el9.x86_64                     3/6
  Running scriptlet: mysql-community-libs-8.4.3-1.el9.x86_64                     3/6
  Installing       : mysql-community-client-8.4.3-1.el9.x86_64                   4/6
  Installing       : mysql-community-icu-data-files-8.4.3-1.el9.x86_64           5/6
  Running scriptlet: mysql-community-server-8.4.3-1.el9.x86_64                   6/6
  Installing       : mysql-community-server-8.4.3-1.el9.x86_64                   6/6
  Running scriptlet: mysql-community-server-8.4.3-1.el9.x86_64                   6/6
  Verifying        : mysql-community-client-8.4.3-1.el9.x86_64                   1/6
  Verifying        : mysql-community-client-plugins-8.4.3-1.el9.x86_64           2/6
  Verifying        : mysql-community-common-8.4.3-1.el9.x86_64                   3/6
  Verifying        : mysql-community-icu-data-files-8.4.3-1.el9.x86_64           4/6
  Verifying        : mysql-community-libs-8.4.3-1.el9.x86_64                     5/6
  Verifying        : mysql-community-server-8.4.3-1.el9.x86_64                   6/6

Installed:
  mysql-community-client-8.4.3-1.el9.x86_64  mysql-community-client-plugins-8.4.3-1.el9.x86_64  mysql-community-common-8.4.3-1.el9.x86_64  mysql-community-icu-data-files-8.4.3-1.el9.x86_64  mysql-community-libs-8.4.3-1.el9.x86_64
  mysql-community-server-8.4.3-1.el9.x86_64

Complete!

Criar os diretórios para serem usados pelas instâncias do MySQL:

[root@mysql01 /]# mkdir -p /mysql1/ /mysql2/ /mysql3/ /mysql4/

Criar os arquivos de logs individuais por instância:

[root@mysql01 /]# > /var/log/mysqld1.log
[root@mysql01 /]# > /var/log/mysqld2.log
[root@mysql01 /]# > /var/log/mysqld3.log
[root@mysql01 /]# > /var/log/mysqld4.log

[root@mysql01 /]# chmod -R --reference /var/log/mysqld.log /var/log/mysqld1.log /var/log/mysqld2.log /var/log/mysqld3.log /var/log/mysqld4.log
[root@mysql01 /]# chown -R --reference /var/log/mysqld.log /var/log/mysqld1.log /var/log/mysqld2.log /var/log/mysqld3.log /var/log/mysqld4.log

Criar o my.cnf individuais por instância, cada instância deve usar uma porta e socket diferente:

[root@mysql01 /]# > /etc/my1.cnf
[root@mysql01 /]# > /etc/my2.cnf
[root@mysql01 /]# > /etc/my3.cnf
[root@mysql01 /]# > /etc/my4.cnf


cat >> /etc/my1.cnf << EOF
[mysqld]
server-id= 1
port=3307
datadir=/mysql1
socket=/var/lib/mysql/mysql1.sock
log-error=/var/log/mysqld1.log
pid-file=/var/run/mysqld/mysqld1.pid
mysqlx-port=33071
mysqlx-bind-address = 0.0.0.0
mysqlx_socket=/var/run/mysqld/mysqlx1.sock
EOF

cat >> /etc/my2.cnf << EOF
[mysqld]
server-id= 2
port=3308
datadir=/mysql2
socket=/var/lib/mysql/mysql2.sock
log-error=/var/log/mysqld2.log
pid-file=/var/run/mysqld/mysqld2.pid
mysqlx-port=33081
mysqlx-bind-address = 0.0.0.0
mysqlx_socket=/var/run/mysqld/mysqlx2.sock
EOF


cat >> /etc/my3.cnf << EOF
[mysqld]
server-id= 3
port=3309
datadir=/mysql3
socket=/var/lib/mysql/mysql3.sock
log-error=/var/log/mysqld3.log
pid-file=/var/run/mysqld/mysqld3.pid
mysqlx-port=33091
mysqlx-bind-address = 0.0.0.0
mysqlx_socket=/var/run/mysqld/mysqlx3.sock
EOF


cat >>  /etc/my4.cnf << EOF
[mysqld]
server-id= 4
port=3310
datadir=/mysql4
socket=/var/lib/mysql/mysql4.sock
log-error=/var/log/mysqld4.log
pid-file=/var/run/mysqld/mysqld4.pid
mysqlx-port=33101
mysqlx-bind-address = 0.0.0.0
mysqlx_socket=/var/run/mysqld/mysqlx4.sock
EOF

Desative o firewall e selinux ou configure de acordo com o seu ambiente:

[root@mysql01 /]# sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
[root@mysql01 /]# setenforce 0
[root@mysql01 /]# systemctl stop firewalld
[root@mysql01 /]# systemctl status firewalld
[root@mysql01 /]# systemctl disable --now firewalld
[root@mysql01 /]# systemctl mask --now firewalld
[root@mysql01 /]# init 6

Criando o service no systemd para gerenciar as instâncias individualmente, modifique a linha do ExecStart, cada service aponta para um arquivo my.cnf:

[root@mysql01 /]# cp /usr/lib/systemd/system/mysqld.service /etc/systemd/system/mysqld1.service
[root@mysql01 /]# cp /usr/lib/systemd/system/mysqld.service /etc/systemd/system/mysqld2.service
[root@mysql01 /]# cp /usr/lib/systemd/system/mysqld.service /etc/systemd/system/mysqld3.service
[root@mysql01 /]# cp /usr/lib/systemd/system/mysqld.service /etc/systemd/system/mysqld4.service

#mysql1 - /etc/my1.cnf
[root@mysql01 /]# cat /etc/systemd/system/mysqld1.service | grep ExecStart
ExecStart=/usr/sbin/mysqld --defaults-file=/etc/my1.cnf

#mysql2 - /etc/my2.cnf
[root@mysql01 /]# cat /etc/systemd/system/mysqld2.service | grep ExecStart
ExecStart=/usr/sbin/mysqld --defaults-file=/etc/my2.cnf

#mysql3 - /etc/my3.cnf
[root@mysql01 /]# cat /etc/systemd/system/mysqld3.service | grep ExecStart
ExecStart=/usr/sbin/mysqld --defaults-file=/etc/my3.cnf

#mysql4 - /etc/my4.cnf
[root@mysql01 /]# cat /etc/systemd/system/mysqld4.service | grep ExecStart
ExecStart=/usr/sbin/mysqld --defaults-file=/etc/my4.cnf

[root@mysql01 system]# systemctl daemon-reload

[root@mysql01 /]# systemctl enable mysqld1
Created symlink /etc/systemd/system/multi-user.target.wants/mysqld1.service  /etc/systemd/system/mysqld1.service.
[root@mysql01 /]# systemctl enable mysqld2
Created symlink /etc/systemd/system/multi-user.target.wants/mysqld2.service  /etc/systemd/system/mysqld2.service.
[root@mysql01 /]# systemctl enable mysqld3
Created symlink /etc/systemd/system/multi-user.target.wants/mysqld3.service  /etc/systemd/system/mysqld3.service.
[root@mysql01 /]# systemctl enable mysqld4
Created symlink /etc/systemd/system/multi-user.target.wants/mysqld4.service  /etc/systemd/system/mysqld4.service.

[root@mysql01 /]# systemctl list-units --type=service --all | grep mysqld1
  mysqld1.service                        loaded    inactive dead    MySQL Server
  
[root@mysql01 /]# systemctl list-units --type=service --all | grep mysqld2

  mysqld2.service                        loaded    inactive dead    MySQL Server
[root@mysql01 /]# systemctl list-units --type=service --all | grep mysqld3

  mysqld3.service                        loaded    inactive dead    MySQL Server
[root@mysql01 /]# systemctl list-units --type=service --all | grep mysqld4
  mysqld4.service                        loaded    inactive dead    MySQL Server

Inicializar as instâncias individualmente:

[root@mysql01 /]# mysqld --initialize --datadir=/mysql1/ --user=mysql
[root@mysql01 /]# mysqld --initialize --datadir=/mysql2/ --user=mysql
[root@mysql01 /]# mysqld --initialize --datadir=/mysql3/ --user=mysql
[root@mysql01 /]# mysqld --initialize --datadir=/mysql4/ --user=mysql
# OR 
[root@mysql01 /]# mysqld --initialize-insecure --datadir=/mysql1/ --user=mysql
[root@mysql01 /]# mysqld --initialize-insecure --datadir=/mysql2/ --user=mysql
[root@mysql01 /]# mysqld --initialize-insecure --datadir=/mysql3/ --user=mysql
[root@mysql01 /]# mysqld --initialize-insecure --datadir=/mysql4/ --user=mysql


[root@mysql01 /]# du -sh /mysql*
191M    /mysql1
191M    /mysql2
191M    /mysql3
191M    /mysql4

Iniciar as instâncias individualmente:

[root@mysql01 ~]# systemctl start mysqld1
[root@mysql01 ~]# systemctl start mysqld2
[root@mysql01 ~]# systemctl start mysqld3
[root@mysql01 ~]# systemctl start mysqld4

[root@mysql01 ~]# systemctl status mysqld1
 mysqld1.service - MySQL Server
     Loaded: loaded (/etc/systemd/system/mysqld1.service; enabled; preset: disabled)
     Active: active (running) since Sat 2025-01-04 22:55:18 -03; 4min 24s ago
       Docs: man:mysqld(8)
             http://dev.mysql.com/doc/refman/en/using-systemd.html
    Process: 837 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
   Main PID: 2682 (mysqld)
     Status: "Server is operational"
      Tasks: 34 (limit: 47636)
     Memory: 437.7M
        CPU: 5.165s
     CGroup: /system.slice/mysqld1.service
             └─2682 /usr/sbin/mysqld --defaults-file=/etc/my1.cnf

Jan 04 22:55:09 mysql01 systemd[1]: Starting MySQL Server...
Jan 04 22:55:18 mysql01 systemd[1]: Started MySQL Server.
[root@mysql01 ~]#
[root@mysql01 ~]# systemctl status mysqld2
 mysqld2.service - MySQL Server
     Loaded: loaded (/etc/systemd/system/mysqld2.service; enabled; preset: disabled)
     Active: active (running) since Sat 2025-01-04 22:55:16 -03; 4min 29s ago
       Docs: man:mysqld(8)
             http://dev.mysql.com/doc/refman/en/using-systemd.html
    Process: 840 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
   Main PID: 2241 (mysqld)
     Status: "Server is operational"
      Tasks: 34 (limit: 47636)
     Memory: 440.6M
        CPU: 4.744s
     CGroup: /system.slice/mysqld2.service
             └─2241 /usr/sbin/mysqld --defaults-file=/etc/my2.cnf

Jan 04 22:55:09 mysql01 systemd[1]: Starting MySQL Server...
Jan 04 22:55:16 mysql01 systemd[1]: Started MySQL Server.
[root@mysql01 ~]#
[root@mysql01 ~]#
[root@mysql01 ~]# systemctl status mysqld3
 mysqld3.service - MySQL Server
     Loaded: loaded (/etc/systemd/system/mysqld3.service; enabled; preset: disabled)
     Active: active (running) since Sat 2025-01-04 22:55:17 -03; 4min 31s ago
       Docs: man:mysqld(8)
             http://dev.mysql.com/doc/refman/en/using-systemd.html
    Process: 842 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
   Main PID: 2463 (mysqld)
     Status: "Server is operational"
      Tasks: 34 (limit: 47636)
     Memory: 437.1M
        CPU: 4.894s
     CGroup: /system.slice/mysqld3.service
             └─2463 /usr/sbin/mysqld --defaults-file=/etc/my3.cnf

Jan 04 22:55:09 mysql01 systemd[1]: Starting MySQL Server...
Jan 04 22:55:17 mysql01 systemd[1]: Started MySQL Server.
[root@mysql01 ~]#
[root@mysql01 ~]#
[root@mysql01 ~]# systemctl status mysqld4
 mysqld4.service - MySQL Server
     Loaded: loaded (/etc/systemd/system/mysqld4.service; enabled; preset: disabled)
     Active: active (running) since Sat 2025-01-04 22:55:14 -03; 4min 35s ago
       Docs: man:mysqld(8)
             http://dev.mysql.com/doc/refman/en/using-systemd.html
    Process: 846 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
   Main PID: 1484 (mysqld)
     Status: "Server is operational"
      Tasks: 34 (limit: 47636)
     Memory: 515.6M
        CPU: 5.155s
     CGroup: /system.slice/mysqld4.service
             └─1484 /usr/sbin/mysqld --defaults-file=/etc/my4.cnf

Jan 04 22:55:09 mysql01 systemd[1]: Starting MySQL Server...
Jan 04 22:55:14 mysql01 systemd[1]: Started MySQL Server.


[root@mysql01 ~]# grep password /var/log/mysqld.log
2025-01-05T01:32:57.169446Z 6 [Warning] [MY-010453] [Server] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option.
2025-01-05T01:33:47.219723Z 6 [Warning] [MY-010453] [Server] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option.
2025-01-05T01:34:12.854861Z 6 [Warning] [MY-010453] [Server] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option.
2025-01-05T01:34:47.666207Z 6 [Warning] [MY-010453] [Server] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option.
2025-01-05T01:38:27.314184Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: oCVug8AErk#/

Verificar MySQL e portas ativas:

[root@mysql01 ~]# for port in {3307..3310}; do
    netstat -tuln | grep ":$port"
done

tcp        0      0 0.0.0.0:33071           0.0.0.0:*               LISTEN
tcp6       0      0 :::3307                 :::*                    LISTEN
tcp        0      0 0.0.0.0:33081           0.0.0.0:*               LISTEN
tcp6       0      0 :::3308                 :::*                    LISTEN
tcp        0      0 0.0.0.0:33091           0.0.0.0:*               LISTEN
tcp6       0      0 :::3309                 :::*                    LISTEN
tcp        0      0 0.0.0.0:33101           0.0.0.0:*               LISTEN
tcp6       0      0 :::3310                 :::*                    LISTEN
[root@mysql01 ~]#


[root@mysql01 ~]#
[root@mysql01 ~]#
[root@mysql01 ~]# ps faux | grep mysql[d]
mysql       1484  0.8  6.3 2167892 489888 ?      Ssl  22:55   0:02 /usr/sbin/mysqld --defaults-file=/etc/my4.cnf
mysql       2241  0.8  6.2 2167896 480264 ?      Ssl  22:55   0:02 /usr/sbin/mysqld --defaults-file=/etc/my2.cnf
mysql       2463  0.8  6.2 2167892 476020 ?      Ssl  22:55   0:02 /usr/sbin/mysqld --defaults-file=/etc/my3.cnf
mysql       2682  0.9  6.1 2168028 474432 ?      Ssl  22:55   0:03 /usr/sbin/mysqld --defaults-file=/etc/my1.cnf

Conectado nas instâncias individualmente:

#MYSQL1
[root@mysql01 ~]# mysql -P 3307 -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 11
Server version: 8.4.3

Copyright (c) 2000, 2024, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> exit
Bye

#MYSQL2
[root@mysql01 ~]# mysql -P 3308 -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 12
Server version: 8.4.3

Copyright (c) 2000, 2024, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> exit
Bye

#MYSQL4
[root@mysql01 ~]# mysql -P 3309 -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 13
Server version: 8.4.3

Copyright (c) 2000, 2024, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> exit
Bye

#MYSQL4
[root@mysql01 ~]# mysql -P 3310 -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 14
Server version: 8.4.3

Copyright (c) 2000, 2024, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>
mysql>
mysql> exit
Bye
[root@mysql01 ~]#

Espero que este tutorial te ajude. Nas minhas pesquisas, encontrei poucos conteúdos que mostrassem de forma simples como instalar e executar várias instâncias do MySQL em um único servidor.

search previous next tag category expand menu location phone mail time cart zoom edit close