mysql
mysqloptions[database]
The mysql client can be used to interact with MySQL in terminal or
monitor mode. To enter monitor mode, enter something like the
following from the command line:
mysql -u russell -p
If the MySQL server is running, the client will prompt the user
for a password (thanks to the -p option). Once in
monitor mode, you can enter SQL statements to view or to change data
as well as the status of the server.
As an alternative to monitor mode, when performing
straightforward tasks in MySQL, you can still use the
mysql client from the command line. For instance,
to execute a batch file that contains several SQL statements that will
insert data into a database, you could do something like this:
mysql -u russell -pmy_pwd db1 < stuff.sqlIn this example, the password is given so that the user isn’t
prompted. It’s entered immediately after the -p
option without a space in between. Although including the password on
the command line poses a security risk for interactive use, it’s a
valuable feature for using mysql in scripts.
Next, the database name db1 is given. The
Unix redirect (the less-than sign) tells the shell to input the test
file stuff.sql to the command. When the client
has finished processing the text file, the user is returned to the
command prompt.
To handle even smaller tasks, you can execute a single SQL
command against the database by running mysql with
the --execute or -e
option.
Several options may be given when calling the
mysql client at the command line. They can also be
included in the options file (my.cnf or
my.ini, depending on your system) under the group
heading of [client]. If used in the options file,
the leading double-dashes are not included. The options are listed
alphabetically here:
--auto-rehashThis option generates a hash of table and column names to complete the names for users when typing in monitor mode; users invoke autocompletion by pressing the Tab key after having entered the first few letters of the name.
--batch, -BThis option causes the client to display data selected
with fields separated by tabs and rows by carriage returns. The
client won’t prompt the user, won’t display error messages to
the stdout, and won’t save to the history
file.
--character-sets-dir=pathThis option specifies the local directory containing character sets for the client to use.
--column-namesThis option instructs the client to return the names of columns in a results set. This is more relevant when executing SQL statements from the command line.
--column-type-info,
-mThis option instructs the client to return the metadata for columns in a results set. This option is available as of version 5.1.14 of MySQL; the short form is available as of version 5.1.21.
--compress, -CThis option instructs the client to compress data passed between it and the server if supported.
--database=database, -D
databaseThis option sets the default database for the client to
use. This is equivalent to executing the USE
statement.
--debug[=options],
-#[options]This option instructs the client to record debugging
information to the log file specified. The set of flags used by
default is d:t:o,logname. See Table 16-1 at the end of the list of
options for mysqldump in the next chapter for
an explanation of these flags and others that may be
used.
--debug-checkThis option causes the client to display debugging information when finished. This option is available as of version 5.1.21 of MySQL.
--debug-info,
-TThis option adds debugging, CPU usage, and memory usage information to the log when the utility ends.
--default-character-sets-dir=pathThis option specifies the local directory that contains
the default character sets for the client to use. Enter
SHOW CHARACTER SET; on the server for a list
of character sets available.
--defaults-group-suffix=valueThe client looks for options in the options file under the
group headings of [mysql] and
[client]. Use this option to specify option
groups that the client is to use, based on their suffixes. For
instance, the value given might be just
_special so that groups such as
[mysql_special] and
[client_special] will be included.
--delimiter=string,
-F stringThis option use this option to specify the delimiter used to terminate each SQL statement when entered into the client. By default, the client expects a semicolon.
--execute='statement',
-e
'statement'This option executes the SQL statement contained in single or double quotes, then terminates the client.
--force, -fThis option makes the client continue executing or processing a statement even if there are SQL errors.
--help, -?This option displays basic help information.
--hostname=host,
-h hostThis option specifies the hostname or IP address of the MySQL server. The default is localhost, which connects to a server on the same system as the client.
--html, -HThis option instructs the client to return results in an HTML format when executing an SQL statement at the command line or from a file containing SQL statements.
--ignore-spaces,
-iThis option instructs the client to ignore spaces after
function names (e.g., CUR_DATE()) when executing
SQL statements at the command line or from a text file
containing SQL statements.
--line-numbersWhen the client is accepting SQL statements from an input
file, this option instructs the client to display the line
number of an SQL statement that has returned an error. This is
the default option; use --skip-line-numbers
to disable this option.
--local-infile[={0|1}]The SQL statement LOAD DATA INFILE
imports data into a database from a file. That file could be
located on the server or on the computer in which the client is
running (i.e., locally). To indicate that a file is local, you
would add the LOCAL flag to that statement.
This option sets that flag: a value of 1 enables the
LOCAL, whereas a value of 0 indicates that
the file is on the server. If the server is set so it imports
data only from files on the server, this option will have no
effect.
--named-commands,
-GThis option permits named commands on the client. See the
next section for this client program for a description of
commands. Enter help or \h
from the mysql client to get a list of
them. This option is enabled by default. To disable it, use the
--skip-named-commands
option.
--no-auto-rehash,
-AAutomatic rehashing is normally used to let the user complete table and column names when typing in monitor mode by pressing the Tab key after having entered the first few letters of the name. This option disables autocompletion and thereby decreases the startup time of the client. This option is deprecated as of version 4 of MySQL.
--no-beepThis option instructs client not to emit a warning sound for errors.
--no-named-commandsThis option disables named commands on the client, except
when at the start of a line (i.e., named commands cannot appear
in the middle of an SQL statement). This option is enabled by
default. See the description of the
--named-commands option and the following
section for more information.
--no-teeThis option instructs the client not to write results to a file.
--one-database,
-oThis option instructs the client to execute SQL statements
only for the default database (set by the
--database option) and to ignore SQL
statements for other databases.
--pager[=utility]With this option, on a Unix type of system, you can pipe
the results of an SQL statement executed from the command line
to a pager utility (e.g., more) that will
allow you to view the results one page at a time and possibly
scroll up and down through the results. If this option is given
without specifying a particular pager utility, the value of the
environment variable PAGER will be used. This
option is enabled by default. Use the
--skip-pager option to disable it.
--password[=password],
-p[password]This option provide the password to give to the MySQL
server. No spaces are allowed between the -p
and the password. If this option is entered without a password,
the user will be prompted for one.
--port=port,
-P portThis option specifies the socket port to use for connecting to the server. The default is 3306. If you run multiple daemons for testing or other purposes, you can use different ports for each by setting this option.
--prompt=stringThis option sets the prompt for monitor mode to the given
string. By default, it’s set to
mysql>.
--protocol=protocolThis option specifies the protocol to use when connecting
to the server. The choices are TCP,
SOCKET, PIPE, and
MEMORY.
--quick, -qThis option causes the client to retrieve and display data one row at a time instead of buffering the entire results set before displaying data. With this option, the history file isn’t used and it may slow the server if the output is suspended.
--raw, -rFor data that may contain characters that would normally
be converted in batch mode to an escape-sequence equivalent
(e.g., newline to \n), this option may be
used to have the client print out the characters without
converting them.
--reconnectThis option instructs the client to attempt to reconnect
to the server if the connection is lost. The client tries only
once, though. This is enabled by default. To disable it, use
--skip-reconnect. To
make the client wait until the server is available, use --wait.
--safe-updates,
-UThis option helps prevent inadvertent deletion of multiple
and possibly all rows in a table. It requires that when the
DELETE or UPDATE
statements are used, a WHERE clause be given
with a key column and value. If this option is included in the
options file, using it at the command line when starting the
client will disable it.
--secure-authThis option prevents authentication of users with passwords created prior to version 4.1 of MySQL or connecting to servers that permit the old format.
--set-variable
var=value,
-o
var=valueThis option sets a server variable. Enter mysql
--help for the current values for a particular
server’s variables.
--show-warningsThis option instructs the client not to suppress warning messages, but to display them after an SQL statement is executed in which a warning is generated, even if there was no error.
--silent, -sThis option suppresses all messages except for error messages. Enter the option multiple times to further reduce the types of messages returned.
--skip-column-namesThis option instructs the client not to return column names in the results.
--skip-line-numbersWhen the client is accepting SQL statements from an input
file, this option instructs the client not to display the line
number of an SQL statement that has returned an error. This
disables --line-numbers, the default.
--skip-named-commandsThis option disables named commands on the client. See the
description of the --named-commands option and the
following section for more information.
--skip-pagerThis option disables paged results on Unix types of
systems. See the --pager option for more
information.
--skip-reconnectThis option instructs the client not to attempt to
reconnect to the server if the connection is lost. It disables
the default option --reconnect.
--skip-sslThis option specifies that an SSL connection should not be used, if SSL is enabled by default.
--socket=socket,
-S socketThis option provides the path and name of the server’s socket file on Unix systems, or the named pipe on Windows systems.
--sslThis option specifies that an SSL connection should be
used. It requires the server to have SSL enabled. If this option
is enabled on the utility by default, use --skip-ssl to disable it.
--ssl-ca=pem_fileThis option specifies the name of the file (i.e., the pem file) containing a list of trusted SSL CAs.
--ssl-capath=pathThis option specifies the path to the trusted certificates file (i.e., the pem file).
--ssl-cert=filenameThis option specifies the name of the SSL certificate file to use for SSL connections.
--ssl-cipher=ciphersThis option gives a list of ciphers that may be used for SSL encryption.
--ssl-key=filenameThis option specifies the SSL key file to use for secure connections.
--ssl-verify-server-certThis option verifies the client’s certificate against the server’s certificate for the client at startup. It is available as of version 5.1.11 of MySQL.
--table, -tThis option displays results from a query in ASCII format,
which is the format normally used in monitor mode. The
alternative is the --xml option.
--tee=filenameThis option instructs the client to write results to the given file. You can include an absolute or relative pathname, or a simple filename. This option doesn’t work in batch mode.
--unbuffered,
-nThis option flushes the memory buffer after each query is performed.
--user=user,
-u userThis option instructs the client to access MySQL with a username different from the current system user.
--verbose, -vThis option displays more information. Use
-vv or -vvv to increase
verbosity.
--version, -VThis option displays the version of the utility.
--verticalThis option displays results in a vertical format instead
of putting each row of data on a single line. This is similar to
using the end of \G of an SQL statement in
monitor mode.
--wait, -wIf the client cannot connect to the server, this option tells the client to wait and retry repeatedly until it can connect.
--xml, -X