Geavanceerd
Zoeken inHow-to-gidsen
Zo zoek je
  • Typ de taak in je eigen woorden (poort vinden, firewall openen, schijf vol) of plak een foutmelding tussen aanhalingstekens.
  • Kies "Snel antwoord" om alleen de commando's te zien, of een gebied om in één vakgebied te blijven.
  • "twee woorden" vindt precies die zin.
  • Gewone woorden vinden namen, teksten en voorbeelden.
  • ↑ ↓ bewegen · Enter openen · Esc sluiten · / zoeken
Probeer:

How-to 61Data · MySQL

How to reset a lost MySQL root password

The admin who set the MySQL root password left, and nobody has it. As long as you have root on the server, you can set a new one. First check the easy case (on many servers root logs in through the socket with no password at all), then use the safe official method: start the server once with an init file that changes the password, with networking off. This how-to covers MySQL 8 and MariaDB.

  • 5 Lessen
  • 5 Commando's

Dit deel is nog niet vertaald. Je ziet de Engelse tekst.

$ als gewone gebruiker# als root (of met sudo)

61.1

The quick answer

First try — this works on Ubuntu and most MariaDB installs (socket login for root):

mysql
mariadb

If you get a prompt, just set the password:

mysql
ALTER USER 'root'@'localhost' IDENTIFIED BY '<new long random password>';

If you get Access denied, use the init-file method below.

Commando's in deze les

61.2

Step by step

  1. Stop the service (the name depends on the system):
systemctl stop mysqld
systemctl stop mysql
systemctl stop mariadb
  1. **Write the change into a file only the mysql user can read:**
install -m 600 -o mysql -g mysql /dev/null /var/lib/mysql-reset.sql
printf "ALTER USER 'root'@'localhost' IDENTIFIED BY '%s';\n" '<new long random password>' > /var/lib/mysql-reset.sql
  1. Start the server once by hand with the init file and no network (nobody else can log in meanwhile):
install -d -o mysql -g mysql /run/mysqld
sudo -u mysql /usr/libexec/mysqld --init-file=/var/lib/mysql-reset.sql --skip-networking &
sudo -u mysql /usr/sbin/mysqld --init-file=/var/lib/mysql-reset.sql --skip-networking &

Use the first line on RHEL (MySQL), the second on Ubuntu (MySQL) or for MariaDB (/usr/sbin/mariadbd on new MariaDB). Wait some seconds and check the error log if it does not start.

  1. Test the new password, then stop the temporary server:
mysql -u root -p -e 'SELECT current_user();'
mysqladmin -u root -p shutdown
  1. Delete the file and start the service normally:
shred -u /var/lib/mysql-reset.sql
systemctl start mysqld
mysql -u root -p -e 'SELECT 1;'
  1. Store the new password safely (a password manager or a vault), and check who else has admin rights:
mysql
SELECT user, host, plugin FROM mysql.user WHERE Super_priv = 'Y' OR user = 'root';

61.3

Check that it worked

The service runs normally (systemctl status), root logs in with the new password, the reset file is gone, and the apps still work (their users were not changed).

61.4

Common problems

ProblemCauseFix
The manual start ends at onceWrong binary path, or the socket folder is missing.Check systemctl cat mysqld for the path; create /run/mysqld owned by mysql; read the error log.
ERROR 1396 … Operation ALTER USER failed for 'root'@'localhost'Root has another host value.SELECT user, host FROM mysql.user; and use that host.
Can't open … init fileThe mysql user can not read the file (or SELinux label).Put it under /var/lib/ with owner mysql; restorecon -v FILE.
Root still logs in without a passwordroot uses auth_socket / unix_socket.This is fine for local admin; apps should never use root.
Old guides use --skip-grant-tablesOlder method — anyone could log in while it runs.Prefer --init-file with --skip-networking.
Managed database (RDS, Azure)No server access.Reset the master/admin password in the cloud console or CLI.

61.5

Go further

1Why use --skip-networking during the reset?

While the server runs in this special mode, nobody can connect over the network.

2Root on Ubuntu logs in with sudo mysql and no password. Is that a problem?

No — it uses socket authentication: only the Linux root user can do it.

Alle commando's in dit hoofdstuk