brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: a custom mariadb prompt shows user, host and database before you run a query

Database
~/tips/tip-database-mariadb-prompt-user-host

mariadb --prompt='\u@\h [\d]> ' shop

Puts the login user, server host and current database in the client prompt.

The default prompt is MariaDB [shop]>. It tells you the database, but not which server you are on, and that is exactly what I want to know before a DELETE.

mariadb --prompt='\u@\h [\d]> ' shop

That gives a prompt like root@localhost [shop]>. \u is the user, \h the server host and \d the current database, which updates after a USE. With a dozen containers that all say MariaDB, a host in the prompt saves you from typing into the wrong one.

To make it permanent, put it in ~/.my.cnf:

[mariadb-client]
prompt=\u@\h [\d]>\_

In the option file a trailing space is easy to lose, so \_ is the safer choice, because the client turns it into a space. You can also change it inside a session with the prompt command, and plain prompt on its own resets it.

The host part shows what you connected to, so over a socket it says localhost everywhere. For servers reached through a local socket or a port forward, that is not enough to tell them apart. In that case I’d put a fixed label in the prompt of each machine’s option file instead, like prompt=PROD [\d]>\_.

Further reading: mariadb command-line client, mysql client commands (prompt)

More Database tips mariadb -e 'CREATE DATABASE IF NOT EXISTS blog' mariadb-dump --no-create-info --insert-ignore shop orders > orders-rows.sql SELECT table_schema, table_name, CHECKSUM TABLE shop.orders; services: All Database tips (53) All topics

Link