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.

1
  • Path components in the PATH variable are separated by a colon in bash. Commented Jun 28, 2023 at 8:26

8 Answers 8

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.

2
  • 4
    For this to work for me, I also had to reload the .bash_profile from the command line, by running: source ~/.bash_profile Commented Sep 23, 2017 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. Commented May 30, 2020 at 18:44
26

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
  • 1
    With MAMP: export PATH="/Applications/MAMP/Library/bin:$PATH"
    – Matt Rek
    Commented Jun 12, 2019 at 22:46
  • 1
    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. Commented May 30, 2020 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!

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.

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
    Commented Oct 29, 2015 at 7:16

You must log in to answer this question.

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