An environment variable has a value that can be accessed
by programs that you run. By setting environment variables, you modify
your operating environment. One such variable is PATH
, which is used by your command
interpreter to determine which directories to search when it looks for
programs such as mysql that you tell
it to execute. If your PATH
is set
correctly, you can invoke programs easily no matter what your current
directory is. If PATH
is not set
correctly, your command interpreter will not find them. For example, if
the PATH
value does not include the
directory where mysql is installed, a
“command not found” error may occur if you attempt to run
mysql by entering its name. You must
run it either by specifying its full pathname, or by changing location
into the directory where it is installed. Both strategies are
unpleasant, and more so with repetition. It’s much better to set your
PATH
value to name the directories
containing the programs you want to use.
Other environment variables are important in other contexts. For
example, if you run Perl or Ruby scripts that use module files that
you’ve installed, you may need to set the PERL5LIB
or RUBYLIB
variable to tell Perl or Ruby where to
find those modules. For Java, the JAVA_HOME
variable should be set to indicate
the location of your Java installation, and CLASSPATH
should be set to the list of
libraries and class files that your Java programs need.
The following discussion describes how to set environment
variables. The examples demonstrate how to make it easier to run MySQL
programs by modifying your PATH
setting to add the directory where MySQL programs are installed.
However, the discussion applies to other environment variables as well
because the variable-setting syntax is the same.
Here are some general principles to keep in mind regarding environment variables:
It’s possible to set an environment variable from the command line, but the variable will retain its value only until you exit the command interpreter. To cause the setting to take effect permanently, set it in the appropriate shell startup file on Unix or by using the Environment Variables interface on Windows (this can be found on the System Control Panel under Advanced). The change takes effect for subsequent invocations of your command interpreter (for example, your next login on Unix, or the next console window that you open on Windows).
Some variables (PATH
among
them) have a value that lists the pathnames for one or more
directories. Under Unix, it’s conventional to separate the pathnames
using the colon character (:
).
Under Windows, pathnames can contain colons, so the separator is the
semicolon character (;
).
You can check your current environment variable settings by executing an env or printenv command on Unix, or a set command in a console window on Windows.
Your choice of command interpreter determines the syntax for setting environment variables. In addition, for settings that you put in a startup file, the file to use is interpreter-specific. The following table shows typical startup files for commonly used Unix command interpreters. If you’ve never looked through your command interpreter’s startup files, it’s a good idea to do so to familiarize yourself with their contents.
Command interpreter | Possible startup files |
---|---|
sh, bash, ksh | .profile, .bash_profile, .bash_login, .bashrc |
csh, tcsh | .login, .cshrc, .tcshrc |
The examples assume that MySQL programs are installed in the
/usr/local/mysql/bin directory
and that you want to add that directory to your existing PATH
value. That is, the examples assume
that there is already a PATH
setting in one of your startup files. If you have no PATH
setting currently, add the appropriate
line or lines to one of the files.
For a shell in the Bourne shell family (sh, bash, ksh), look in your startup files for a
line that sets and exports the PATH
variable:
export PATH=/bin:/usr/bin:/usr/local/bin
Change the setting to add the appropriate directory:
export PATH=/usr/local/mysql/bin:/bin:/usr/bin:/usr/local/bin
The assignment and the export might be on separate lines:
PATH=/bin:/usr/bin:/usr/local/bin export PATH
Change the setting to this:
PATH=/usr/local/mysql/bin:/bin:/usr/bin:/usr/local/bin export PATH
For a shell in the C shell family (csh, tcsh), look in your startup files for a setenv PATH command:
setenv PATH /bin:/usr/bin:/usr/local/bin
Change the setting to add the appropriate directory:
setenv PATH /usr/local/mysql/bin:/bin:/usr/bin:/usr/local/bin
It’s also possible that your path will be set with a set path command, which uses different syntax:
set path = (/usr/local/mysql/bin /bin /usr/bin /usr/local/bin)
path
is not an
environment variable, but for C shells, setting path
also sets PATH
and vice versa.
Adjust the instructions for the pathname actually used on your system.
To set environment variables on Windows, click the Start Menu,
right-click My Computer → Properties →
Advanced → Environment Variables. You
should see a window that enables you to define environment variables
or change the values of existing variables. For example, your PATH
variable might have a value like
this:
C:WINDOWS;C:WINDOWSCOMMAND
To make it easier to run MySQL programs such as mysql, change the value to include the
directory where those programs are installed. Adding a directory of
C:Program FilesMySQLMySQL Server
5.0in results in a PATH
setting that should look like
this:
C:Program FilesMySQLMySQL Server 5.0in;C:WINDOWS;C:WINDOWSCOMMAND
3.138.135.80