35

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.

47

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.

3
  • People upvoting, where did you get this faulty path from?
    – Spotlight
    Jul 28 '16 at 1:44
  • 3
    For this to work for me, I also had to reload the .bash_profile from the command line, by running: source ~/.bash_profile Sep 23 '17 at 15:01
  • 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. May 30 '20 at 18:44
23

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
2
  • With MAMP: export PATH="/Applications/MAMP/Library/bin:$PATH"
    – Maciek Rek
    Jun 12 '19 at 22:46
  • The is the correct way to go. Without adding it to the bash_profile you'll have to export the path to mysql every time you open the terminal. May 30 '20 at 18:45
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!

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.

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).

0

For MAMP users,

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

edit the system wide configuration file:

/etc/paths
1
  • This file gets overwritten by every OS X upgrade.
    – nohillside
    Oct 29 '15 at 7:16

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .