This chapter provides a brief overview of the MySQL command-line programs provided by Oracle Corporation. It also discusses the general syntax for specifying options when you run these programs. Most programs have options that are specific to their own operation, but the option syntax is similar for all of them. Finally, the chapter provides more detailed descriptions of individual programs, including which options they recognize. Show
4.1. Overview of MySQL ProgramsThere are many different programs in a MySQL installation. This section provides a brief overview of them. Later sections provide a more detailed description of each one. Each program's description indicates its invocation syntax and the options that it supports. Most MySQL distributions include all of these programs, except for those programs that are platform-specific. (For example, the server startup scripts are not used on Windows.) The exception is that RPM distributions are more specialized. There is one RPM for the server, another for client programs, and so forth. If you appear to be missing one or more programs, see Chapter 2, Installing and Upgrading MySQL, for information on types of distributions and what they contain. It may be that you have a distribution that does not include all programs and you need to install an additional package. Each MySQL program takes many different options. Most programs provide a shell>50 option that you can use to get a description of the program's different options. For example, try . You can override default option values for MySQL programs by specifying options on the command line or in an option file. See , for general information on invoking programs and specifying program options. The MySQL server, , is the main program that does most of the work in a MySQL installation. The server is accompanied by several related scripts that assist you in starting and stopping the server: Several programs perform setup operations during MySQL installation or upgrading: MySQL client programs that connect to the MySQL server: MySQL administrative and utility programs: MySQL program-development utilities: Miscellaneous utilities: Oracle Corporation also provides the MySQL Workbench GUI tool, which is used to administer MySQL servers and databases, to create, execute, and evaluate queries, and to migrate schemas and data from other relational database management systems for use with MySQL. Additional GUI tools include and MySQL for Excel. MySQL client programs that communicate with the server using the MySQL client/server library use the following environment variables. Environment VariableMeaning shell>51The default Unix socket file; used for connections to shell>52 shell>53The default port number; used for TCP/IP connections shell>54The default password shell>55Debug trace options when debugging shell>56The directory where temporary tables and files are created For a full list of environment variables used by MySQL programs, see . Use of shell>54 is insecure. See . 4.2. Using MySQL Programs4.2.1. Invoking MySQL ProgramsTo invoke a MySQL program from the command line (that is, from your shell or command prompt), enter the program name followed by any options or other arguments needed to instruct the program what you want it to do. The following commands show some sample program invocations. “ shell>58” represents the prompt for your command interpreter; it is not part of what you type. The particular prompt you see depends on your command interpreter. Typical prompts are shell>59 for sh, ksh, or bash, shell>60 for csh or tcsh, and shell>61 for the Windows command.com or cmd.exe command interpreters. shell> Arguments that begin with a single or double dash (“ shell>62”, “ shell>63”) specify program options. Options typically indicate the type of connection a program should make to the server or affect its operational mode. Option syntax is described in . Nonoption arguments (arguments with no leading dash) provide additional information to the program. For example, the program interprets the first nonoption argument as a database name, so the command shell>64 indicates that you want to use the shell>65 database. Later sections that describe individual programs indicate which options a program supports and describe the meaning of any additional nonoption arguments. Some options are common to a number of programs. The most frequently used of these are the (or shell>67), (or shell>69), and (or shell>71) options that specify connection parameters. They indicate the host where the MySQL server is running, and the user name and password of your MySQL account. All MySQL client programs understand these options; they enable you to specify which server to connect to and the account to use on that server. Other connection options are (or shell>73) to specify a TCP/IP port number and (or shell>75) to specify a Unix socket file on Unix (or named pipe name on Windows). For more information on options that specify connection options, see . You may find it necessary to invoke MySQL programs using the path name to the shell>76 directory in which they are installed. This is likely to be the case if you get a “program not found” error whenever you attempt to run a MySQL program from any directory other than the shell>76 directory. To make it more convenient to use MySQL, you can add the path name of the shell>76 directory to your shell>79 environment variable setting. That enables you to run a program by typing only its name, not its entire path name. For example, if is installed in shell>80, you can run the program by invoking it as , and it is not necessary to invoke it as /usr/local/mysql/bin/mysql. Consult the documentation for your command interpreter for instructions on setting your shell>79 variable. The syntax for setting environment variables is interpreter-specific. (Some information is given in .) After modifying your shell>79 setting, open a new console window on Windows or log in again on Unix so that the setting goes into effect. 4.2.2. Connecting to the MySQL ServerFor a client program to be able to connect to the MySQL server, it must use the proper connection parameters, such as the name of the host where the server is running and the user name and password of your MySQL account. Each connection parameter has a default value, but you can override them as necessary using program options specified either on the command line or in an option file. The examples here use the client program, but the principles apply to other clients such as , , or . This command invokes without specifying any connection parameters explicitly: shell> Because there are no parameter options, the default values apply:
To specify the host name and user name explicitly, as well as a password, supply appropriate options on the command line: shell> For password options, the password value is optional:
As just mentioned, including the password value on the command line can be a security risk. To avoid this problem, specify the shell>70 or shell>71 option without any following password value: shell> When the password option has no password value, the client program prints a prompt and waits for you to enter the password. (In these examples, shell>95 is not interpreted as a password because it is separated from the preceding password option by a space.) On some systems, the library routine that MySQL uses to prompt for a password automatically limits the password to eight characters. That is a problem with the system library, not with MySQL. Internally, MySQL does not have any limit for the length of the password. To work around the problem, change your MySQL password to a value that is eight or fewer characters long, or put your password in an option file. On Unix, MySQL programs treat the host name shell>52 specially, in a way that is likely different from what you expect compared to other network-based programs. For connections to shell>52, MySQL programs attempt to connect to the local server by using a Unix socket file. This occurs even if a or shell>73 option is given to specify a port number. To ensure that the client makes a TCP/IP connection to the local server, use or shell>67 to specify a host name value of shell>02, or the IP address or name of the local server. You can also specify the connection protocol explicitly, even for shell>52, by using the option. For example: shell> The option enables you to establish a particular type of connection even when the other options would normally default to some other protocol. If the server is configured to accept IPv6 connections, client can connect over IPv6 using . See . On Windows, you can force a MySQL client to use a named-pipe connection by specifying the or option, or by specifying shell>09 (period) as the host name. If named-pipe connections are not enabled, an error occurs. Use the option to specify the name of the pipe if you do not want to use the default pipe name. Connections to remote servers always use TCP/IP. This command connects to the server running on shell>11 using the default port number (3306): shell> To specify a port number explicitly, use the or shell>73 option: shell> You can specify a port number for connections to a local server, too. However, as indicated previously, connections to shell>52 on Unix will use a socket file by default. You will need to force a TCP/IP connection as already described or any option that specifies a port number will be ignored. For this command, the program uses a socket file on Unix and the option is ignored: shell> To cause the port number to be used, invoke the program in either of these ways: shell> The following list summarizes the options that can be used to control how client programs connect to the server:
It is possible to specify different default values to be used when you make a connection so that you need not enter them on the command line each time you invoke a client program. This can be done in a couple of ways:
4.2.3. Specifying Program OptionsThere are several ways to specify options for MySQL programs:
Options are processed in order, so if an option is specified multiple times, the last occurrence takes precedence. The following command causes to connect to the server running on shell>52: shell> 2If conflicting or related options are given, later options take precedence over earlier options. The following command runs in “no column names” mode: shell> 3MySQL programs determine which options are given first by examining environment variables, then by reading option files, and then by checking the command line. This means that environment variables have the lowest precedence and command-line options the highest. You can take advantage of the way that MySQL programs process options by specifying default option values for a program in an option file. That enables you to avoid typing them each time you run the program while enabling you to override the defaults if necessary by using command-line options. An option can be specified by writing it in full or as any unambiguous prefix. For example, the option can be given to as shell>56, but not as shell>57 because the latter is ambiguous: shell> 4Be aware that the use of option prefixes can cause problems in the event that new options are implemented for a program. A prefix that is unambiguous now might become ambiguous in the future. 4.2.3.1. Using Options on the Command LineProgram options specified on the command line follow these rules:
Option values that contain spaces must be quoted when given on the command line. For example, the (or shell>83) option can be used with to pass SQL statements to the server. When this option is used, executes the statements in the option value and exits. The statements must be enclosed by quotation marks. For example, you can use the following command to obtain a list of user accounts: shell> 7Note that the long form () is followed by an equals sign ( shell>66). If you wish to use quoted values within a statement, you will either need to escape the inner quotation marks, or use a different type of quotation marks within the statement from those used to quote the statement itself. The capabilities of your command processor dictate your choices for whether you can use single or double quotation marks and the syntax for escaping quote characters. For example, if your command processor supports quoting with single or double quotation marks, you can use double quotation marks around the statement, and single quotation marks for any quoted values within the statement. Multiple SQL statements may be passed in the option value on the command line, separated by semicolons: shell> 84.2.3.2. Program Option ModifiersSome options are “boolean” and control behavior that can be turned on or off. For example, the client supports a option that determines whether or not to display a row of column names at the beginning of query results. By default, this option is enabled. However, you may want to disable it in some instances, such as when sending the output of into another program that expects to see only data and not an initial header line. To disable column names, you can specify the option using any of these forms: shell> 9The shell>87 and shell>88 prefixes and the shell>89 suffix all have the same effect: They turn the option off. The “enabled” form of the option may be specified in any of these ways: shell>0 As of MySQL 5.6.2, the values shell>90, shell>91, shell>92, and shell>93 are also recognized for boolean options (not case sensitive). If an option is prefixed by shell>94, a program does not exit with an error if it does not recognize the option, but instead issues only a warning: shell>1 The shell>94 prefix can be useful when you run programs from multiple installations of MySQL on the same machine and list options in an option file, An option that may not be recognized by all versions of a program can be given using the shell>94 prefix (or shell>97 in an option file). Versions of the program that recognize the option process it normally, and versions that do not recognize it issue a warning and ignore it. enables a limit to be placed on how large client programs can set dynamic system variables. To do this, use a shell>98 prefix with the variable name. For example, shell>99 prevents any client from making the query cache size larger than 4MB. 4.2.3.3. Using Option FilesMost MySQL programs can read startup options from option files (also sometimes called configuration files). Option files provide a convenient way to specify commonly used options so that they need not be entered on the command line each time you run a program. For the MySQL server, MySQL provides a number of . To determine whether a program reads option files, invoke it with the shell>50 option. (For , use and .) If the program reads option files, the help message indicates which files it looks for and which option groups it recognizes. The shell>03 file that contains login path options is created by the utility. See . A “login path” is an option group that permits only a limited set of options: shell>04, shell>05, and shell>06. Client programs specify which login path to read from shell>03 using the option. On Windows, MySQL programs read startup options from the following files, in the specified order (top items are used first). File NamePurpose shell>09\my.ini, shell>09\my.cnfGlobal options shell>11, shell>12Global options shell>13\my.ini, shell>13\my.cnfGlobal options shell>15The file specified with , if any shell>17\.mylogin.cnfLogin path options shell>18 represents the location of your Windows directory. This is commonly shell>19. You can determine its exact location from the value of the shell>18 environment variable using the following command: shell>2 shell>21 represents the MySQL installation directory. This is typically shell>22\MySQL\MySQL 5.6 Server where shell>23 represents the programs directory (usually shell>24 on English-language versions of Windows), when MySQL 5.6 has been installed using the installation and configuration wizards. See . shell>25 represents the value of the Windows application data directory. You can determine its exact location from the value of the shell>25 environment variable using the following command: shell>3 On Unix, Linux and Mac OS X, MySQL programs read startup options from the following files, in the specified order (top items are used first). File NamePurpose shell>27Global options shell>28Global options shell>29/my.cnfGlobal options shell>30Server-specific options shell>15The file specified with , if any shell>33User-specific options shell>34Login path options shell>35 represents the current user's home directory (the value of shell>36). shell>37 represents the directory specified with the option to CMake when MySQL was built. By default, this is the shell>39 directory located under the compiled-in installation directory. shell>40 is an environment variable containing the path to the directory in which the server-specific shell>41 file resides. If shell>40 is not set and you start the server using the program, attempts to set shell>40 as follows:
In MySQL 5.6, use of shell>45 as the location for shell>41 is deprecated. Typically, shell>45 is shell>59 for a binary installation or shell>60 for a source installation. Note that this is the data directory location that was specified at configuration time, not the one specified with the option when starts. Use of at runtime has no effect on where the server looks for option files, because it looks for them before processing any options. MySQL looks for option files in the order just described and reads any that exist. If an option file that you want to use does not exist, create it with a plain text editor. If multiple instances of a given option are found, the last instance takes precedence. There is one exception: For , the first instance of the option is used as a security precaution, to prevent a user specified in an option file from being overridden on the command line. On Unix platforms, MySQL ignores configuration files that are world-writable. This is intentional as a security measure. Any long option that may be given on the command line when running a MySQL program can be given in an option file as well. To get the list of available options for a program, run it with the shell>50 option. The syntax for specifying options in an option file is similar to command-line syntax (see ). However, in an option file, you omit the leading two dashes from the option name and you specify only one option per line. For example, shell>65 and shell>65 on the command line should be specified as shell>67 and shell>68 on separate lines in an option file. To specify an option of the form shell>69 in an option file, write it as shell>70. Empty lines in option files are ignored. Nonempty lines can take any of the following forms:
Leading and trailing spaces are automatically deleted from option names and values. You can use the escape sequences “ shell>86”, “ shell>87”, “ shell>88”, “ shell>89”, “ shell>90”, and “ shell>91” in option values to represent the backspace, tab, newline, carriage return, backslash, and space characters. The escaping rules in option files are:
The preceding rules mean that a literal backslash can be given as “ shell>90”, or as “ shell>95” if it is not followed by a valid escape sequence character. The rules for escape sequences in option files differ slightly from the rules for escape sequences in string literals in SQL statements. In the latter context, if “ shell>96” is not a value escape sequence character, “ \ ” becomes “shell>96” rather than “ \ ”. See .The escaping rules for option file values are especially pertinent for Windows path names, which use “ shell>95” as a path name separator. A separator in a Windows path name must be written as “ shell>90” if it is followed by an escape sequence character. It can be written as “ shell>90” or “ shell>95” if it is not. Alternatively, “ shell> 04” may be used in Windows path names and will be treated as “shell>95”. Suppose that you want to specify a base directory of shell> 06 in an option file. This can be done several ways. Some examples:shell>4 If an option group name is the same as a program name, options in the group apply specifically to that program. For example, the shell> 07 and shell> 08 groups apply to the server and the client program, respectively.The shell>50 option group is read by all client programs (but not by ). This enables you to specify options that apply to all clients. For example, shell>50 is the perfect group to use to specify the password that you use to connect to the server. (But make sure that the option file is readable and writable only by yourself, so that other people cannot find out your password.) Be sure not to put an option in the shell>50 group unless it is recognized by all client programs that you use. Programs that do not understand the option quit after displaying an error message if you try to run them. Here is a typical global option file: shell>5 The preceding option file uses shell> 12=shell>81 syntax for the lines that set the and variables. Here is a typical user option file: shell>6 If you want to create option groups that should be read by servers from a specific MySQL release series only, you can do this by using groups with names of shell> 16, shell> 17, and so forth. The following group indicates that the shell> 18 option should be used only by MySQL servers with 5.6.x version numbers:shell>7 It is possible to use shell> 19 directives in option files to include other option files and shell> 20 to search specific directories for option files. For example, to include the shell> 21 file, use the following directive:shell>8 To search the shell> 22 directory and read option files found there, use this directive:shell>9 There is no guarantee about the order in which the option files in the directory will be read. Currently, any files to be found and included using the shell> 20 directive on Unix operating systems must have file names ending in shell> 24. On Windows, this directive checks for files with the shell> 25 or shell> 24 extension.Write the contents of an included option file like any other option file. That is, it should contain groups of options, each preceded by a shell>76] line that indicates the program to which the options apply. While an included file is being processed, only those options in groups that the current program is looking for are used. Other groups are ignored. Suppose that a shell>41 file contains this line: shell>8 And suppose that shell> 21 looks like this:shell>1 If shell>41 is processed by , only the shell> 07 group in shell> 21 is used. If the file is processed by , only the shell> 33 group is used. If the file is processed by any other program, no options in shell> 21 are used.The shell> 20 directive is processed similarly except that all option files in the named directory are read.4.2.3.4. Command-Line Options that Affect Option-File HandlingMost MySQL programs that support option files handle the following options. They affect option-file handling, so they must be given on the command line and not in an option file. To work properly, each of these options must immediately follow the command name, with these exceptions: When specifying file names, you should avoid the use of the “ shell>35” shell metacharacter because it might not be interpreted as you expect.
4.2.3.5. Using Options to Set Program VariablesMany MySQL programs have internal variables that can be set at runtime using the statement. See , and . Most of these program variables also can be set at server startup by using the same syntax that applies to specifying program options. For example, has a shell> 15 variable that controls the maximum size of its communication buffer. To set the shell> 15 variable for to a value of 16MB, use either of the following commands:shell>2 The first command specifies the value in bytes. The second specifies the value in megabytes. For variables that take a numeric value, the value can be given with a suffix of shell>79, shell>80, or shell>81 (either uppercase or lowercase) to indicate a multiplier of 1024, 10242 or 10243. (For example, when used to set shell> 15, the suffixes indicate units of kilobytes, megabytes, or gigabytes.)In an option file, variable settings are given without the leading dashes: shell>3 Or: shell>4 If you like, underscores in a variable name can be specified as dashes. The following option groups are equivalent. Both set the size of the server's key buffer to 512MB: shell>5 A variable can be specified by writing it in full or as any unambiguous prefix. For example, the shell> 15 variable can be set for as shell> 68, but not as shell> 69 because the latter is ambiguous:shell>6 Be aware that the use of variable prefixes can cause problems in the event that new variables are implemented for a program. A prefix that is unambiguous now might become ambiguous in the future. Suffixes for specifying a value multiplier can be used when setting a variable at server startup, but not to set the value with at runtime. On the other hand, with you can assign a variable's value using an expression, which is not true when you set a variable at server startup. For example, the first of the following lines is legal at server startup, but the second is not: shell>7 Conversely, the second of the following lines is legal at runtime, but the first is not: shell>8 4.2.3.6. Option Defaults, Options Expecting Values, and the mysql --host=localhost --user=myname --password mydb
shell> mysql -h localhost -u myname -p mydb
|