Can't access mysql from command line mac - mysql

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?

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

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

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...?

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.

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

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.

I think this is the more simpler approach:
Install mySQL-Shell package from mySQL site
Run mysqlsh (should be added to your path by default after install)
Connect to your database server like so: MySQL JS > \connect --mysql [username]#[endpoint/server]:3306
Switch to SQL Mode by typing "\sql" in your prompt
The console should print out the following to let you know you are good to go:
Switching to SQL mode... Commands end with ;
Go forth and do great things! :)

Open terminal and run:
/usr/local/mysql/bin/mysql --user=root -p

Related

MySql Command not found : MacOS

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

Trying to edit the my.cnf file in mysql and it does not work

I'm attempting to use cat /etc/my.cnf in mysql but when I do this, it just shows -> wanting me to keep putting in inputs.
However, when I put a semicolon at the end.
cat /etc/my.cnf;
it throws an error.
How can I edit the /etc/my.cnf file?
I'm running this on terminal on a Mac and I got into the mysql server by using the command
sudo /usr/local/mysql/bin/mysql -u root
.
Are you typing this in to the mysql> shell? If so, UNIX-shell commands like cat won't do anything useful. These aren't commands MySQL knows how to deal with.
You need to run these in a plain Terminal (shell) window outside of MySQL.
By default most systems launch the bash shell which has access to command-line tools like vi, etc. The MySQL shell is SQL only.
It seems you are trying to edit MySQL configuration from MySQL shell but that will not work. Follow below
steps to edit the file
Open terminal and type below command to open mysql config file in textedit
sudo nano /etc/my.cnf
Save the file and restart MySQL
Hope this helps you

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

MYSQL terminal Mac command not working 'mysql'

I have a hidden file in my home directory called .profile with the following:
MYSQL_HOME = /usr/local/mysql/bin/mysql
export PATH=$MYSQL_HOME/bin:$PATH
The following command works:
$ /usr/local/mysql/bin/mysql
However mysql does not. How can I change the path to make the command work?
I have MAMP installed, I'm just not sure how to use it. Thanks!
I believe the error is in your first line:
MYSQL_HOME=/usr/local/mysql/bin/mysql
Which should be like this:
MYSQL_HOME=/usr/local/mysql

Terminal Command not working to connect to MySQL

I'm working on a Mac 10.6.6 with MAMP installed.
Previously, I have been able to connect to mysql using this from my command line
mysql --host=127.0.0.1 --port=8889 --user=root -p
but now it is saying
-bash: mysql: command not found
Do you have any idea how I can fix this problem?
add to your ~/.bash_profile
PATH=$PATH:/usr/local/mysql/bin:/usr/local/postgre/bin
export PATH
Then, in an existing terminal: source ~/.bash_profile
So, to get back to the question about MAMP, I'm going to try to simplify #SnapShot's answer.
Edit your bash profile (using whatever text editor you like, but in the example below it's using vi:
vi ~/.bash_profile
Then insert
export PATH=/Applications/MAMP/Library/bin:$PATH
That should work for you.
There are a number of ways to do this:
Probably the best way is to type at the terminal:
vi ~/.bash_profile
and add
export PATH=/Applications/XAMPP/xamppfiles/bin:$PATH
to your path statement
and then run
source ~/.bash_profile
Depending on how much you run it you can also just type in:
/Applications/XAMPP/xamppfiles/bin/mysql
or make an alias like this:
alias m='/Applications/XAMPP/xamppfiles/bin/mysql --host=localhost --port=8889 --user=root -p'
then just typing m will start it for you.
You can also make a shell script to pass different parameters if you login as a different user:
vi ~/m.sh
/Applications/XAMPP/xamppfiles/bin/mysql --host=localhost --port=8889 --user=$1 -p
and run it by using sh ~/m.sh username
Also, if you only use those same parameters you don't really to include them - they are the defaults for MAMP. You can just run the mysql commmand without them.