mysql(1)
MySQL command-line client for connecting to and interacting with MySQL databases.
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
| Flag | What it does |
|---|---|
-u, --user=NAME | Username to connect with (default: current system user) |
-p, --password[=PASSWORD] | Prompt for password (omit PASSWORD to be prompted securely) |
-h, --host=HOSTNAME | Server hostname or IP address (default: localhost) |
-P, --port=PORT | TCP port number (default: 3306) |
-e, --execute=STATEMENT | Execute SQL statement and exit (no interactive prompt) |
-D, --database=NAME | Select database to use on connection |
-t, --table | Output results in table format (default behavior) |
-H, --html | Output results as HTML table |
-N, --skip-column-names | Don't print column headers |
-B, --batch | Print results with tabs between columns (batch mode) |
-v, --verbose | Print statements and result counts |
Examples
Connect to localhost as root user; prompts for password interactively
mysql -u root -pConnect to remote server at 192.168.1.10 and select 'mydb' database
mysql -h 192.168.1.10 -u user -p mydbExecute a single query and exit without entering interactive mode
mysql -u user -p -e 'SELECT * FROM users LIMIT 5;' mydbImport SQL statements from a file to restore a database
mysql -u user -p mydb < backup.sqlExecute 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 -f1Connect 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.htmlConnect to non-standard port 3307 instead of default 3306
mysql -u user -p --port=3307 localhost