207

I cant get my mysql to start on os x 10.7. It is located in /usr/local/mysql/bin/mysql

I get command not found when I type mysql --version in the terminal.

I tried this can't access mysql from command line mac but still get command not found. I tried installing mysql with the dmg and i have tried to install it through homebrew and I cant get it to work. When I try to start mysql with homebrew I get MySQL won't start

This is my $PATH:

/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin:/usr/local/git/bin:/Users/Victoria/bin:/usr/local//usr/local/mysql/bin/private/var/mysql/private/var/mysql/bin

Community
  • 1
  • 1
SilverNightaFall
  • 4,100
  • 10
  • 34
  • 46
  • 1
    Is the install directory in your PATH? – Greg Hewgill May 14 '12 at 04:03
  • 1
    There are a number of things to check. Firstly follow the install instructions carefully that are provided with the DMG & cover all the relevant steps for OS X. There is a system prefs panel provided to start/stop mysql too. Additionally OSX is notorious for changing the location of mysql.sock - you will need to set it to /tmp/mysql.sock in my.cnf & php.ini - google that, there will be many guides – RiquezJP May 14 '12 at 04:09
  • mysql --version will only work if you instruct BASH to have that shortcut command - this is detailed in the install read me – RiquezJP May 14 '12 at 04:16
  • 7
    If you navigate to mysql folder cd /usr/local/mysql/bin & then issue the version command, it will work. ./mysql --version – RiquezJP May 14 '12 at 04:22

19 Answers19

438

This is the problem with your $PATH:

/usr/local//usr/local/mysql/bin/private/var/mysql/private/var/mysql/bin.

$PATH is where the shell searches for command files. Folders to search in need to be separated with a colon. And so you want /usr/local/mysql/bin/ in your path but instead it searches in /usr/local//usr/local/mysql/bin/private/var/mysql/private/var/mysql/bin, which probably doesn't exist.

Instead you want ${PATH}:/usr/local/mysql/bin.

So do export PATH=${PATH}:/usr/local/mysql/bin.

If you want this to be run every time you open terminal put it in the file .bash_profile, which is run when Terminal opens.

user987339
  • 10,519
  • 8
  • 40
  • 45
Hophat Abc
  • 5,203
  • 3
  • 18
  • 18
  • 3
    now I am getting the error I was getting with homebrew ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2) – SilverNightaFall May 14 '12 at 04:18
  • 2
    This means that the mysql socket is misplaced, which is a very different error. That is a problem with mysql, not your `$PATH`. – Hophat Abc May 14 '12 at 04:19
  • 1
    Do what the other answer said: Remove this pid file, make sure all the instances are stopped (kill any processes not going down), check nothing is listening the MySQL port 3306 (netstat should help) and try to start MySQL again. – Hophat Abc May 14 '12 at 04:21
  • 1
    I would recommend opening Activity Monitor and killing all instances of mysql – Hophat Abc May 14 '12 at 04:21
  • in which file i need to set path – Hardeep Singh Oct 07 '17 at 05:17
  • 1
    My mysql binary wasn't in `/usr/local/mysql/bin`. – user124384 Jul 23 '18 at 21:59
  • Works without parenthesis too. So, `export PATH=$PATH:/usr/local/mysql/bin` will also do. – backslashN Feb 19 '19 at 20:37
  • To automatically add the executable files from the current dir., i would suggest to add `export PATH=$PATH:.` (notice `.` after `:`), in to `.bash_profile` so, no need to manually export every executable file – Jimmy_Rw Sep 28 '19 at 02:19
  • do export PATH=${PATH}:/usr/local/mysql/bin. this code worked for me – Vinicius Cardoso Jan 12 '20 at 10:44
  • I noticed that I needed to issue the "export PATH=${PATH}:/usr/local/mysql/bin" command before the mysql command would start working. In other words, I couldn't go to the bin folder and directly execute mysql. Also, it seems /usr/local/mysql is the correct folder and not the other mysql-versionName folder next to it. I realized this after launching mySQL.prefPane and then seeing the configuration settings pointing to /user/local/mysql. – Mikhael Loo Mar 27 '20 at 16:17
  • if this is not working for you, edit your **zshrc** file. `open -t ~/.zshrc`, `export PATH=${PATH}:/usr/local/mysql/bin/`, `source ~/.zshrc`. https://stackoverflow.com/a/58404895/10505343 for reference. – Wimukthi Rajapaksha Mar 26 '21 at 15:13
45

One alternative way is creating soft link in /usr/local/bin

ln -s /usr/local/mysql/bin/mysql /usr/local/bin/mysql

But if you need other executables like mysqldump, you will need to create soft link for them.

gzc
  • 8,180
  • 8
  • 42
  • 62
37

If you installed MySQL Server and you still get

mysql -u root -p command not found

You're most likely experiencing this because you have an older mac version.

Try this:

in the home directory in terminal open -t .bash_profile

paste export PATH=${PATH}:/usr/local/mysql/bin/ inside and save it

instead of writing mysql -u root -p paste the following in your terminal:

/usr/local/mysql/bin/mysql -u root -p 

Or use Alias instead of writing the full path

alias mysql=/usr/local/mysql/bin/mysql

Enter your password. Now you're in.

Inamur Rahman
  • 2,913
  • 1
  • 27
  • 29
Jackie Santana
  • 1,290
  • 14
  • 15
35

I faced the same issue, and finally i got a solution. Please go through with the below steps, if you are using MAMP.

  1. Start MAMP or MAMP PRO
  2. Start the server
  3. Open Terminal (Applications -> Utilities)
  4. Type in: (one line) 
 /Applications/MAMP/Library/bin/mysql --host=localhost -uroot -proot

This works for me.

SreerajS
  • 546
  • 4
  • 8
29

You have to set PATH for mysql in your .bashrc file using following:

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

But If you are using oh my zsh then you have to add path inside .zshrc file.

shivam
  • 688
  • 8
  • 10
11

Your PATH might not setup. Go to terminal and type:

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

Essentially, this allows you to access mysql from anywhere.

Type cat .bash_profile to check the PATH has been setup.

Check mysql version now: mysql --version

If this still doesn't work, close the terminal and reopen. Check the version now, it should work. Good luck!

nurealam siddiq
  • 1,567
  • 10
  • 9
10

Use these two commands in your terminal

alias mysql=/usr/local/mysql/bin/mysql
mysql --user=root -p

Then it will ask you to enter password of your user pc

Enter password:

Martin Brisiak
  • 3,872
  • 12
  • 37
  • 51
Ayush Raj
  • 348
  • 3
  • 14
8

I have tried a lot of the suggestions on SO but this is the one that actually worked for me:

sudo sh -c 'echo /usr/local/mysql/bin > /etc/paths.d/mysql'

then you type

mysql

It will prompt you to enter your password.

josliber
  • 43,891
  • 12
  • 98
  • 133
zazu
  • 344
  • 3
  • 9
4

If you are using terminal you will want to add the following to ./bash_profile

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

If you are using zsh, you will want to add the above line to your ~/.zshrc

Jon Gear
  • 1,008
  • 1
  • 11
  • 22
3

Add the following lines in bash_profile:

alias startmysql='sudo /usr/local/mysql/support-files/mysql.server start'
alias stopmysql='sudo /usr/local/mysql/support-files/mysql.server stop'

and save the bash_profile.

Now, in the terminal start and stop the mysql server using the following commands:

startmysql //to start mysql server

and

stopmysql //to stop mysql server
KayV
  • 12,987
  • 11
  • 98
  • 148
2

With MAMP

  1. Locate mysql usually at /Applications/MAMP/Library/bin/mysql
  2. sudo vi /etc/paths
  3. Add this path to file /Applications/MAMP/Library/bin
  4. :wq (Save and quit file)
  5. Close Terminal windows
  6. Reopen Terminal and type mysql and it should work
user1502826
  • 395
  • 4
  • 12
2

I installed MAMP and phpmyadmin was working.

But cannot find /usr/local/bin/mysql

This fixed it

sudo ln -s /Applications/MAMP/Library/bin/mysql /usr/local/bin/mysql
zod
  • 12,092
  • 24
  • 70
  • 106
  • its creating a symlink . To remove it try this https://linuxize.com/post/how-to-remove-symbolic-links-in-linux/ – zod Jan 16 '20 at 20:57
2

in terminal do:

vi ~/.bash_profile 

And add this line:

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

and the type this in terminal:

mysql -u [username] -p [password]
Ru Chern Chong
  • 3,692
  • 13
  • 33
  • 43
Boney
  • 1,462
  • 1
  • 11
  • 19
2

Maybe I'll help someone else. None of the above answers worked for Catalina. Finally, this solved the problem

echo 'export PATH="/usr/local/opt/mysql@5.7/bin:$PATH"' >> /Users/$(whoami)/.bash_profile

Of course, you have to change for the version of mysql you have installed

Manuel Ortiz
  • 593
  • 5
  • 11
0

May be i will help out some of you that even though if you are unable to open mysql from terminal after trying changing path in .bash_profile

then you always found the error "MYSQL not found" hence you can use the following command directly it will ask for your password and sql bash is opened

/usr/local/mysql/bin/mysql -u root -p

0

I had same issue after installing mariadb via HomeBrew, brew doctor suggested to run brew link mariadb - which fixed the issue.

m.e
  • 213
  • 2
  • 6
0

It is possible you are using zsh instead of bash then you have to enter the above mentioned commands in .zshenv instead of .bash_profile

0

after appending mysql path to your environment variables:

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

you may need to reload your environment variables with one of these commands:

source ~/.bashrc  # If you use Default Bash

source ~/.zshrc   # If you use Oh-My-Zsh
Mike Elahi
  • 1,103
  • 1
  • 13
  • 28
0

I have tried the same to run mysql on MacOS terminal but it was showing an error.

Then I tried this

export PATH=${PATH}:/usr/local/mysql/bin
mysql -u -root -p

Then enter your MySQL password in prompt

Bharathwaj
  • 11
  • 3