GNU/Linux >> Linux Esercitazione >  >> Panels >> Panels

Come installare e utilizzare MySQL su Ubuntu 20.04

In questo tutorial, ti mostreremo come installare MySQL su Ubuntu 20.04 e come usarlo con i comandi MySQL di base. MySQL è un sistema di gestione di database relazionali open source e con la sua popolarità è ampiamente utilizzato su diversi sistemi per l'archiviazione dei dati.

In questo post imparerai di più sull'accesso a MySQL con o senza utente root, creazione di database, creazione di utenti, concessione di privilegi, accesso esterno ai tuoi database, importazione di un database, creazione di un dump di un database e così via. Iniziamo!

1. Aggiorna il sistema

Partiamo dal presupposto che tu abbia una nuova installazione di Ubuntu 20.04 come sistema operativo ed è per questo che aggiorneremo il sistema prima di iniziare con l'installazione.

sudo apt update -y && sudo apt upgrade -y

2. Installa il server MySQL

Installa il server del database MySQL con il seguente comando:

sudo apt install mysql-server -y

Dopo aver eseguito questo comando, inizierà il processo di installazione di MySQL 8.0. MySQL 8.0 è incluso per impostazione predefinita nel repository di Ubuntu 20.04. Una volta completata l'installazione, puoi verificare se il servizio è attivo e funzionante:

sudo systemctl status mysql

Dovresti ottenere il seguente output:

root@vps:~# systemctl status mysql
● mysql.service - MySQL Community Server
     Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: enabled)
     Active: active (running) since Thu 2021-11-04 17:07:12 UTC; 39s ago
   Main PID: 98088 (mysqld)
     Status: "Server is operational"
      Tasks: 38 (limit: 4617)
     Memory: 355.4M
     CGroup: /system.slice/mysql.service
             └─98088 /usr/sbin/mysqld

3. Proteggi il server MySQL

Prima di iniziare a utilizzare MySQL, è necessario proteggerlo con più passaggi con il seguente comando:

sudo mysql_secure_installation

Questo comando ci guiderà attraverso più passaggi che ci aiuteranno a configurare la sicurezza del nostro MySQL, impostare una password di root complessa e, ecc. Dovresti usare le opzioni con lettere in grassetto dall'output di seguito:

root@vps:~# sudo mysql_secure_installation

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
Please set the password for root here.

New password: YourStrongRootPassword

Re-enter new password:YourStrongRootPassword

Estimated strength of the password: 100
Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : Y
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
Success.


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

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
 - Dropping test database...
Success.

 - Removing privileges on test database...
Success.

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

All done!

L'installazione di MySQL sarà ora protetta e potremo procedere con alcuni comandi utili nei prossimi passi

4. Accedi al server MySQL con e senza password di root

Dopo una nuova installazione di MySQL, puoi accedere al prompt di MySQL digitando solo "mysql ” sulla riga di comando del server senza MySQL per richiedere la password di root impostata nel passaggio precedente.

Dopo aver digitato "mysql ” verrai registrato nel prompt di MySQL.

root@vps:~# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 13
Server version: 8.0.27-0ubuntu0.20.04.1 (Ubuntu)

Copyright (c) 2000, 2021, 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>

Lo stesso accadrà se esegui il seguente comando e premi invio senza digitare la password:

mysql -u root -p

Per configurare MySQL in modo che richieda la password di root, eseguire il comando seguente nel prompt di MySQL:

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'YourStrongRootPassword';

Dovresti vedere il seguente input dopo l'esecuzione

mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'YourStrongRootPassword';
Query OK, 1 rows affected (0.01 sec)

mysql

Ora, se provi ad accedere con "mysql ” comando otterrai il messaggio qui sotto:

root@vps:~# mysql
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)

È necessario utilizzare il comando seguente, con la password di root precedentemente impostata:

mysql -u root -p

È una tua scelta decidere se vuoi usare una password per l'accesso come root o meno. Se vuoi accedere senza la password, ignora semplicemente questo passaggio e vai avanti.

5. Database, creazione utenti e concessione di privilegi

Se elenchi i database nel prompt di MySQL con il comando "mostra database", otterrai i database predefiniti da MySQL:

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

Per creare un database, eseguire il seguente comando:

Create database 'admin';

Una volta creato, puoi elencare nuovamente i database:

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

mysql>

Creiamo un utente chiamato "adminuser" e concediamo i privilegi al database "admin" con una password.

CREATE USER 'adminuser'@'localhost' IDENTIFIED by 'YourStrongPassword';
 GRANT ALL PRIVILEGES ON admin* TO 'adminuser'@'localhost';
 FLUSH PRIVILEGES;
 EXIT;

Una volta, l'utente del database viene creato con i privilegi di "admin ” database puoi facilmente verificare se tutto è impostato correttamente:

mysql -u adminuser -p

Dovresti vedere solo "admin ” e l'impostazione predefinita “schema_informativo banche dati:

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| admin              |
| information_schema |
+--------------------+
2 rows in set (0.01 sec)

mysql>

6. Accesso esterno all'Utente

Per concedere l'accesso remoto all'utente “adminuser " e il nostro "amministratore esistente ” database eseguire il seguente comando:

GRANT ALL ON admin.* TO adminuser@'remote_ip_address' IDENTIFIED BY 'YourStrongPassword';

Se desideri che l'utente si connetta da qualsiasi indirizzo IP, utilizza il % :

GRANT ALL ON admin.* TO adminuser@'%' IDENTIFIED BY 'YourStrongPassword';

7. Crea un dump del database

Per eseguire un dump del database non è necessario accedere al prompt di MySQL. Può essere eseguito tramite il terminale della riga di comando del server utilizzando il nome dell'utente, il database e la password. Faremo un dump del nostro "amministratore database con l'utente root:

mysqldump -u root -p admin > dump.sql

Una volta completato il dump, puoi elencare per verificare se tutto è a posto

root@vps:/# mysqldump -u root -p admin > dump.sql
Enter password:
root@vps:/# ls -al
total 12
drwxr-xr-x  2 root root 4096 Nov  4 21:38 .
drwxr-xr-x 19 root root 4096 Oct 18 14:09 ..
-rw-r--r--  1 root root 1266 Nov  4 21:40 dump.sql

8. Importa il dump del database

Per importare il dump del database in "admin ” database con l'utente root è necessario utilizzare il seguente comando:

mysql -u root -p admin < dump.sql

Questa è la sintassi per il dump e l'importazione del database:

mysqldump -u "database user" -p "database name" > "name of dump file".sql

mysql -u "database user" -p "database name" < "name of dumped database".sql

Si noti che l'utente del database deve disporre dei privilegi per il database di cui è necessario eseguire il dump. L'utente root MySQL ha i privilegi per eseguire il dump e importare tutti i database.

Congratulazioni, sei riuscito a installare e utilizzare i comandi più importanti nel server MySQL

Se vuoi saperne di più sui comandi MySQL, visita la loro documentazione ufficiale. Ovviamente, se trovi difficoltà puoi contattare il nostro supporto tecnico 24 ore su 24, 7 giorni su 7, e i nostri amministratori installeranno e configureranno il server MySQL su Ubuntu 20.04 per te.

Se ti è piaciuto questo post su come installare e utilizzare MySQL su Ubuntu 20.04, condividilo con i tuoi amici sui social network utilizzando i pulsanti a sinistra o semplicemente lascia una risposta qui sotto. Grazie.


Panels
  1. Come installare MySQL su Ubuntu 20.04

  2. Come installare e utilizzare Wine su Ubuntu 20.04

  3. Come installare MySQL su Ubuntu 18.04

  4. Come installare il database MySQL su Ubuntu 20.04

  5. Come installare MySQL su Ubuntu 22.04

Come installare e utilizzare Kontact in Ubuntu 20.04

Come installare e utilizzare iTunes su Ubuntu 20.04

Come installare e utilizzare R su Ubuntu

Come installare MySQL 8.0 su Ubuntu 18.04

Come installare MySQL 8.0 su Ubuntu 18.04

Come installare e utilizzare MySQL Workbench su Ubuntu 18.04