Setting MySQL Environment Variables on Linux

Let us understand how to set environment variables on Linux for MySQL −

Environment variables can be set at the command prompt. This is done to affect the current invocation of the command processor, or to permanently set to affect the future invocations.

To set a variable permanently, it can be set in a startup file or with the help of the interface provided by the system for the same purpose. The documentation of the command interpreter needs to be consulted for specific details. To specify a value for an environment variable, the syntax relevant for the command processor needs to be used.

Let us take an example −


The syntax on Unix depends on the user’s shell. If a TCP/IP port number needs to be specified with the help of the MYSQL_TCP_PORT variable, syntax could be for sh, ksh, bash, zsh, and so on. Below is an example −


The first command will set the variable. The export command will export the variable to the shell environment so that its value can be accessed by MySQL and other processes.

csh and tcsh

For csh and tcsh, use setenv to make the shell variable available to the environment, the below syntax can be used −

setenv MYSQL_TCP_PORT 3306

The command to set environment variables can be executed at the command prompt which will take effect immediately, but the settings will last as long as the user is logged in. Once they log out, the changes are lost.

On Unix, typical shell startup files are .bashrc or .bash_profile for bash, or .tcshrc for tcsh. If the MySQL programs are installed in /usr/local/mysql/bin and the user wishes to make it easy to invoke these programs, the value of the PATH environment variable can be set to include that directory.

Example – bash shell

If the user’s shell is bash, the below line can be added to the .bashrc file −


‘bash’ uses different startup files for login and nonlogin shells, so it might be important to add the setting to .bashrc for login shells and to .bash_profile for nonlogin shells to ensure that the PATH is set irrespective of this setting.

Example tcsh shell

If the shell is tcsh, the below line can be added to the .tcshrc file −

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

Updated on: 10-Mar-2021


Kickstart Your Career

Get certified by completing the course

Get Started