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.

informatik01
  • 16,038
  • 10
  • 74
  • 104
user1574598
  • 3,771
  • 7
  • 44
  • 67

6 Answers6

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.

kishanio
  • 6,979
  • 8
  • 25
  • 33
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

Justin Kiang
  • 1,270
  • 6
  • 13
  • 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? – user1574598 Oct 24 '14 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. – Justin Kiang Oct 24 '14 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 – Justin Kiang Oct 24 '14 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. – user1574598 Oct 24 '14 at 20:12
8

for me the following commands worked:

$ brew install mysql

$ brew services start mysql
0bserver07
  • 3,390
  • 1
  • 28
  • 56
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 .

Mora
  • 718
  • 1
  • 8
  • 12
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.

Pravin Bansal
  • 4,315
  • 1
  • 28
  • 19
0

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

$ brew link --force mysql@5.6

see this post by Alex Todd

bisi
  • 1