$linuxjunkies
>

mysql(1)

MySQL command-line client for connecting to and interacting with MySQL databases.

UbuntuDebianFedoraArch

Synopsis

mysql [OPTION]... [DATABASE]

Description

The mysql command-line tool is the primary interactive client for MySQL servers. It allows you to connect to a database server, execute SQL queries, and retrieve results. You can run queries interactively or from files, making it essential for database administration, development, and troubleshooting.

Connection credentials can be provided via command-line options, environment variables, or a configuration file. By default, mysql connects to localhost as the current system user (no password).

Common options

FlagWhat it does
-u, --user=NAMEUsername to connect with (default: current system user)
-p, --password[=PASSWORD]Prompt for password (omit PASSWORD to be prompted securely)
-h, --host=HOSTNAMEServer hostname or IP address (default: localhost)
-P, --port=PORTTCP port number (default: 3306)
-e, --execute=STATEMENTExecute SQL statement and exit (no interactive prompt)
-D, --database=NAMESelect database to use on connection
-t, --tableOutput results in table format (default behavior)
-H, --htmlOutput results as HTML table
-N, --skip-column-namesDon't print column headers
-B, --batchPrint results with tabs between columns (batch mode)
-v, --verbosePrint statements and result counts

Examples

Connect to localhost as root user; prompts for password interactively

mysql -u root -p

Connect to remote server at 192.168.1.10 and select 'mydb' database

mysql -h 192.168.1.10 -u user -p mydb

Execute a single query and exit without entering interactive mode

mysql -u user -p -e 'SELECT * FROM users LIMIT 5;' mydb

Import SQL statements from a file to restore a database

mysql -u user -p mydb < backup.sql

Execute query in batch mode without headers, pipe to cut for first column

mysql -u user -p -B -N -e 'SELECT id, name FROM users;' | cut -f1

Connect with password provided directly (no space after -p); list all databases

mysql -u root -pSecretPass123 -e 'SHOW DATABASES;'

Export query results as HTML table to a file

mysql -u user -p -H -e 'SELECT * FROM products;' > report.html

Connect to non-standard port 3307 instead of default 3306

mysql -u user -p --port=3307 localhost

Related commands