Linux and Unix mysql command

Quick links

About mysql
Syntax
Examples
Related commands
Linux and Unix main page

About mysql

mysql is a simple SQL shell (with GNU readline capabilities). It supports interactive and noninteractive use. When
used interactively, query results are presented in an ASCII-table format. When used noninteractively (for example, as a filter), the result is presented in tab-separated format. The output format can be changed using command options.

Syntax

mysql command line options
mysql commands

mysql command line options

Below is a listing of mysql options that can be used while at the command line.

--help, -? Display a help message and exit.
--auto-rehash Enable automatic rehashing. This option is on by default, which enables database, table, and column name completion. Use --disable-auto-rehash to disable rehashing. That causes mysql to start faster, but you must issue the rehash command if you want to use name completion.

To complete a name, enter the first part and press Tab. If the name is unambiguous, mysql completes it. Otherwise, you can press Tab again to see the possible names that begin with what you have typed so far. Completion does not occur if there is no default database.
--batch, -B Print results using tab as the column separator, with each row on a new line. With this option, mysql does not use the history file. Batch mode results in nontabular output format and escaping of special characters. Escaping may be disabled by using raw mode; see the description for the --raw option.
--character-sets-dir=path The directory where character sets are installed. See Section 9.2, The Character Set Used for Data and Sorting
--column-names Write column names in results.
--comments, -c Whether to preserve comments in statements sent to the server. The default is --skip-comments (discard comments), enable with --comments (preserve comments). This option was added in MySQL 5.0.52.
--compress, -C Compress all information sent between the client and the server if both support compression.
--database=db_name, -D db_nameThe database to use. This is useful primarily in an option file.
--debug[=debug_options], -# [debug_options]Write a debugging log. A typical debug_options string is 'd:t:o,file_name'. The default is 'd:t:o,/tmp/mysql.trace
--debug-info, -T Print some debugging information when the program exits.
--default-character-set=charset_name Use charset_name as the default character set. See Section 9.2, The Character Set Used for Data and Sorting
--delimiter=str Set the statement delimiter. The default is the semicolon character
--disable-named-commands Disable named commands. Use the \* form only, or use named commands only at the beginning of a line ending with a semicolon (;mysql starts with this option enabled by default. However, even with this option, long-format commands still work from the first line. See the section called MYSQL COMMANDS
--execute=statement, -e statementExecute the statement and quit. The default output format is like that produced with --batch.
--force, -f Continue even if an SQL error occurs.
--host=host_name, -h host_nameConnect to the MySQL server on the given host.
--html, -H Produce HTML output.
--ignore-spaces, -i Ignore spaces after function names. The effect of this is described in the discussion for the IGNORE_SPACE SQL mode
--line-numbers Write line numbers for errors. Disable this with --skip-line-numbers.
--local-infile[={0|1}] Enable or disable LOCAL capability for LOAD DATA INFILE. With no value, the option enables LOCAL. The option may be given as --local-infile=0 or --local-infile=1 to explicitly disable or enable LOCAL. Enabling LOCAL has no effect if the server does not also support it.
--named-commands, -G Enable named mysql commands. Long-format commands are allowed, not just short-format commands. For example, quit and \q both are recognized. Use --skip-named-commands to disable named commands.
--no-auto-rehash, -A Deprecated form of -skip-auto-rehash. Use --disable-auto-rehash instead. See the description for --auto-rehash.
--no-beep, -b Do not beep when errors occur.
--no-named-commands, -g Deprecated, use --disable-named-commands instead.
--no-pager Deprecated form of --skip-pager. See the --pager option.
--no-tee Do not copy output to a file.
--one-database, -o Ignore statements except those for the default database named on the command line. This is useful for skipping updates to other databases in the binary log.
--pager[=command] Use the given command for paging query output. If the command is omitted, the default pager is the value of your PAGER environment variable. Valid pagers are less, more, cat [> filename], and so forth. This option works only on Unix. It does not work in batch mode. To disable paging, use --skip-pager. the section called MYSQL COMMANDS
--password[=password], -p[password]The password to use when connecting to the server. If you use the short option form (-p), you cannot have a space between the option and the password. If you omit the password value following the --password or -p option on the command line, you are prompted for one. Specifying a password on the command line should be considered insecure.
--pipe, -W On Windows, connect to the server via a named pipe. This option applies only for connections to a local server, and only if the server supports named-pipe connections.
--port=port_num, -P port_numThe TCP/IP port number to use for the connection.
--prompt=format_str Set the prompt to the specified format. The default is mysql>. The special sequences that the prompt can contain are described in the section called MYSQL COMMANDS
--protocol={TCP|SOCKET|PIPE|MEMORY}
The connection protocol to use for connecting to the server. It is useful when the other connection parameters normally would cause a protocol to be used other than the one you want. For details on the allowable values, see Section 4.2.2, Connecting to the MySQL Server
--quick, -q Do not cache each query result, print each row as it is received. This may slow down the server if the output is suspended. With this option, mysql does not use the history file.
--raw, -r For tabular output, the boxing nontabular output (such as is produced in batch mode or when the --batch or --silent option is given), special characters are escaped in the output so they can be identified easily. Newline, tab, NUL, and backslash are written as \n, \t, \0, and \\. The --raw option disables this character escaping.

The following example demonstrates tabular versus nontabular output and the use of raw mode to disable escaping:
% mysql mysql> SELECT CHAR(92); +----------+ | CHAR(92) | +----------+ | \ | +----------+ % mysql -s mysql> SELECT CHAR(92); CHAR(92) \\ % mysql -s -r mysql> SELECT CHAR(92); CHAR(92) \
--reconnect If the connection to the server is lost, automatically try to reconnect. A single reconnect attempt is made each time the connection is lost. To suppress reconnection behavior, use --skip-reconnect.
--safe-updates, --i-am-a-dummy, -UAllow only those UPDATE and DELETE statements that specify which rows to modify by using key values. If you have set this option in an option file, you can override it by using --safe-updates on the command line.
--secure-auth Do not send passwords to the server in old (pre-4.1.1) format. This prevents connections except for servers that use the newer password format.
--show-warnings Cause warnings to be shown after each statement if there are any. This option applies to interactive and batch mode. This option was added in MySQL 5.0.6.
--sigint-ignore Ignore SIGINT signals (typically the result of typing Control-C).
--silent, -s Silent mode. Produce less output. This option can be given multiple times to produce less and less output.

This option results in nontabular output format and escaping of special characters. Escaping may be disabled by using raw mode; see the description for the --raw option.
--skip-column-names, -N Do not write column names in results. The short format, -N is deprecated, use the long format instead.
--skip-line-numbers, -L Do not write line numbers for errors. Useful when you want to compare result files that include error messages. The short format, -L is deprecated, use the long format instead.
--socket=path, -S path For connections to localhost, the Unix socket file to use, or, on Windows, the name of the named pipe to use.
--ssl* Options that begin with --ssl specify whether to connect to the server via SSL and indicate where to find SSL keys and certificates.
--table, -t Display output in table format. This is the default for interactive use, but can be used to produce table output in batch mode.
--tee=file_name Append a copy of output to the given file. This option does not work in batch mode.
--unbuffered, -n Flush the buffer after each query.
--user=user_name, -u user_nameThe MySQL user name to use when connecting to the server.
--verbose, -v Verbose mode. Produce more output about what the program does. This option can be given multiple times to produce more and more output. (For example, -v -v -v produces table output format even in batch mode.)
--version, -V Display version information and exit.
--vertical, -E Print query output rows vertically (one line per column value). Without this option, you can specify vertical output for individual statements by terminating them with \G.
--wait, -w If the connection cannot be established, wait and retry instead of aborting.
--xml, -X Produce XML output.

mysql commands

Below are commands that can be used while in mysql at the mysql> prompt.

? (\?) Synonym for help'.
clear (\c) Clear command.
connect (\r) Reconnect to the server. Optional arguments are db and host.
delimiter (\d) Set statement delimiter.
edit (\e) Edit command with $EDITOR.
ego (\G) Send command to mysql server, display result vertically.
exit (\q) Exit mysql. Same as quit.
go (\g) Send command to mysql server.
help (\h) Display this help.
nopager (\n) Disable pager, print to stdout.
notee (\t) Don't write into outfile.
pager (\P) Set PAGER [to_pager]. Print the query results via PAGER.
print (\p) Print current command.
prompt (\R) Change your mysql prompt,
quit (\q) Quit mysql.
rehash (\#) Rebuild completion hash.
source (\.) Execute an SQL script file. Takes a file name as an argument.
status (\s) Get status information from the server.
system (\!) Execute a system shell command.
tee (\T) Set outfile [to_outfile]. Append everything into given outfile.
use (\u) Use another database. Takes database name as argument.
charset (\C) Switch to another charset. Might be needed for processing binlog with multi-byte charsets.
warnings (\W) Show warnings after every statement.
nowarning (\w) Don't show warnings after every statement.

Examples

mysqldump -u hope -p -h localhost hope_SMF > smf.sql

Running the above command at the command prompt would backup the database "hope_SMF" to the smf.sql file after the username and password were verified.

Running status while at the mysql> prompt would give you MySQL status results similar to what is shown below.

mysql> status -------------- mysql Ver 14.12 Distrib 5.0.87, for unknown-linux-gnu (x86_64) using readline 5.1 Connection id: 1121703 Current database: Current user: root@localhost SSL: Not in use Current pager: stdout Using outfile: '' Using delimiter: ; Server version: 5.0.87-community MySQL Community Edition (GPL) Protocol version: 10 Connection: Localhost via UNIX socket Server characterset: latin1 Db characterset: latin1 Client characterset: latin1 Conn. characterset: latin1 UNIX socket: /var/lib/mysql/mysql.sock Uptime: 9 days 11 hours 4 min 16 sec Threads: 2 Questions: 19501870 Slow queries: 25 Opens: 62646 Flush tables: 1 Open tables: 64 Queries per second avg: 23.857 -------------- 

Related commands

myisamchk