If you've just installed MariaDB on your Debian or 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
It will prompt you for the root password. Enter the new password to verify that the changes are successful.
That’s it! You’ve successfully set or changed the MySQL root password on your Debian/Ubuntu system. This ensures that your database is secure and ready for use.