Step 1: Check whether you need a password at all
sudo mysql
If you get a mysql> prompt, root is using auth_socket (MySQL) or unix_socket (MariaDB) authentication: the database trusts the Linux root user and ignores passwords. Check how root is set up:
SELECT user, host, plugin FROM mysql.user WHERE user = 'root';
| Plugin shown | What it means |
|---|---|
auth_socket / unix_socket | Root logs in through sudo; there is no password to forget |
caching_sha2_password | MySQL 8 default password authentication |
mysql_native_password | Older password authentication (MySQL 5.7, MariaDB) |
Socket authentication is secure and convenient for administrators. Applications should never connect as root anyway, so in most cases the right fix is not to give root a password but to create a dedicated user for the application:
CREATE USER 'shopapp'@'localhost' IDENTIFIED BY 'a-long-random-password';
GRANT ALL PRIVILEGES ON shopdb.* TO 'shopapp'@'localhost';
FLUSH PRIVILEGES;

Step 2: Changing a password you know
If you can log in (with sudo mysql or mysql -u root -p), changing the password is one statement:
ALTER USER 'root'@'localhost' IDENTIFIED BY 'new-long-password';
If you want root to use a password instead of socket authentication on MySQL 8:
ALTER USER 'root'@'localhost' IDENTIFIED WITH caching_sha2_password BY 'new-long-password';
Be careful with that change on Ubuntu: some maintenance scripts expect socket login. Keeping socket login for root and using named accounts for people and apps is usually simpler.
Step 3: Reset the MySQL root password when it is forgotten
Use this only if sudo mysql fails and you have no other admin account. MySQL's documentation recommends the --init-file method in How to Reset the Root Password, because it never leaves the server running without authentication.
The --init-file method (recommended)
- Create a file the MySQL user can read, containing the new password:
echo "ALTER USER 'root'@'localhost' IDENTIFIED BY 'new-long-password';" | sudo tee /var/lib/mysql-files/reset.sql sudo chown mysql:mysql /var/lib/mysql-files/reset.sql sudo chmod 600 /var/lib/mysql-files/reset.sql - Stop the service:
sudo systemctl stop mysql(the unit ismysqldon some distributions). - Start MySQL once in the foreground with the file:
If it complains thatsudo -u mysql mysqld --init-file=/var/lib/mysql-files/reset.sql/var/run/mysqlddoes not exist, create it withsudo mkdir -p /var/run/mysqld && sudo chown mysql:mysql /var/run/mysqldand try again. - When the log shows it is ready for connections, stop it with Ctrl+C (or
sudo mysqladmin -u root -p shutdownfrom another terminal using the new password). - Start the normal service and delete the file:
sudo systemctl start mysql sudo rm /var/lib/mysql-files/reset.sql mysql -u root -p
The --skip-grant-tables method (fallback)
Many older guides start MySQL with --skip-grant-tables, which disables all permission checks. If you use it, always add --skip-networking so nobody can connect remotely while the server is unprotected, then run FLUSH PRIVILEGES; before ALTER USER, and restart normally straight away.
MariaDB differences
- Root on Debian and Ubuntu uses
unix_socket, sosudo mariadbnormally works. ALTER USERworks in MariaDB 10.2 and later; older versions useSET PASSWORD.- The service is
mariadb, and the client command ismariadb(withmysqlstill available on many versions).
After the reset: tighten things up
- Store the password in a password manager, not in a text file on the server.
- Keep MySQL off the internet. Check
bind-address = 127.0.0.1in the MySQL configuration unless remote access is truly needed, and firewall port 3306. - Update applications that used root, moving them to their own limited users.
- Check logs if the password changed unexpectedly. An unexplained change may mean the server was compromised; see our ClamAV malware scanning guide and VPS troubleshooting guide.
- Back up your databases regularly with
mysqldumpor physical backups, stored off the server. Our guide to exporting and importing MySQL databases shows the safe options.
Once access is restored, our guide to tuning MySQL and PostgreSQL on a VPS covers the settings worth checking next, and optimising an Ubuntu VPS covers the rest of the stack.
Why developers choose SkyRain Cloud VPS
Databases deserve fast, reliable hardware. SkyRain Cloud Intel VPS plans give you:
- KVM virtualization. Your own kernel and allocated vCores, so you can tune the system the way this guide describes.
- Intel Xeon Platinum 8269CY hosts. Server-class Cascade Lake CPUs with DDR4 ECC memory.
- NVMe SSD storage on every plan, which keeps I/O wait low for databases and builds.
- Full root access to install and configure whatever your stack needs.
- Unmetered 1 Gbps port in India, close to your users.
- Simple billing and real support. Monthly billing in rupees with UPI, plus 24/7 tickets, WhatsApp and Discord.
Frequently Asked Questions
What is the default MySQL root password on Ubuntu?
How do I change the MySQL root password?
How do I reset a forgotten MySQL root password?
Is --skip-grant-tables safe?
Should my website connect to MySQL as root?
Does this work for MariaDB?
Conclusion
Resetting the MySQL root password is usually easier than it looks. Try sudo mysql first, because socket authentication means there may be no password at all. If you know the password, ALTER USER changes it in one line. If you have truly lost it, the --init-file method resets it without exposing an unauthenticated server. Then move applications to their own users and keep MySQL off the public internet.
For a VPS with root access, NVMe storage and ECC memory in India, see our Intel VPS plans.
SkyRain Cloud Intel Xeon VPS Plans
KVM VPS on Intel Xeon Platinum 8269CY (up to 3.80 GHz Turbo) with DDR4 ECC memory, NVMe SSD, full root access and an unmetered 1 Gbps port.
| Plan | vCores | RAM | Storage | Network | Price | Good for | Buy |
|---|---|---|---|---|---|---|---|
| VPS 1 — Intel | 2 | 8 GB DDR4 ECC | 40 GB NVMe SSD | 1 Gbps, unmetered | ₹649/month | Business websites, small apps, scheduled jobs and bots | Order |
| VPS 2 — Intel | 4 | 16 GB DDR4 ECC | 80 GB NVMe SSD | 1 Gbps, unmetered | ₹1,169/month | Business software, mid-size databases, several sites on one server | Check stock |
| VPS 3 — Intel | 8 | 32 GB DDR4 ECC | 150 GB NVMe SSD | 1 Gbps, unmetered | ₹2,249/month | ERP/CRM stacks, heavier databases, multiple production apps | Check stock |
| VPS 4 — Intel | 16 | 64 GB DDR4 ECC | 250 GB NVMe SSD | 1 Gbps, unmetered | ₹4,299/month | High-memory workloads and consolidated production servers | Check stock |
Prices are monthly in INR as listed on the SkyRain Cloud store (verified 2026-11-23). Live stock and final price are shown at checkout.