Knowledgebase

Find frequently asked questions and setup guides by searching or by browsing the categories.

The Query

SELECT user, host FROM mysql.user;

Each row is one account. An account is the pair of user name and host, so 'app_user'@'localhost' and 'app_user'@'%' are two separate accounts with their own passwords and privileges. localhost means connections from the server itself; % means any host.

Log In as an Administrator

Reading mysql.user needs an administrative account. On Ubuntu and Debian, the root account of the database usually logs in through the system's root user, without a password, as described in how to install MySQL on Ubuntu:

sudo mysql

Where root has a password, let the client ask for it:

mysql -u root -p

Never write the password after -p on the command line: it stays in your shell history and is visible to other users in the process list. From the shell, one query runs without opening the client:

sudo mysql -e "SELECT user, host FROM mysql.user;"

More Useful Columns

SELECT user, host, plugin, account_locked FROM mysql.user;

This form is for MySQL; on MariaDB, leave out account_locked, which its mysql.user view does not have. The plugin column shows how the account logs in:

  • caching_sha2_password: the default password method in MySQL 8.0 and later.
  • auth_socket: MySQL checks the Linux user that runs the client; this is how sudo mysql works for root on Ubuntu.
  • mysql_native_password: the older password method, disabled by default in MySQL 8.4 and removed in MySQL 9.0. MariaDB still supports it.
  • unix_socket: MariaDB's plugin for login through the Linux user, used by root by default.

To see one account in full, end the query with \G instead of a semicolon: SELECT * FROM mysql.user WHERE user = 'app_user'\G prints one column per line.

Which Account Am I Using?

SELECT USER(), CURRENT_USER();

USER() shows the name and host you connected with. CURRENT_USER() shows the account MySQL matched, whose privileges apply. When they differ, for example a login matched by a % account, the second one is the one that counts.

Show the Privileges of an Account

SHOW GRANTS FOR 'app_user'@'localhost';
SHOW GRANTS;

The first form shows another account; it needs both the name and the host. The second shows your own. A typical application account has ALL PRIVILEGES ON app.*, all rights on one database and nothing else. An application account with rights on *.* deserves a closer look.

System Accounts You Will See

Besides root and your own accounts, the list contains accounts the server creates for itself:

  • mysql.sys, mysql.session, mysql.infoschema: reserved accounts on MySQL 8.0 and later. They are locked and cannot log in.
  • mariadb.sys: the internal account on MariaDB, owner of the mysql.user view.
  • debian-sys-maint: may appear on Debian and Ubuntu, used by the package scripts.

Leave all of them alone. Removing one can break upgrades or system views.

MariaDB Differences

On MariaDB, mysql.user is a view over the mysql.global_priv table, and the query at the top works unchanged. Roles appear in the list too; on MariaDB they are marked by the is_role column. The client is called mariadb; the old name mysql still works through a link.

Remove an Account You No Longer Need

Before you drop an account, check with SHOW GRANTS what it can do and make sure no application still logs in with it: the application fails at its next connection. Then:

DROP USER 'old_app'@'localhost';

This removes the account and its privileges, not its databases. To list the system users of the server rather than the database, see how to list users in Linux. To list databases, see how to list databases in MySQL.

Was this answer helpful?
Loading...