# How to reset MySQL/MariaDB root password

If you forget the MySQL/MariaDB root password, you can reset it by starting MySQL/MariaDB in safe mode, which doesn’t require authentication.

## How to reset a MySQL root password step by step

The root password for [MySQL](https://www.ionos.ca/digitalguide/server/know-how/what-is-mysql/) can be reset and changed in just a few steps directly in the terminal.

### Step 1: Shutdown database

First, shut down the database. Choose the option that suits your system. In both cases, the [sudo command](https://www.ionos.ca/digitalguide/server/configuration/linux-sudo-command/) is used to act as a superuser:

- **[Ubuntu](https://www.ionos.ca/digitalguide/server/know-how/ubuntu-the-linux-system-for-everyone/)/[Debian](https://www.ionos.ca/digitalguide/server/know-how/debian-the-universal-system-software/)**: `sudo systemctl stop mysql`
- **[CentOS](https://www.ionos.ca/digitalguide/server/know-how/centos-versions-system-requirements/)/Red Hat**: `sudo systemctl stop mysqld`

### Step 2: Start MySQL in safe mode

Then restart the [database](https://www.ionos.ca/digitalguide/hosting/technical-matters/databases/) in safe mode to perform a MySQL root password reset:

```none
sudo mysqld --skip-grant-tables --skip-networking --pid-file=/tmp/mysqld-reset.pid &
```

You can now access MySQL as the root user without entering a password.

```none
mysql -u root
```

### Step 3: Set a new MySQL root password

In the next step, you can change your MySQL root password to a new, [secure password](https://www.ionos.ca/digitalguide/server/security/protect-sensitive-data-with-a-strong-password/) by using the following command:

```none
ALTER USER 'root'@'localhost' IDENTIFIED BY 'New!Secure!Password';
```

Then reload the privilege tables:

```none
FLUSH PRIVILEGES;
```

### Step 4: Exit and restart MySQL

Exit the MySQL client:

```none
quit;
```

Then exit MySQL:

```none
mysqladmin -u root -p shutdown
```

Enter the root password you created in the previous step.

Next, restart the database in normal mode. Once again, use the command that matches your system:

- **Ubuntu/Debian**: `sudo systemctl start mysql`
- **CentOS/Red Hat**: `sudo systemctl start mysqld`

## How to reset a MariaDB root password step by step

You can also change your root password in [MariaDB](https://www.ionos.ca/digitalguide/server/know-how/what-is-mariadb/) in just a few steps. This works similarly to MySQL.

### Step 1: Shut down the database

The first step with MariaDB is to shut down the database. You can do this with the following command:

```none
sudo systemctl stop mariadb
```

### Step 2: Start MariaDB in safe mode

To perform a MariaDB root password reset, you must now start the database in safe mode:

```none
sudo mysqld_safe --skip-grant-tables --skip-networking --pid-file=/tmp/mariadb-reset.pid &
```

You can then log in to MariaDB as the root user. You no longer need a password:

```none
mysql -u root
```

### Step 3: Set a new MariaDB root password

Finally, change the MariaDB root password to a new password of your choice. Use the following command:

```none
ALTER USER 'root'@'localhost' IDENTIFIED BY 'New!Secure!Password';
```

Reload the privilege tables:

```none
FLUSH PRIVILEGES;
```

### Step 4: Stop and restart MariaDB

Exit the MariaDB client:

```none
quit;
```

Next, stop MariaDB:

```none
mysqladmin -u root -p shutdown
```

You can then restart your database in normal mode:

```none
sudo systemctl start mariadb
```


This is a markdown version of: [https://www.ionos.ca/digitalguide/hosting/technical-matters/reset-mysql-mariadb-root-password/](https://www.ionos.ca/digitalguide/hosting/technical-matters/reset-mysql-mariadb-root-password/) for AI/LLM consumption.