After some research I ended on these exact SQL commands. Sharing here for comments and in case others find them useful.
CREATING DATABASES AND USERS FOR APPS
On Debian/MariaDB running apt install mariadb-server will set up a sane and
secure MariaDB server by default. The commands mariadb-install-db and
mariadb-upgrade run automatically when needed.
The primary tasks for the administrator is to create new users and databases,
and add custom settings in /etc/mysql/mariadb.conf.d/.
To add a custom database for an app, and create a custom password-authenticated
user that has full access to that database over the network, run the following
commands using the mariadb client (e.g., via sudo mariadb):
-- Create the database
CREATE DATABASE app_db;
-- Create the user 'app_user' with a secure password, allowing connections from any IP address
CREATE USER 'app_user'@'%' IDENTIFIED BY 'your_secure_password';
-- Grant full privileges to the 'app_user' user on the 'app_db' database
GRANT ALL PRIVILEGES ON app_db.* TO 'app_user'@'%';
-- Reload the privilege tables to apply changes
FLUSH PRIVILEGES;
Remember to replace 'your_secure_password' with a strong, unique password.
Using '%' for the host allows connections from any IP address, which is less
secure than restricting connections to a specific IP address or subnet if
possible.
REMOTE CONNECTIONS OVER THE NETWORK AND TLS
For security reasons, the Debian package has enabled networking only on the
loop-back device using bind-address in
/etc/mysql/mariadb.conf.d/50-server.cnf. Check current setting by running:
SHOW VARIABLES LIKE 'bind_address';
Alternatively, check with sudo netstat -tlnp on what ports services are
listening. If your connection is aborted immediately, check your firewall rules
or network routes.
To allow remote connections from the network, the easiest way is to create a new
configuration file and allow the MariaDB Server to listen for remote
connections. To ensure passwords don't leak on the network, enforce that
connections must use TLS encryption.
Create file /etc/mysql/mariadb.conf.d/99-server-customizations.cnf
with contents:
[mariadbd]
# Listen for connections from anywhere
bind-address = 0.0.0.0
# Only allow TLS encrypted connections
require-secure-transport = on
For settings to take effect, restart the server:
systemctl restart mariadb
Note that in MariaDB 11.8 the TLS certificates are generated automatically, so
there is no need to create them manually.
Test connections with e.g.
mariadb --user=app_user --password=your_secure_password --host=192.168.1.66 -e '\s'
mariadb --user=root --password=your_secure_password --host=192.168.1.66 --ssl