GNU/Linux >> Linux Esercitazione >  >> Linux

Come modificare/reimpostare la password di root di MySQL o MariaDB

Di recente, ho cambiato la password dell'utente root MySQL del mio sito Web. Quindi ho pensato cosa sarebbe successo se avessi dimenticato la password di root di MySQL?

Esiste un modo semplice per reimpostare la password di root di MySQL o MariaDB?

Ho esaminato alcuni tutorial online, ma nessuno di essi sembra essere abbastanza completo da distinguere tra la modifica della password e la reimpostazione della password. Sembravano mancare i dettagli sulla tabella MySQL in cui sono archiviate le password degli utenti e in quali colonne.

In questo tutorial impareremo i seguenti argomenti.

  1. Come modificare la password principale di MySQL/MariaDB
  2. Come reimpostare la password di root MySQL/MariaDB

Cercherò di renderlo il più completo possibile e, si spera, dopo aver letto questo, riuscirai a portare a termine questo compito facilmente senza bisogno di ulteriore aiuto.

Qual ​​è la differenza tra modificare e reimpostare la password?

Se si conosce la password di root, è possibile connettersi al database come utente root e quindi modificare la password molto facilmente. Puoi modificare la password di root così come qualsiasi altra password utente.

Se hai dimenticato la password di root, significa che non puoi connetterti al server MySQL come utente root. L'utente root ha i privilegi più alti e non puoi cambiare la sua password tramite altri account. In questo caso, dobbiamo eseguire alcuni passaggi aggiuntivi per reimpostare la password di root di MySQL.

I passaggi per MySQL e MariaDB sono gli stessi?

MariaDB è costruito su MySQL. È molto popolare per i requisiti di hosting web. In effetti, JournalDev e tutti i miei siti Web utilizzano il database MariaDB. Qualsiasi comando che funzioni per MySQL funzionerà anche per MariaDB.

L'unico aggiustamento nei comandi che potresti dover fare è fermare e avviare il server MySQL. Sto usando Ubuntu per questo tutorial e utilizzo systemctl per avviare/arrestare servizi. Puoi anche usare /etc/init.d/mysql per eseguire le stesse operazioni.

Se utilizzi un sistema operativo Windows, utilizza mysqld o mysqladmin dal prompt dei comandi per avviare o arrestare il server MySQL. Si trovano nella cartella bin di installazione di MySQL.

Come modificare la password di root di MySQL o MariaDB

Sto usando il database MariaDB, possiamo usare l'opzione –version per scoprire la sua versione.

# mariadb --version
mariadb  Ver 15.1 Distrib 10.1.44-MariaDB, for debian-linux-gnu (x86_64) using readline 5.2
# 

1. Connettiti a MySQL come utente root

# mysql -uroot -p
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 9
Server version: 10.1.44-MariaDB-0ubuntu0.18.04.1 Ubuntu 18.04

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

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

MariaDB [(none)]> 

2. Modifica la password e il valore della stringa_autenticazione nella tabella mysql.user

Le password utente MySQL sono archiviate nella tabella mysql.user password e nelle colonne stringa_autenticazione nel formato crittografato. Possiamo usare la funzione PASSWORD() per convertire una stringa di testo normale nel valore crittografato e impostare queste colonne.

MariaDB [(none)]> use mysql
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
MariaDB [mysql]> select password, authentication_string from user where User = 'root' AND Host = 'localhost';
+-------------------------------------------+-------------------------------------------+
| password                                  | authentication_string                     |
+-------------------------------------------+-------------------------------------------+
| *E510A8BC6807F8BF4913D893620792C432FCBA5B | *E510A8BC6807F8BF4913D893620792C432FCBA5B |
+-------------------------------------------+-------------------------------------------+
1 row in set (0.00 sec)

MariaDB [mysql]> UPDATE user SET authentication_string = PASSWORD('qwerty2021') WHERE User = 'root' AND Host = 'localhost';
Query OK, 1 row affected (0.01 sec)
Rows matched: 1  Changed: 1  Warnings: 0

MariaDB [mysql]> UPDATE user SET password = PASSWORD('qwerty2021') WHERE User = 'root' AND Host = 'localhost';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

MariaDB [mysql]> select password, authentication_string from user where User = 'root' AND Host = 'localhost';
+-------------------------------------------+-------------------------------------------+
| password                                  | authentication_string                     |
+-------------------------------------------+-------------------------------------------+
| *6F168491676C70E51CB8D0F14D6B581D1322A77A | *6F168491676C70E51CB8D0F14D6B581D1322A77A |
+-------------------------------------------+-------------------------------------------+
1 row in set (0.00 sec)

MariaDB [mysql]> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)

MariaDB [mysql]> exit
Bye
root@localhost:~# 

Capiamo cosa sta succedendo nelle query di cui sopra.

  • Prima di tutto, stiamo cambiando il database in 'mysql'
  • Quindi, stiamo impostando i valori delle colonne "stringa_autenticazione" e "password" per l'utente "root"@"localhost" con la nuova password.
  • Poi stiamo ricaricando le tabelle delle sovvenzioni utilizzando il comando FLUSH PRIVILEGES.
  • Quindi chiudi la sessione MySQL. La password di root è stata modificata correttamente.

NOTA :Ho provato a usare ALTER USER comando per cambiare la password di root, ma non ha funzionato.

MariaDB [mysql]> ALTER USER root@localhost IDENTIFIED BY 'qwerty2022';
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'USER root@localhost IDENTIFIED BY 'qwerty2022'' at line 1
MariaDB [mysql]>

3. Verifica l'accesso dell'utente root utilizzando la nuova password

# mysql -uroot -pqwerty2021
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 11
Server version: 10.1.44-MariaDB-0ubuntu0.18.04.1 Ubuntu 18.04

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

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

MariaDB [(none)]>

Questo è tutto. Abbiamo modificato con successo la password utente MySQL/MariaDB.

Come reimpostare la password di root MySQL/MariaDB

Se hai dimenticato la password di root, dobbiamo eseguire un ulteriore passaggio in modo da poter accedere al terminale MySQL senza fornire la password.

1. Arrestare il server MySQL

# systemctl stop mysql

Puoi anche eseguire systemctl stop mariadb , l'effetto sarà lo stesso.

2. Avvio del server MySQL senza l'impostazione del controllo dei permessi

L'idea è di avviare il server MySQL senza caricare le informazioni sulle tabelle di concessione, in modo da poter accedere come utente root senza fornire la password.

È un rischio per la sicurezza eseguire il server MySQL in questo modo, quindi deve essere eseguito brevemente e spento immediatamente dopo aver reimpostato la password di root.

Possiamo avviare il server MySQL in modalità provvisoria e passare –skip-grant-tables opzione per saltare il caricamento delle tabelle di concessione che memorizzano le impostazioni dei privilegi utente.

# sudo mysqld_safe --skip-grant-tables --skip-networking &
[1] 11734
root@localhost:~# 200427 20:05:40 mysqld_safe Logging to syslog.
200427 20:05:40 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql
#

È importante eseguire il comando che termina con &in modo che venga eseguito in background. Sto anche superando –skip-networking opzione per saltare la rete che impedisce ad altri client di connettersi al server MySQL.

3. Connettiti a MySQL Server come root senza passare la password

# mysql -u root
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 10.1.44-MariaDB-0ubuntu0.18.04.1 Ubuntu 18.04

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

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

MariaDB [(none)]> 

Nota che non stiamo fornendo la password di root, ma siamo comunque in grado di connetterci al server MySQL.

4. Reimposta la password di root nella tabella mysql.user

MariaDB [(none)]> use mysql
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
MariaDB [mysql]> UPDATE user SET authentication_string = PASSWORD('qwerty2022') WHERE User = 'root' AND Host = 'localhost';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

MariaDB [mysql]> UPDATE user SET password = PASSWORD('qwerty2022') WHERE User = 'root' AND Host = 'localhost';
Query OK, 1 row affected (0.01 sec)
Rows matched: 1  Changed: 1  Warnings: 0

MariaDB [mysql]> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)

MariaDB [mysql]> exit
Bye
# 

5. Arresta e avvia il server MySQL

Prima di tutto, uccideremo il server MySQL in esecuzione. Il PID è presente nel /var/run/mysqld/mysqld.pid file.

# cat /var/run/mysqld/mysqld.pid
11891
# sudo kill 11891
# 

Ora avvia il server MySQL in modalità normale.

# systemctl start mysql

6. Verifica accedendo come utente root con una nuova password

# mysql -uroot -pqwerty2022
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 5
Server version: 10.1.44-MariaDB-0ubuntu0.18.04.1 Ubuntu 18.04

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

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

MariaDB [(none)]> 

Se proverai ad accedere come root senza password, verrà generato l'errore "Accesso negato".

# mysql
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
# mysql -u root
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
# mysql -uroot
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
# 

Conclusione

L'utente root di MySQL è proprio come qualsiasi altro utente. Tuttavia, modificare o reimpostare la sua password è un po' complicato perché è il super utente e non possiamo cambiare la password di root da un altro accesso utente.


Linux
  1. Come reimpostare la password di root MySQL su CentOS 6

  2. Come reimpostare la password di root MySql

  3. Come reimpostare la password di root MySql

  4. Come reimpostare la password di root di MySQL o MariaDB

  5. Come cambiamo la password di root?

Come reimpostare la password dell'utente root MySQL in Linux

Come modificare o reimpostare la password di root dimenticata in RHEL 8

Come reimpostare la password di root di MySQL

Come modificare la password di root in Linux

Come ripristinare la password di root di MySQL 8.0 su Centos 7.x?

Come reimpostare la password di root in CentOS/RHEL 8