Salta al contenuto principale

Come Installare MySQL su CentOS Stream 9: Guida Completa

In questa guida vedremo come Installare MySQL su CentOS Stream 9 con dnf, il gestore di pacchetti di default per RPM

MySQL è un servizio che ogni sistemista Linux utilizza molto spesso, essendo la base per gli stack web LAMP (Linux, Apache, MySQL, PHP), LEMP (Linux, NginX, MySQL, PHP) e molti altri, necessari per CMS, applicazioni web ed e-Commerce

Installare MySQL su CentOS Stream 9

Come primo passaggio aggiorniamo i pacchetti di CentOS Stream 9 con il gestore dnf:

sudo dnf upgrade

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

CentOS Stream 9 - BaseOS
Importing GPG key 0x8483C65D:
 Userid     : "CentOS (CentOS Official Signing Key) <[email protected]>"
 Fingerprint: 99DB 70FA E1D7 CE22 7FB6 4882 05B5 55B3 8483 C65D
 From       : /etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial
 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 al boot di MySQL

sudo systemctl enable mysqld
Created symlink /etc/systemd/system/multi-user.target.wants/mysqld.service → /usr/lib/systemd/system/mysqld.service.

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 prossimo passaggio per Installare MySQL su CentOS Stream 9, è configurare MySQL. Al primo avvio le configurazioni utilizzate di default hanno scarsa sicurezza. Per migliorare la sicurezza, è consigliato eseguire lo script di configurazione del DBMS, settare l'autenticazione MySQL 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 per l'utente root

New password: 
Re-enter new password: 
Estimated strength of the password: 90 
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

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 su CentOS Stream 9, possiamo utilizzare con la solita sintassi il nostro MySQL, connettere le nostre web app e CMS

Ottimizza il tuo Server CentOS

Vuoi migliorare le performance del tuo sistema? Affidati a un sistemista CentOS professionale, 30 minuti di analisi gratuita!

Modificare la password di root di MySQL

Nel caso vogliamo resettare la password di root di MySQL. Possiamo utilizzare questo comando, 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

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