هذا الجزء لم يُترجم بعد. أنت ترى النص الإنجليزي.
$ مستخدم عادي# بصلاحية root (أو مع sudo)
The quick answer
Step by step
- Stop the service (the name depends on the system):
- **Write the change into a file only the
mysqluser 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- 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.
- Test the new password, then stop the temporary server:
mysql -u root -p -e 'SELECT current_user();'
mysqladmin -u root -p shutdown- Delete the file and start the service normally:
- Store the new password safely (a password manager or a vault), and check who else has admin rights:
SELECT user, host, plugin FROM mysql.user WHERE Super_priv = 'Y' OR user = 'root';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).
Common problems
| Problem | Cause | Fix |
|---|---|---|
| The manual start ends at once | Wrong 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 file | The 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 password | root uses auth_socket / unix_socket. | This is fine for local admin; apps should never use root. |
Old guides use --skip-grant-tables | Older 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. |
Go further
- Command pages: mysql, systemctl.
- Next how-tos: Back up and restore MySQL, See who is connected to a database, Create a user with sudo rights.
- Full guide: Secure the first install of MariaDB / MySQL.
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.