Hardware FixRecommendedDevice not working? Your driver may be the problemCheck updates for common hardware issues.Fix DriversOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsSlow PC?RecommendedPC slow today? Run a repair scan before it gets worseResolve common Windows issues and optimize system performance.Scan Now×
Skip to content
World desk7 min

How to Change the MySQL Root Password on Ubuntu (and Fix `auth_socket` Login Errors)

Ubuntu often configures MySQL root with auth_socket, so sudo mysql -u root works while mysql -u root -p fails. This guide shows how to inspect the account, choose socket or password authentication, change the password safely, verify the correct host row, and harden the installation.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

On Ubuntu, a freshly installed MySQL server commonly creates root@localhost with the auth_socket authentication plugin. That means sudo mysql -u root works without a MySQL password, while mysql -u root -p can fail with “Access denied.” To use password login, connect through the Unix socket, change the account to a password-based plugin, set a strong password with ALTER USER, and test the exact command your client will use.

What you are changing

A MySQL account is identified by both its user name and host. The administrative account normally discussed here is 'root'@'localhost', not every account named root on the server. Its password and its authentication plugin are separate settings:

As an Amazon Associate I earn from qualifying purchases.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
  • Password: the secret MySQL checks when a password-based plugin is used.
  • Authentication plugin: the method MySQL uses to verify the connection, such as auth_socket or caching_sha2_password.

Changing a password does not automatically change the plugin. If root remains on auth_socket, a password supplied with -p will not be used for authentication. Conversely, changing the plugin without setting a usable password can leave password login unusable.

Before you begin

  • You need an Ubuntu account with sudo privileges. Local socket authentication depends on operating-system access, so a user who cannot run sudo cannot use this recovery path.
  • Confirm that the server is MySQL, not MariaDB. Plugin names and account syntax can differ. The command-line client usually prints a server banner after connection; you can also run SELECT VERSION(); once connected.
  • Use a long, unique password that is not reused for Ubuntu, applications, or another database. Do not put the real secret in a shell command, screenshot, script, or documentation.
  • Plan how the account will be used. Keep root local for administration whenever possible; applications should have a separate least-privilege account.

Step 1: Inspect the root account

Ubuntu’s packaged MySQL commonly expects the first administrative connection to use the local Unix socket. Open a root session with:

sudo mysql -u root

If that command opens a mysql> prompt, inspect every root row and its plugin:

SELECT user, host, plugin FROM mysql.user WHERE user = 'root';

A typical result includes root, localhost, and auth_socket. The host value matters: an 'root'@'localhost' account is a different account from 'root'@'127.0.0.1' or 'root'@'%'. Apply changes to the row that matches the connection you intend to test.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

While connected, you can record the server version for plugin compatibility:

SELECT VERSION();

Do not copy an authentication-plugin name from an old tutorial without checking this result. Current MySQL 8 installations support caching_sha2_password; older releases and MariaDB packages may expose different defaults or names.

Choose the authentication method first

Method How root proves identity Advantages Trade-offs Typical connection
auth_socket A local Unix-socket client is checked against the operating-system user. No database password to store for local administration; access is constrained to the machine and its local users. Clients that expect a password cannot log in as root; recovery requires a working sudo path. sudo mysql -u root
Password plugin, such as caching_sha2_password MySQL verifies the password supplied by the client. Works with clients and automation that require password authentication. The secret must be protected, rotated, and kept out of logs; exposing root for remote use increases risk. mysql -u root -p

There is no requirement to switch away from auth_socket merely because a password has not been set. Choose password authentication only when a specific client or operational workflow needs it.

Switch root to password authentication and set the password

Keep the session opened with sudo mysql -u root. For a current MySQL 8 server, assign the password and select the supported password plugin in one statement:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
ALTER USER 'root'@'localhost'
  IDENTIFIED WITH caching_sha2_password BY 'NEW_STRONG_PASSWORD';

Replace NEW_STRONG_PASSWORD with the secret you selected. The account host must exactly match the row returned by the inspection query. If your server reports a different supported password plugin, use that plugin’s documented name rather than blindly substituting an older mysql_native_password example.

MySQL’s ALTER USER statement is the supported way to assign or change the password of an existing account. It updates the account; it does not create another root user and it does not grant additional privileges.

Exit the administrative session:

EXIT;

Now test password authentication without sudo:

mysql -u root -p

At the password prompt, enter the new secret. Nothing is echoed while you type. A successful login returns a MySQL prompt. Test from the same environment your application will use, because socket, TCP, user, and host combinations can select different account rows.

Keep socket authentication instead

If local-only administration is the goal, leave the root account on auth_socket. Use:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
sudo mysql -u root

The plugin authenticates a local Unix-socket client and does not require a MySQL password. Setting a password while leaving the plugin unchanged will not make mysql -u root -p work; the authentication method still determines how the login is checked.

This arrangement is appropriate when administrators have controlled sudo access and no application needs to connect as root. Give applications their own account instead of weakening the local-only root policy.

Verify the result and the account you actually changed

After testing, reconnect through the administrative method you chose and confirm the plugin:

sudo mysql -u root
SELECT user, host, plugin FROM mysql.user WHERE user = 'root';

For a password-based setup, the intended row should show the selected password plugin. For a socket-based setup, it should show auth_socket. If several rows are returned, inspect each host value and remove ambiguity from your client tests: localhost commonly uses a Unix socket, while an explicit IP address requests TCP and can match another account row.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use the least powerful test that proves your requirement. A local administrative check does not prove that a remote client can or should connect as root, and a successful root login does not validate an application’s account permissions.

Optional hardening with mysql_secure_installation

Ubuntu provides an interactive hardening utility:

sudo mysql_secure_installation

On Ubuntu Noble’s packaged MySQL, the utility can offer to:

  • Set a password for root accounts.
  • Remove root accounts that are accessible from outside the local host.
  • Remove anonymous users.
  • Remove the test database.

Read each prompt for the server’s real use case. This workflow is optional; it does not replace inspecting the account’s plugin and host. If you already changed root with ALTER USER, avoid making a second change blindly. Confirm the resulting account and test the intended login afterward.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Troubleshoot common failures

mysql -u root -p returns “Access denied”

First try the Ubuntu socket path:

sudo mysql -u root

Then inspect plugin for 'root'@'localhost'. If it is auth_socket, password login is expected to fail until you deliberately switch the account to a supported password plugin with ALTER USER. If the plugin is already password-based, check that you are using the correct host row and the current password.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

sudo mysql -u root also fails

Verify that the Linux account can run sudo, that the MySQL service is running, and that the client is connecting to the intended local server. If the machine has more than one MySQL-compatible installation, confirm the server banner and package before applying MySQL-specific account commands. Without operating-system administrator access, an Ubuntu administrator or the server’s documented recovery procedure is required.

ALTER USER reports an error

Check the exact account identity and server capabilities:

  • Run the inspection query and use the returned host value, such as localhost, in the ALTER USER statement.
  • Run SELECT VERSION(); and verify that the chosen plugin is supported by that release.
  • Confirm that you are connected to MySQL rather than MariaDB, whose syntax and plugin set can differ.
  • Check that the SQL statement’s quotes and semicolon are intact. A password containing a single quote requires proper SQL escaping; do not paste an unescaped secret into a command copied from a shell.

The password works locally but not from an application

The application may be selecting a different account row because it uses a different host, socket, or TCP setting. Compare its connection parameters with the rows returned by SELECT user, host, plugin FROM mysql.user WHERE user = 'root';. More importantly, do not use root for routine application access. Create a separate account with only the permissions the application needs, and test that account using the application’s exact connection mode.

A remote root login is being considered

Do not expose the root account for remote application access. Remote root expands the impact of a leaked secret and conflicts with the local-administration purpose of auth_socket. Keep root local and use a dedicated, least-privilege account for remote administration or application traffic, protected by the server’s normal network and access controls.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Password-handling practices that prevent a second incident

  • Enter the password at MySQL’s prompt instead of placing it after -p, which can expose it through shell history or process inspection.
  • Store credentials in the application’s supported protected configuration mechanism, not in source control or world-readable files.
  • Use a unique secret for each environment and rotate it through a planned maintenance procedure.
  • After rotation, test both the administrative path and each legitimate client path, then remove obsolete credentials from deployment systems.
  • Keep a documented break-glass procedure that requires controlled sudo access if root remains socket-authenticated.

MySQL versus MariaDB: stop before copying commands

This procedure targets Ubuntu’s MySQL packages. MariaDB may display a different server banner, use different authentication plugins, or interpret account-management syntax differently. Identify the server and package first, then follow that server’s documentation. A command that is valid on MySQL 8 is not automatically valid on every MariaDB release.

Quick decision checklist

  1. Connect with sudo mysql -u root and inspect user, host, and plugin.
  2. If local socket administration is sufficient, keep auth_socket and use sudo mysql -u root.
  3. If a client requires a password, run ALTER USER for the exact root host row with a plugin supported by the installed MySQL version.
  4. Exit and verify with mysql -u root -p, entering the password only at the prompt.
  5. Run sudo mysql_secure_installation only if its hardening choices fit this server, and review every prompt.
  6. Keep root local and create a separate least-privilege account for applications or remote connections.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Outdated Drivers Are Slowing You DownFree scan - exact matches
Windows Errors? Fix Them Before They SpreadFree repair scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.