> ## Documentation Index
> Fetch the complete documentation index at: https://help.freakhosting.com/llms.txt
> Use this file to discover all available pages before exploring further.

# How to Reset the MySQL or MariaDB Root Password

> Reset a lost MySQL or MariaDB root password: try sudo mysql first, else restart with skip-grant-tables, run FLUSH PRIVILEGES and ALTER USER, then restart.

To reset the MySQL root password, first try `sudo mysql` (or `sudo mariadb`): on Ubuntu and Debian root often logs in through the Linux socket with no password, and you can run `ALTER USER` straight away. If that fails, add `skip-grant-tables` and `skip-networking` to the server config, restart, run `FLUSH PRIVILEGES;` and `ALTER USER 'root'@'localhost' IDENTIFIED BY 'new-password';`, then remove the two lines and restart again.

| Difficulty | Time | Works on | Needs |
| - | - | - | - |
| Intermediate | 10 minutes | MySQL 8.0 / 8.4 and MariaDB 10.5+ on Ubuntu, Debian, AlmaLinux and Rocky | sudo or root access to the server (SSH or console) |

## Before you start

* You need `sudo` or root on the Linux server. This guide resets the **database** root password, not your Linux login. For a lost Linux root password on a VPS, see [reset your VPS root password](https://help.freakhosting.com/portals/vps-panel/how-to-reset-your-vps-root-password.md).
* Websites lose database access for a minute or two during the reset. Do it at a quiet time.
* Take a snapshot or [back up the data folder](https://help.freakhosting.com/portals/other/how-to-back-up-a-linux-server.md) if you can.

## Step 1: Try socket login first

```bash theme={null}
sudo mariadb      # MySQL: sudo mysql
```

If you get a prompt such as `MariaDB [(none)]>` or `mysql>`, you are in. Set a new password and you are done:

> [tab] MariaDB
>
> ```sql theme={null}
> ALTER USER 'root'@'localhost' IDENTIFIED VIA unix_socket OR mysql_native_password USING PASSWORD('N3w-Root-Pass!');
> ```
>
> This keeps `sudo mariadb` working **and** sets a password.

> [tab] MySQL
>
> ```sql theme={null}
> ALTER USER 'root'@'localhost' IDENTIFIED BY 'N3w-Root-Pass!';
> ```

The [MariaDB documentation](https://mariadb.com/docs/server/security/user-account-management/authentication-from-mariadb-10-4) puts it simply: if you have forgotten the root password, you can still connect using `sudo` and change it.

If you see `ERROR 1045 (28000): Access denied for user 'root'@'localhost'`, root uses a password only. Continue with step 2.

## Step 2: Reset the password with skip-grant-tables

`skip-grant-tables` starts the server without checking any passwords. `skip-networking` makes sure nobody can connect over the network while it runs.

> [!WARNING]
>
> While `skip-grant-tables` is active, anyone with a shell on the server can open any database without a password. Never leave it in place, and always use it together with `skip-networking`.

**1. Create a temporary config file**

> [tab] Ubuntu / Debian
>
> ```bash theme={null}
> sudo nano /etc/mysql/conf.d/99-reset.cnf
> ```

> [tab] AlmaLinux / Rocky
>
> ```bash theme={null}
> sudo nano /etc/my.cnf.d/99-reset.cnf
> ```

Add these lines and save:

```ini theme={null}
[mysqld]
skip-grant-tables
skip-networking
```

**2. Restart the database server**

```bash theme={null}
sudo systemctl restart mariadb
```

The service is `mysql` for MySQL on Ubuntu and `mysqld` for MySQL on AlmaLinux / Rocky.

**3. Log in without a password**

```bash theme={null}
sudo mariadb -u root      # MySQL: sudo mysql -u root
```

**4. Reload the grant tables and set the password**

`ALTER USER` does not work until you reload the grant tables, so run `FLUSH PRIVILEGES` first.

> [tab] MariaDB
>
> ```sql theme={null}
> FLUSH PRIVILEGES;
> ALTER USER 'root'@'localhost' IDENTIFIED VIA unix_socket OR mysql_native_password USING PASSWORD('N3w-Root-Pass!');
> EXIT;
> ```

> [tab] MySQL
>
> ```sql theme={null}
> FLUSH PRIVILEGES;
> ALTER USER 'root'@'localhost' IDENTIFIED BY 'N3w-Root-Pass!';
> EXIT;
> ```
>
> Root now signs in with this password (`sudo mysql -u root -p`) instead of the socket.

**5. Remove the temporary file and restart**

```bash theme={null}
sudo rm /etc/mysql/conf.d/99-reset.cnf      # AlmaLinux / Rocky: /etc/my.cnf.d/99-reset.cnf
sudo systemctl restart mariadb
```

## How to check it worked

Log in with the new password over TCP:

```bash theme={null}
mariadb -u root -p -h 127.0.0.1 -e "SELECT CURRENT_USER();"
# MySQL: mysql -u root -p -h 127.0.0.1 -e "SELECT CURRENT_USER();"
```

Because `-h 127.0.0.1` connects over the network, this only works once `skip-networking` is gone, which also proves the server is back to normal. If you see `Can't connect to server on '127.0.0.1'`, the temporary file is still being read. Remove it and restart.

## What if I cannot log in to the server at all?

If you are locked out of SSH too, use the VPS console instead. See [use the VNC console](https://help.freakhosting.com/portals/vps-panel/how-to-enable-and-use-vnc-console-access.md) or [use rescue mode](https://help.freakhosting.com/portals/vps-panel/how-to-use-rescue-mode.md). On a dedicated server, [open a ticket](https://freakhosting.com/clientarea/submitticket.php) for console access.

## Frequently asked questions

### Why does the server not start after adding skip-grant-tables?

Check the log with `sudo journalctl -u mariadb -n 50` (or `-u mysql` / `-u mysqld`). A typo in the `.cnf` file is the usual cause. See [read logs with journalctl](https://help.freakhosting.com/portals/other/how-to-read-logs-with-journalctl.md).

### Can I reset a normal user's password the same way?

You do not need to. Log in as root and run `ALTER USER 'shopuser'@'localhost' IDENTIFIED BY 'N3w-Pass!';`. See [create a database and user](https://help.freakhosting.com/portals/other/how-to-create-a-mysql-database-and-user.md).

### Should root have a password at all?

On Ubuntu and Debian, socket login for root is safer for daily admin work. Give apps and tools such as phpMyAdmin their own users instead of root.

### Does resetting the root password delete any data?

No. Only the root account's login changes. Your databases are not touched.

## Related guides

> [card] [VPS and dedicated server guides](https://help.freakhosting.com/portals/other/vps-and-dedicated-server-guides.md)
>
> Every guide in this section.

> [card] [Secure MySQL and MariaDB](https://help.freakhosting.com/portals/other/how-to-secure-mysql-and-mariadb.md)
>
> Set root up safely from the start.

> [card] [Manage services with systemctl](https://help.freakhosting.com/portals/other/how-to-manage-services-with-systemctl.md)
>
> Start, stop and restart MySQL.

> [card] [Back up and restore MySQL](https://help.freakhosting.com/portals/other/how-to-back-up-and-restore-mysql-databases.md)
>
> Protect your data first.

> [card] [VNC console](https://help.freakhosting.com/portals/vps-panel/how-to-enable-and-use-vnc-console-access.md)
>
> Get in when SSH does not work.

***

> [!NOTE]
>
> **Checked against the official [MySQL 8.0 Reference Manual](https://dev.mysql.com/doc/refman/8.0/en/resetting-permissions.html) and [MariaDB documentation](https://mariadb.com/docs/server/security/user-account-management/authentication-from-mariadb-10-4)** on 4 November 2026; the skip-grant-tables reset and MariaDB `ALTER USER` statements tested on Ubuntu 24.04 LTS with MariaDB 10.11. Commands can differ on other distributions or releases.

> [!NOTE]
>
> ### Need Extra Help?
>
> If you encounter any issues, our support team is ready to assist:
>
> * **Live Chat:** Quick assistance via our website.
> * **Support Ticket:** [Open a Ticket](https://freakhosting.com/clientarea/submitticket.php)
> * **Discord:** [Join our Community](https://discord.gg/freakhosting)
> * **Email:** [support@freakhosting.com](mailto:support@freakhosting.com)

> [!TIP]
>
> ### Save on Your Hosting
>
> Ready to upgrade your hosting? Use code **KB20** at checkout for **20% off** your first order!

***

**Last Updated:** November 2026


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.