46

I installed MySQL on my Mac, and now I would like to add it to my $PATH variable. I want to be able to type mysql anywhere.

I tried adding the following to my .profile file:

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

I have restarted my Terminal but it does not do the trick. It still says: -bash: mysql: command not found.

How can I add this properly to my $PATH?

I am using OS X Yosemite.

bandrade
  • 103
JNevens
  • 639

8 Answers8

58

Here is why your current code is not working:

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

You forgot the colon and the trailing "/" is unnecessary.

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

is the correct code.

fd0
  • 10,708
Spotlight
  • 1,548
  • 14
  • 19
  • 3
    For this to work for me, I also had to reload the .bash_profile from the command line, by running: source ~/.bash_profile – Isak La Fleur Sep 23 '17 at 15:01
  • 2
    Alistair's answer below is better. If you use this without adding it permanently to the bash file, you will have to keep exporting everytime you open the terminal. – PhillipKregg May 30 '20 at 18:44
25

Try adding the following line to your .bash_profile file.

export PATH="/usr/local/mysql/bin:$PATH"

You can do this easily with the following command, which will append the line if the file already exists or create a new file with the line if it doesn't.

echo 'export PATH="/usr/local/mysql/bin:$PATH"' >> ~/.bash_profile
7

You can accomplish this by going to your .bash_profile file and adding these lines to it.

alias mysql.start="sudo /usr/local/mysql/support-files/mysql.server start"
alias mysql.stop="sudo /usr/local/mysql/support-files/mysql.server stop"
alias mysql.restart="sudo /usr/local/mysql/support-files/mysql.server restart"
alias mysql.status="sudo /usr/local/mysql/support-files/mysql.server status"

After you've done this you can start, stop, restart and check the status of your connection anywhere in your terminal like this:

mysql.start - starts mysql
mysql.stop - stop mysql
mysql.restart - restarts mysql
mysql.status - checks the status of mysql

Hope that helps even tho I might be late. CHEERS!

Mekanic
  • 71
1

In your .zshrc file, just add this following line:

alias mysql="sudo /usr/local/mysql/bin/mysql -u root -p"

You can now access it from anywhere by writing mysql in the terminal.

This will simply ask you for the password of the root user and done.

Greenonline
  • 2,004
0

You can also use an alias to do things like setting username and database.

In .bash_profile ( just .profile on some systems )

alias mysql='/usr/local/mysql/bin/mysql -u username --default-character-set=utf8' -p databasename

logout and open a new terminal or it won't get setup

Now typing mysql will ask for your database password and take you straight there, instead of fiddling with the options each time.

paul
  • 2,665
  • 12
  • 8
0

If you need to modify your .bash_profile file, take a look at this article: My Mac Osx Bash Profile, which is very helpful and easy to understand.

It solved my $PATH issue (which appeared out of nowhere).

jaume
  • 15,010
0

For MAMP users,

export PATH=${PATH}:/Applications/MAMP/Library/bin
-2

edit the system wide configuration file:

/etc/paths