52

I'm trying to get MySQL up and running on my Mac OS X 10.9.5.

I've installed the latest version 5.6.21 of MySQL Community Server. I've gone to system preferences and started the mysql server, then launched terminal and typed this:

/usr/local/mysql/bin/mysql --version

which should return the version. But when I type any of the mysql commands I get command not found.

I've also tried:

sudo mysql_secure_installation
mysql -u root --password=password`

I do have web hosting with MySQL etc installed, but I want to be able to get to grips with it in the command line first.

1
  • What's in your $PATH?
    – admdrew
    Oct 24, 2014 at 19:21

6 Answers 6

149

So there are few places where terminal looks for commands. This places are stored in your $PATH variable. Think of it as a global variable where terminal iterates over to look up for any command. This are usually binaries look how /bin folder is usually referenced.

/bin folder has lots of executable files inside it. Turns out this are command. This different folder locations are stored inside one Global variable i.e. $PATH separated by :

Now usually programs upon installation takes care of updating PATH & telling your terminal that hey i can be all commands inside my bin folder.

Turns out MySql doesn't do it upon install so we manually have to do it.

We do it by following command,

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

If you break it down, export is self explanatory. Think of it as an assignment. So export a variable PATH with value old $PATH concat with new bin i.e. /usr/local/mysql/bin

This way after executing it all the commands inside /usr/local/mysql/bin are available to us.

There is a small catch here. Think of one terminal window as one instance of program and maybe something like $PATH is class variable ( maybe ). Note this is pure assumption. So upon close we lose the new assignment. And if we reopen terminal we won't have access to our command again because last when we exported, it was stored in primary memory which is volatile.

Now we need to have our mysql binaries exported every-time we use terminal. So we have to persist concat in our path.

You might be aware that our terminal using something called dotfiles to load configuration on terminal initialisation. I like to think of it's as sets of thing passed to constructer every-time a new instance of terminal is created ( Again an assumption but close to what it might be doing ). So yes by now you get the point what we are going todo.

.bash_profile is one of the primary known dotfile.

So in following command,

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

What we are doing is saving result of echo i.e. output string to ~/.bash_profile

So now as we noted above every-time we open terminal or instance of terminal our dotfiles are loaded. So .bash_profile is loaded respectively and export that we appended above is run & thus a our global $PATH gets updated and we get all the commands inside /usr/local/mysql/bin.

P.s.

if you are not running first command export directly but just running second in order to persist it? Than for current running instance of terminal you have to,

source ~/.bash_profile

This tells our terminal to reload that particular file.

2
  • 5
    For zsh, it should be in the "~/.zshrc" file and use command source ~/.zshrc
    – huynguyen
    Dec 4, 2019 at 7:56
  • 2
    I had to run "source ~/.bash_profile" in order for this to work. Even rebooting did not fix it for my
    – yazmnh87
    Nov 1, 2020 at 20:14
23

That means /usr/local/mysql/bin/mysql is not in the PATH variable..

Either execute /usr/local/mysql/bin/mysql to get your mysql shell,

or type this in your terminal:

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

to add that to your PATH variable so you can just run mysql without specifying the path

4
  • Thanks - I've just manually changed my path to /usr/local/mysql/bin/ before I add it to the path. I then typed ls and I can see all the mysql files, but when I type mysql I still get command not found? Oct 24, 2014 at 19:36
  • the PATH variable and your current working directory are two different things. $PATH, which is set by the line I gave you above, is where the system will look at when you just type a standalone command without the absolute/relative path. Oct 24, 2014 at 19:55
  • when you changed you working directory to /usr/local/mysql/bin using cd, you have to invoke mysql by typing ./mysql to let the system know to use mysql in the current directory, and not try to find it in one of the directory in your PATH variable Oct 24, 2014 at 19:58
  • 1
    Thanks, things seem a lot clearer after your information. I have just seen a a more complicated version of your line of code, but I am struggling to differentiate between your code and whats going on here: echo 'export PATH=$PATH:/usr/local/mysql/bin' >> ~/.bash_profile Then he uses the cat command to open it. All's I can see so far is that an invisible file has been created in the home directory. Oct 24, 2014 at 20:12
8

for me the following commands worked:

$ brew install mysql

$ brew services start mysql
0
1

You can just modified the .bash_profile by adding the MySQL $PATH as the following:
export PATH=$PATH:/usr/local/mysql/bin.

I did the following:

1- Open Terminal then $ nano .bash_profile or $ vim .bash_profile

2- Add the following PATH code to the .bash_profile

# Set architecture flags
export ARCHFLAGS="-arch x86_64"
# Ensure user-installed binaries take precedence
export PATH=/usr/local/mysql/bin:$PATH
# Load .bashrc if it exists
test -f ~/.bashrc && source ~/.bashrc 

3- Save the file.

4- Refresh Terminal using $ source ~/.bash_profile

5- To verify, type in Terminal $ mysql --version

6- It should print the output something like this:

$ mysql Ver 14.14 Distrib 5.7.17, for macos10.12 (x86_64)

The Terminal is now configured to read the MySQL commands from $PATH which is placed in the .bash_profile .

0

modify your bash profile as follows <>$vim ~/.bash_profile export PATH=/usr/local/mysql/bin:$PATH Once its saved you can type in mysql to bring mysql prompt in your terminal.

0
0

You have to create a symlink to your mysql installation if it is not the most recent version of mysql.

$ brew link --force [email protected]

see this post by Alex Todd

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.