1MYSQL(1) MySQL Database System MYSQL(1)
2
3
4
6 mysql - the MySQL command-line tool
7
9 mysql [options] db_name
10
12 mysql is a simple SQL shell (with GNU readline 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
47 mysql supports the following options, which can be specified on the
48 command line or in the [mysql] and [client] option file groups. mysql
49 also supports the options for processing option files described at
50 Section 4.2.3.3.1, “Command-Line Options that Affect Option-File
51 Handling”.
52
53 · --help, -?, -I
54
55 Display a help message and exit.
56
57 · --auto-rehash
58
59 Enable automatic rehashing. This option is on by default, which
60 enables database, table, and column name completion. Use
61 --disable-auto-rehash to disable rehashing. That causes mysql to
62 start faster, but you must issue the rehash command if you want to
63 use name completion.
64
65 To complete a name, enter the first part and press Tab. If the name
66 is unambiguous, mysql completes it. Otherwise, you can press Tab
67 again to see the possible names that begin with what you have typed
68 so far. Completion does not occur if there is no default database.
69
70 · --batch, -B
71
72 Print results using tab as the column separator, with each row on a
73 new line. With this option, mysql does not use the history file.
74
75 Batch mode results in nontabular output format and escaping of
76 special characters. Escaping may be disabled by using raw mode; see
77 the description for the --raw option.
78
79 · --character-sets-dir=path
80
81 The directory where character sets are installed. See Section 9.5,
82 “Character Set Configuration”.
83
84 · --column-names
85
86 Write column names in results.
87
88 · --column-type-info, -m
89
90 Display result set metadata. This option was added in MySQL 5.1.14.
91 (Before that, use --debug-info.) The -m short option was added in
92 MySQL 5.1.21.
93
94 · --comments, -c
95
96 Whether to preserve comments in statements sent to the server. The
97 default is --skip-comments (discard comments), enable with
98 --comments (preserve comments). This option was added in MySQL
99 5.1.23.
100
101 · --compress, -C
102
103 Compress all information sent between the client and the server if
104 both support compression.
105
106 · --connect-timeout=seconds
107
108 Set the number of seconds before connection timeout. (Default value
109 is 0.)
110
111 · --database=db_name, -D db_name
112
113 The database to use. This is useful primarily in an option file.
114
115 · --debug[=debug_options], -# [debug_options]
116
117 Write a debugging log. A typical debug_options string is
118 ´d:t:o,file_name´. The default is ´d:t:o,/tmp/mysql.trace´.
119
120 · --debug-check
121
122 Print some debugging information when the program exits. This
123 option was added in MySQL 5.1.21.
124
125 · --debug-info, -T
126
127 Before MySQL 5.1.14, this option prints debugging information and
128 memory and CPU usage statistics when the program exits, and also
129 causes display of result set metadata during execution. As of MySQL
130 5.1.14, use --column-type-info to display result set metadata.
131
132 · --default-character-set=charset_name
133
134 Use charset_name as the default character set for the client and
135 connection.
136
137 A common issue that can occur when the operating system uses utf8
138 or another multi-byte character set is that output from the mysql
139 client is formatted incorrectly, due to the fact that the MySQL
140 client uses the latin1 character set by default. You can usually
141 fix such issues by using this option to force the client to use the
142 system character set instead.
143
144 See Section 9.5, “Character Set Configuration”, for more
145 information.
146
147 · --defaults-file=filename
148
149 Set filename as the file to read default options from, override
150 global defaults files. Must be given as first option.
151
152 · --defaults-extra-file=filename
153
154 Set filename as the file to read default options from after the
155 global defaults files has been read. Must be given as first
156 option.
157
158 · --delimiter=str
159
160 Set the statement delimiter. The default is the semicolon character
161 (“;”).
162
163 · --disable-named-commands
164
165 Disable named commands. Use the \* form only, or use named commands
166 only at the beginning of a line ending with a semicolon (“;”).
167 mysql starts with this option enabled by default. However, even
168 with this option, long-format commands still work from the first
169 line. See the section called “MYSQL COMMANDS”.
170
171 · --execute=statement, -e statement
172
173 Execute the statement and quit. The default output format is like
174 that produced with --batch. See Section 4.2.3.1, “Using Options on
175 the Command Line”, for some examples.
176
177 · --force, -f
178
179 Continue even if an SQL error occurs.
180
181 · --host=host_name, -h host_name
182
183 Connect to the MySQL server on the given host.
184
185 · --html, -H
186
187 Produce HTML output.
188
189 · --ignore-spaces, -i
190
191 Ignore spaces after function names. The effect of this is described
192 in the discussion for the IGNORE_SPACE SQL mode (see Section 5.1.8,
193 “Server SQL Modes”).
194
195 · --line-numbers
196
197 Write line numbers for errors. Disable this with
198 --skip-line-numbers.
199
200 · --local-infile[={0|1}]
201
202 Enable or disable LOCAL capability for LOAD DATA INFILE. With no
203 value, the option enables LOCAL. The option may be given as
204 --local-infile=0 or --local-infile=1 to explicitly disable or
205 enable LOCAL. Enabling LOCAL has no effect if the server does not
206 also support it.
207
208 · --max-allowed-packet=num
209
210 Set the maximum packet length to send to or receive from the
211 server. (Default value is 16MB.)
212
213 · --max-join-size=num
214
215 Set the automatic limit for rows in a join when using
216 --safe-updates. (Default value is 1,000,000.)
217
218 · --named-commands, -G
219
220 Enable named mysql commands. Long-format commands are allowed, not
221 just short-format commands. For example, quit and \q both are
222 recognized. Use --skip-named-commands to disable named commands.
223 See the section called “MYSQL COMMANDS”.
224
225 ·· --net-buffer-lenght=size
226
227 Set the buffer size for TCP/IP and socket communication. (Default
228 value is 16KB.)
229
230 · --no-auto-rehash, -A
231
232 This has the same effect as --skip-auto-rehash. See the description
233 for --auto-rehash.
234
235 · --no-beep, -b
236
237 Do not beep when errors occur.
238
239 · --no-defaults
240
241 Do not read default options from any option file. This must be
242 given as the first argument.
243
244 · --no-named-commands, -g
245
246 Deprecated, use --disable-named-commands instead.
247 --no-named-commands is removed in MySQL 5.5.
248
249 · --no-pager
250
251 Deprecated form of --skip-pager. See the --pager option.
252 --no-pager is removed in MySQL 5.5.
253
254 · --no-tee
255
256 Deprecated form of --skip-tee. See the --tee option. --no-tee is
257 removed in MySQL 5.5.
258
259 · --one-database, -o
260
261 Ignore statements except those for the default database named on
262 the command line. This is useful for skipping updates to other
263 databases in the binary log.
264
265 · --pager[=command]
266
267 Use the given command for paging query output. If the command is
268 omitted, the default pager is the value of your PAGER environment
269 variable. Valid pagers are less, more, cat [> filename], and so
270 forth. This option works only on Unix and only in interactive mode.
271 To disable paging, use --skip-pager. the section called “MYSQL
272 COMMANDS”, discusses output paging further.
273
274 · --password[=password], -p[password]
275
276 The password to use when connecting to the server. If you use the
277 short option form (-p), you cannot have a space between the option
278 and the password. If you omit the password value following the
279 --password or -p option on the command line, mysql prompts for one.
280
281 Specifying a password on the command line should be considered
282 insecure. See Section 5.3.2.2, “End-User Guidelines for Password
283 Security”. You can use an option file to avoid giving the password
284 on the command line.
285
286 · --pipe, -W
287
288 On Windows, connect to the server via a named pipe. This option
289 applies only if the server supports named-pipe connections.
290
291 · --port=port_num, -P port_num
292
293 The TCP/IP port number to use for the connection.
294
295 · --print-defaults
296
297 Print the program argument list and exit. This must be given as the
298 first argument.
299
300 · --prompt=format_str
301
302 Set the prompt to the specified format. The default is mysql>. The
303 special sequences that the prompt can contain are described in the
304 section called “MYSQL COMMANDS”.
305
306 · --protocol={TCP|SOCKET|PIPE|MEMORY}
307
308 The connection protocol to use for connecting to the server. It is
309 useful when the other connection parameters normally would cause a
310 protocol to be used other than the one you want. For details on the
311 allowable values, see Section 4.2.2, “Connecting to the MySQL
312 Server”.
313
314 · --quick, -q
315
316 Do not cache each query result, print each row as it is received.
317 This may slow down the server if the output is suspended. With this
318 option, mysql does not use the history file.
319
320 · --raw, -r
321
322 For tabular output, the “boxing” around columns enables one column
323 value to be distinguished from another. For nontabular output (such
324 as is produced in batch mode or when the --batch or --silent option
325 is given), special characters are escaped in the output so they can
326 be identified easily. Newline, tab, NUL, and backslash are written
327 as \n, \t, \0, and \\. The --raw option disables this character
328 escaping.
329
330 The following example demonstrates tabular versus nontabular output
331 and the use of raw mode to disable escaping:
332
333 % mysql
334 mysql> SELECT CHAR(92);
335 +----------+
336 | CHAR(92) |
337 +----------+
338 | \ |
339 +----------+
340 % mysql -s
341 mysql> SELECT CHAR(92);
342 CHAR(92)
343 \\
344 % mysql -s -r
345 mysql> SELECT CHAR(92);
346 CHAR(92)
347 \
348
349 · --reconnect
350
351 If the connection to the server is lost, automatically try to
352 reconnect. A single reconnect attempt is made each time the
353 connection is lost. To suppress reconnection behavior, use
354 --skip-reconnect.
355
356 · --safe-updates, --i-am-a-dummy, -U
357
358 Allow only those UPDATE and DELETE statements that specify which
359 rows to modify by using key values. If you have set this option in
360 an option file, you can override it by using --safe-updates on the
361 command line. See the section called “MYSQL TIPS”, for more
362 information about this option.
363
364 · --secure-auth
365
366 Do not send passwords to the server in old (pre-4.1.1) format. This
367 prevents connections except for servers that use the newer password
368 format.
369
370 · --select-limit=limit
371
372 Set automatic limit for SELECT when using --safe-updates. (Default
373 value is 1,000.)
374
375 · --server-arg=name
376
377 Send name as a parameter to the embedded server.
378
379 · --show-warnings
380
381 Cause warnings to be shown after each statement if there are any.
382 This option applies to interactive and batch mode.
383
384 · --sigint-ignore
385
386 Ignore SIGINT signals (typically the result of typing Control-C).
387
388 · --silent, -s
389
390 Silent mode. Produce less output. This option can be given multiple
391 times to produce less and less output.
392
393 This option results in nontabular output format and escaping of
394 special characters. Escaping may be disabled by using raw mode; see
395 the description for the --raw option.
396
397 · --skip-auto-rehash
398
399 Disable automatic rehashing. Synonym for --disable-auto-rehash.
400
401 · --skip-column-names, -N
402
403 Do not write column names in results.
404
405 · --skip-line-numbers, -L
406
407 Do not write line numbers for errors. Useful when you want to
408 compare result files that include error messages.
409
410 · --socket=path, -S path
411
412 For connections to localhost, the Unix socket file to use, or, on
413 Windows, the name of the named pipe to use.
414
415 · --ssl*
416
417 Options that begin with --ssl specify whether to connect to the
418 server via SSL and indicate where to find SSL keys and
419 certificates. See Section 5.5.6.3, “SSL Command Options”.
420
421 · --table, -t
422
423 Display output in table format. This is the default for interactive
424 use, but can be used to produce table output in batch mode.
425
426 · --tee=file_name
427
428 Append a copy of output to the given file. This option works only
429 in interactive mode. the section called “MYSQL COMMANDS”,
430 discusses tee files further.
431
432 · --unbuffered, -n
433
434 Flush the buffer after each query.
435
436 · --user=user_name, -u user_name
437
438 The MySQL user name to use when connecting to the server.
439
440 · --verbose, -v
441
442 Verbose mode. Produce more output about what the program does. This
443 option can be given multiple times to produce more and more output.
444 (For example, -v -v -v produces table output format even in batch
445 mode.)
446
447 · --version, -V
448
449 Display version information and exit.
450
451 · --vertical, -E
452
453 Print query output rows vertically (one line per column value).
454 Without this option, you can specify vertical output for individual
455 statements by terminating them with \G.
456
457 · --wait, -w
458
459 If the connection cannot be established, wait and retry instead of
460 aborting.
461
462 · --xml, -X
463
464 Produce XML output.
465
466 Note
467 Prior to MySQL 5.1.12, there was no differentiation in the
468 output when using this option between columns containing the
469 NULL value and columns containing the string literal ´NULL´;
470 both were represented as
471
472 <field name="column_name">NULL</field>
473
474 Beginning with MySQL 5.1.12, the output when --xml is used with
475 mysql matches that of mysqldump --xml. See mysqldump(1) for
476 details.
477
478 Beginning with MySQL 5.1.18, the XML output also uses an XML
479 namespace, as shown here:
480
481 shell> mysql --xml -uroot -e "SHOW VARIABLES LIKE ´version%´"
482 <?xml version="1.0"?>
483 <resultset statement="SHOW VARIABLES LIKE ´version%´" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
484 <row>
485 <field name="Variable_name">version</field>
486 <field name="Value">5.0.40-debug</field>
487 </row>
488 <row>
489 <field name="Variable_name">version_comment</field>
490 <field name="Value">Source distribution</field>
491 </row>
492 <row>
493 <field name="Variable_name">version_compile_machine</field>
494 <field name="Value">i686</field>
495 </row>
496 <row>
497 <field name="Variable_name">version_compile_os</field>
498 <field name="Value">suse-linux-gnu</field>
499 </row>
500 </resultset>
501
502 (See Bug#25946[1].)
503
504 You can also set the following variables by using --var_name=value. The
505 --set-variable format is deprecated and is removed in MySQL 5.5.
506
507 · connect_timeout
508
509 The number of seconds before connection timeout. (Default value is
510 0.)
511
512 · max_allowed_packet
513
514 The maximum packet length to send to or receive from the server.
515 (Default value is 16MB.)
516
517 · max_join_size
518
519 The automatic limit for rows in a join when using --safe-updates.
520 (Default value is 1,000,000.)
521
522 · net_buffer_length
523
524 The buffer size for TCP/IP and socket communication. (Default value
525 is 16KB.)
526
527 · select_limit
528
529 The automatic limit for SELECT statements when using
530 --safe-updates. (Default value is 1,000.)
531
532 On Unix, the mysql client writes a record of executed statements to a
533 history file. By default, this file is named .mysql_history and is
534 created in your home directory. To specify a different file, set the
535 value of the MYSQL_HISTFILE environment variable.
536
537 The .mysql_history should be protected with a restrictive access mode
538 because sensitive information might be written to it, such as the text
539 of SQL statements that contain passwords. See Section 5.3.2.2, “End-
540 User Guidelines for Password Security”.
541
542 If you do not want to maintain a history file, first remove
543 .mysql_history if it exists, and then use either of the following
544 techniques:
545
546 · Set the MYSQL_HISTFILE variable to /dev/null. To cause this setting
547 to take effect each time you log in, put the setting in one of your
548 shell´s startup files.
549
550 · Create .mysql_history as a symbolic link to /dev/null:
551
552 shell> ln -s /dev/null $HOME/.mysql_history
553
554 You need do this only once.
555
557 mysql sends each SQL statement that you issue to the server to be
558 executed. There is also a set of commands that mysql itself interprets.
559 For a list of these commands, type help or \h at the mysql> prompt:
560
561 mysql> help
562 List of all MySQL commands:
563 Note that all text commands must be first on line and end with ´;´
564 ? (\?) Synonym for `help´.
565 clear (\c) Clear command.
566 connect (\r) Reconnect to the server. Optional arguments are db and host.
567 delimiter (\d) Set statement delimiter.
568 edit (\e) Edit command with $EDITOR.
569 ego (\G) Send command to mysql server, display result vertically.
570 exit (\q) Exit mysql. Same as quit.
571 go (\g) Send command to mysql server.
572 help (\h) Display this help.
573 nopager (\n) Disable pager, print to stdout.
574 notee (\t) Don´t write into outfile.
575 pager (\P) Set PAGER [to_pager]. Print the query results via PAGER.
576 print (\p) Print current command.
577 prompt (\R) Change your mysql prompt.
578 quit (\q) Quit mysql.
579 rehash (\#) Rebuild completion hash.
580 source (\.) Execute an SQL script file. Takes a file name as an argument.
581 status (\s) Get status information from the server.
582 system (\!) Execute a system shell command.
583 tee (\T) Set outfile [to_outfile]. Append everything into given
584 outfile.
585 use (\u) Use another database. Takes database name as argument.
586 charset (\C) Switch to another charset. Might be needed for processing
587 binlog with multi-byte charsets.
588 warnings (\W) Show warnings after every statement.
589 nowarning (\w) Don´t show warnings after every statement.
590 For server side help, type ´help contents´
591
592 Each command has both a long and short form. The long form is not case
593 sensitive; the short form is. The long form can be followed by an
594 optional semicolon terminator, but the short form should not.
595
596 The use of short-form commands within multi-line /* ... */ comments is
597 not supported.
598
599 · help [arg], \h [arg], \? [arg], ? [arg]
600
601 Display a help message listing the available mysql commands.
602
603 If you provide an argument to the help command, mysql uses it as a
604 search string to access server-side help from the contents of the
605 MySQL Reference Manual. For more information, see the section
606 called “MYSQL SERVER-SIDE HELP”.
607
608 · charset charset_name, \C charset_name
609
610 Change the default character set and issue a SET NAMES statement.
611 This enables the character set to remain synchronized on the client
612 and server if mysql is run with auto-reconnect enabled (which is
613 not recommended), because the specified character set is used for
614 reconnects. This command was added in MySQL 5.1.7.
615
616 · clear, \c
617
618 Clear the current input. Use this if you change your mind about
619 executing the statement that you are entering.
620
621 · connect [db_name host_name]], \r [db_name host_name]]
622
623 Reconnect to the server. The optional database name and host name
624 arguments may be given to specify the default database or the host
625 where the server is running. If omitted, the current values are
626 used.
627
628 · delimiter str, \d str
629
630 Change the string that mysql interprets as the separator between
631 SQL statements. The default is the semicolon character (“;”).
632
633 The delimiter can be specified as an unquoted or quoted argument.
634 Quoting can be done with either single quote (´) or douple quote
635 (") characters. To include a quote within a quoted string, either
636 quote the string with the other quote character or escape the quote
637 with a backslash (“\”) character. Backslash should be avoided
638 outside of quoted strings because it is the escape character for
639 MySQL. For an unquoted argument, the delmiter is read up to the
640 first space or end of line. For a quoted argument, the delimiter is
641 read up to the matching quote on the line.
642
643 When the delimiter recognized by mysql is set to something other
644 than the default of “;”, instances of that character are sent to
645 the server without interpretation. However, the server itself still
646 interprets “;” as a statement delimiter and processes statements
647 accordingly. This behavior on the server side comes into play for
648 multiple-statement execution (see Section 21.9.12, “C API Support
649 for Multiple Statement Execution”), and for parsing the body of
650 stored procedures and functions, triggers, and events (see
651 Section 19.1, “Defining Stored Programs”).
652
653 · edit, \e
654
655 Edit the current input statement. mysql checks the values of the
656 EDITOR and VISUAL environment variables to determine which editor
657 to use. The default editor is vi if neither variable is set.
658
659 The edit command works only in Unix.
660
661 · ego, \G
662
663 Send the current statement to the server to be executed and display
664 the result using vertical format.
665
666 · exit, \q
667
668 Exit mysql.
669
670 · go, \g
671
672 Send the current statement to the server to be executed.
673
674 · nopager, \n
675
676 Disable output paging. See the description for pager.
677
678 The nopager command works only in Unix.
679
680 · notee, \t
681
682 Disable output copying to the tee file. See the description for
683 tee.
684
685 · nowarning, \w
686
687 Enable display of warnings after each statement.
688
689 · pager [command], \P [command]
690
691 Enable output paging. By using the --pager option when you invoke
692 mysql, it is possible to browse or search query results in
693 interactive mode with Unix programs such as less, more, or any
694 other similar program. If you specify no value for the option,
695 mysql checks the value of the PAGER environment variable and sets
696 the pager to that. Pager functionality works only in interactive
697 mode.
698
699 Output paging can be enabled interactively with the pager command
700 and disabled with nopager. The command takes an optional argument;
701 if given, the paging program is set to that. With no argument, the
702 pager is set to the pager that was set on the command line, or
703 stdout if no pager was specified.
704
705 Output paging works only in Unix because it uses the popen()
706 function, which does not exist on Windows. For Windows, the tee
707 option can be used instead to save query output, although it is not
708 as convenient as pager for browsing output in some situations.
709
710 · print, \p
711
712 Print the current input statement without executing it.
713
714 · prompt [str], \R [str]
715
716 Reconfigure the mysql prompt to the given string. The special
717 character sequences that can be used in the prompt are described
718 later in this section.
719
720 If you specify the prompt command with no argument, mysql resets
721 the prompt to the default of mysql>.
722
723 · quit, \q
724
725 Exit mysql.
726
727 · rehash, \#
728
729 Rebuild the completion hash that enables database, table, and
730 column name completion while you are entering statements. (See the
731 description for the --auto-rehash option.)
732
733 · source file_name, \. file_name
734
735 Read the named file and executes the statements contained therein.
736 On Windows, you can specify path name separators as / or \\.
737
738 · status, \s
739
740 Provide status information about the connection and the server you
741 are using. If you are running in --safe-updates mode, status also
742 prints the values for the mysql variables that affect your queries.
743
744 · system command, \! command
745
746 Execute the given command using your default command interpreter.
747
748 The system command works only in Unix.
749
750 · tee [file_name], \T [file_name]
751
752 By using the --tee option when you invoke mysql, you can log
753 statements and their output. All the data displayed on the screen
754 is appended into a given file. This can be very useful for
755 debugging purposes also. mysql flushes results to the file after
756 each statement, just before it prints its next prompt. Tee
757 functionality works only in interactive mode.
758
759 You can enable this feature interactively with the tee command.
760 Without a parameter, the previous file is used. The tee file can be
761 disabled with the notee command. Executing tee again re-enables
762 logging.
763
764 · use db_name, \u db_name
765
766 Use db_name as the default database.
767
768 · warnings, \W
769
770 Enable display of warnings after each statement (if there are any).
771
772 Here are a few tips about the pager command:
773
774 · You can use it to write to a file and the results go only to the
775 file:
776
777 mysql> pager cat > /tmp/log.txt
778
779 You can also pass any options for the program that you want to use
780 as your pager:
781
782 mysql> pager less -n -i -S
783
784 · In the preceding example, note the -S option. You may find it very
785 useful for browsing wide query results. Sometimes a very wide
786 result set is difficult to read on the screen. The -S option to
787 less can make the result set much more readable because you can
788 scroll it horizontally using the left-arrow and right-arrow keys.
789 You can also use -S interactively within less to switch the
790 horizontal-browse mode on and off. For more information, read the
791 less manual page:
792
793 shell> man less
794
795 · The -F and -X options may be used with less to cause it to exit if
796 output fits on one screen, which is convenient when no scrolling is
797 necessary:
798
799 mysql> pager less -n -i -S -F -X
800
801 · You can specify very complex pager commands for handling query
802 output:
803
804 mysql> pager cat | tee /dr1/tmp/res.txt \
805 | tee /dr2/tmp/res2.txt | less -n -i -S
806
807 In this example, the command would send query results to two files
808 in two different directories on two different file systems mounted
809 on /dr1 and /dr2, yet still display the results onscreen via less.
810
811 You can also combine the tee and pager functions. Have a tee file
812 enabled and pager set to less, and you are able to browse the results
813 using the less program and still have everything appended into a file
814 the same time. The difference between the Unix tee used with the pager
815 command and the mysql built-in tee command is that the built-in tee
816 works even if you do not have the Unix tee available. The built-in tee
817 also logs everything that is printed on the screen, whereas the Unix
818 tee used with pager does not log quite that much. Additionally, tee
819 file logging can be turned on and off interactively from within mysql.
820 This is useful when you want to log some queries to a file, but not
821 others.
822
823 The prompt command reconfigures the default mysql> prompt. The string
824 for defining the prompt can contain the following special sequences.
825
826 ┌───────┬────────────────────────────┐
827 │Option │ Description │
828 ├───────┼────────────────────────────┤
829 │\c │ A counter that increments │
830 │ │ for each statement you │
831 │ │ issue │
832 ├───────┼────────────────────────────┤
833 │\D │ The full current date │
834 ├───────┼────────────────────────────┤
835 │\d │ The default database │
836 ├───────┼────────────────────────────┤
837 │\h │ The server host │
838 ├───────┼────────────────────────────┤
839 │\l │ The current delimiter (new │
840 │ │ in 5.1.12) │
841 ├───────┼────────────────────────────┤
842 │\m │ Minutes of the current │
843 │ │ time │
844 ├───────┼────────────────────────────┤
845 │\n │ A newline character │
846 ├───────┼────────────────────────────┤
847 │\O │ The current month in │
848 │ │ three-letter format (Jan, │
849 │ │ Feb, ...) │
850 ├───────┼────────────────────────────┤
851 │\o │ The current month in │
852 │ │ numeric format │
853 ├───────┼────────────────────────────┤
854 │\P │ am/pm │
855 ├───────┼────────────────────────────┤
856 │\p │ The current TCP/IP port or │
857 │ │ socket file │
858 ├───────┼────────────────────────────┤
859 │\R │ The current time, in │
860 │ │ 24-hour military time │
861 │ │ (0–23) │
862 ├───────┼────────────────────────────┤
863 │\r │ The current time, standard │
864 │ │ 12-hour time (1–12) │
865 ├───────┼────────────────────────────┤
866 │\S │ Semicolon │
867 ├───────┼────────────────────────────┤
868 │\s │ Seconds of the current │
869 │ │ time │
870 ├───────┼────────────────────────────┤
871 │\t │ A tab character │
872 ├───────┼────────────────────────────┤
873 │\U │ │
874 │ │ Your full │
875 │ │ user_name@host_name │
876 │ │ account name │
877 ├───────┼────────────────────────────┤
878 │\u │ Your user name │
879 ├───────┼────────────────────────────┤
880 │\v │ The server version │
881 ├───────┼────────────────────────────┤
882 │\w │ The current day of the │
883 │ │ week in three-letter │
884 │ │ format (Mon, Tue, ...) │
885 ├───────┼────────────────────────────┤
886 │\Y │ The current year, four │
887 │ │ digits │
888 ├───────┼────────────────────────────┤
889 │\y │ The current year, two │
890 │ │ digits │
891 ├───────┼────────────────────────────┤
892 │\_ │ A space │
893 ├───────┼────────────────────────────┤
894 │\ │ A space (a space follows │
895 │ │ the backslash) │
896 ├───────┼────────────────────────────┤
897 │\´ │ Single quote │
898 ├───────┼────────────────────────────┤
899 │\" │ Double quote │
900 ├───────┼────────────────────────────┤
901 │\\ │ A literal “\” backslash │
902 │ │ character │
903 ├───────┼────────────────────────────┤
904 │\x │ │
905 │ │ x, for any “x” not │
906 │ │ listed above │
907 └───────┴────────────────────────────┘
908
909 You can set the prompt in several ways:
910
911 · Use an environment variable. You can set the MYSQL_PS1 environment
912 variable to a prompt string. For example:
913
914 shell> export MYSQL_PS1="(\u@\h) [\d]> "
915
916 · Use a command-line option. You can set the --prompt option on the
917 command line to mysql. For example:
918
919 shell> mysql --prompt="(\u@\h) [\d]> "
920 (user@host) [database]>
921
922 · Use an option file. You can set the prompt option in the [mysql]
923 group of any MySQL option file, such as /etc/my.cnf or the .my.cnf
924 file in your home directory. For example:
925
926 [mysql]
927 prompt=(\\u@\\h) [\\d]>\\_
928
929 In this example, note that the backslashes are doubled. If you set
930 the prompt using the prompt option in an option file, it is
931 advisable to double the backslashes when using the special prompt
932 options. There is some overlap in the set of allowable prompt
933 options and the set of special escape sequences that are recognized
934 in option files. (The rules for escape sequences in option files
935 are listed in Section 4.2.3.3, “Using Option Files”.) The overlap
936 may cause you problems if you use single backslashes. For example,
937 \s is interpreted as a space rather than as the current seconds
938 value. The following example shows how to define a prompt within an
939 option file to include the current time in HH:MM:SS> format:
940
941 [mysql]
942 prompt="\\r:\\m:\\s> "
943
944 · Set the prompt interactively. You can change your prompt
945 interactively by using the prompt (or \R) command. For example:
946
947 mysql> prompt (\u@\h) [\d]>\_
948 PROMPT set to ´(\u@\h) [\d]>\_´
949 (user@host) [database]>
950 (user@host) [database]> prompt
951 Returning to default PROMPT of mysql>
952 mysql>
953
955 mysql> help search_string
956
957 If you provide an argument to the help command, mysql uses it as a
958 search string to access server-side help from the contents of the MySQL
959 Reference Manual. The proper operation of this command requires that
960 the help tables in the mysql database be initialized with help topic
961 information (see Section 5.1.9, “Server-Side Help”).
962
963 If there is no match for the search string, the search fails:
964
965 mysql> help me
966 Nothing found
967 Please try to run ´help contents´ for a list of all accessible topics
968
969 Use help contents to see a list of the help categories:
970
971 mysql> help contents
972 You asked for help about help category: "Contents"
973 For more information, type ´help <item>´, where <item> is one of the
974 following categories:
975 Account Management
976 Administration
977 Data Definition
978 Data Manipulation
979 Data Types
980 Functions
981 Functions and Modifiers for Use with GROUP BY
982 Geographic Features
983 Language Structure
984 Plugins
985 Storage Engines
986 Stored Routines
987 Table Maintenance
988 Transactions
989 Triggers
990
991 If the search string matches multiple items, mysql shows a list of
992 matching topics:
993
994 mysql> help logs
995 Many help items for your request exist.
996 To make a more specific request, please type ´help <item>´,
997 where <item> is one of the following topics:
998 SHOW
999 SHOW BINARY LOGS
1000 SHOW ENGINE
1001 SHOW LOGS
1002
1003 Use a topic as the search string to see the help entry for that topic:
1004
1005 mysql> help show binary logs
1006 Name: ´SHOW BINARY LOGS´
1007 Description:
1008 Syntax:
1009 SHOW BINARY LOGS
1010 SHOW MASTER LOGS
1011 Lists the binary log files on the server. This statement is used as
1012 part of the procedure described in [purge-binary-logs], that shows how
1013 to determine which logs can be purged.
1014 mysql> SHOW BINARY LOGS;
1015 +---------------+-----------+
1016 | Log_name | File_size |
1017 +---------------+-----------+
1018 | binlog.000015 | 724935 |
1019 | binlog.000016 | 733481 |
1020 +---------------+-----------+
1021
1023 The mysql client typically is used interactively, like this:
1024
1025 shell> mysql db_name
1026
1027 However, it is also possible to put your SQL statements in a file and
1028 then tell mysql to read its input from that file. To do so, create a
1029 text file text_file that contains the statements you wish to execute.
1030 Then invoke mysql as shown here:
1031
1032 shell> mysql db_name < text_file
1033
1034 If you place a USE db_name statement as the first statement in the
1035 file, it is unnecessary to specify the database name on the command
1036 line:
1037
1038 shell> mysql < text_file
1039
1040 If you are already running mysql, you can execute an SQL script file
1041 using the source command or \. command:
1042
1043 mysql> source file_name
1044 mysql> \. file_name
1045
1046 Sometimes you may want your script to display progress information to
1047 the user. For this you can insert statements like this:
1048
1049 SELECT ´<info_to_display>´ AS ´ ´;
1050
1051 The statement shown outputs <info_to_display>.
1052
1053 You can also invoke mysql with the --verbose option, which causes each
1054 statement to be displayed before the result that it produces.
1055
1056 As of MySQL 5.1.23, mysql ignores Unicode byte order mark (BOM)
1057 characters at the beginning of input files. Previously, it read them
1058 and sent them to the server, resulting in a syntax error. Presence of a
1059 BOM does not cause mysql to change its default character set. To do
1060 that, invoke mysql with an option such as --default-character-set=utf8.
1061
1062 For more information about batch mode, see Section 3.5, “Using mysql in
1063 Batch Mode”.
1064
1066 This section describes some techniques that can help you use mysql more
1067 effectively.
1068
1069 Displaying Query Results Vertically
1070 Some query results are much more readable when displayed vertically,
1071 instead of in the usual horizontal table format. Queries can be
1072 displayed vertically by terminating the query with \G instead of a
1073 semicolon. For example, longer text values that include newlines often
1074 are much easier to read with vertical output:
1075
1076 mysql> SELECT * FROM mails WHERE LENGTH(txt) < 300 LIMIT 300,1\G
1077 *************************** 1. row ***************************
1078 msg_nro: 3068
1079 date: 2000-03-01 23:29:50
1080 time_zone: +0200
1081 mail_from: Monty
1082 reply: monty@no.spam.com
1083 mail_to: "Thimble Smith" <tim@no.spam.com>
1084 sbj: UTF-8
1085 txt: >>>>> "Thimble" == Thimble Smith writes:
1086 Thimble> Hi. I think this is a good idea. Is anyone familiar
1087 Thimble> with UTF-8 or Unicode? Otherwise, I´ll put this on my
1088 Thimble> TODO list and see what happens.
1089 Yes, please do that.
1090 Regards,
1091 Monty
1092 file: inbox-jani-1
1093 hash: 190402944
1094 1 row in set (0.09 sec)
1095
1096 Using the --safe-updates Option
1097 For beginners, a useful startup option is --safe-updates (or
1098 --i-am-a-dummy, which has the same effect). It is helpful for cases
1099 when you might have issued a DELETE FROM tbl_name statement but
1100 forgotten the WHERE clause. Normally, such a statement deletes all rows
1101 from the table. With --safe-updates, you can delete rows only by
1102 specifying the key values that identify them. This helps prevent
1103 accidents.
1104
1105 When you use the --safe-updates option, mysql issues the following
1106 statement when it connects to the MySQL server:
1107
1108 SET sql_safe_updates=1, sql_select_limit=1000, sql_max_join_size=1000000;
1109
1110 See Section 5.1.5, “Session System Variables”.
1111
1112 The SET statement has the following effects:
1113
1114 · You are not allowed to execute an UPDATE or DELETE statement unless
1115 you specify a key constraint in the WHERE clause or provide a LIMIT
1116 clause (or both). For example:
1117
1118 UPDATE tbl_name SET not_key_column=val WHERE key_column=val;
1119 UPDATE tbl_name SET not_key_column=val LIMIT 1;
1120
1121 · The server limits all large SELECT results to 1,000 rows unless the
1122 statement includes a LIMIT clause.
1123
1124 · The server aborts multiple-table SELECT statements that probably
1125 need to examine more than 1,000,000 row combinations.
1126
1127 To specify limits different from 1,000 and 1,000,000, you can override
1128 the defaults by using the --select-limit and --max-join-size options:
1129
1130 shell> mysql --safe-updates --select-limit=500 --max-join-size=10000
1131
1132 Disabling mysql Auto-Reconnect
1133 If the mysql client loses its connection to the server while sending a
1134 statement, it immediately and automatically tries to reconnect once to
1135 the server and send the statement again. However, even if mysql
1136 succeeds in reconnecting, your first connection has ended and all your
1137 previous session objects and settings are lost: temporary tables, the
1138 autocommit mode, and user-defined and session variables. Also, any
1139 current transaction rolls back. This behavior may be dangerous for you,
1140 as in the following example where the server was shut down and
1141 restarted between the first and second statements without you knowing
1142 it:
1143
1144 mysql> SET @a=1;
1145 Query OK, 0 rows affected (0.05 sec)
1146 mysql> INSERT INTO t VALUES(@a);
1147 ERROR 2006: MySQL server has gone away
1148 No connection. Trying to reconnect...
1149 Connection id: 1
1150 Current database: test
1151 Query OK, 1 row affected (1.30 sec)
1152 mysql> SELECT * FROM t;
1153 +------+
1154 | a |
1155 +------+
1156 | NULL |
1157 +------+
1158 1 row in set (0.05 sec)
1159
1160 The @a user variable has been lost with the connection, and after the
1161 reconnection it is undefined. If it is important to have mysql
1162 terminate with an error if the connection has been lost, you can start
1163 the mysql client with the --skip-reconnect option.
1164
1165 For more information about auto-reconnect and its effect on state
1166 information when a reconnection occurs, see Section 21.9.11,
1167 “Controlling Automatic Reconnection Behavior”.
1168
1170 Copyright 2007-2008 MySQL AB, 2008-2010 Sun Microsystems, Inc.
1171
1172 This documentation is free software; you can redistribute it and/or
1173 modify it only under the terms of the GNU General Public License as
1174 published by the Free Software Foundation; version 2 of the License.
1175
1176 This documentation is distributed in the hope that it will be useful,
1177 but WITHOUT ANY WARRANTY; without even the implied warranty of
1178 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1179 General Public License for more details.
1180
1181 You should have received a copy of the GNU General Public License along
1182 with the program; if not, write to the Free Software Foundation, Inc.,
1183 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA or see
1184 http://www.gnu.org/licenses/.
1185
1186
1188 1. Bug#25946
1189 http://bugs.mysql.com/bug.php?id=25946
1190
1192 For more information, please refer to the MySQL Reference Manual, which
1193 may already be installed locally and which is also available online at
1194 http://dev.mysql.com/doc/.
1195
1197 Sun Microsystems, Inc. (http://www.mysql.com/).
1198
1199
1200
1201MySQL 5.1 04/06/2010 MYSQL(1)