Checking the current database in MySQL or MariaDB prevents commands from running against the wrong schema when statements omit a database name. Confirming session context before commands such as SHOW TABLES, DESCRIBE orders, or data-changing statements reduces the risk of reading or modifying the wrong application data.

Each client connection keeps its own default database, and the server uses that setting whenever a statement references an unqualified table or view name. SELECT DATABASE() asks the server which database is current for the session, and NULL means the session has no default database yet.

The current database is session-scoped rather than server-wide, so it can differ between terminals, scripts, or separate client tabs. MySQL systems typically use the mysql client command, while current MariaDB packages prefer mariadb; if a session should start in a specific schema, select it after login or start the client with –database=<db_name> and then repeat the check.

Steps to show the current database in MySQL or MariaDB:

  1. Connect to the server with the mysql or mariadb command-line client.
    $ mysql --host=db.example.net --user=dbadmin --password

    On current MariaDB systems, use mariadb if that is the client installed on the host.

  2. Check the current database for the active session.
    mysql> SELECT DATABASE();
    +------------+
    | DATABASE() |
    +------------+
    | NULL       |
    +------------+
    1 row in set (0.00 sec)

    NULL means the session does not have a default database selected yet.

  3. Select a database for the current session when the result is NULL or when a different schema is required.
    mysql> USE appdb;
    Database changed

    USE changes the default database only for the current connection.

  4. Run the check again to confirm the current database name.
    mysql> SELECT DATABASE();
    +------------+
    | DATABASE() |
    +------------+
    | appdb      |
    +------------+
    1 row in set (0.00 sec)

    To start the client in a known schema, connect with --database=appdb and then run SELECT DATABASE();.

  5. Exit the client when the check is complete.
    mysql> exit
    Bye