Linux VPS7 min read•Last updated: •By SkyRain Cloud Team

How to Reset the MySQL Root Password on a Linux Server

Few things stall a project like being locked out of your own database. Whether you inherited a server with no notes, an installer generated a password nobody saved, or a colleague changed it, you can reset the MySQL root password as long as you have root or sudo access to the Linux server itself.

Before using the recovery method, check the easy case: on Ubuntu and Debian, MySQL's root account often does not use a password at all. This guide covers that check, the normal way to change a known password, the safe recovery method for a forgotten one, and the MariaDB differences.

Quick answer

First try sudo mysql: on Ubuntu and Debian, MySQL and MariaDB root accounts usually authenticate through the Unix socket, so this works without a password. If it does, run ALTER USER 'root'@'localhost' IDENTIFIED BY 'new-password'; or create a separate admin user. If it does not, stop MySQL, start it once with --init-file pointing to a file containing that ALTER USER statement, then restart normally and delete the file.

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 shownWhat it means
auth_socket / unix_socketRoot logs in through sudo; there is no password to forget
caching_sha2_passwordMySQL 8 default password authentication
mysql_native_passwordOlder 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;
Reset the MySQL root password – SkyRain Cloud servers in India

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)

  1. 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
  2. Stop the service: sudo systemctl stop mysql (the unit is mysqld on some distributions).
  3. Start MySQL once in the foreground with the file:
    sudo -u mysql mysqld --init-file=/var/lib/mysql-files/reset.sql
    If it complains that /var/run/mysqld does not exist, create it with sudo mkdir -p /var/run/mysqld && sudo chown mysql:mysql /var/run/mysqld and try again.
  4. When the log shows it is ready for connections, stop it with Ctrl+C (or sudo mysqladmin -u root -p shutdown from another terminal using the new password).
  5. 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, so sudo mariadb normally works.
  • ALTER USER works in MariaDB 10.2 and later; older versions use SET PASSWORD.
  • The service is mariadb, and the client command is mariadb (with mysql still 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.1 in 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 mysqldump or 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?
There usually is none. On Ubuntu, MySQL's root account uses auth_socket, so you log in with sudo mysql instead of a password.
How do I change the MySQL root password?
Log in as root and run ALTER USER 'root'@'localhost' IDENTIFIED BY 'new-password'; in MySQL 5.7, MySQL 8 or MariaDB 10.2 and later.
How do I reset a forgotten MySQL root password?
Stop MySQL, start it once with --init-file pointing to a file containing an ALTER USER statement, then restart the service normally and delete the file.
Is --skip-grant-tables safe?
Only briefly and with --skip-networking, because it disables all permission checks. The --init-file method is safer.
Should my website connect to MySQL as root?
No. Create a separate user with privileges only on the application's database, so a compromised application cannot affect other databases.
Does this work for MariaDB?
Yes. MariaDB root on Debian and Ubuntu uses unix_socket (sudo mariadb), and ALTER USER works from version 10.2 onwards.

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.

PlanvCoresRAMStorageNetworkPriceGood forBuy
VPS 1 — Intel28 GB DDR4 ECC40 GB NVMe SSD1 Gbps, unmetered₹649/monthBusiness websites, small apps, scheduled jobs and botsOrder
VPS 2 — Intel416 GB DDR4 ECC80 GB NVMe SSD1 Gbps, unmetered₹1,169/monthBusiness software, mid-size databases, several sites on one serverCheck stock
VPS 3 — Intel832 GB DDR4 ECC150 GB NVMe SSD1 Gbps, unmetered₹2,249/monthERP/CRM stacks, heavier databases, multiple production appsCheck stock
VPS 4 — Intel1664 GB DDR4 ECC250 GB NVMe SSD1 Gbps, unmetered₹4,299/monthHigh-memory workloads and consolidated production serversCheck 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.

Instant UPI • ₹649/mo
Fast KVM Cloud Provisioning