Command-Line Scripting
This page is part of MariaDB's Documentation.
The parent of this page is: Connect and Query
Topics on this page:
Overview
Command-line scripts can connect to MariaDB database products using the mariadb client:
MariaDB Client (
mariadb) enables non-interactive connections from command-line scriptsMariaDB Client is available on Linux, macOS, and Microsoft Windows
Command-line options define client behavior
Alternatively, you can connect interactively or from your application.
Use Cases
Execute queries from the command-line
Execute queries from a script, such as a bash shell script
Execute queries from a file or piped in from another program
Save query output to a file
Compatibility
MariaDB Client (mariadb) has been tested by MariaDB to properly connect and execute queries with:
MariaDB Enterprise ColumnStore
MariaDB Enterprise Server
MariaDB MaxScale
MariaDB Xpand
Connection Parameters
The connection parameters must be provided to the mariadb client using command-line options.
When connecting to MariaDB Xpand, the --no-auto-rehash command-line option must also be set.
The command-line options can be specified in multiple ways.
Command-Line Arguments
The database's connection parameters can be specified as command-line arguments:
mariadb --host mariadb1.example.com \
--port 5001 \
--user DB_USER_NAME \
--password 'DB_USER_PASSWORD' \
--ssl-verify-server-cert \
--ssl-ca ca_chain.pem
In the example above, the database user's password is hard-coded. Passwords are ideally stored in a shared secret system. Please adapt the example to satisfy your security requirements.
Configuration File
The database's connection parameters can be specified in a configuration file in the [client] or [mariadb-client] groups:
[client]
host=mariadb1.example.com
port=5001
user=DB_USER_NAME
password=DB_USER_PASSWORD
ssl-verify-server-cert
ssl-ca=/path/to/ca_chain.pem
On Linux and macOS, the mariadb client reads options from ~/.my.cnf by default.
On any OS, the mariadb client can read options from a custom configuration file by specifying the --defaults-file or the --defaults-extra-file options:
mariadb --defaults-file ~/connection_parameters.cnf
If a configuration file contains sensitive information, such as a password, then access to the file should be restricted by changing the file's permissions:
$ chmod 0600 ~/connection_parameters.cnf
Execute queries from a command-line argument
The mariadb client can execute queries from a command-line argument by specifying the --execute option:
mariadb --defaults-file ~/connection_parameters.cnf \
--execute 'CALL run_daily_reports();'
Execute queries from a file
The mariadb client can execute queries from a file using a shell redirection for standard input:
mariadb --defaults-file ~/connection_parameters.cnf \
< run_daily_reports.sql
Execute queries output by another command-line utility
The mariadb client can execute queries output by another command-line utility by piping the utility's output to the client:
generate_report_sql.sh | mariadb --defaults-file ~/connection_parameters.cnf
Write query output to a file
The mariadb client can write query output to a file using a shell redirection for standard output:
mariadb --defaults-file ~/connection_parameters.cnf \
< run_daily_reports.sql \
> daily_report_output.log
The output can be customized with command-line options:
To reduce output, specify
--silent. Errors can still be caught by inspecting the command-line client's exit code.To output data in a tab-separated values (TSV) format, specify
--batch. The--batchoption implicitly enables--silent.To remove the column name header from the output, specify
--skip-column-names.To prevent special characters from being escaped in the output, specify
--raw.
