Guides

MySQL and MariaDB: listing users and databases

SHOW DATABASES lists databases a user can see, and there is no SHOW USERS, so query mysql.user. How to list accounts, check grants and size each database.

Published ~5 min read
On this page

Short answer: SHOW DATABASES; lists every database the connected user can see; SELECT User, Host FROM mysql.user; lists every account, which SHOW DATABASES has no equivalent for on its own. Both work identically on MySQL and MariaDB, since MariaDB started as a drop-in fork and kept the same system tables and syntax for this.

TaskCommand
List databasesSHOW DATABASES;
List tables in the current databaseSHOW TABLES;
List every user accountSELECT User, Host FROM mysql.user;
List users, readable formSELECT CONCAT(User, '@', Host) AS account FROM mysql.user;
Who am I connected as?SELECT CURRENT_USER();
Permissions of the current userSHOW GRANTS;
Permissions of another userSHOW GRANTS FOR 'alice'@'localhost';
Which database is selected?SELECT DATABASE();
Switch databasesUSE dbname;
Size of every database, in MBSELECT table_schema, ROUND(SUM(data_length+index_length)/1024/1024,1) AS mb FROM information_schema.tables GROUP BY table_schema;

SHOW DATABASES: what you can actually see

mysql -u root -p
SHOW DATABASES;
Example output
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql               |
| performance_schema |
| sys                 |
| myapp               |
| wordpress           |
+--------------------+

information_schema, mysql, performance_schema and sys are system databases present on every install, not something you created; your own databases are whatever is left after those four. This list only shows databases the connected account has at least one privilege on: connect as root and you see everything; connect as an application account created for one database and you often see only that one, which is expected behaviour, not a bug.

Tables inside a database

mysql
USE myapp;
SHOW TABLES;
SHOW TABLES FROM myapp;    -- without switching first

Listing user accounts: there is no SHOW USERS

Neither MySQL nor MariaDB has a SHOW USERS statement. User accounts live in a real table, mysql.user, and you query it like any other table:

mysql -u root -p
SELECT User, Host FROM mysql.user;
Example output
+------------------+-----------+
| User             | Host      |
+------------------+-----------+
| root             | localhost |
| mariadb.sys      | localhost |
| myapp            | localhost |
| myapp            | %         |
+------------------+-----------+

The same user name with two different Host values is two separate accounts with potentially different passwords and privileges, not one account that happens to connect from two places, because MySQL and MariaDB key every account on the (user, host) pair together. % means “any host”, so a server with both myapp@localhost and myapp@% picks whichever is the more specific match for where the connection actually came from.

Why this needs an account with access to mysql.user

A regular application account usually cannot read mysql.user at all and gets ERR_ACCESS_DENIED or an empty result. It needs the SELECT privilege on the mysql database specifically, which root or another administrative account has by default. If you only have a non-root account and need this list, connect as root once rather than trying to grant yourself extra privileges from a limited account.

Checking what a user is actually allowed to do

your own permissions
SHOW GRANTS;
another account's permissions (needs admin rights)
SHOW GRANTS FOR 'myapp'@'localhost';
Example output
+---------------------------------------------------------------------+
| Grants for myapp@localhost                                          |
+---------------------------------------------------------------------+
| GRANT USAGE ON *.* TO `myapp`@`localhost`                            |
| GRANT ALL PRIVILEGES ON `myapp`.* TO `myapp`@`localhost`             |
+---------------------------------------------------------------------+

USAGE ON *.* means no real global privileges, just the ability to connect; the line below it is where the actual access is: here, every privilege but only on the myapp database, which is the shape a well-scoped application account should have. An account you expect to be limited that instead shows GRANT ALL PRIVILEGES ON *.* has full admin rights on everything, which is worth catching before it is used anywhere untrusted.

Creating a user and giving it exactly one database

mysql -u root -p
CREATE USER 'myapp'@'localhost' IDENTIFIED BY 'a-strong-password';
GRANT ALL PRIVILEGES ON myapp.* TO 'myapp'@'localhost';
FLUSH PRIVILEGES;

FLUSH PRIVILEGES is only needed after editing the grant tables directly (rare); CREATE USER and GRANT take effect immediately on their own, but running it anyway is harmless and a common habit from older guides. The full install-from-scratch walkthrough, including setting the root password itself, is in installing MariaDB/MySQL on Ubuntu and Debian.

Removing a user

mysql -u root -p
DROP USER 'myapp'@'localhost';

Dropping the user does not drop its database; the two are managed independently. DROP DATABASE myapp; removes the database and everything in it, with no undo, so back it up first with mysqldump if there is any doubt.

How big is each database?

mysql -u root -p
SELECT table_schema AS db,
       ROUND(SUM(data_length + index_length) / 1024 / 1024, 1) AS size_mb
FROM information_schema.tables
GROUP BY table_schema
ORDER BY size_mb DESC;

information_schema is itself a database (a virtual one built from the server’s own metadata), which is why this query reads from a table inside it rather than needing a separate command. It is the fastest way to see which database is actually using the disk space check disk usage showed filling up.

mysql: command not found, or Access denied

If the mysql client itself is missing, or the server rejects every password you try, those are both covered with their own fixes in MySQL Access denied for user ‘root’@‘localhost’, which also explains the socket-authentication default that trips people up on a fresh Debian or Ubuntu install.

Frequently asked

How do I list all databases in MySQL or MariaDB?

Run SHOW DATABASES. It only lists databases the connected account has at least one privilege on. Connect as root to see every database on the server, including the always-present system ones (information_schema, mysql, performance_schema, sys).

How do I list all users in MySQL? Is there a SHOW USERS command?

There is no SHOW USERS statement in either MySQL or MariaDB. Accounts live in a real table that an account with SELECT access to the mysql database, such as root, queries like any other: SELECT User, Host FROM mysql.user;

Why does the same username appear twice with different hosts?

MySQL and MariaDB key every account on the (user, host) pair together, not the username alone: 'myapp'@'localhost' and 'myapp'@'%' are two separate accounts that can have different passwords and different privileges, even though they share a name.

How do I check exactly what a user is allowed to do?

SHOW GRANTS FOR 'user'@'host'; lists that account's privileges. GRANT USAGE ON *.* alone means the account can only connect with no real privileges; look for the lines after it naming specific databases to see the actual access.

Related articles

All guides

Install MariaDB or MySQL on Ubuntu 24.04 and Debian 12/13, and secure it

Install MariaDB (Debian's default) or MySQL 8 on Ubuntu 24.04 and Debian 12/13, harden it, create an app user and allow remote access safely.

MySQL Access denied for root@localhost: the socket, not the password

On modern MySQL/MariaDB, root authenticates by OS identity, not a password. Why sudo mysql works, how to set a password, and how to reset if locked out.

Install phpMyAdmin on Ubuntu and Debian, and lock it down

Install phpMyAdmin on Ubuntu 24.04 or Debian 12/13: sudo apt install phpmyadmin, tick apache2 with Space (nginx: add a location block), then lock it down.

MySQL error 1064: how to read it and fix the SQL syntax

MySQL error 1064 is a SQL syntax error. How to read near '...' at line N, and fixes for reserved words, quotes, commas, MariaDB syntax and DELIMITER.

MySQL port: 3306 and 33060, how to check, change and open it safely

The MySQL port is 3306 by default, plus 33060 for MySQL 8's X Protocol. Check and change it, -P vs -p, safe remote access and connection errors (111, 110).

PostgreSQL: list databases, create users and grant access in psql

Postgres list databases with \l and users with \du, create a user and a database it owns, grant access, change passwords and fix permission errors.

MySQL backup: back up and restore a database with mysqldump

MySQL backup with mysqldump or mariadb-dump: --single-transaction, gzip, restore, a cron script with rotation, offsite copies with rsync and restore tests.

MySQL vs SQLite: differences, concurrency and when to switch

MySQL vs SQLite: embedded file vs database server, one writer and WAL, users, types, backups with .backup vs mysqldump, and when to switch to MySQL.

DynamoDB vs MongoDB: data model, queries, consistency, pricing and self-hosting

DynamoDB vs MongoDB: key-based tables vs documents, query and consistency models, on-demand vs Atlas pricing, the SSPL, and which one runs on a VPS.