Salta al contenuto principale

Installare MySQL su Rocky Linux 9 Blue Onyx

In questa guida vedremo come installare MySQL 8.0 su Rocky Linux 9 "Blue Onyx" con dnf, il gestore di pacchetti default di Red Hat

È comune nella nostra Consulenza Linux, essendo base per gli stack web LAMP (Linux, Apache, MySQL, PHP), LEMP (Linux, NginX, MySQL, PHP) e svariati altri, necessari per webapp, CMS ed e-Commerce

Installare MySQL in Rocky Linux 9

Come primo passaggio aggiorniamo i pacchetti di Rocky Linux con:

sudo dnf upgrade

Accettiamo l'aggiornamento dei pacchetti e l'import della nuova chiave se richiesta

Rocky Linux 9 - AppStream                                   1.7 MB/s | 1.7 kB     00:00     
Importing GPG key 0x350D275D: 
Userid     : "Rocky Enterprise Software Foundation - Release key 2022 <[email protected]>" 
Fingerprint: 21CB 256A E16F C54C 6E65 2949 702D 426D 350D 275D 
From       : /etc/pki/rpm-gpg/RPM-GPG-KEY-Rocky-9 
Is this ok [y/N]: Y

Procediamo a installare MySQL con dnf install:

sudo dnf install mysql-server

Accettiamo l'installazione dei pacchetti necessari

Total download size: 48 M 
Installed size: 273 M 
Is this ok [y/N]: Y

Controlliamo se il processo mysql è attivo

sudo systemctl status mysqld
○ mysqld.service - MySQL 8.0 database server
     Loaded: loaded (/usr/lib/systemd/system/mysqld.service; disabled; preset: disabled)
     Active: inactive (dead)

Nel caso non sia attivo possiamo riavviare MySQL e ripetere il controllo precedente

sudo systemctl restart mysqld.service
sudo systemctl status mysqld
● mysqld.service - MySQL 8.0 database server
    Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; preset: disabled)
    Active: active (running) since Sat 2024-08-10 09:44:42 UTC; 32s ago

Settiamo l'avvio automatico per MySQL al boot

sudo systemctl enable mysqld

Controlliamo la porta default di MySQL, la 3306 TCP, che a questo punto dovrebbe essere in ascolto

sudo dnf install net-tools
sudo netstat -tnplu |grep 3306

Il comando deve restituirci l'output seguente, mostrando la porta 3306 in ascolto in locale

tcp6       0      0 :::3306                 :::*                    LISTEN      86216/mysqld        
tcp6       0      0 :::33060                :::*                    LISTEN      86216/mysqld        

Configurare MySQL

Il server è attivo, per una nuova installazione le configurazioni utilizzate sono quelle di default, la sicurezza è scarsa, a questo punto è consigliato eseguire lo script di configurazione del DBMS per settare le autenticazioni e rimuovere gli accessi anonimi

sudo mysql_secure_installation

Lo script ci guiderà nella configurazione delle opzioni di sicurezza del server.

Come prima cosa abilitiamo il controllo delle password per gli utenti, e selezioniamo le password forti, con ricerca a dizionario di password comuni

Securing the MySQL server deployment.
Connecting to MySQL using a blank password.
VALIDATE PASSWORD COMPONENT can be used to test passwords
and improve security. It checks the strength of password
and allows the users to set only those passwords which are
secure enough. Would you like to setup VALIDATE PASSWORD component?
Press y|Y for Yes, any other key for No: Y
There are three levels of password validation policy:
LOW    Length >= 8
MEDIUM Length >= 8, numeric, mixed case, and special characters
STRONG Length >= 8, numeric, mixed case, special characters and dictionary                  file
Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 2

Inseriamo la password

New password: 
Re-enter new password: 
Estimated strength of the password: 50 
Do you wish to continue with the password provided?
(Press y|Y for Yes, any other key for No) : Y

Rimuoviamo gli utenti anonimi, per aumentare la sicurezza

By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them. This is intended only for
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production
environment.
Remove anonymous users? (Press y|Y for Yes, any other key for No) : Y

Configuriamo autenticazione locale dell'utente root, che avverrà tramite socket, solamente sulla macchina locale

Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network.
Disallow root login remotely? (Press y|Y for Yes, any other key for No) : Y

Rimuoviamo i database di test, inutili in produzione

By default, MySQL comes with a database named 'test' that
anyone can access. This is also intended only for testing,
and should be removed before moving into a production
environment.
Remove test database and access to it? 
(Press y|Y for Yes, any other key for No) : Y

Effettuiamo il reload delle tabelle dei privilegi, in modo da applicare le nostre modifiche immediatamente.

Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.
Reload privilege tables now? (Press y|Y for Yes, any other key for No) : Y
All done!

Accesso a MySQL da console

L'installazione è terminata è possiamo accedere con l'utente root e la password precedente dalla console del nostro server

sudo mysql -u root -p
Enter password:

Come test possiamo listare i database presenti

show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)

L'output ci indica i database presenti, siamo riusciti a installare MySQL in Rocky Linux 9, possiamo utilizzare con la solita sintassi il nostro MySQL, connettere le nostre web app e CMS

Consulenza e Assistenza Linux Professionale

Affidati ai nostri esperti per i tuoi deploy in produzione.

Password di root di MySQL

Per accedere da remoto sarà necessario settare la password di root di MySQL. Possiamo utilizzare questo comando, ma consigliamo di utilizzare una password "forte", con numeri, maiuscole e minuscole e caratteri speciali.

ALTER USER 'root'@'localhost' IDENTIFIED BY 'LA_MIA_PASSWORD';

Accedere a MySQL remoto

Per accedere a un MySQL remoto è consigliato utilizzare un tunnel SSH ridirigendo la porta remota 3306 del nostro server, che ascolta solamente in localhost, sulla nostra porta 33006 locale

ssh root@IP-o-DNS -L 33006:localhost:3306

Grazie al tunnel potremmo connetterci direttamente alla nostra porta 33006 in localhost o 127.0.0.1 con le credenziali di root create prima

Per questo possiamo utilizzare uno di questi comandi:

mysql -h localhost -P 33006 -u root -p
mysql -h 127.0.0.1 -P 33006 -u root -p

Accedere con MySQL Workbench

MySQL Workbench è un ottimo strumento per Database Admin (D.B.A.) MySQL e per le Operation, permette di creare DB, gestire la configurazione del server, amministrare gli utenti, effettuare backup e molto altro, e incorpora anche la possibilità di effettuare tunnel SSH già descritto sopra

  1. Creiamo una nuova connessione
  2. Selezioniamo Standard TCP/IP over SSH
  3. Inseriamo le configurazioni di SSH per il nostro server di
    1. SSH Host name
    2. SSH User name
    3. Password o chiave SSH
  4. Le configurazioni del Database MySQL
    1. Hostname
    2. Server Port
    3. Username
    4. Password
  5. Testiamo la connessione

Aggiungi un commento

Comment

  • Elementi HTML permessi: <br> <p> <code class="language-*"> <pre>
  • Linee e paragrafi vanno a capo automaticamente.
  • Solo le immagini ospitate su questo sito possono essere utilizzate nel tag <img>.