71

MySQL on OS x 10.6 is located in /usr/local/mysql/bin/mysql.

I get command not found when I type mysql --version in the terminal. Is this because the socket path is wrong? If so how do I fix it?

halfer
  • 19,824
  • 17
  • 99
  • 186
Michael Insalaco
  • 851
  • 2
  • 9
  • 12
  • comnand not found means it's not in your path. If it was a socket problem, you'd get "unable to connect" or something similar – Marc B Nov 19 '11 at 16:29
  • possible duplicate of [Mysql command not found](http://stackoverflow.com/questions/10577374/mysql-command-not-found) – Sahil Kapoor Jun 29 '15 at 12:29
  • possible duplicate of [Using Mysql in the command line in osx - command not found?](http://stackoverflow.com/questions/26554818/using-mysql-in-the-command-line-in-osx-command-not-found) – kishanio Aug 17 '15 at 09:16

8 Answers8

141

Just do the following in your terminal:

echo $PATH

If your given path is not in that string, you have to add it like this: export PATH=$PATH:/usr/local/ or export PATH=$PATH:/usr/local/mysql/bin

sascha
  • 4,671
  • 3
  • 36
  • 54
  • I tried doing both. When i echo $PATH it still says -bash: /Users/minsalaco/.rvm/gems/ruby-1.9.3-p0/bin:/Users/minsalaco/.rvm/gems/ruby-1.9.3-p0@global/bin:/Users/minsalaco/.rvm/rubies/ruby-1.9.3-p0/bin:/Users/minsalaco/.rvm/bin:/Applications/MAMP/Library/bin:/Applications/MAMP/Library/bin:/Applications/MAMP/Library/bin:/Applications/MAMP/Library/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/local/git/bin:/usr/X11/bin: No such file or directory – Michael Insalaco Nov 19 '11 at 16:36
  • 1
    i removed MAMP and just want to use mysql from the command line – Michael Insalaco Nov 19 '11 at 16:37
  • Please read my answer once again. I just told you how to fix your issue. `/usr/local/`is missing in your path. Just add it as explained. – sascha Nov 19 '11 at 16:51
  • Please echo $PATH again after doing the above example. – sascha Nov 19 '11 at 17:03
  • 1
    This works only temperary. if you use command + T to open a new tab. mysql is not in the $PATH anymore. –  Dec 02 '15 at 05:17
  • Well, then you have to add the line to your `.profile` or `.bashrc`. Not sure about the exact file that's being used. – sascha Dec 03 '15 at 09:04
26

I'm using OS X 10.10, open the shell, type

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

it works temporary.if you use Command+T to open a new tab ,mysql command will not work anymore.

We need to create a .bash_profile file to make it work each time you open a new tab.

nano ~/.bash_profile

add the following line to the file.

# 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

Save the file, then open a new shell tab, it works like a charm..

by the way, why not try https://github.com/dbcli/mycli

pip install -U mycli

it's a tool way better than the mysqlcli.. A command line client for MySQL that can do auto-completion and syntax highlighting

  • it would be worth mentioning that path to `/mysql/bin` folder may differ and it's best to see for yourself where it is on your computer. Then change above commands accordingly. – Tomasz Mularczyk Feb 27 '16 at 20:59
  • Nice tool! I am SICK of MySQL Workbench, and have issues getting `mysql` working at the command line on my MacBook - this worked right off the bat. Love the auto-completion! – horcle_buzz Oct 11 '19 at 00:59
  • My `mysql` command line tool on mac worked like a slacker. It only worked after I added `export ARCHFLAGS="-arch x86_64"` in the `.bash_profile` file. – Abel Callejo Jul 28 '20 at 20:46
8

On OSX 10.11, you can sudo nano /etc/paths and add the path(s) you want here, one per line. Way simpler than figuring which of ~/.bashrc, /etc/profile, '~/.bash_profile` etc... you should add to. Besides, why export and append $PATH to itself when you can just go and modify PATH directly...?

Tom Auger
  • 19,421
  • 22
  • 81
  • 104
7

On mac, open the terminal and type:

cd /usr/local/mysql/bin

then type:

./mysql -u root -p

It will ask you for the mysql root password. Enter your password and use mysql database in the terminal.

ButchMonkey
  • 1,873
  • 18
  • 30
user12974639
  • 71
  • 1
  • 1
3

I've tried all the solutions from the answers but couldn't get mysql command to work from the terminal, always getting the message

bash: command not found

The solution is to change the .bash_profile, and add the mysql path to .bash_profile

To do so follow these steps: 1. Open a new Terminal window or make sure you are in the home directory 2. Open .bash_profile using

nano .bash_profile

3. Add the following command to add the mysql path

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

4. Press Ctrl+X, then press y and press enter.

The following is how my .bash_profile looks like enter image description here

Amit Kumar
  • 804
  • 2
  • 11
  • 16
0

adding this code to my .profile worked for me: :/usr/local/mysql/bin

Thanks.

P.S This .profile is located in your user/ path. Its a hidden file so you will have to get to it either by a command in Terminal or using an html editor.

0

I think this is the simpler approach:

  1. Install mySQL-Shell package from mySQL site
  2. Run mysqlsh (should be added to your path by default after install)
  3. Connect to your database server like so: MySQL JS > \connect --mysql [username]@[endpoint/server]:3306
  4. Switch to SQL Mode by typing "\sql" in your prompt
  5. The console should print out the following to let you know you are good to go:

Switching to SQL mode... commands end with ;.

halfer
  • 19,824
  • 17
  • 99
  • 186
lowlow20
  • 3
  • 3
0

Open terminal and run:

/usr/local/mysql/bin/mysql --user=root -p
Donald Duck
  • 8,409
  • 22
  • 75
  • 99
LSun
  • 11
  • 1