Ricerca nel sito web

Come configurare un cluster MySQL 8 a tre nodi su Debian 10


Su questa pagina

  1. Prerequisiti
  2. Per iniziare
  3. Installa e configura MySQL Cluster Manager
  4. Installa e configura i nodi di dati
  5. Installa e configura MySQL Server
  6. Prova MySQL Cluster
  7. Conclusione

MySQL è un sistema di gestione di database gratuito, open source e relazionale. Viene utilizzato per archiviare i dati in vari scopi, tra cui data warehousing, e-commerce e applicazioni di registrazione. MySQL Cluster è una tecnologia che fornisce scalabilità e disponibilità a basso costo.

In questo tutorial, utilizzeremo un nodo master per archiviare la configurazione dei cluster e due nodi dati per archiviare i dati del cluster. L'indirizzo IP di ciascun nodo è mostrato di seguito:

  • Nodo principale o gestore - 104.245.33.61
  • Nodo dati1 - 104.245.32.195
  • Nodo dati2 - 69.87.218.169

Prerequisiti

  • Tre server che eseguono Debian 10 uno per il nodo gestore e gli altri sono nodi dati.
  • Una password di root è impostata su ciascun server.

Iniziare

Innanzitutto, dovrai aggiornare tutti i server all'ultima versione. Puoi aggiornare tutto con il seguente comando:

apt-get update -y

Una volta aggiornati tutti i server, puoi procedere al passaggio successivo.

Installa e configura MySQL Cluster Manager

Innanzitutto, dovrai installare il pacchetto di gestione del cluster MySQL sul nodo master. Per impostazione predefinita, questo pacchetto non è incluso nel repository predefinito di Debian 10. Quindi dovrai scaricarlo dal sito Web ufficiale di MySQL.

Puoi scaricarlo con il seguente comando:

wget https://cdn.mysql.com//Downloads/MySQL-Cluster-8.0/mysql-cluster-community-management-server_8.0.24-1debian10_amd64.deb

Dopo aver scaricato il file deb, installalo utilizzando il seguente comando:

dpkg -i mysql-cluster-community-management-server_8.0.24-1debian10_amd64.deb

Una volta completata l'installazione, creare una directory per memorizzare la configurazione del cluster con il seguente comando:

mkdir /var/lib/mysql-cluster

Successivamente, crea un nuovo file di configurazione con il seguente comando:

nano /var/lib/mysql-cluster/config.ini

Aggiungi le seguenti righe:

[ndbd default]
NoOfReplicas=2  # Number of replicas

[ndb_mgmd]
# Management process options:
hostname=104.245.33.61 #IP of the MySQL Cluster Manager
datadir=/var/lib/mysql-cluster
[ndbd]
hostname=104.245.32.195 #IP of the first data node
NodeId=2            # Node ID for this data node
datadir=/usr/local/mysql/data   # Remote directory for the data files
[ndbd]
hostname=69.87.218.169 #IP of the second data node
NodeId=3            # Node ID for this data node
datadir=/usr/local/mysql/data   # Remote directory for the data files
[mysqld]
# SQL node options:
hostname=104.245.33.61 #IP of the MySQL Cluster Manager

Salva e chiudi il file quando hai finito, quindi avvia il gestore cluster con il seguente comando:

ndb_mgmd -f /var/lib/mysql-cluster/config.ini

Dovresti ottenere il seguente output:

MySQL Cluster Management Server mysql-8.0.24 ndb-8.0.24
2021-05-10 08:23:05 [MgmtSrvr] INFO     -- The default config directory '/usr/mysql-cluster' does not exist. Trying to create it...
2021-05-10 08:23:05 [MgmtSrvr] INFO     -- Sucessfully created config directory

Ora, uccidi il server in esecuzione con il seguente comando:

pkill -f ndb_mgmd

Quindi, crea un file di servizio systemd per MySQL Cluster Manager per gestire il servizio:

nano /etc/systemd/system/ndb_mgmd.service

Aggiungi le seguenti righe:

[Unit]
Description=MySQL NDB Cluster Management Server
After=network.target auditd.service
[Service]
Type=forking
ExecStart=/usr/sbin/ndb_mgmd -f /var/lib/mysql-cluster/config.ini
ExecReload=/bin/kill -HUP $MAINPID
KillMode=process
Restart=on-failure
[Install]
WantedBy=multi-user.target

Salva e chiudi il file, quindi ricarica il demone systemd con il seguente comando:

systemctl daemon-reload

Successivamente, avvia Cluster Manager e abilitalo per l'avvio al riavvio del sistema con il seguente comando:

systemctl start ndb_mgmd
systemctl enable ndb_mgmd

Ora puoi controllare lo stato del servizio con il seguente comando:

systemctl status ndb_mgmd

Dovresti ottenere il seguente output:

    ? ndb_mgmd.service - MySQL NDB Cluster Management Server
   Loaded: loaded (/etc/systemd/system/ndb_mgmd.service; disabled; vendor preset: enabled)
   Active: active (running) since Mon 2021-05-10 08:23:53 UTC; 6s ago
  Process: 984 ExecStart=/usr/sbin/ndb_mgmd -f /var/lib/mysql-cluster/config.ini (code=exited, status=0/SUCCESS)
 Main PID: 985 (ndb_mgmd)
    Tasks: 12 (limit: 2359)
   Memory: 6.5M
   CGroup: /system.slice/ndb_mgmd.service
           ??985 /usr/sbin/ndb_mgmd -f /var/lib/mysql-cluster/config.ini

May 10 08:23:53 master systemd[1]: Starting MySQL NDB Cluster Management Server...
May 10 08:23:53 master ndb_mgmd[984]: MySQL Cluster Management Server mysql-8.0.24 ndb-8.0.24
May 10 08:23:53 master systemd[1]: Started MySQL NDB Cluster Management Server.

Una volta terminato, puoi procedere al passaggio successivo.

Installa e configura i nodi di dati

Successivamente, creeremo il pacchetto del nodo dati su altri server e lo configureremo per comunicare con MySQL Cluster Manager.

Innanzitutto, installa le dipendenze richieste su entrambi i nodi di dati con il seguente comando:

apt-get install libclass-methodmaker-perl -y

Una volta installate tutte le dipendenze, scarica l'ultima versione del file deb di MySQL Data Nodes con il seguente comando:

wget https://cdn.mysql.com//Downloads/MySQL-Cluster-8.0/mysql-cluster-community-data-node_8.0.24-1debian10_amd64.deb

Una volta completato il download, installa il file scaricato con il seguente comando:

dpkg -i mysql-cluster-community-data-node_8.0.24-1debian10_amd64.deb

Per impostazione predefinita, i nodi dati memorizzano tutte le configurazioni nel file /etc/my.cnf.

Quindi crea un nuovo file /etc/my.cnf su entrambi i nodi di dati con il seguente comando:

nano /etc/my.cnf

Aggiungere l'IP del cluster come mostrato di seguito:

[mysql_cluster]
# Options for NDB Cluster processes:
ndb-connectstring=104.245.33.61 #IP of the MySQL Cluster Manager

Salva e chiudi il file, quindi crea una directory di dati su entrambi i nodi di dati:

mkdir -p /usr/local/mysql/data

Ora, avvia i nodi dati con il seguente comando:

ndbd

Dovresti ottenere il seguente output:

2021-05-10 08:27:13 [ndbd] INFO     -- Angel connected to '104.245.33.61:1186'
2021-05-10 08:27:13 [ndbd] INFO     -- Angel allocated nodeid: 2

Quindi, interrompi il processo ndbd in esecuzione con il seguente comando:

pkill -f ndbd

Successivamente, crea un file di servizio systemd per ndbd su entrambi i nodi di dati con il seguente comando:

nano /etc/systemd/system/ndbd.service

Aggiungi le seguenti righe:

[Unit]
Description=MySQL NDB Data Node Daemon
After=network.target auditd.service
[Service]
Type=forking
ExecStart=/usr/sbin/ndbd
ExecReload=/bin/kill -HUP $MAINPID
KillMode=process
Restart=on-failure
[Install]
WantedBy=multi-user.target

Salva e chiudi il file, quindi ricarica il demone systemd con il seguente comando:

systemctl daemon-reload

Successivamente, avvia il servizio ndbd e fallo partire al riavvio del sistema con il seguente comando:

systemctl start ndbd
systemctl enable ndbd

Puoi anche controllare lo stato del servizio ndbd usando il seguente comando:

systemctl status ndbd

Dovresti ottenere il seguente output:

? ndbd.service - MySQL NDB Data Node Daemon
   Loaded: loaded (/etc/systemd/system/ndbd.service; disabled; vendor preset: enabled)
   Active: active (running) since Mon 2021-05-10 08:28:28 UTC; 12s ago
  Process: 740 ExecStart=/usr/sbin/ndbd (code=exited, status=0/SUCCESS)
 Main PID: 741 (ndbd)
    Tasks: 46 (limit: 2359)
   Memory: 827.1M
   CGroup: /system.slice/ndbd.service
           ??741 /usr/sbin/ndbd
           ??742 /usr/sbin/ndbd

May 10 08:28:28 data1 systemd[1]: Starting MySQL NDB Data Node Daemon...
May 10 08:28:28 data1 ndbd[740]: 2021-05-10 08:28:28 [ndbd] INFO     -- Angel connected to '104.245.33.61:1186'
May 10 08:28:28 data1 ndbd[740]: 2021-05-10 08:28:28 [ndbd] INFO     -- Angel allocated nodeid: 2
May 10 08:28:28 data1 systemd[1]: Started MySQL NDB Data Node Daemon.

Installa e configura il server MySQL

Successivamente, scaricheremo e installeremo i pacchetti MySQL Server e Client sul nodo MySQL Cluster Manager.

Innanzitutto, scarica l'ultima versione di MySQL Cluster Bundle utilizzando il seguente comando:

wget https://cdn.mysql.com/Downloads/MySQL-Cluster-8.0/mysql-cluster_8.0.24-1debian10_amd64.deb-bundle.tar

Successivamente, estrai il file scaricato all'interno della directory /opt con il seguente comando:

tar -xvf mysql-cluster_8.0.24-1debian10_amd64.deb-bundle.tar -C /opt

Successivamente, modifica la directory in /opt e installa le dipendenze richieste con il seguente comando:

cd /opt
apt-get install libaio1 libmecab2 libnuma1 psmisc -y

Successivamente, installa il file deb MySQL Common con il seguente comando:

dpkg -i mysql-common*

Successivamente, installa i pacchetti client MySQL con il seguente comando:

dpkg -i mysql-cluster-community-client_8.0.24-1debian10_amd64.deb mysql-cluster-community-client-core_8.0.24-1debian10_amd64.deb mysql-cluster-community-client-plugins_8.0.24-1debian10_amd64.deb
dpkg -i mysql-client_8.0.24-1debian10_amd64.deb
dpkg -i mysql-cluster-community-server*

Se ricevi un errore di dipendenza, esegui il seguente comando:

apt-get install -f

Successivamente, installa il pacchetto MySQL Server con il seguente comando:

dpkg -i mysql-server_8.0.24-1debian10_amd64.deb

Durante l'installazione, ti verrà chiesto di impostare una password di root MySQL.

Dopo aver installato il server MySQL, sarà necessario modificare il file di configurazione principale di MySQL e definire l'IP del cluster:

nano /etc/mysql/my.cnf

Aggiungi le seguenti righe:

[mysqld]
# Options for mysqld process:
ndbcluster                      # run NDB storage engine
[mysql_cluster]
# Options for NDB Cluster processes:
ndb-connectstring=104.245.33.61  #IP of the MySQL Cluster Manager

Salva e chiudi il file quando hai finito. Successivamente, riavvia il servizio MySQL e abilitalo per l'avvio al riavvio del sistema:

systemctl restart mysql
systemctl enable mysql

Puoi controllare lo stato del server MySQL con il seguente comando:

systemctl status mysql

Dovresti vedere il seguente output:

? mysql.service - MySQL Cluster Community Server
   Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: enabled)
   Active: active (running) since Mon 2021-05-10 08:35:04 UTC; 7s ago
     Docs: man:mysqld(8)
           http://dev.mysql.com/doc/refman/en/using-systemd.html
  Process: 1950 ExecStartPre=/usr/share/mysql-8.0/mysql-systemd-start pre (code=exited, status=0/SUCCESS)
 Main PID: 1985 (mysqld)
   Status: "Server is operational"
    Tasks: 47 (limit: 2359)
   Memory: 372.0M
   CGroup: /system.slice/mysql.service
           ??1985 /usr/sbin/mysqld

May 10 08:35:01 master systemd[1]: Starting MySQL Cluster Community Server...
May 10 08:35:04 master systemd[1]: Started MySQL Cluster Community Server.

Una volta terminato, puoi procedere al passaggio successivo.

Prova il cluster MySQL

Ora abbiamo installato e configurato correttamente il MySQL Cluster a tre nodi. Successivamente, dovrai verificare se funziona o meno.

Innanzitutto, accedi al nodo MySQL Cluster Manager e accedi a MySQL con il seguente comando:

mysql -u root -p

Fornisci la tua password di root MySQL, quindi controlla le informazioni sul cluster con il seguente comando:

mysql> SHOW ENGINE NDB STATUS \G

Se tutto va bene, dovresti ottenere il seguente output:

*************************** 1. row ***************************
  Type: ndbclus
  Name: connection
Status: cluster_node_id=4, connected_host=104.245.33.61, connected_port=1186, number_of_data_nodes=2, number_of_ready_data_nodes=2, connect_count=0
*************************** 2. row ***************************
  Type: ndbclus
  Name: NdbTransaction
Status: created=2, free=2, sizeof=392
*************************** 3. row ***************************
  Type: ndbclus
  Name: NdbOperation
Status: created=4, free=4, sizeof=944
*************************** 4. row ***************************
  Type: ndbclus
  Name: NdbIndexScanOperation
Status: created=0, free=0, sizeof=1152
*************************** 5. row ***************************
  Type: ndbclus
  Name: NdbIndexOperation
Status: created=0, free=0, sizeof=952
*************************** 6. row ***************************
  Type: ndbclus
  Name: NdbRecAttr
Status: created=0, free=0, sizeof=88
*************************** 7. row ***************************
  Type: ndbclus
  Name: NdbApiSignal
Status: created=16, free=16, sizeof=144
*************************** 8. row ***************************
  Type: ndbclus
  Name: NdbLabel
Status: created=0, free=0, sizeof=200
*************************** 9. row ***************************
  Type: ndbclus
  Name: NdbBranch
Status: created=0, free=0, sizeof=32
*************************** 10. row ***************************
  Type: ndbclus
  Name: NdbSubroutine
Status: created=0, free=0, sizeof=72
*************************** 11. row ***************************
  Type: ndbclus
  Name: NdbCall
Status: created=0, free=0, sizeof=24
*************************** 12. row ***************************
  Type: ndbclus
  Name: NdbBlob
Status: created=0, free=0, sizeof=592
*************************** 13. row ***************************
  Type: ndbclus
  Name: NdbReceiver
Status: created=0, free=0, sizeof=128
*************************** 14. row ***************************
  Type: ndbclus
  Name: NdbLockHandle
Status: created=0, free=0, sizeof=48
*************************** 15. row ***************************
  Type: ndbclus
  Name: binlog
Status: latest_epoch=897648164875, latest_trans_epoch=820338753551, latest_received_binlog_epoch=0, latest_handled_binlog_epoch=0, latest_applied_binlog_epoch=0
15 rows in set (0.00 sec)

Ora esci dalla shell MySQL con il seguente comando:

mysql> exit

Ora eseguiremo un altro test per confermare che il cluster funzioni correttamente.

Connetti la console di gestione del cluster con il seguente comando:

ndb_mgm

Dovresti vedere il seguente output:

-- NDB Cluster -- Management Client --
ndb_mgm>

Ora, esegui il seguente comando per controllare tutti i dati:

ndb_mgm> SHOW

Dovresti ottenere il seguente output:

Connected to Management Server at: 104.245.33.61:1186
Cluster Configuration
---------------------
[ndbd(NDB)]	2 node(s)
id=2	@104.245.32.195  (mysql-8.0.24 ndb-8.0.24, Nodegroup: 0, *)
id=3	@69.87.218.169  (mysql-8.0.24 ndb-8.0.24, Nodegroup: 0)

[ndb_mgmd(MGM)]	1 node(s)
id=1	@104.245.33.61  (mysql-8.0.24 ndb-8.0.24)

[mysqld(API)]	1 node(s)
id=4	@104.245.33.61  (mysql-8.0.24 ndb-8.0.24)

Per controllare lo stato del primo nodo dati, eseguire il seguente comando:

ndb_mgm> 2 STATUS

Dovresti vedere il seguente output:

Node 2: started (mysql-8.0.24 ndb-8.0.24)

Per verificare lo stato del secondo nodo dati, eseguire il seguente comando:

ndb_mgm> 3 STATUS

Dovresti vedere il seguente output:

Node 3: started (mysql-8.0.24 ndb-8.0.24)

Conclusione

Congratulazioni! hai installato e configurato correttamente un cluster MySQL a tre nodi sul server Debian 10. È ora possibile utilizzare questa configurazione nell'ambiente di produzione per ottenere scalabilità e disponibilità.