LinuxCommandLibrary

mysql

TLDR

Connect to database

$ mysql -u [username] -p [database]
copy
Connect to remote host
$ mysql -h [hostname] -u [username] -p [database]
copy
Execute SQL command
$ mysql -u [user] -p -e "[SELECT * FROM table]" [database]
copy
Execute SQL file
$ mysql -u [user] -p [database] < [script.sql]
copy
Connect with specific port
$ mysql -h [host] -P [3306] -u [user] -p
copy
Import database dump
$ mysql -u [user] -p [database] < [dump.sql]
copy

SYNOPSIS

mysql [options] [database]

DESCRIPTION

mysql is the MySQL command-line client. It connects to MySQL/MariaDB servers.
The tool executes SQL queries. Provides interactive and batch modes.
mysql is MySQL CLI client.

PARAMETERS

DATABASE

Database name.
-h HOST
Server hostname.
-u USER
Username.
-p [PASSWORD]
Password (prompts if empty).
-P PORT
Port number.
-e COMMAND
Execute SQL and exit.
--help
Display help information.

CAVEATS

Requires server access. Password prompt recommended. Beware SQL injection in scripts.

HISTORY

mysql was created as the standard CLI for MySQL database, maintained by Oracle and the community.

SEE ALSO

Copied to clipboard