1MYSQL(1)                     MySQL Database System                    MYSQL(1)
2
3
4

NAME

6       mysql - the MySQL command-line tool
7

SYNOPSIS

9       mysql [options] db_name
10

DESCRIPTION

12       mysql is a simple SQL shell with input line editing capabilities. It
13       supports interactive and noninteractive use. When used interactively,
14       query results are presented in an ASCII-table format. When used
15       noninteractively (for example, as a filter), the result is presented in
16       tab-separated format. The output format can be changed using command
17       options.
18
19       If you have problems due to insufficient memory for large result sets,
20       use the --quick option. This forces mysql to retrieve results from the
21       server a row at a time rather than retrieving the entire result set and
22       buffering it in memory before displaying it. This is done by returning
23       the result set using the mysql_use_result() C API function in the
24       client/server library rather than mysql_store_result().
25
26       Using mysql is very easy. Invoke it from the prompt of your command
27       interpreter as follows:
28
29           shell> mysql db_name
30
31       Or:
32
33           shell> mysql --user=user_name --password=your_password db_name
34
35       Then type an SQL statement, end it with “;”, \g, or \G and press Enter.
36
37       As of MySQL 5.1.10, typing Control+C causes mysql to attempt to kill
38       the current statement. If this cannot be done, or Control+C is typed
39       again before the statement is killed, mysql exits. Previously,
40       Control+C caused mysql to exit in all cases.
41
42       You can execute SQL statements in a script file (batch file) like this:
43
44           shell> mysql db_name < script.sql > output.tab
45
46       On Unix, the mysql client logs statements executed interactively to a
47       history file. See the section called “MYSQL LOGGING”.
48

MYSQL OPTIONS

50       mysql supports the following options, which can be specified on the
51       command line or in the [mysql] and [client] groups of an option file.
52       mysql also supports the options for processing option files described
53       at Section 4.2.3.4, “Command-Line Options that Affect Option-File
54       Handling”.
55
56       ·   --help, -?
57
58           Display a help message and exit.
59
60       ·   --auto-rehash
61
62           Enable automatic rehashing. This option is on by default, which
63           enables database, table, and column name completion. Use
64           --disable-auto-rehash to disable rehashing. That causes mysql to
65           start faster, but you must issue the rehash command if you want to
66           use name completion.
67
68           To complete a name, enter the first part and press Tab. If the name
69           is unambiguous, mysql completes it. Otherwise, you can press Tab
70           again to see the possible names that begin with what you have typed
71           so far. Completion does not occur if there is no default database.
72
73       ·   --batch, -B
74
75           Print results using tab as the column separator, with each row on a
76           new line. With this option, mysql does not use the history file.
77
78           Batch mode results in nontabular output format and escaping of
79           special characters. Escaping may be disabled by using raw mode; see
80           the description for the --raw option.
81
82       ·   --bind-address=ip_address
83
84           On a computer having multiple network interfaces, this option can
85           be used to select which interface is employed when connecting to
86           the MySQL server.
87
88           This option is supported only in the version of the mysql client
89           that is supplied with MySQL Cluster, beginning with MySQL Cluster
90           NDB 6.3.4. It is not available in standard MySQL 5.1 releases.
91
92       ·   --character-sets-dir=path
93
94           The directory where character sets are installed. See Section 10.5,
95           “Character Set Configuration”.
96
97       ·   --column-names
98
99           Write column names in results.
100
101       ·   --column-type-info, -m
102
103           Display result set metadata. This option was added in MySQL 5.1.14.
104           (Before that, use --debug-info.) The -m short option was added in
105           MySQL 5.1.21.
106
107       ·   --comments, -c
108
109           Whether to preserve comments in statements sent to the server. The
110           default is --skip-comments (discard comments), enable with
111           --comments (preserve comments). This option was added in MySQL
112           5.1.23.
113
114       ·   --compress, -C
115
116           Compress all information sent between the client and the server if
117           both support compression.
118
119       ·   --database=db_name, -D db_name
120
121           The database to use. This is useful primarily in an option file.
122
123       ·   --debug[=debug_options], -# [debug_options]
124
125           Write a debugging log. A typical debug_options string is
126           'd:t:o,file_name'. The default is 'd:t:o,/tmp/mysql.trace'.
127
128       ·   --debug-check
129
130           Print some debugging information when the program exits. This
131           option was added in MySQL 5.1.21.
132
133       ·   --debug-info, -T
134
135           Before MySQL 5.1.14, this option prints debugging information and
136           memory and CPU usage statistics when the program exits, and also
137           causes display of result set metadata during execution. As of MySQL
138           5.1.14, use --column-type-info to display result set metadata.
139
140       ·   --default-character-set=charset_name
141
142           Use charset_name as the default character set for the client and
143           connection.
144
145           A common issue that can occur when the operating system uses utf8
146           or another multi-byte character set is that output from the mysql
147           client is formatted incorrectly, due to the fact that the MySQL
148           client uses the latin1 character set by default. You can usually
149           fix such issues by using this option to force the client to use the
150           system character set instead.
151
152           See Section 10.5, “Character Set Configuration”, for more
153           information.
154
155       ·   --delimiter=str
156
157           Set the statement delimiter. The default is the semicolon character
158           (“;”).
159
160       ·   --disable-named-commands
161
162           Disable named commands. Use the \* form only, or use named commands
163           only at the beginning of a line ending with a semicolon (“;”).
164           mysql starts with this option enabled by default. However, even
165           with this option, long-format commands still work from the first
166           line. See the section called “MYSQL COMMANDS”.
167
168       ·   --execute=statement, -e statement
169
170           Execute the statement and quit. The default output format is like
171           that produced with --batch. See Section 4.2.3.1, “Using Options on
172           the Command Line”, for some examples. With this option, mysql does
173           not use the history file.
174
175       ·   --force, -f
176
177           Continue even if an SQL error occurs.
178
179       ·   --host=host_name, -h host_name
180
181           Connect to the MySQL server on the given host.
182
183       ·   --html, -H
184
185           Produce HTML output.
186
187       ·   --ignore-spaces, -i
188
189           Ignore spaces after function names. The effect of this is described
190           in the discussion for the IGNORE_SPACE SQL mode (see Section 5.1.7,
191           “Server SQL Modes”).
192
193       ·   --line-numbers
194
195           Write line numbers for errors. Disable this with
196           --skip-line-numbers.
197
198       ·   --local-infile[={0|1}]
199
200           Enable or disable LOCAL capability for LOAD DATA INFILE. With no
201           value, the option enables LOCAL. The option may be given as
202           --local-infile=0 or --local-infile=1 to explicitly disable or
203           enable LOCAL. Enabling LOCAL has no effect if the server does not
204           also support it.
205
206       ·   --named-commands, -G
207
208           Enable named mysql commands. Long-format commands are permitted,
209           not just short-format commands. For example, quit and \q both are
210           recognized. Use --skip-named-commands to disable named commands.
211           See the section called “MYSQL COMMANDS”.
212
213       ·   --no-auto-rehash, -A
214
215           This has the same effect as -skip-auto-rehash. See the description
216           for --auto-rehash.
217
218       ·   --no-beep, -b
219
220           Do not beep when errors occur.
221
222       ·   --no-named-commands, -g
223
224           Deprecated, use --disable-named-commands instead.
225           --no-named-commands is removed in MySQL 5.5.
226
227       ·   --no-pager
228
229           Deprecated form of --skip-pager. See the --pager option.
230           --no-pager is removed in MySQL 5.5.
231
232       ·   --no-tee
233
234           Deprecated form of --skip-tee. See the --tee option.  --no-tee is
235           removed in MySQL 5.5.
236
237       ·   --one-database, -o
238
239           Ignore statements except those that occur while the default
240           database is the one named on the command line. This option is
241           rudimentary and should be used with care. Statement filtering is
242           based only on USE statements.
243
244           Initially, mysql executes statements in the input because
245           specifying a database db_name on the command line is equivalent to
246           inserting USE db_name at the beginning of the input. Then, for each
247           USE statement encountered, mysql accepts or rejects following
248           statements depending on whether the database named is the one on
249           the command line. The content of the statements is immaterial.
250
251           Suppose that mysql is invoked to process this set of statements:
252
253               DELETE FROM db2.t2;
254               USE db2;
255               DROP TABLE db1.t1;
256               CREATE TABLE db1.t1 (i INT);
257               USE db1;
258               INSERT INTO t1 (i) VALUES(1);
259               CREATE TABLE db2.t1 (j INT);
260
261           If the command line is mysql --force --one-database db1, mysql
262           handles the input as follows:
263
264           ·   The DELETE statement is executed because the default database
265               is db1, even though the statement names a table in a different
266               database.
267
268           ·   The DROP TABLE and CREATE TABLE statements are not executed
269               because the default database is not db1, even though the
270               statements name a table in db1.
271
272           ·   The INSERT and CREATE TABLE statements are executed because the
273               default database is db1, even though the CREATE TABLE statement
274               names a table in a different database.
275
276       ·   --pager[=command]
277
278           Use the given command for paging query output. If the command is
279           omitted, the default pager is the value of your PAGER environment
280           variable. Valid pagers are less, more, cat [> filename], and so
281           forth. This option works only on Unix and only in interactive mode.
282           To disable paging, use --skip-pager.  the section called “MYSQL
283           COMMANDS”, discusses output paging further.
284
285       ·   --password[=password], -p[password]
286
287           The password to use when connecting to the server. If you use the
288           short option form (-p), you cannot have a space between the option
289           and the password. If you omit the password value following the
290           --password or -p option on the command line, mysql prompts for one.
291
292           Specifying a password on the command line should be considered
293           insecure. See Section 6.1.2.1, “End-User Guidelines for Password
294           Security”. You can use an option file to avoid giving the password
295           on the command line.
296
297       ·   --pipe, -W
298
299           On Windows, connect to the server using a named pipe. This option
300           applies only if the server supports named-pipe connections.
301
302       ·   --port=port_num, -P port_num
303
304           The TCP/IP port number to use for the connection.
305
306       ·   --prompt=format_str
307
308           Set the prompt to the specified format. The default is mysql>. The
309           special sequences that the prompt can contain are described in the
310           section called “MYSQL COMMANDS”.
311
312       ·   --protocol={TCP|SOCKET|PIPE|MEMORY}
313
314           The connection protocol to use for connecting to the server. It is
315           useful when the other connection parameters normally would cause a
316           protocol to be used other than the one you want. For details on the
317           permissible values, see Section 4.2.2, “Connecting to the MySQL
318           Server”.
319
320       ·   --quick, -q
321
322           Do not cache each query result, print each row as it is received.
323           This may slow down the server if the output is suspended. With this
324           option, mysql does not use the history file.
325
326       ·   --raw, -r
327
328           For tabular output, the “boxing” around columns enables one column
329           value to be distinguished from another. For nontabular output (such
330           as is produced in batch mode or when the --batch or --silent option
331           is given), special characters are escaped in the output so they can
332           be identified easily. Newline, tab, NUL, and backslash are written
333           as \n, \t, \0, and \\. The --raw option disables this character
334           escaping.
335
336           The following example demonstrates tabular versus nontabular output
337           and the use of raw mode to disable escaping:
338
339               % mysql
340               mysql> SELECT CHAR(92);
341               +----------+
342               | CHAR(92) |
343               +----------+
344               | \        |
345               +----------+
346               % mysql -s
347               mysql> SELECT CHAR(92);
348               CHAR(92)
349               \\
350               % mysql -s -r
351               mysql> SELECT CHAR(92);
352               CHAR(92)
353               \
354
355       ·   --reconnect
356
357           If the connection to the server is lost, automatically try to
358           reconnect. A single reconnect attempt is made each time the
359           connection is lost. To suppress reconnection behavior, use
360           --skip-reconnect.
361
362       ·   --safe-updates, --i-am-a-dummy, -U
363
364           Permit only those UPDATE and DELETE statements that specify which
365           rows to modify by using key values. If you have set this option in
366           an option file, you can override it by using --safe-updates on the
367           command line. See the section called “MYSQL TIPS”, for more
368           information about this option.
369
370       ·   --secure-auth
371
372           Do not send passwords to the server in old (pre-4.1) format. This
373           prevents connections except for servers that use the newer password
374           format.
375
376       ·   --show-warnings
377
378           Cause warnings to be shown after each statement if there are any.
379           This option applies to interactive and batch mode.
380
381       ·   --sigint-ignore
382
383           Ignore SIGINT signals (typically the result of typing Control+C).
384
385       ·   --silent, -s
386
387           Silent mode. Produce less output. This option can be given multiple
388           times to produce less and less output.
389
390           This option results in nontabular output format and escaping of
391           special characters. Escaping may be disabled by using raw mode; see
392           the description for the --raw option.
393
394       ·   --skip-column-names, -N
395
396           Do not write column names in results.
397
398       ·   --skip-line-numbers, -L
399
400           Do not write line numbers for errors. Useful when you want to
401           compare result files that include error messages.
402
403       ·   --socket=path, -S path
404
405           For connections to localhost, the Unix socket file to use, or, on
406           Windows, the name of the named pipe to use.
407
408       ·   --ssl*
409
410           Options that begin with --ssl specify whether to connect to the
411           server using SSL and indicate where to find SSL keys and
412           certificates. See Section 6.3.6.4, “SSL Command Options”.
413
414       ·   --table, -t
415
416           Display output in table format. This is the default for interactive
417           use, but can be used to produce table output in batch mode.
418
419       ·   --tee=file_name
420
421           Append a copy of output to the given file. This option works only
422           in interactive mode.  the section called “MYSQL COMMANDS”,
423           discusses tee files further.
424
425       ·   --unbuffered, -n
426
427           Flush the buffer after each query.
428
429       ·   --user=user_name, -u user_name
430
431           The MySQL user name to use when connecting to the server.
432
433       ·   --verbose, -v
434
435           Verbose mode. Produce more output about what the program does. This
436           option can be given multiple times to produce more and more output.
437           (For example, -v -v -v produces table output format even in batch
438           mode.)
439
440       ·   --version, -V
441
442           Display version information and exit.
443
444       ·   --vertical, -E
445
446           Print query output rows vertically (one line per column value).
447           Without this option, you can specify vertical output for individual
448           statements by terminating them with \G.
449
450       ·   --wait, -w
451
452           If the connection cannot be established, wait and retry instead of
453           aborting.
454
455       ·   --xml, -X
456
457           Produce XML output.
458
459               Note
460               Prior to MySQL 5.1.12, there was no differentiation in the
461               output when using this option between columns containing the
462               NULL value and columns containing the string literal 'NULL';
463               both were represented as
464
465               <field name="column_name">NULL</field>
466
467           Beginning with MySQL 5.1.12, the output when --xml is used with
468           mysql matches that of mysqldump --xml. See mysqldump(1) for
469           details.
470
471           Beginning with MySQL 5.1.18, the XML output also uses an XML
472           namespace, as shown here:
473
474               shell> mysql --xml -uroot -e "SHOW VARIABLES LIKE 'version%'"
475               <?xml version="1.0"?>
476               <resultset statement="SHOW VARIABLES LIKE 'version%'" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
477               <row>
478               <field name="Variable_name">version</field>
479               <field name="Value">5.0.40-debug</field>
480               </row>
481               <row>
482               <field name="Variable_name">version_comment</field>
483               <field name="Value">Source distribution</field>
484               </row>
485               <row>
486               <field name="Variable_name">version_compile_machine</field>
487               <field name="Value">i686</field>
488               </row>
489               <row>
490               <field name="Variable_name">version_compile_os</field>
491               <field name="Value">suse-linux-gnu</field>
492               </row>
493               </resultset>
494
495           (See Bug #25946.)
496
497       You can also set the following variables by using --var_name=value. The
498       --set-variable format is deprecated and is removed in MySQL 5.5.
499
500       ·   connect_timeout
501
502           The number of seconds before connection timeout. (Default value is
503           0.)
504
505       ·   max_allowed_packet
506
507           The maximum size of the buffer for client/server communication. The
508           default is 16MB, the maximum is 1GB.
509
510       ·   max_join_size
511
512           The automatic limit for rows in a join when using --safe-updates.
513           (Default value is 1,000,000.)
514
515       ·   net_buffer_length
516
517           The buffer size for TCP/IP and socket communication. (Default value
518           is 16KB.)
519
520       ·   select_limit
521
522           The automatic limit for SELECT statements when using
523           --safe-updates. (Default value is 1,000.)
524

MYSQL COMMANDS

526       mysql sends each SQL statement that you issue to the server to be
527       executed. There is also a set of commands that mysql itself interprets.
528       For a list of these commands, type help or \h at the mysql> prompt:
529
530           mysql> help
531           List of all MySQL commands:
532           Note that all text commands must be first on line and end with ';'
533           ?         (\?) Synonym for `help'.
534           clear     (\c) Clear command.
535           connect   (\r) Reconnect to the server. Optional arguments are db and host.
536           delimiter (\d) Set statement delimiter.
537           edit      (\e) Edit command with $EDITOR.
538           ego       (\G) Send command to mysql server, display result vertically.
539           exit      (\q) Exit mysql. Same as quit.
540           go        (\g) Send command to mysql server.
541           help      (\h) Display this help.
542           nopager   (\n) Disable pager, print to stdout.
543           notee     (\t) Don't write into outfile.
544           pager     (\P) Set PAGER [to_pager]. Print the query results via PAGER.
545           print     (\p) Print current command.
546           prompt    (\R) Change your mysql prompt.
547           quit      (\q) Quit mysql.
548           rehash    (\#) Rebuild completion hash.
549           source    (\.) Execute an SQL script file. Takes a file name as an argument.
550           status    (\s) Get status information from the server.
551           system    (\!) Execute a system shell command.
552           tee       (\T) Set outfile [to_outfile]. Append everything into given
553                          outfile.
554           use       (\u) Use another database. Takes database name as argument.
555           charset   (\C) Switch to another charset. Might be needed for processing
556                          binlog with multi-byte charsets.
557           warnings  (\W) Show warnings after every statement.
558           nowarning (\w) Don't show warnings after every statement.
559           For server side help, type 'help contents'
560
561       Each command has both a long and short form. The long form is not case
562       sensitive; the short form is. The long form can be followed by an
563       optional semicolon terminator, but the short form should not.
564
565       The use of short-form commands within multi-line /* ... */ comments is
566       not supported.
567
568       ·   help [arg], \h [arg], \? [arg], ? [arg]
569
570           Display a help message listing the available mysql commands.
571
572           If you provide an argument to the help command, mysql uses it as a
573           search string to access server-side help from the contents of the
574           MySQL Reference Manual. For more information, see the section
575           called “MYSQL SERVER-SIDE HELP”.
576
577       ·   charset charset_name, \C charset_name
578
579           Change the default character set and issue a SET NAMES statement.
580           This enables the character set to remain synchronized on the client
581           and server if mysql is run with auto-reconnect enabled (which is
582           not recommended), because the specified character set is used for
583           reconnects. This command was added in MySQL 5.1.7.
584
585       ·   clear, \c
586
587           Clear the current input. Use this if you change your mind about
588           executing the statement that you are entering.
589
590       ·   connect [db_name host_name]], \r [db_name host_name]]
591
592           Reconnect to the server. The optional database name and host name
593           arguments may be given to specify the default database or the host
594           where the server is running. If omitted, the current values are
595           used.
596
597       ·   delimiter str, \d str
598
599           Change the string that mysql interprets as the separator between
600           SQL statements. The default is the semicolon character (“;”).
601
602           The delimiter string can be specified as an unquoted or quoted
603           argument on the delimiter command line. Quoting can be done with
604           either single quote ('), double quote ("), or backtick (`)
605           characters. To include a quote within a quoted string, either quote
606           the string with a different quote character or escape the quote
607           with a backslash (“\”) character. Backslash should be avoided
608           outside of quoted strings because it is the escape character for
609           MySQL. For an unquoted argument, the delimiter is read up to the
610           first space or end of line. For a quoted argument, the delimiter is
611           read up to the matching quote on the line.
612
613           mysql interprets instances of the delimiter string as a statement
614           delimiter anywhere it occurs, except within quoted strings. Be
615           careful about defining a delimiter that might occur within other
616           words. For example, if you define the delimiter as X, you will be
617           unable to use the word INDEX in statements.  mysql interprets this
618           as INDE followed by the delimiter X.
619
620           When the delimiter recognized by mysql is set to something other
621           than the default of “;”, instances of that character are sent to
622           the server without interpretation. However, the server itself still
623           interprets “;” as a statement delimiter and processes statements
624           accordingly. This behavior on the server side comes into play for
625           multiple-statement execution (see Section 21.8.16, “C API Support
626           for Multiple Statement Execution”), and for parsing the body of
627           stored procedures and functions, triggers, and events (see
628           Section 19.1, “Defining Stored Programs”).
629
630       ·   edit, \e
631
632           Edit the current input statement.  mysql checks the values of the
633           EDITOR and VISUAL environment variables to determine which editor
634           to use. The default editor is vi if neither variable is set.
635
636           The edit command works only in Unix.
637
638       ·   ego, \G
639
640           Send the current statement to the server to be executed and display
641           the result using vertical format.
642
643       ·   exit, \q
644
645           Exit mysql.
646
647       ·   go, \g
648
649           Send the current statement to the server to be executed.
650
651       ·   nopager, \n
652
653           Disable output paging. See the description for pager.
654
655           The nopager command works only in Unix.
656
657       ·   notee, \t
658
659           Disable output copying to the tee file. See the description for
660           tee.
661
662       ·   nowarning, \w
663
664           Enable display of warnings after each statement.
665
666       ·   pager [command], \P [command]
667
668           Enable output paging. By using the --pager option when you invoke
669           mysql, it is possible to browse or search query results in
670           interactive mode with Unix programs such as less, more, or any
671           other similar program. If you specify no value for the option,
672           mysql checks the value of the PAGER environment variable and sets
673           the pager to that. Pager functionality works only in interactive
674           mode.
675
676           Output paging can be enabled interactively with the pager command
677           and disabled with nopager. The command takes an optional argument;
678           if given, the paging program is set to that. With no argument, the
679           pager is set to the pager that was set on the command line, or
680           stdout if no pager was specified.
681
682           Output paging works only in Unix because it uses the popen()
683           function, which does not exist on Windows. For Windows, the tee
684           option can be used instead to save query output, although it is not
685           as convenient as pager for browsing output in some situations.
686
687       ·   print, \p
688
689           Print the current input statement without executing it.
690
691       ·   prompt [str], \R [str]
692
693           Reconfigure the mysql prompt to the given string. The special
694           character sequences that can be used in the prompt are described
695           later in this section.
696
697           If you specify the prompt command with no argument, mysql resets
698           the prompt to the default of mysql>.
699
700       ·   quit, \q
701
702           Exit mysql.
703
704       ·   rehash, \#
705
706           Rebuild the completion hash that enables database, table, and
707           column name completion while you are entering statements. (See the
708           description for the --auto-rehash option.)
709
710       ·   source file_name, \. file_name
711
712           Read the named file and executes the statements contained therein.
713           On Windows, you can specify path name separators as / or \\.
714
715       ·   status, \s
716
717           Provide status information about the connection and the server you
718           are using. If you are running in --safe-updates mode, status also
719           prints the values for the mysql variables that affect your queries.
720
721       ·   system command, \! command
722
723           Execute the given command using your default command interpreter.
724
725           The system command works only in Unix.
726
727       ·   tee [file_name], \T [file_name]
728
729           By using the --tee option when you invoke mysql, you can log
730           statements and their output. All the data displayed on the screen
731           is appended into a given file. This can be very useful for
732           debugging purposes also.  mysql flushes results to the file after
733           each statement, just before it prints its next prompt. Tee
734           functionality works only in interactive mode.
735
736           You can enable this feature interactively with the tee command.
737           Without a parameter, the previous file is used. The tee file can be
738           disabled with the notee command. Executing tee again re-enables
739           logging.
740
741       ·   use db_name, \u db_name
742
743           Use db_name as the default database.
744
745       ·   warnings, \W
746
747           Enable display of warnings after each statement (if there are any).
748
749       Here are a few tips about the pager command:
750
751       ·   You can use it to write to a file and the results go only to the
752           file:
753
754               mysql> pager cat > /tmp/log.txt
755
756           You can also pass any options for the program that you want to use
757           as your pager:
758
759               mysql> pager less -n -i -S
760
761       ·   In the preceding example, note the -S option. You may find it very
762           useful for browsing wide query results. Sometimes a very wide
763           result set is difficult to read on the screen. The -S option to
764           less can make the result set much more readable because you can
765           scroll it horizontally using the left-arrow and right-arrow keys.
766           You can also use -S interactively within less to switch the
767           horizontal-browse mode on and off. For more information, read the
768           less manual page:
769
770               shell> man less
771
772       ·   The -F and -X options may be used with less to cause it to exit if
773           output fits on one screen, which is convenient when no scrolling is
774           necessary:
775
776               mysql> pager less -n -i -S -F -X
777
778       ·   You can specify very complex pager commands for handling query
779           output:
780
781               mysql> pager cat | tee /dr1/tmp/res.txt \
782                         | tee /dr2/tmp/res2.txt | less -n -i -S
783
784           In this example, the command would send query results to two files
785           in two different directories on two different file systems mounted
786           on /dr1 and /dr2, yet still display the results onscreen using
787           less.
788
789       You can also combine the tee and pager functions. Have a tee file
790       enabled and pager set to less, and you are able to browse the results
791       using the less program and still have everything appended into a file
792       the same time. The difference between the Unix tee used with the pager
793       command and the mysql built-in tee command is that the built-in tee
794       works even if you do not have the Unix tee available. The built-in tee
795       also logs everything that is printed on the screen, whereas the Unix
796       tee used with pager does not log quite that much. Additionally, tee
797       file logging can be turned on and off interactively from within mysql.
798       This is useful when you want to log some queries to a file, but not
799       others.
800
801       The prompt command reconfigures the default mysql> prompt. The string
802       for defining the prompt can contain the following special sequences.
803
804       ┌───────┬────────────────────────────┐
805Option Description                
806       ├───────┼────────────────────────────┤
807       │\c     │ A counter that increments  │
808       │       │ for each statement you     │
809       │       │ issue                      │
810       ├───────┼────────────────────────────┤
811       │\D     │ The full current date      │
812       ├───────┼────────────────────────────┤
813       │\d     │ The default database       │
814       ├───────┼────────────────────────────┤
815       │\h     │ The server host            │
816       ├───────┼────────────────────────────┤
817       │\l     │ The current delimiter (new │
818       │       │ in 5.1.12)                 │
819       ├───────┼────────────────────────────┤
820       │\m     │ Minutes of the current     │
821       │       │ time                       │
822       ├───────┼────────────────────────────┤
823       │\n     │ A newline character        │
824       ├───────┼────────────────────────────┤
825       │\O     │ The current month in       │
826       │       │ three-letter format (Jan,  │
827       │       │ Feb, ...)                  │
828       ├───────┼────────────────────────────┤
829       │\o     │ The current month in       │
830       │       │ numeric format             │
831       ├───────┼────────────────────────────┤
832       │\P     │ am/pm                      │
833       ├───────┼────────────────────────────┤
834       │\p     │ The current TCP/IP port or │
835       │       │ socket file                │
836       ├───────┼────────────────────────────┤
837       │\R     │ The current time, in       │
838       │       │ 24-hour military time      │
839       │       │ (0–23)                     │
840       ├───────┼────────────────────────────┤
841       │\r     │ The current time, standard │
842       │       │ 12-hour time (1–12)        │
843       ├───────┼────────────────────────────┤
844       │\S     │ Semicolon                  │
845       ├───────┼────────────────────────────┤
846       │\s     │ Seconds of the current     │
847       │       │ time                       │
848       ├───────┼────────────────────────────┤
849       │\t     │ A tab character            │
850       ├───────┼────────────────────────────┤
851       │\U     │                            │
852       │       │        Your full           │
853       │       │        user_name@host_name
854       │       │        account name        │
855       ├───────┼────────────────────────────┤
856       │\u     │ Your user name             │
857       ├───────┼────────────────────────────┤
858       │\v     │ The server version         │
859       ├───────┼────────────────────────────┤
860       │\w     │ The current day of the     │
861       │       │ week in three-letter       │
862       │       │ format (Mon, Tue, ...)     │
863       ├───────┼────────────────────────────┤
864       │\Y     │ The current year, four     │
865       │       │ digits                     │
866       ├───────┼────────────────────────────┤
867       │\y     │ The current year, two      │
868       │       │ digits                     │
869       ├───────┼────────────────────────────┤
870       │\_     │ A space                    │
871       ├───────┼────────────────────────────┤
872       │\      │ A space (a space follows   │
873       │       │ the backslash)             │
874       ├───────┼────────────────────────────┤
875       │\'     │ Single quote               │
876       ├───────┼────────────────────────────┤
877       │\"     │ Double quote               │
878       ├───────┼────────────────────────────┤
879       │\\     │ A literal “\” backslash    │
880       │       │ character                  │
881       ├───────┼────────────────────────────┤
882       │\x     │                            │
883       │       │        x, for any “x” not  │
884       │       │        listed above        │
885       └───────┴────────────────────────────┘
886
887       You can set the prompt in several ways:
888
889       ·   Use an environment variable.  You can set the MYSQL_PS1 environment
890           variable to a prompt string. For example:
891
892               shell> export MYSQL_PS1="(\u@\h) [\d]> "
893
894       ·   Use a command-line option.  You can set the --prompt option on the
895           command line to mysql. For example:
896
897               shell> mysql --prompt="(\u@\h) [\d]> "
898               (user@host) [database]>
899
900       ·   Use an option file.  You can set the prompt option in the [mysql]
901           group of any MySQL option file, such as /etc/my.cnf or the .my.cnf
902           file in your home directory. For example:
903
904               [mysql]
905               prompt=(\\u@\\h) [\\d]>\\_
906
907           In this example, note that the backslashes are doubled. If you set
908           the prompt using the prompt option in an option file, it is
909           advisable to double the backslashes when using the special prompt
910           options. There is some overlap in the set of permissible prompt
911           options and the set of special escape sequences that are recognized
912           in option files. (The rules for escape sequences in option files
913           are listed in Section 4.2.3.3, “Using Option Files”.) The overlap
914           may cause you problems if you use single backslashes. For example,
915           \s is interpreted as a space rather than as the current seconds
916           value. The following example shows how to define a prompt within an
917           option file to include the current time in HH:MM:SS> format:
918
919               [mysql]
920               prompt="\\r:\\m:\\s> "
921
922       ·   Set the prompt interactively.  You can change your prompt
923           interactively by using the prompt (or \R) command. For example:
924
925               mysql> prompt (\u@\h) [\d]>\_
926               PROMPT set to '(\u@\h) [\d]>\_'
927               (user@host) [database]>
928               (user@host) [database]> prompt
929               Returning to default PROMPT of mysql>
930               mysql>
931

MYSQL LOGGING

933       On Unix, the mysql client logs statements executed interactively to a
934       history file. By default, this file is named .mysql_history in your
935       home directory. To specify a different file, set the value of the
936       MYSQL_HISTFILE environment variable.
937               How Logging Occurs
938
939       Statement logging occurs as follows:
940
941       ·   Statements are logged only when executed interactively. Statements
942           are noninteractive, for example, when read from a file or a pipe.
943           It is also possible to suppress statement logging by using the
944           --batch or --execute option.
945
946       ·   mysql logs each nonempty statement line individually.
947
948       ·   If a statement spans multiple lines (not including the terminating
949           delimiter), mysql concatenates the lines to form the complete
950           statement, maps newlines to spaces, and logs the result, plus a
951           delimiter.
952
953       Consequently, an input statement that spans multiple lines can be
954       logged twice. Consider this input:
955
956           mysql> SELECT
957               -> 'Today is'
958               -> ,
959               -> CONCAT()
960               -> ;
961
962       In this case, mysql logs the “SELECT”, “'Today is'”, “,”, “CONCAT()”,
963       and “;” lines as it reads them. It also logs the complete statement,
964       after mapping SELECT\n'Today is'\n,\nCURDATE() to SELECT 'Today is' ,
965       CURDATE(), plus a delimiter. Thus, these lines appear in logged output:
966
967           SELECT
968           'Today is'
969           ,
970           CURDATE()
971           ;
972           SELECT 'Today is' , CURDATE();
973
974               Controlling the History File
975
976       The .mysql_history file should be protected with a restrictive access
977       mode because sensitive information might be written to it, such as the
978       text of SQL statements that contain passwords. See Section 6.1.2.1,
979       “End-User Guidelines for Password Security”.
980
981       If you do not want to maintain a history file, first remove
982       .mysql_history if it exists. Then use either of the following
983       techniques to prevent it from being created again:
984
985       ·   Set the MYSQL_HISTFILE environment variable to /dev/null. To cause
986           this setting to take effect each time you log in, put it in one of
987           your shell's startup files.
988
989       ·   Create .mysql_history as a symbolic link to /dev/null; this need be
990           done only once:
991
992               shell> ln -s /dev/null $HOME/.mysql_history
993

MYSQL SERVER-SIDE HELP

995           mysql> help search_string
996
997       If you provide an argument to the help command, mysql uses it as a
998       search string to access server-side help from the contents of the MySQL
999       Reference Manual. The proper operation of this command requires that
1000       the help tables in the mysql database be initialized with help topic
1001       information (see Section 5.1.9, “Server-Side Help”).
1002
1003       If there is no match for the search string, the search fails:
1004
1005           mysql> help me
1006           Nothing found
1007           Please try to run 'help contents' for a list of all accessible topics
1008
1009       Use help contents to see a list of the help categories:
1010
1011           mysql> help contents
1012           You asked for help about help category: "Contents"
1013           For more information, type 'help <item>', where <item> is one of the
1014           following categories:
1015              Account Management
1016              Administration
1017              Data Definition
1018              Data Manipulation
1019              Data Types
1020              Functions
1021              Functions and Modifiers for Use with GROUP BY
1022              Geographic Features
1023              Language Structure
1024              Plugins
1025              Storage Engines
1026              Stored Routines
1027              Table Maintenance
1028              Transactions
1029              Triggers
1030
1031       If the search string matches multiple items, mysql shows a list of
1032       matching topics:
1033
1034           mysql> help logs
1035           Many help items for your request exist.
1036           To make a more specific request, please type 'help <item>',
1037           where <item> is one of the following topics:
1038              SHOW
1039              SHOW BINARY LOGS
1040              SHOW ENGINE
1041              SHOW LOGS
1042
1043       Use a topic as the search string to see the help entry for that topic:
1044
1045           mysql> help show binary logs
1046           Name: 'SHOW BINARY LOGS'
1047           Description:
1048           Syntax:
1049           SHOW BINARY LOGS
1050           SHOW MASTER LOGS
1051           Lists the binary log files on the server. This statement is used as
1052           part of the procedure described in [purge-binary-logs], that shows how
1053           to determine which logs can be purged.
1054           mysql> SHOW BINARY LOGS;
1055           +---------------+-----------+
1056           | Log_name      | File_size |
1057           +---------------+-----------+
1058           | binlog.000015 |    724935 |
1059           | binlog.000016 |    733481 |
1060           +---------------+-----------+
1061
1062       The search string can contain the the wildcard characters “%” and “_”.
1063       These have the same meaning as for pattern-matching operations
1064       performed with the LIKE operator. For example, HELP rep% returns a list
1065       of topics that begin with rep:
1066
1067           mysql> HELP rep%
1068           Many help items for your request exist.
1069           To make a more specific request, please type 'help <item>',
1070           where <item> is one of the following
1071           topics:
1072              REPAIR TABLE
1073              REPEAT FUNCTION
1074              REPEAT LOOP
1075              REPLACE
1076              REPLACE FUNCTION
1077

EXECUTING SQL STATEMENTS FROM A TEXT FILE

1079       The mysql client typically is used interactively, like this:
1080
1081           shell> mysql db_name
1082
1083       However, it is also possible to put your SQL statements in a file and
1084       then tell mysql to read its input from that file. To do so, create a
1085       text file text_file that contains the statements you wish to execute.
1086       Then invoke mysql as shown here:
1087
1088           shell> mysql db_name < text_file
1089
1090       If you place a USE db_name statement as the first statement in the
1091       file, it is unnecessary to specify the database name on the command
1092       line:
1093
1094           shell> mysql < text_file
1095
1096       If you are already running mysql, you can execute an SQL script file
1097       using the source command or \.  command:
1098
1099           mysql> source file_name
1100           mysql> \. file_name
1101
1102       Sometimes you may want your script to display progress information to
1103       the user. For this you can insert statements like this:
1104
1105           SELECT '<info_to_display>' AS ' ';
1106
1107       The statement shown outputs <info_to_display>.
1108
1109       You can also invoke mysql with the --verbose option, which causes each
1110       statement to be displayed before the result that it produces.
1111
1112       As of MySQL 5.1.23, mysql ignores Unicode byte order mark (BOM)
1113       characters at the beginning of input files. Previously, it read them
1114       and sent them to the server, resulting in a syntax error. Presence of a
1115       BOM does not cause mysql to change its default character set. To do
1116       that, invoke mysql with an option such as --default-character-set=utf8.
1117
1118       For more information about batch mode, see Section 3.5, “Using mysql in
1119       Batch Mode”.
1120

MYSQL TIPS

1122       This section describes some techniques that can help you use mysql more
1123       effectively.
1124
1125   Input-Line Editing
1126       mysql supports input-line editing, which enables you to modify the
1127       current input line in place or recall previous input lines. For
1128       example, the left-arrow and right-arrow keys move horizontally within
1129       the current input line, and the up-arror and down-arrow keys move up
1130       and down through the set of previously entered lines.  Backspace
1131       deletes the character before the cursor and typing new characters
1132       enters them at the cursor position. To enter the line, press Enter.
1133
1134       On Windows, the editing key sequences are the same as supported for
1135       command editing in console windows. On Unix, the key sequences depend
1136       on the input library used to build mysql (for example, the libedit or
1137       readline library).
1138
1139       Documentation for the libedit and readline libraries is available
1140       online. To change the set of key sequences permitted by a given input
1141       library, define key bindings in the library startup file. This is a
1142       file in your home directory: .editrc for libedit and .inputrc for
1143       readline.
1144
1145       For example, in libedit, Control+W deletes everything before the
1146       current cursor position and Control+U deletes the entire line. In
1147       readline, Control+W deletes the word before the cursor and Control+U
1148       deletes everything before the current cursor position. If mysql was
1149       built using libedit, a user who prefers the readline behavior for these
1150       two keys can put the following lines in the .editrc file (creating the
1151       file if necessary):
1152
1153           bind "^W" ed-delete-prev-word
1154           bind "^U" vi-kill-line-prev
1155
1156       To see the current set of key bindings, temporarily put a line that
1157       says only bind at the end of .editrc.  mysql will show the bindings
1158       when it starts.
1159
1160   Displaying Query Results Vertically
1161       Some query results are much more readable when displayed vertically,
1162       instead of in the usual horizontal table format. Queries can be
1163       displayed vertically by terminating the query with \G instead of a
1164       semicolon. For example, longer text values that include newlines often
1165       are much easier to read with vertical output:
1166
1167           mysql> SELECT * FROM mails WHERE LENGTH(txt) < 300 LIMIT 300,1\G
1168           *************************** 1. row ***************************
1169             msg_nro: 3068
1170                date: 2000-03-01 23:29:50
1171           time_zone: +0200
1172           mail_from: Monty
1173               reply: monty@no.spam.com
1174             mail_to: "Thimble Smith" <tim@no.spam.com>
1175                 sbj: UTF-8
1176                 txt: >>>>> "Thimble" == Thimble Smith writes:
1177           Thimble> Hi.  I think this is a good idea.  Is anyone familiar
1178           Thimble> with UTF-8 or Unicode? Otherwise, I'll put this on my
1179           Thimble> TODO list and see what happens.
1180           Yes, please do that.
1181           Regards,
1182           Monty
1183                file: inbox-jani-1
1184                hash: 190402944
1185           1 row in set (0.09 sec)
1186
1187   Using the --safe-updates Option
1188       For beginners, a useful startup option is --safe-updates (or
1189       --i-am-a-dummy, which has the same effect). It is helpful for cases
1190       when you might have issued a DELETE FROM tbl_name statement but
1191       forgotten the WHERE clause. Normally, such a statement deletes all rows
1192       from the table. With --safe-updates, you can delete rows only by
1193       specifying the key values that identify them. This helps prevent
1194       accidents.
1195
1196       When you use the --safe-updates option, mysql issues the following
1197       statement when it connects to the MySQL server:
1198
1199           SET sql_safe_updates=1, sql_select_limit=1000, sql_max_join_size=1000000;
1200
1201       See Section 5.1.4, “Server System Variables”.
1202
1203       The SET statement has the following effects:
1204
1205       ·   You are not permitted to execute an UPDATE or DELETE statement
1206           unless you specify a key constraint in the WHERE clause or provide
1207           a LIMIT clause (or both). For example:
1208
1209               UPDATE tbl_name SET not_key_column=val WHERE key_column=val;
1210               UPDATE tbl_name SET not_key_column=val LIMIT 1;
1211
1212       ·   The server limits all large SELECT results to 1,000 rows unless the
1213           statement includes a LIMIT clause.
1214
1215       ·   The server aborts multiple-table SELECT statements that probably
1216           need to examine more than 1,000,000 row combinations.
1217
1218       To specify limits different from 1,000 and 1,000,000, you can override
1219       the defaults by using the --select_limit and --max_join_size options:
1220
1221           shell> mysql --safe-updates --select_limit=500 --max_join_size=10000
1222
1223   Disabling mysql Auto-Reconnect
1224       If the mysql client loses its connection to the server while sending a
1225       statement, it immediately and automatically tries to reconnect once to
1226       the server and send the statement again. However, even if mysql
1227       succeeds in reconnecting, your first connection has ended and all your
1228       previous session objects and settings are lost: temporary tables, the
1229       autocommit mode, and user-defined and session variables. Also, any
1230       current transaction rolls back. This behavior may be dangerous for you,
1231       as in the following example where the server was shut down and
1232       restarted between the first and second statements without you knowing
1233       it:
1234
1235           mysql> SET @a=1;
1236           Query OK, 0 rows affected (0.05 sec)
1237           mysql> INSERT INTO t VALUES(@a);
1238           ERROR 2006: MySQL server has gone away
1239           No connection. Trying to reconnect...
1240           Connection id:    1
1241           Current database: test
1242           Query OK, 1 row affected (1.30 sec)
1243           mysql> SELECT * FROM t;
1244           +------+
1245           | a    |
1246           +------+
1247           | NULL |
1248           +------+
1249           1 row in set (0.05 sec)
1250
1251       The @a user variable has been lost with the connection, and after the
1252       reconnection it is undefined. If it is important to have mysql
1253       terminate with an error if the connection has been lost, you can start
1254       the mysql client with the --skip-reconnect option.
1255
1256       For more information about auto-reconnect and its effect on state
1257       information when a reconnection occurs, see Section 21.8.15,
1258       “Controlling Automatic Reconnection Behavior”.
1259
1261       Copyright © 1997, 2013, Oracle and/or its affiliates. All rights
1262       reserved.
1263
1264       This documentation is free software; you can redistribute it and/or
1265       modify it only under the terms of the GNU General Public License as
1266       published by the Free Software Foundation; version 2 of the License.
1267
1268       This documentation is distributed in the hope that it will be useful,
1269       but WITHOUT ANY WARRANTY; without even the implied warranty of
1270       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1271       General Public License for more details.
1272
1273       You should have received a copy of the GNU General Public License along
1274       with the program; if not, write to the Free Software Foundation, Inc.,
1275       51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA or see
1276       http://www.gnu.org/licenses/.
1277
1278

SEE ALSO

1280       For more information, please refer to the MySQL Reference Manual, which
1281       may already be installed locally and which is also available online at
1282       http://dev.mysql.com/doc/.
1283

AUTHOR

1285       Oracle Corporation (http://dev.mysql.com/).
1286
1287
1288
1289MySQL 5.1                         11/04/2013                          MYSQL(1)
Impressum