MySql Command not found : MacOS - mysql

I'm new to using the terminal and I'm having trouble troubleshooting the following issue:
After looking at some other similar problems I've tried the following commands that might enlighten you to what is wrong.
First I'll post a summary of the things I've tried in the terminal and the responses I got from the terminal. Below that I'll "briefly" write about installation process and workbench issue.
(MyUserName)$ sudo ln -s /usr/local/mysql/bin/mysql usr/local/bin
Password:
(I input my Mac password here)*
ln: usr/local/bin: No such file or directory
Sometimes I get "No such file or directory" but sometimes I get the following:
(MyUserName)$ sudo ln -s /usr/local/mysql/bin/mysql /usr/local/bin
ln: /usr/local/bin: File exists
I still can't access mysql though, even if I try straight after the "File Exists" reply.
Perhaps I messed something up yesterday when I did the above command but forgot the "s" after the dash?
(MyUsername)$ ls /usr/local/mysql
LICENSE README bin data docs include keyring lib man share support-files
(MyUsername)$ echo $PATH
/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local//bin
Something I tried because of similar questions*
(MyUsername)$ export PATH=$PATH:/usr/local//bin
I just got another prompt from the terminal and tried the following login after that:*
(MyUserName)$ mysql -u root -p
-bash: mysql: command not found
I have successfully installed mysql and workbench and used it with no problem, however I just factory reset my MacBook and when installing again, I'm having these problems.
At first I noticed that the default shell in the terminal was zsh, and I changed it to be default bash because I wasn't sure the commands were the same, and I am learning from a video and he uses the bash shell.
When installing mysql, I was not allowed to change the path or change where it was installed, not that I would've done so, but perhaps that's weird?
I also did not get to choose anything else except the type of password and then setting the password. (I remember choosing which components or something when doing this before)
In workbench, my root server says it's running, when I try to make another connection everything seems normal except that there is no input field for the password. So I've tried making the connection anyway but when trying to use it, it asks me for a password, where my root password does not work.
I appreciate all the help I can get, I need this for my studies (Teacher couldn't help due to the OS). Please let me know if you have any suggestions, thanks :)!

Your command should have been:
sudo ln -s /usr/local/mysql/bin/mysql /usr/local/bin/mysql
Basically, the command does a symbolic link for the mysql binary in /usr/local/bin which is already in your search path.
Alternatively you could have added '/usr/local/mysql/bin/mysql' to PATH

Related

MySQL - Can't connect to local MYSQL server...ERROR 2002?

Hi I've looked all over the internet to find an answer to this problem but nothing seems to work. I get the below problem when I try to log in to mysql on terminal in OSX El Capitan.
The server is running.
What should I do, I've spent 5 hours trying to figure this out before posting this. I cannot find the my.cnf file in /etc and I'm not sure what to do with it. Someone please help.
Edit:
I've downloaded and installed(just double clicking - no terminal) this:
When everything is installed and i go into /usr/local/mysql/bin/ and try mysql -u root -p I get the following error:
This is my my.cnf file in /etc:
When I do sudo /usr/local/mysql/bin/mysqld_safe I get this:
df -h returns this, could it be something to do with space?
If you feel more comfortable installing it through the GUI here is an instruction guide on how to do that.
http://dev.mysql.com/doc/refman/5.7/en/osx-installation-pkg.html
Make sure you install the MySQLStartupItem.pkg and MySQL.prefPane. After you have done that you can verify your installation by opening up Terminal and running:
cd /usr/local
hitting enter then typing:
ls
hit enter again and verify the existence of two directories, one will just be MySQL and the other will be MySQL-(version number you installed).
At this stage I would restart your computer and then on restart the mysql server will start up.
Once it's restarted fire up terminal window again and type this:
/usr/local/mysql/bin/mysql -u root -p
EDIT
Open your /etc/my.cnf file and edit these two configurations.
[mysqld]
socket=/var/mysql/mysql.sock
[client]
socket=/var/mysql/mysql.sock
Hope this helps.

Installing MySQL on a mac

i have downloaded and installed mySQL my double clicking on its icon. It was installed successfully.
When i goto startup and preference i see the icon of mysql added and when i click on it i see a screen where it says 'MySQL server instance is running'.
But when i open terminal and cd to /usr/local/mysql and then when i type sudo ./bin/mysqld_safe i was prompted for a password. and i have not added a password when i installed mySQL, so i tried leaving it blank, and then i tried various passwords to login but all attempts failed.
So now i need to know how to login to mySQL via the terminal ?
mysql version - 5.5.24-osx10.6x86_64
my Mac OS - 10.7.3
What I found installing mysql on MacOs, there are a few differences. One is that it installs it without a password. The other thing is that it by default allows for anonymous logins.
Use this to set the password:
mysqladmin -u root -h localhost password yourpassword
You can remove anonymous logins this way:
shell> mysql -u root -p
Enter password: (enter root password here)
mysql> DROP USER ''#'localhost';
mysql> DROP USER ''#'host_name';
The other thing is that I found that the install does not modify the path variable. What I did to run mysql from the command line was to add /usr/local/mysql/bin to path by adding it to /etc/paths or /etc/paths.d . This may be what you need in order to run mysql. Like someone said in the comments, mysqld_safe is one way to start the mysql server, and it seems that is already set to run.
Here are specific instructions to add something to /etc/paths.d
$ cd /etc/paths.d
$ cat > mysql
/usr/local/bin/mysql
(and then type Ctrl-D
that should put a file there)
you may have to sudo if you do not have permissions.
The sudo command, by default, lets anyone in the admin group run a command as root by giving his own password. That's why it asked for your password when you typed "sudo ./bin/mysqld_safe". It has nothing whatsoever to do with mysql.
If you don't have a password, you cannot use sudo in the default configuration. Either give yourself a password, or edit the sudoers file. (I would strongly suggest the former over the latter, especially if you have no idea what sudo does.)
For more information, type "man sudo" (and then "man sudoers") from your Terminal.
Meanwhile, the reason "it says -bash: mysql: command not found when i type mysql in the terminal" is because you've clearly installed it into /usr/local/mysql/bin/mysql, and that isn't on your path. If it were on your path, you could have just done "sudo mysqld_safe" above, instead of "sudo ./bin/mysqld_safe". Since it's not, you have to do "./bin/mysqld_safe".
For more information, consult a good primer on the Unix shell.
Finally, if you've got the mysql daemon running, and are trying to start the client, it's "mysql" that you want to run, not "mysqld_safe".

Mysql command not found in OS X 10.7

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
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.
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.
I faced the same issue, and finally i got a solution. Please go through with the below steps, if you are using MAMP.
Start MAMP or MAMP PRO
Start the server
Open Terminal (Applications -> Utilities)
Type in: (one line)

 /Applications/MAMP/Library/bin/mysql --host=localhost -uroot -proot
This works for me.
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.
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.
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!
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:
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.
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
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
With MAMP
Locate mysql usually at /Applications/MAMP/Library/bin/mysql
sudo vi /etc/paths
Add this path to file /Applications/MAMP/Library/bin
:wq (Save and quit file)
Close Terminal windows
Reopen Terminal and type mysql and it should work
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
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]
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
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
I had same issue after installing mariadb via HomeBrew, brew doctor suggested to run brew link mariadb - which fixed the issue.
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

Command line MySQL from XAMPP in Cygwin [duplicate]

I can successfully connect to MySQL from a DOS prompt, but when I try to connect from cygwin, it just hangs.
$/cygdrive/c/Program\ Files/MySQL/MySQL\ Server\ 5.1/bin/mysql -u root -p
What's wrong?
I just came across this, and when I read someone's mention of it being a windows/DOS command that you run in cygwin I did a which mysql and that gave me:
$ which mysql
/cygdrive/c/Program Files/MySQL/MySQL Server 5.5/bin/mysql
So I ran the cygwin Setup.exe searched for "mysql" and installed the latest "mysql client". Now which mysql looks like:
$ which mysql
/usr/bin/mysql
And the MySQL command works in cygwin :)
Though it's an old question, it would be nice to have the actual answer here, as people (like myself) might still stumble across it.
If your attempts to run the MySQL client from Cygwin return the following error:
$ mysql -u root -p
Enter password:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysql.sock' (2)
Then you can fix it by adding the explicit -h 127.0.0.1 options to the command line, as in:
$ mysql -u root -p -h 127.0.0.1
Updates based on comments:
To avoid specifying -h 127.0.0.1 on the command line every time you connect, you can add:
[client]
host=127.0.0.1
to the file /etc/my.cnf
On some installations of Cygwin, specifying the host with -h might not be enough. If so, try also specifying:
--protocol=tcp
or add
protocol=tcp
to the config file.
Assuming that you have a native Windows build of MySQL, there is a terminal emulation incompatibility between DOS (command prompt) windows and bash. The prompt for mysql isn't showing up.
To confirm this, type a command and return - it will probably work, but the prompt and the echo of the command (what you're typing) is getting lost.
There may be a workaround in either the CYGWIN sytem properties or in bash, but I've never taken the time to work this one out.
Other answers lack the following key detail:
Cygwin has two shells:
Default: c:\cygwin\bin\mintty.exe
Basic: c:\cygwin\Cygwin.bat (which launches c:\cygwin\bin\bash.exe)
The Win32 MySQL can write properly to #2, but not #1, because Win32 MySQL cannot probe stdin properly (thanks #PeterNore)
Want to know if you're using Win32 MySQL? Use which, e.g.
$ which mysql
/cygdrive/c/Program Files/MySQL/MySQL Server 5.1/bin/mysql
Bonus: Cygwin guide to overcoming path problems (thanks #Dustin)
I posted a solution/workaround here:
enter key sometimes not recognized in windows apps under cygwin
Run bash from the cmd.exe executable and then mysql will work inside bash.
Create a shortcut for cmd.exe on your desktop.
Open up the properties for the shortcut and change the startup directory to the cygwin bin directory (usually C:\cygwin\bin).
Add "/c bash.exe" to the end of the command in the target parameter.
This will run bash under the windows cmd.exe environment and when you attempt to run mysql it will execute as you would expect. This is working under windows 7 but has not been tested in any other version.
Put cygwin bin directory in path env variable.
Use command window by running cmd
Run bash -l in cmd window
Then MySQL can be run without problem.
Svend Hansen's answer is the right one:
Install windows mysql server files (from mysql-5.5.25-win32.msi for example)
Install Cygwin mysql client with cygwin installer (setup.exe)
Connect to your server in a cygwin window using cygwin client "mysql -u[user] -p[Password] -h[host]", in my case "mysql -uroot -pXXXX -h127.0.0.1"
I think that when the question was posted, the cygwin setup did not provide mysql components, which is solved now.
Althoug Svend Hansen answer has some points, another thing is the PATH in Environment variables - if the path to mysql is before that of cygwin
which mysql
will show
/cygdrive/c/Program Files/MySQL/MySQL Server 5.5/bin/mysql
otherwise it will show the cygwin client.
As reference Wikipedia says:
Some programs may add their directory to the front of the PATH
variable's content during installation, to speed up the search process
and/or override OS commands.
Download Cygwin
Install mysql client app
create an alias in .bashrc file
alias mysql='mysql -h 127.0.0.1'
execute source .bashrc
Now you can connect to mysql
mysql -u user -p
I have created a semi-fix for this that satisfies me.
I ran cygwin.bat in cmd.exe, then typing mysql in- everything worked fine.
I realized right there that the problem was mintty.
Easy solution? Download Console2, and under settings you can point
it to the cygwin shell. Restart Console2, run mysql and the output
appears.
This is advantageous anyways, because Console2 has a more robust interface/customization than Mintty. I really like the transparency and color mapping options.
Do This:
just copy ur mysql.exe from C:\Program Files\MySQL\MySQL Server 5.5\bin
paste this mysql.exe in C:\cygwin\usr\local\bin
now run which mysql, It will
Disclaimer: The following solved this issue for me under MinTTY on MinGW/MSYS. From research, I believe this same root cause affects Cygwin as well.
Answer is posted here: https://stackoverflow.com/a/23164362/1034436
In a nutshell, you'll need to prepend your mysql command with winpty's console.exe (or have aliases that does so). This solution worked with native Windows MySQL executables and not a special cygwin/mingw build. You do, however, have to compile winpty, but that was simple and painless, and worked as per their documentation for me.
Note: This also solved my issue with several other native Windows console applications, namely Python and Mercurial with OpenSSH.
Reinstall cygwin and during reinstallation search for mysql in packages, install the mysql client and then it would work fine.
Found this question today 2018-03-18 looking for some answers to
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysql.sock' (2 "No such file or directory")
The file /etc/my.conf references config files in /etc/my.cnf.d
I added this to /etc/my.cnf.d/client.cnf:
[client]
host=127.0.0.1
protocol=tcp
After that I was able to access the local windows MySQL instance from a cygwin terminal using mysql -u root -p

connecting to mysql from cygwin

I can successfully connect to MySQL from a DOS prompt, but when I try to connect from cygwin, it just hangs.
$/cygdrive/c/Program\ Files/MySQL/MySQL\ Server\ 5.1/bin/mysql -u root -p
What's wrong?
I just came across this, and when I read someone's mention of it being a windows/DOS command that you run in cygwin I did a which mysql and that gave me:
$ which mysql
/cygdrive/c/Program Files/MySQL/MySQL Server 5.5/bin/mysql
So I ran the cygwin Setup.exe searched for "mysql" and installed the latest "mysql client". Now which mysql looks like:
$ which mysql
/usr/bin/mysql
And the MySQL command works in cygwin :)
Though it's an old question, it would be nice to have the actual answer here, as people (like myself) might still stumble across it.
If your attempts to run the MySQL client from Cygwin return the following error:
$ mysql -u root -p
Enter password:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysql.sock' (2)
Then you can fix it by adding the explicit -h 127.0.0.1 options to the command line, as in:
$ mysql -u root -p -h 127.0.0.1
Updates based on comments:
To avoid specifying -h 127.0.0.1 on the command line every time you connect, you can add:
[client]
host=127.0.0.1
to the file /etc/my.cnf
On some installations of Cygwin, specifying the host with -h might not be enough. If so, try also specifying:
--protocol=tcp
or add
protocol=tcp
to the config file.
Assuming that you have a native Windows build of MySQL, there is a terminal emulation incompatibility between DOS (command prompt) windows and bash. The prompt for mysql isn't showing up.
To confirm this, type a command and return - it will probably work, but the prompt and the echo of the command (what you're typing) is getting lost.
There may be a workaround in either the CYGWIN sytem properties or in bash, but I've never taken the time to work this one out.
Other answers lack the following key detail:
Cygwin has two shells:
Default: c:\cygwin\bin\mintty.exe
Basic: c:\cygwin\Cygwin.bat (which launches c:\cygwin\bin\bash.exe)
The Win32 MySQL can write properly to #2, but not #1, because Win32 MySQL cannot probe stdin properly (thanks #PeterNore)
Want to know if you're using Win32 MySQL? Use which, e.g.
$ which mysql
/cygdrive/c/Program Files/MySQL/MySQL Server 5.1/bin/mysql
Bonus: Cygwin guide to overcoming path problems (thanks #Dustin)
I posted a solution/workaround here:
enter key sometimes not recognized in windows apps under cygwin
Run bash from the cmd.exe executable and then mysql will work inside bash.
Create a shortcut for cmd.exe on your desktop.
Open up the properties for the shortcut and change the startup directory to the cygwin bin directory (usually C:\cygwin\bin).
Add "/c bash.exe" to the end of the command in the target parameter.
This will run bash under the windows cmd.exe environment and when you attempt to run mysql it will execute as you would expect. This is working under windows 7 but has not been tested in any other version.
Put cygwin bin directory in path env variable.
Use command window by running cmd
Run bash -l in cmd window
Then MySQL can be run without problem.
Svend Hansen's answer is the right one:
Install windows mysql server files (from mysql-5.5.25-win32.msi for example)
Install Cygwin mysql client with cygwin installer (setup.exe)
Connect to your server in a cygwin window using cygwin client "mysql -u[user] -p[Password] -h[host]", in my case "mysql -uroot -pXXXX -h127.0.0.1"
I think that when the question was posted, the cygwin setup did not provide mysql components, which is solved now.
Althoug Svend Hansen answer has some points, another thing is the PATH in Environment variables - if the path to mysql is before that of cygwin
which mysql
will show
/cygdrive/c/Program Files/MySQL/MySQL Server 5.5/bin/mysql
otherwise it will show the cygwin client.
As reference Wikipedia says:
Some programs may add their directory to the front of the PATH
variable's content during installation, to speed up the search process
and/or override OS commands.
Download Cygwin
Install mysql client app
create an alias in .bashrc file
alias mysql='mysql -h 127.0.0.1'
execute source .bashrc
Now you can connect to mysql
mysql -u user -p
I have created a semi-fix for this that satisfies me.
I ran cygwin.bat in cmd.exe, then typing mysql in- everything worked fine.
I realized right there that the problem was mintty.
Easy solution? Download Console2, and under settings you can point
it to the cygwin shell. Restart Console2, run mysql and the output
appears.
This is advantageous anyways, because Console2 has a more robust interface/customization than Mintty. I really like the transparency and color mapping options.
Do This:
just copy ur mysql.exe from C:\Program Files\MySQL\MySQL Server 5.5\bin
paste this mysql.exe in C:\cygwin\usr\local\bin
now run which mysql, It will
Disclaimer: The following solved this issue for me under MinTTY on MinGW/MSYS. From research, I believe this same root cause affects Cygwin as well.
Answer is posted here: https://stackoverflow.com/a/23164362/1034436
In a nutshell, you'll need to prepend your mysql command with winpty's console.exe (or have aliases that does so). This solution worked with native Windows MySQL executables and not a special cygwin/mingw build. You do, however, have to compile winpty, but that was simple and painless, and worked as per their documentation for me.
Note: This also solved my issue with several other native Windows console applications, namely Python and Mercurial with OpenSSH.
Reinstall cygwin and during reinstallation search for mysql in packages, install the mysql client and then it would work fine.
Found this question today 2018-03-18 looking for some answers to
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysql.sock' (2 "No such file or directory")
The file /etc/my.conf references config files in /etc/my.cnf.d
I added this to /etc/my.cnf.d/client.cnf:
[client]
host=127.0.0.1
protocol=tcp
After that I was able to access the local windows MySQL instance from a cygwin terminal using mysql -u root -p