![]() |
|
|
Go to the first, previous, next, last section, table of contents.
8 MySQL Client and Utility ProgramsThere are many different MySQL client programs that connect to the server to access databases or perform administrative tasks. Other utilities are available as well. These do not communicate with the server but perform MySQL-related operations. This chapter provides a brief overview of these programs and then a more detailed description of each one. The descriptions indicate how to invoke the programs and the options they understand. See section 4 Using MySQL Programs for general information on invoking programs and specifying program options. 8.1 Overview of the Client-Side Scripts and UtilitiesThe following list briefly describes the MySQL client programs and utilities:
Each MySQL program takes many different options. However, every MySQL program
provides a
MySQL clients that communicate with the server using the
Use of You can override the default option values or values specified in environment variables for all standard programs by specifying options in an option file or on the command line. section 4.3 Specifying Program Options. 8.2
|
| Option | Description |
\v | The server version |
\d | The current database |
\h | The server host |
\p | The current TCP/IP host |
\u | Your username |
\U | Your full user_name@host_name account name
|
\\ | A literal `\' backslash character |
\n | A newline character |
\t | A tab character |
\ | A space (a space follows the backslash) |
\_ | A space |
\R | The current time, in 24-hour military time (0-23) |
\r | The current time, standard 12-hour time (1-12) |
\m | Minutes of the current time |
\y | The current year, two digits |
\Y | The current year, four digits |
\D | The full current date |
\s | Seconds of the current time |
\w | The current day of the week in three-letter format (Mon, Tue, ...) |
\P | am/pm |
\o | The current month in numeric format |
\O | The current month in three-letter format (Jan, Feb, ...) |
\c | A counter that increments for each statement you issue |
\S | Semicolon |
\' | Single quote |
\" | Double quote |
`\' followed by any other letter just becomes that letter.
If you specify the prompt command with no argument, mysql resets
the prompt to the default of mysql>.
You can set the prompt in several ways:
MYSQL_PS1 environment variable to a prompt string. For
example:
shell> export MYSQL_PS1="(\u@\h) [\d]> "
prompt option in the [mysql] group of any MySQL
option file, such as `/etc/my.cnf' or the `.my.cnf' file in
your home directory.
For example:
[mysql] prompt=(\\u@\\h) [\\d]>\\_In this example, note that the backslashes are doubled. If you set the prompt using the
prompt option in an option
file, it is advisable to double the backslashes when using the special
prompt options. There is
some overlap in the set of allowable prompt options and the set of special
escape sequences that are recognized in option files.
(These sequences are listed in section 4.3.2 Using Option Files.)
The overlap may cause you problems if you use single backslashes.
For example, \s will be interpreted as a space rather than as the
current seconds value. The following example shows how to define a prompt
within an option file to
include the current time in HH:MM:SS> format:
[mysql] prompt="\\r:\\m:\\s> "
--prompt option on the command line to mysql.
For example:
shell> mysql --prompt="(\u@\h) [\d]> " (user@host) [database]>
prompt (or
\R) command. For example:
mysql> prompt (\u@\h) [\d]>\_ PROMPT set to '(\u@\h) [\d]>\_' (user@host) [database]> (user@host) [database]> prompt Returning to default PROMPT of mysql> mysql>
The mysql client typically is used interactively, like this:
shell> mysql db_name
However, it's also possible to put your SQL statements in a file and then
tell mysql to read its input from that file. To do so, create a text
file `text_file' that contains the statements you wish to execute.
Then invoke mysql as shown here:
shell> mysql db_name < text_file
You can also start your text file with a USE db_name statement. In
this case, it is unnecessary to specify the database name on the command
line:
shell> mysql < text_file
If you are already running mysql, you can execute an SQL
script file using the source or \. command:
mysql> source filename mysql> \. filename
Sometimes you may want your script to display progress information to the user; for this you can insert some lines like
SELECT '<info_to_display>' AS ' ';
which will output <info_to_display>.
For more information about batch mode, see section 3.5 Using mysql in Batch Mode.
mysql Tips
This section describes some techniques that can help you use mysql more
effectively.
Some query results are much more readable when displayed vertically, instead of in the usual horizontal table format. Queries can be displayed vertically by terminating the query with \G instead of a semicolon. For example, longer text values that include newlines often are much easier to read with vertical output:
mysql> SELECT * FROM mails WHERE LENGTH(txt) < 300 LIMIT 300,1\G
*************************** 1. row ***************************
msg_nro: 3068
date: 2000-03-01 23:29:50
time_zone: +0200
mail_from: Monty
reply: monty@no.spam.com
mail_to: "Thimble Smith" <tim@no.spam.com>
sbj: UTF-8
txt: >>>>> "Thimble" == Thimble Smith writes:
Thimble> Hi. I think this is a good idea. Is anyone familiar
Thimble> with UTF-8 or Unicode? Otherwise, I'll put this on my
Thimble> TODO list and see what happens.
Yes, please do that.
Regards,
Monty
file: inbox-jani-1
hash: 190402944
1 row in set (0.09 sec)
--safe-updates Option
For beginners, a useful startup option is --safe-updates (or
--i-am-a-dummy, which has the same effect). This option was
introduced in MySQL 3.23.11. It is helpful for cases when you might
have issued a DELETE FROM tbl_name statement but forgotten the
WHERE clause. Normally, such a statement will delete all rows from the
table. With --safe-updates, you can delete rows only by specifying
the key values that identify them. This helps prevent accidents.
When you use the --safe-updates option, mysql issues the
following statement when it connects to the MySQL server:
SET SQL_SAFE_UPDATES=1,SQL_SELECT_LIMIT=1000, SQL_MAX_JOIN_SIZE=1000000;
See section 13.5.3 SET Syntax.
The SET statement has the following effects:
UPDATE or DELETE statement
unless you specify a key constraint in the WHERE clause or provide a
LIMIT clause (or both).
For example:
UPDATE tbl_name SET not_key_column=# WHERE key_column=#; UPDATE tbl_name SET not_key_column=# LIMIT 1;
SELECT results are automatically limited to 1,000 rows
unless the statement includes a LIMIT clause.
SELECT statements that will probably need to examine
more than 1,000,000 row combinations are aborted.
To specify limits other than 1,000 and 1,000,000, you can override the
defaults by using --select_limit and --max_join_size options:
shell> mysql --safe-updates --select_limit=500 --max_join_size=10000
mysql Auto-Reconnect
If the mysql client loses its connection to the server while sending
a query, it will immediately and automatically try to reconnect once to the
server and send the query again. However, even if mysql succeeds in
reconnecting, your first connection has ended and all your previous session
objects and settings are lost: temporary tables, the autocommit mode, and
user and session variables. This behavior may be dangerous for you, as in
the following example where the server was shut down and restarted without
you knowing it:
mysql> SET @a=1; Query OK, 0 rows affected (0.05 sec) mysql> INSERT INTO t VALUES(@a); ERROR 2006: MySQL server has gone away No connection. Trying to reconnect... Connection id: 1 Current database: test Query OK, 1 row affected (1.30 sec) mysql> SELECT * FROM t; +------+ | a | +------+ | NULL | +------+ 1 row in set (0.05 sec)
The @a user variable has been lost with the connection, and after
the reconnection it is undefined. If it is important to have mysql
terminate with an error if the connection has been lost, you can start the
mysql client with the --skip-reconnect option.
mysqladmin, Administering a MySQL Server
mysqladmin is a client for performing administrative operations.
You can use it to check the server's configuration and current status, create
and drop databases, and more.
Invoke mysqladmin like this:
shell> mysqladmin [options] command [command-option] command ...
mysqladmin supports the following commands:
create db_name
drop db_name
extended-status
flush-hosts
flush-logs
flush-privileges
reload).
flush-status
flush-tables
flush-threads
kill id,id,...
old-password new-password
password command but stores the password using
the old (pre-4.1) password-hashing format. This command was added in MySQL
4.1.0.
password new-password
new-password
for the account that you use with mysqladmin for connecting to the
server.
If new-password contains spaces or other characters that are special
to your command interpreter, you will need to enclose it within quotes.
On Windows, be sure to use double quotes rather than single quotes;
single quotes will be not be stripped from the password, they will be
interpreted as part of the password. For example:
shell> mysqladmin password "my new password"
ping
mysqladmin is 0 if the server is running,
1 if it is not. Beginning with MySQL 4.0.22, the status is 0 even in case
of an error such as Access denied, because that means the server is
running but disallowed the connection, which is different from the server
not running.
processlist
SHOW PROCESSLIST statement.
If the --verbose option is given, the output is like that of
SHOW FULL PROCESSLIST.
reload
refresh
shutdown
start-slave
status
stop-slave
variables
version
All commands can be shortened to any unique prefix. For example:
shell> mysqladmin proc stat +----+-------+-----------+----+-------------+------+-------+------+ | Id | User | Host | db | Command | Time | State | Info | +----+-------+-----------+----+-------------+------+-------+------+ | 6 | monty | localhost | | Processlist | 0 | | | +----+-------+-----------+----+-------------+------+-------+------+ Uptime: 10077 Threads: 1 Questions: 9 Slow queries: 0 Opens: 6 Flush tables: 1 Open tables: 2 Memory in use: 1092K Max memory used: 1116K
The mysqladmin status command result displays the following values:
Uptime
Threads
Questions
Slow queries
long_query_time
seconds. See section 5.9.5 The Slow Query Log.
Opens
Flush tables
flush ..., refresh, and reload commands the
server has executed.
Open tables
Memory in use
mysqld code.
This value is displayed only when MySQL has been compiled with
--with-debug=full.
Maximum memory used
mysqld code.
This value is displayed only when MySQL has been compiled with
--with-debug=full.
If you execute mysqladmin shutdown when connecting to a local server
using a Unix socket file, mysqladmin waits until the server's process
ID file has been removed, to ensure that the server has stopped properly.
mysqladmin supports the following options:
--help, -?
--character-sets-dir=path
--compress, -C
--count=#, -c #
--sleep
(-i).
--debug[=debug_options], -# [debug_options]
'd:t:o,file_name'.
The default is 'd:t:o,/tmp/mysqladmin.trace'.
--default-character-set=charset
--force, -f
drop database command.
With multiple commands, continue even if an error occurs.
--host=host_name, -h host_name
--password[=password], -p[password]
-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 will be
prompted for one.
--port=port_num, -P port_num
--protocol={TCP | SOCKET | PIPE | MEMORY}
--relative, -r
-i. Currently, this option works only with the extended-status
command.
--silent, -s
--sleep=delay, -i delay
--socket=path, -S path
--user=user_name, -u user_name
--verbose, -v
--version, -V
--vertical, -E
--relative, but
prints output vertically.
--wait[=#], -w[#]
You can also set the following variables by using --var_name=value
options:
connect_timeout
shutdown_timeout
It is also possible to set variables by using
--set-variable=var_name=value or -O var_name=value
syntax. However, this syntax is deprecated as of MySQL 4.0.
mysqlbinlog Binary Log Utility
The binary log files that the server generates are written in binary format.
To examine these files in text format, use the mysqlbinlog utility.
It is available as of MySQL 3.23.14.
Invoke mysqlbinlog like this:
shell> mysqlbinlog [options] log-file ...
For example, to display the contents of the binary log `binlog.000003', use this command:
shell> mysqlbinlog binlog.0000003
The output includes all statements contained in `binlog.000003', together with other information such as the time each statement took, the thread ID of the client that issued it, the timestamp when it was issued, and so forth.
Normally, you use mysqlbinlog to read binary log files directly and
apply them to the local MySQL server. It is also possible to read binary
logs from a remote server by using the --read-from-remote-server
option.
When you read remote binary logs, the connection parameter options can be
given to indicate how to connect to the server, but they are ignored unless
you also specify the --read-from-remote-server option. These options
are --host, --password, --port, --protocol,
--socket, and --user.
You can also use mysqlbinlog to read relay log files written by a
slave server in a replication setup. Relay logs have the same format as
binary log files.
The binary log is discussed further in section 5.9.4 The Binary Log.
mysqlbinlog supports the following options:
--help, -?
--database=db_name, -d db_name
--force-read, -f
mysqlbinlog reads a binary log event that
it does not recognize, it prints a warning, ignores the event, and continues.
Without this option, mysqlbinlog stops if it reads such an event.
--host=host_name, -h host_name
--local-load=path, -l path
LOAD DATA INFILE in the specified
directory.
--offset=N, -o N
--password[=password], -p[password]
-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 will be
prompted for one.
--port=port_num, -P port_num
--position=N, -j N
--start-position instead (starting from MySQL 4.1.4).
--protocol={TCP | SOCKET | PIPE | MEMORY}
--read-from-remote-server, -R
--host, --password, --port,
--protocol,
--socket, and --user.
--result-file=name, -r name
--short-form, -s
--socket=path, -S path
--start-datetime=datetime
datetime argument. Available as of MySQL 4.1.4.
--stop-datetime=datetime
datetime argument. Available as of MySQL 4.1.4. Useful
for point-in-time recovery.
--start-position=N
N argument. Available as of MySQL 4.1.4 (previously named
--position).
--stop-position=N
N argument. Available as of MySQL 4.1.4.
--to-last-log, -t
--read-from-remote-server. Available as of MySQL 4.1.2.
--disable-log-bin, -D
--to-last-log option and are sending the output to the same
MySQL server. This option also is useful when restoring after a crash
to avoid duplication of the statements you already have logged. Note:
This option requires that you have the SUPER privilege. Available
as of MySQL 4.1.8.
--user=user_name, -u user_name
--version, -V
You can also set the following variable by using --var_name=value
options:
open_files_limit
You can pipe the output of mysqlbinlog into a mysql client to
execute the statements contained in the binary log. This is used to recover
from a crash when you have an old backup (see section 5.7.1 Database Backups):
shell> mysqlbinlog hostname-bin.000001 | mysql
Or:
shell> mysqlbinlog hostname-bin.[0-9]* | mysql
You can also redirect the output of mysqlbinlog to a text file
instead, if you need to modify the statement log first (for example, to
remove statements that you don't want to execute for some reason). After
editing the file, execute the statements that it contains by using it as
input to the mysql program.
mysqlbinlog has the --position option, which prints only
those statements with an offset in the binary log greater than or equal to
a given position (the given position must match the start of one event). It
also has options to stop or start when it sees an event of a given date and
time. This enables you to perform point-in-time recovery using the
--stop-datetime option (to be able to say, for example, "roll forward
my databases to how they were today at 10:30 AM").
If you have more than one binary log to execute on the MySQL server, the safe method is to process them all using a single connection to the server. Here is an example that demonstrates what may be unsafe:
shell> mysqlbinlog hostname-bin.000001 | mysql # DANGER!! shell> mysqlbinlog hostname-bin.000002 | mysql # DANGER!!
Processing binary logs this way using different connections to the server
will cause problems if the first log file contains a CREATE TEMPORARY
TABLE statement and the second log contains a statement that uses the
temporary table. When the first mysql process terminates, the server
will drop the temporary table. When the second mysql process attempts
to use the table, the server will report ``unknown table.''
To avoid problems like this, use a single connection to execute the contents of all binary logs that you want to process. Here is one way to do that:
shell> mysqlbinlog hostname-bin.000001 hostname-bin.000002 | mysql
Another approach is to do this:
shell> mysqlbinlog hostname-bin.000001 > /tmp/statements.sql shell> mysqlbinlog hostname-bin.000002 >> /tmp/statements.sql shell> mysql -e "source /tmp/statements.sql"
In MySQL 3.23, the binary log did not contain the data to load for
LOAD DATA INFILE statements. To execute such a statement from a
binary log file, the original data file was needed. Starting from MySQL
4.0.14, the binary log does contain the data, so mysqlbinlog can
produce output that reproduces the LOAD DATA INFILE operation without
the original data file. mysqlbinlog copies the data to a temporary
file and writes a LOAD DATA LOCAL INFILE statement that refers to the
file. The default location of the directory where these files are written
is system-specific. To specify a directory explicitly, use the
--local-load option.
Because mysqlbinlog converts LOAD DATA INFILE statements to
LOAD DATA LOCAL INFILE statements (that is, it adds LOCAL),
both the client and the server that you use to process the statements must be
configured to allow LOCAL capability.
See section 5.4.4 Security Issues with LOAD DATA LOCAL.
Warning: The temporary files created for LOAD DATA LOCAL
statements are not automatically deleted
because they are needed until you actually execute those statements. You
should delete the temporary files yourself after you no longer need the
statement log. The files can be found in the temporary file directory and
have names like `original_file_name-#-#'.
In the future, we will fix this problem by allowing mysqlbinlog
to connect directly to a mysqld server. Then it will be possible
to safely remove the log files automatically as soon as the LOAD DATA
INFILE statements have been executed.
Before MySQL 4.1, mysqlbinlog could not prepare output suitable for
mysql if the binary log contained interlaced statements originating
from different clients that used temporary tables of the same name. This is
fixed in MySQL 4.1. However, the problem still existed for LOAD DATA
INFILE statements until it was fixed in MySQL 4.1.8.
mysqlcc, the MySQL Control Center
mysqlcc, the MySQL Control Center, is a platform-independent client that
provides a graphical user interface (GUI) to the MySQL database server.
It supports interactive use, including syntax highlighting and tab completion.
It provides database and table management, and allows server administration.
mysqlcc is now deprecated and it is recommended that users choose the new
MySQL Administrator and MySQL Query Browser, found at http://dev.mysql.com/downloads/.
Currently, mysqlcc runs on Windows and Linux platforms.
Invoke mysqlcc by double-clicking its icon in a graphical environment.
From the command line, invoke it like this:
shell> mysqlcc [options]
mysqlcc supports the following options:
--help, -?
--blocking_queries, -b
--compress, -C
--connection_name=name, -c name
--server.
--database=db_name, -d db_name
--history_size=#, -H #
--host=host_name, -h host_name
--local-infile[={0|1}]
LOCAL capability for LOAD DATA INFILE.
With no value, the option enables LOCAL. It 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.
--password[=password], -p[password]
-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 will be
prompted for one.
--plugins_path=name, -g name
--port=port_num, -P port_num
--query, -q
--register, -r
--server=name, -s name
--socket=path, -S path
--syntax, -y
--syntax_file=name, -Y name
--translations_path=name, -T name
--user=user_name, -u user_name
--version, -V
You can also set the following variables by using --var_name=value
options:
connect_timeout
max_allowed_packet
max_join_size
net_buffer_length
select_limit
SELECT statements.
(Default value is 1,000.)
It is also possible to set variables by using
--set-variable=var_name=value or -O var_name=value
syntax. However, this syntax is deprecated as of MySQL 4.0.
mysqlcheck Table Maintenance and Repair Program
The mysqlcheck client checks and repairs MyISAM tables. It
can also optimize and analyze tables. mysqlcheck is available as of
MySQL 3.23.38.
mysqlcheck is similar in function to myisamchk, but works
differently. The main operational difference is that mysqlcheck must
be used when the mysqld server is running, whereas myisamchk
should be used when it is not. The benefit of using mysqlcheck is
that you do not have to stop the server to check or repair your tables.
mysqlcheck uses the SQL statements CHECK TABLE, REPAIR
TABLE, ANALYZE TABLE, and OPTIMIZE TABLE in a convenient way
for the user. It determines which statements to use for the operation you want
to perform, then sends the statements to the server to be executed.
There are three general ways to invoke mysqlcheck:
shell> mysqlcheck [options] db_name [tables] shell> mysqlcheck [options] --databases DB1 [DB2 DB3...] shell> mysqlcheck [options] --all-databases
If you don't name any tables or use the --databases or
--all-databases option, entire databases will be checked.
mysqlcheck has a special feature compared to the other clients. The
default behavior of checking tables (--check) can be changed by renaming
the binary. If you want to have a tool that repairs tables by default, you
should just make a copy of mysqlcheck named mysqlrepair, or make
a symbolic link to mysqlcheck named mysqlrepair. If you invoke
mysqlrepair, it will repair tables by command.
The following names can be used to change mysqlcheck default behavior:
mysqlrepair | The default option will be --repair
|
mysqlanalyze | The default option will be --analyze
|
mysqloptimize | The default option will be --optimize
|
mysqlcheck supports the following options:
--help, -?
--all-databases, -A
--databases option and naming all the databases on the command line.
--all-in-1, -1
--analyze, -a
--auto-repair
--character-sets-dir=path
--check, -c
--check-only-changed, -C
--compress
--databases, -B
--debug[=debug_options], -# [debug_options]
'd:t:o,file_name'.
--default-character-set=charset
--extended, -e
--fast, -F
--force, -f
--host=host_name, -h host_name
--medium-check, -m
--extended operation.
This finds only 99.99% of all errors, which should be good enough in most cases.
--optimize, -o
--password[=password], -p[password]
-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 will be
prompted for one.
--port=port_num, -P port_num
--protocol={TCP | SOCKET | PIPE | MEMORY}
--quick, -q
--repair, -r
--silent, -s
--socket=path, -S path
--tables
--databases or -B option. All arguments
following the option are regarded as table names.
--user=user_name, -u user_name
--verbose, -v
--version, -V
mysqldump Database Backup Program
The mysqldump client can be used
to dump a database or a collection of databases for backup or for
transferring the data to another SQL server (not necessarily a MySQL
server). The dump will contain SQL statements to create the table
and/or populate the table.
If you are doing a backup on the server, and your tables all are
MyISAM tables, you could consider using
the mysqlhotcopy instead (faster backup, faster
restore). See section 8.9 The mysqlhotcopy Database Backup Program.
There are three general ways to invoke mysqldump:
shell> mysqldump [options] db_name [tables] shell> mysqldump [options] --databases DB1 [DB2 DB3...] shell> mysqldump [options] --all-databases
If you don't name any tables or use the --databases or
--all-databases option, entire databases will be dumped.
To get a list of the options your version of mysqldump supports,
execute mysqldump --help.
If you run mysqldump without the --quick or
--opt option, mysqldump will load the whole result set into
memory before dumping the result. This will probably be a problem if
you are dumping a big database. As of MySQL 4.1, --opt is on by
default, but can be disabled with --skip-opt.
If you are using a recent copy of the mysqldump program
and you are going to generate a dump that will be reloaded into a very old MySQL
server, you should not use the --opt or -e options.
Out-of-range numeric values such as -inf and inf, as well
as NaN (not-a-number) values are dumped by mysqldump as NULL.
You can see this using the following sample table:
mysql> CREATE TABLE t (f DOUBLE); mysql> INSERT INTO t VALUES(1e+111111111111111111111); mysql> INSERT INTO t VALUES(-1e111111111111111111111); mysql> SELECT f FROM t; +------+ | f | +------+ | inf | | -inf | +------+
For this table, mysqldump produces the following data output:
-- -- Dumping data for table `t` -- INSERT INTO t VALUES (NULL); INSERT INTO t VALUES (NULL);
The significance of this behavior is that if you dump and restore the
table, the new table has contents that differ from the original contents.
Note that since MySQL 4.1.2 you cannot insert inf in the table,
so this mysqldump behavior is only relevant when you deal
with old servers.
mysqldump supports the following options:
--help, -?
--add-drop-table
DROP TABLE statement before each CREATE TABLE statement.
--add-locks
LOCK TABLES and UNLOCK TABLES
statements. This results in faster inserts when the dump file is reloaded.
See section 7.2.14 Speed of INSERT Statements.
--all-databases, -A
--databases option and naming all the databases on the command line.
--allow-keywords
--comments[={0|1}]
0, suppresses additional information in the dump file such
as program version, server version, and host. --skip-comments has the
same effect as --comments=0. The default value is 1 to not
suppress the extra information. New in MySQL 4.0.17.
--compatible=name
name can be
ansi,
mysql323,
mysql40,
postgresql,
oracle,
mssql,
db2,
maxdb,
no_key_options,
no_table_options,
or
no_field_options.
To use several values, separate them by commas.
These values have the same meaning as the corresponding options for setting
the server SQL mode.
See section 5.2.2 The Server SQL Mode.
This option requires a server version of 4.1.0 or higher.
With older servers, it does nothing.
--complete-insert, -c
INSERT statements that include column names.
--compress, -C
--create-options
CREATE TABLE statements.
Before MySQL 4.1.2, use --all instead.
--databases, -B
USE db_name statement is included in the
output before each new database.
--debug[=debug_options], -# [debug_options]
'd:t:o,file_name'.
--default-character-set=charset
mysqldump from MySQL 4.1.2 or later
uses utf8; earlier versions use latin1.
--delayed
INSERT DELAYED statements.
--delete-master-logs
--first-slave.
It was added in MySQL 3.23.57 (for MySQL 3.23) and MySQL 4.0.13 (for MySQL
4.0).
--disable-keys, -K
INSERT statements with
/*!40000 ALTER TABLE tbl_name DISABLE KEYS */; and
/*!40000 ALTER TABLE tbl_name ENABLE KEYS */; statements.
This makes loading the dump file into a MySQL 4.0 server faster because the
indexes are created after all rows are inserted.
This option is effective only for MyISAM tables.
--extended-insert, -e
INSERT syntax that include several VALUES lists.
This results in a smaller dump file and speeds up inserts when the file is
reloaded.
--fields-terminated-by=...
--fields-enclosed-by=...
--fields-optionally-enclosed-by=...
--fields-escaped-by=...
--lines-terminated-by=...
-T option and have the same
meaning as the corresponding clauses for LOAD DATA INFILE.
See section 13.1.5 LOAD DATA INFILE Syntax.
--first-slave, -x
--lock-all-tables in MySQL 4.1.8.
--flush-logs, -F
--all-databases (or
-A) option, the logs are flushed for each database dumped.
The exception is when using --lock-all-tables
or --master-data:
In this case, the logs are flushed only once, corresponding to the moment
that all tables are locked. If you want your dump and the log flush to
happen at exactly the same moment, you should use --flush-logs
together with either --lock-all-tables or --master-data.
--force, -f
--host=host_name, -h host_name
localhost.
--hex-blob
'abc' becomes 0x616263. The affected columns are BINARY,
VARBINARY, and BLOB in MySQL 4.1 and up, and CHAR
BINARY, VARCHAR BINARY, and BLOB in MySQL 4.0. This option
was added in MySQL 4.0.23 and 4.1.8.
--lock-all-tables, -x
--single-transaction and --lock-tables. Added in
MySQL 4.1.8.
--lock-tables, -l
READ LOCAL to allow concurrent inserts in the case of MyISAM
tables. For InnoDB tables, --single-transaction is a much better
option, because it does not need to lock the tables at all.
Please note that when dumping multiple databases, --lock-tables locks
tables for each database separately. So, using this option will not guarantee
that the tables in the dump file will be logically consistent between
databases. Tables in different databases may be dumped in completely
different states.
--master-data[=value]
CHANGE MASTER statement
that will make a slave server start from the correct
position in the master's binary logs if you use this SQL dump of the master
to set up a slave.
If the option value is equal to 2, the CHANGE MASTER statement is
written as an SQL comment.
This is the default action if value is omitted.
value may be given as of MySQL 4.1.8; before that, do not specify
an option value.
The --master-data option turns on --lock-all-tables, unless
--single-transaction also is specified (in which case, a
global read lock is only acquired a short time at the beginning of the
dump. See also the description for
--single-transaction. In all cases,
any action on logs happens at the exact moment of the dump.
This option automatically turns off --lock-tables.
--no-create-db, -n
CREATE DATABASE /*!32312 IF NOT EXISTS*/
db_name statements that are otherwise included in the output if the
--databases or --all-databases option is given.
--no-create-info, -t
CREATE TABLE statements that re-create each dumped table.
--no-data, -d
--opt
--add-drop-table
--add-locks --create-options --disable-keys --extended-insert
--lock-tables --quick --set-charset. It should
give you a fast dump operation and produce a dump file that can be reloaded
into a MySQL server quickly. As of MySQL 4.1, --opt is on by default,
but can be disabled with --skip-opt. To disable only certain of the
options enabled by --opt, use their --skip forms; for example,
--skip-add-drop-table or --skip-quick.
--password[=password], -p[password]
-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 will be
prompted for one.
--port=port_num, -P port_num
--protocol={TCP | SOCKET | PIPE | MEMORY}
--quick, -q
mysqldump to retrieve rows for a table from
the server a row at a time rather than retrieving the entire row set
and buffering it in memory before writing it out.
--quote-names, -Q
ANSI_QUOTES option, names are
quoted within `"' characters.
As of MySQL 4.1.1, --quote-names is on by default,
but can be disabled with --skip-quote-names.
--result-file=file, -r file
--set-charset
SET NAMES default_character_set to the output. This option
is enabled by default. To suppress the SET NAMES statement, use
--skip-set-charset. This option was added in MySQL 4.1.2.
--single-transaction
BEGIN SQL statement before dumping data from
the server. It is mostly useful with InnoDB tables and the default
REPEATABLE READ transaction isolation level, because in this mode it
will dump the consistent state of the database at the time then
BEGIN was issued without blocking any applications.
When using this option, you should keep in mind that only InnoDB
tables will be dumped in a consistent state. For example, any MyISAM or
HEAP tables dumped while using this option may still change
state.
The --single-transaction option was added in MySQL 4.0.2.
This option is mutually exclusive with the --lock-tables option,
because LOCK TABLES causes any pending transactions to be committed
implicitly.
To dump big tables, you should combine this option with --quick.
--socket=path, -S path
localhost (which is the
default host).
--skip-comments
--comments option.
--tab=path, -T path
mysqldump
creates a `tbl_name.sql' file that contains the CREATE TABLE
statement that creates the table, and a `tbl_name.txt' file that
contains its data. The option value is the directory in which to write the
files.
By default, the `.txt' data files are formatted using tab characters
between column values and a newline at the end of each line. The format can
be specified explicitly using the --fields-xxx and
--lines--xxx options.
Note: This option should be used only when mysqldump is run
on the same machine as the mysqld server. You must use a MySQL
account that has the FILE privilege, and the server must have
permission to write files in the directory you specify.
--tables
--databases or -B option. All arguments
following the option are regarded as table names.
--user=user_name, -u user_name
--verbose, -v
--version, -V
--where='where-condition', -w 'where-condition'
WHERE condition. Note that
quotes around the condition are mandatory if it contains spaces or
characters that are special to your command interpreter.
Examples:
"--where=user='jimf'" "-wuserid>1" "-wuserid<1"
--xml, -X
You can also set the following variables by using --var_name=value
options:
max_allowed_packet
--extended-insert or --opt), mysqldump will create
rows up to max_allowed_packet length. If you increase this
variable, you should also ensure that the max_allowed_packet
variable in the MySQL server is at least this large.
net_buffer_length
It is also possible to set variables by using
--set-variable=var_name=value or -O var_name=value
syntax. However, this syntax is deprecated as of MySQL 4.0.
The most common use of mysqldump is probably for making a backup of
entire databases.
shell> mysqldump --opt db_name > backup-file.sql
You can read the dump file back into the server with:
shell> mysql db_name < backup-file.sql
Or:
shell> mysql -e "source /path-to-backup/backup-file.sql" db_name
mysqldump is also very useful for populating databases by copying
data from one MySQL server to another:
shell> mysqldump --opt db_name | mysql --host=remote_host -C db_name
It is possible to dump several databases with one command:
shell> mysqldump --databases db_name1 [db_name2 ...] > my_databases.sql
If you want to dump all databases, use the --all-databases option:
shell> mysqldump --all-databases > all_databases.sql
If tables are stored in the InnoDB storage engine, mysqldump provides a
way of making an online backup of these (see command below). This backup just
needs to acquire a global read lock on all tables (using FLUSH TABLES
WITH READ LOCK) at the beginning of the dump. As soon as this lock has been
acquired, the binary log coordinates are read and lock is released. So if and
only if one long updating statement is running when the FLUSH... is
issued, the MySQL server may get stalled until that long statement finishes,
and then the dump becomes lock-free. So if the MySQL server receives only
short (in the sense of "short execution time") updating statements, even if
there are plenty of them, the initial lock period should not be noticeable.
shell> mysqldump --all-databases --single-transaction > all_databases.sql
For point-in-time recovery (also known as "roll-forward", when you need to restore an old backup and replay the changes which happened since that backup), it is often useful to rotate the binary log (section 5.9.4 The Binary Log) or at least know the binary log coordinates to which the dump corresponds:
shell> mysqldump --all-databases --master-data=2 > all_databases.sql or shell> mysqldump --all-databases --flush-logs --master-data=2 > all_databases.sql
The simultaneous use of --master-data and --single-transaction
works as of MySQL 4.1.8. It provides a convenient way to make an online backup
suitable for point-in-time recovery, if tables are stored in the InnoDB storage
engine.
For more information on making backups, see section 5.7.1 Database Backups.
mysqlhotcopy Database Backup Program
mysqlhotcopy is a Perl script that was originally written and
contributed by Tim Bunce. It uses LOCK TABLES,
FLUSH TABLES, and cp or scp to quickly make a backup of
a database. It's the fastest way to make a backup of the database or single
tables, but it can be run only on the same machine where the database
directories are located. mysqlhotcopy works only for backing up
MyISAM and ISAM tables.
It runs on Unix, and as of MySQL 4.0.18 also on NetWare.
shell> mysqlhotcopy db_name [/path/to/new_directory]
shell> mysqlhotcopy db_name_1 ... db_name_n /path/to/new_directory
shell> mysqlhotcopy db_name./regex/
mysqlhotcopy supports the following options:
--help, -?
--allowold
_old
suffix).
--checkpoint=db_name.tbl_name
--debug
--dryrun, -n
--flushlog
--keepold
--method=#
cp or scp).
--noindices
myisamchk -rq
for MyISAM tables or isamchk -rq for ISAM tables.
--password=password, -ppassword
--port=port_num, -P port_num
--quiet, -q
--regexp=expr
--socket=path, -S path
--suffix=str
--tmpdir=path
--user=user_name, -u user_name
mysqlhotcopy reads the [client] and [mysqlhotcopy]
option groups from option files.
To execute mysqlhotcopy, you must have access to the files
for the tables that you are backing up, the SELECT privilege
for those tables, and the RELOAD privilege (to be able to
execute FLUSH TABLES).
Use perldoc for additional mysqlhotcopy documentation:
shell> perldoc mysqlhotcopy
mysqlimport Data Import Program
The mysqlimport client provides a command-line interface to the
LOAD DATA INFILE SQL statement. Most options to mysqlimport
correspond directly to clauses of LOAD DATA INFILE.
See section 13.1.5 LOAD DATA INFILE Syntax.
Invoke mysqlimport like this:
shell> mysqlimport [options] db_name textfile1 [textfile2 ...]
For each text file named on the command line, mysqlimport strips any
extension from the filename and uses the result to determine the name of the
table into which to import the file's contents. For example, files named
`patient.txt', `patient.text', and `patient' all would be
imported into a table named patient.
mysqlimport supports the following options:
--help, -?
--columns=column_list, -c column_list
--compress, -C
--debug[=debug_options], -# [debug_options]
'd:t:o,file_name'.
--delete, -D
--fields-terminated-by=...
--fields-enclosed-by=...
--fields-optionally-enclosed-by=...
--fields-escaped-by=...
--lines-terminated-by=...
LOAD DATA INFILE. See section 13.1.5 LOAD DATA INFILE Syntax.
--force, -f
--force,
mysqlimport exits if a table doesn't exist.
--host=host_name, -h host_name
localhost.
--ignore, -i
--replace option.
--ignore-lines=n
--local, -L
--lock-tables, -l
--password[=password], -p[password]
-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 will be
prompted for one.
--port=port_num, -P port_num
--protocol={TCP | SOCKET | PIPE | MEMORY}
--replace, -r
--replace and --ignore options control handling of input
records that duplicate existing records on unique key values. If you specify
--replace, new rows replace existing rows that have the same unique key
value. If you specify --ignore, input rows that duplicate an existing
row on a unique key value are skipped. If you don't specify either option, an
error occurs when a duplicate key value is found, and the rest of the text
file is ignored.
--silent, -s
--socket=path, -S path
localhost (which is the
default host).
--user=user_name, -u user_name
--verbose, -v
--version, -V
Here is a sample session that demonstrates use of mysqlimport:
shell> mysql -e 'CREATE TABLE imptest(id INT, n VARCHAR(30))' test shell> ed a 100 Max Sydow 101 Count Dracula . w imptest.txt 32 q shell> od -c imptest.txt 0000000 1 0 0 \t M a x S y d o w \n 1 0 0000020 1 \t C o u n t D r a c u l a \n 0000040 shell> mysqlimport --local test imptest.txt test.imptest: Records: 2 Deleted: 0 Skipped: 0 Warnings: 0 shell> mysql -e 'SELECT * FROM imptest' test +------+---------------+ | id | n | +------+---------------+ | 100 | Max Sydow | | 101 | Count Dracula | +------+---------------+
mysqlshow, Showing Databases, Tables, and Columns
The mysqlshow client can be used to quickly look at which databases
exist, their tables, and a table's columns or indexes.
mysqlshow provides a command-line interface to several SQL
SHOW statements. The same information can be obtained by using those
statements directly. For example, you can issue them from the mysql
client program.
See section 13.5.4 SHOW Syntax.
Invoke mysqlshow like this:
shell> mysqlshow [options] [db_name [tbl_name [col_name]]]