4.2.9 Setting Environment Variables设置环境变量

Environment variables can be set at the command prompt to affect the current invocation of your command processor, or set permanently to affect future invocations. 可以在命令提示符处设置环境变量,以影响命令处理器的当前调用,也可以永久设置环境变量以影响未来的调用。To set a variable permanently, you can set it in a startup file or by using the interface provided by your system for this purpose. 要永久设置变量,您可以在启动文件中设置它,也可以使用系统为此提供的接口进行设置。Consult the documentation for your command interpreter for specific details. 有关具体详细信息,请参阅命令解释器的文档。Section 4.9, “Environment Variables”, lists all environment variables that affect MySQL program operation.第4.9节,“环境变量”列出了影响MySQL程序运行的所有环境变量。

To specify a value for an environment variable, use the syntax appropriate for your command processor. For example, on Windows, you can set the USER variable to specify your MySQL account name. To do so, use this syntax:要为环境变量指定值,请使用适合您的命令处理器的语法。例如,在Windows上,您可以设置USER变量来指定MySQL帐户名。为此,请使用以下语法:

SET USER=your_name

The syntax on Unix depends on your shell. Suppose that you want to specify the TCP/IP port number using the MYSQL_TCP_PORT variable. Unix上的语法取决于你的shell。假设您想使用MYSQL_TCP_PORT变量指定TCP/IP端口号。Typical syntax (such as for sh, ksh, bash, zsh, and so on) is as follows:典型的语法(如shkshbashzsh等)如下:

MYSQL_TCP_PORT=3306
export MYSQL_TCP_PORT

The first command sets the variable, and the export command exports the variable to the shell environment so that its value becomes accessible to MySQL and other processes.第一个命令设置变量,export命令将变量导出到shell环境,以便MySQL和其他进程可以访问其值。

For csh and tcsh, use setenv to make the shell variable available to the environment:对于cshtcsh,使用setenv使shell变量对环境可用:

setenv MYSQL_TCP_PORT 3306

The commands to set environment variables can be executed at your command prompt to take effect immediately, but the settings persist only until you log out. 可以在命令提示符下执行设置环境变量的命令以立即生效,但这些设置只会持续到您注销为止。To have the settings take effect each time you log in, use the interface provided by your system or place the appropriate command or commands in a startup file that your command interpreter reads each time it starts.要使设置在每次登录时生效,请使用系统提供的界面,或将适当的命令放入命令解释器每次启动时读取的启动文件中。

On Windows, you can set environment variables using the System Control Panel (under Advanced).在Windows上,您可以使用系统控制面板(高级下)设置环境变量。

On Unix, typical shell startup files are .bashrc or .bash_profile for bash, or .tcshrc for tcsh.在Unix上,典型的shell启动文件是bash的.bashrc.bash_profiletcsh.tcshrc

Suppose that your MySQL programs are installed in /usr/local/mysql/bin and that you want to make it easy to invoke these programs. 假设你的MySQL程序安装在/usr/local/mysql/bin中,你想让调用这些程序变得容易。To do this, set the value of the PATH environment variable to include that directory. For example, if your shell is bash, add the following line to your .bashrc file:为此,请设置PATH环境变量的值以包含该目录。例如,如果你的shell是bash,请在.bashrc文件中添加以下行:

PATH=${PATH}:/usr/local/mysql/bin

bash uses different startup files for login and nonlogin shells, so you might want to add the setting to .bashrc for login shells and to .bash_profile for nonlogin shells to make sure that PATH is set regardless.bash对登录和非登录shell使用不同的启动文件,因此您可能希望将该设置添加到登录shell的.bashrc和非登录Shell的.bash_profile中,以确保始终设置PATH。

If your shell is tcsh, add the following line to your .tcshrc file:如果你的shell是tcsh,请在.tcshrc文件中添加以下行:

setenv PATH ${PATH}:/usr/local/mysql/bin

If the appropriate startup file does not exist in your home directory, create it with a text editor.如果您的主目录中不存在相应的启动文件,请使用文本编辑器创建它。

After modifying your PATH setting, open a new console window on Windows or log in again on Unix so that the setting goes into effect.修改PATH设置后,在Windows上打开一个新的控制台窗口,或在Unix上重新登录,使设置生效。