My Blog List


Thursday, March 6, 2025

How to Set or Change the MySQL Root Password on Ubuntu

 

How to Set or Change the MySQL Root Password on Ubuntu

If you've just installed MariaDB on your Ubuntu system, it’s important to secure the root user by setting a password for it. Here’s a step-by-step guide on how to set or change the root password for MySQL/MariaDB:

Step 1: Install MariaDB

Start by installing MariaDB server using the following command:

sudo apt install -y mariadb-server

This will install the MariaDB server and the necessary dependencies.

Step 2: Access MySQL as Root

Next, log in to MySQL as the root user:

sudo mysql -u root

Step 3: Set a New Root Password

Once logged in, you can set or change the root password. Execute the following command:

ALTER USER 'root'@'localhost' IDENTIFIED BY 'newpassword';

Ensure the root user has all privileges by running this command:

GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' WITH GRANT OPTION;

This ensures the root user can access and manage all databases and has the option to grant permissions to other users.

Replace 'newpassword' with your desired password.

Step 4: Flush Privileges

To apply the changes, run:

FLUSH PRIVILEGES;
exit;

This ensures that the changes to user privileges take effect immediately.

Step 5: Test the New Password

Finally, test the new root password by logging in again:

sudo mysql -u root -p

No comments:

Post a Comment