mysql command is not found in macOS - mysql

I have installed MySQL with .dmg installation file according to the official page. But it returns command not found: mysql when I execute mysql command.
How to fix this issue?

The documentation for MySQL says:
When installing using the package installer, the files are installed into a directory within /usr/local matching the name of the installation version and platform. For example, the installer file mysql-5.7.29-osx10.13-x86_64.dmg installs MySQL into /usr/local/mysql-5.7.29-osx10.13-x86_64/.
Once you verify that there is a bin folder in this directory, you have to make sure that the terminal looks for the MySQL command there. This can be done by executing the following command:
export PATH=$PATH:/usr/local/<my-path>/bin

Adding the following line to .bash_profile worked for me:
export PATH=${PATH}:/usr/local/mysql/bin/
Then either restart the terminal or to apply the changes to an existing session, run:
source ~/.bash_profile

If you had installed mysql#5.7 using brew:
paste/type below command in terminal:
echo 'export PATH="/usr/local/opt/mysql#5.7/bin:$PATH"' >> ~/.zshrc
then paste/type:
mysql -u root
boom!!!!
reason: brew files are installed in usr/local/opt

Try this if you have not upgraded your OS and wants to access mysql
instead of -> mysql -u root -p
use -> /usr/local/mysql/bin/mysql -u root -p

Related

How to run MYSQL in MINGW64 on Windows10?

I really liked the environment of MINGW64 on Windows10 since Python, TMUX and VIM come in handy through Pacman. However, there's no MYSQL package available for MINGW64. How could I run/install MYSQL in MINGW64 on Windows10?
I found a little trick since I was not successful in building the package for MingW64.
I downloaded the mariadb-10.4.11-winx64.msi from the mariadb download site and install the usual way. Added the MariaDB in the path:
export PATH=$PATH:''C:\Program Files\MariaDB 10.4\bin'
Install winpty using pacman:
pacman -Syu winpty
Enter winpty mysql -u root -p:

Mysqldump isn't working: "command not found"

I have to export a database from the command line. I tried using this command:
mysqldump -u root -p db_name > backup.sql
But it returns this error:
-bash: mysqldump: command not found
After this, I also tried with
sudo mysqldump
but the error is the same.
I'm at the beginning and I'm not very good at it at the moment. If I have to work on directory, please be clear because I'm not confident with the terminal.
If you have the latest mysql installation in El Capitan, the mysqldump executable should be in the /usr/local/mysql/bin directory.
In order to use it, you can either run /usr/local/mysql/bin/mysqldump directly, create a symlink, or add the whole bin directory to your path, so you can use any of the executable files without typing the full path.
As suggested below, you can easily make a symlink in your /usr/bin directory, which should already be in your path, by running this command: ln -s /usr/bin/mysqldump /usr/local/mysql/bin/mysqldump
That command should create a link called mysqldump in your /usr/bin directory, which will redirect to the full path of the mysqldump program.
If you would rather add the entire mysql library of tools, all at once, you can follow this guide: https://coolestguidesontheplanet.com/add-shell-path-osx/ and learn how to add new directories to your path.
If you not installed MySql.
Ubuntu
sudo apt update
sudo apt-get install mysql-client
Add a semi-colon to the end of your command, it could make all the difference. I was getting the same error and that fixed it for me.
I'd also suggest declaring everything explicitly in the command you're running. The following worked for me:
1) Find the direct path to your mysqldump file. Check usr/local/mysql/bin/mysqldump if installed using MySQL Server DMG, or if you're using homebrew check in usr/local/Cellar/mysql... (even just do a spotlight search for it).
2) Create a folder to dump the backup to. I made mine ~/dumps.
3) Tie it all together, ensuring you have a semi-colon at the end!
/usr/local/mysql/bin/mysqldump -u root -p db_name > ~/dumps/db_name.sql ;

How do I install command line MySQL client on mac?

I want to install the MySQL client for the command line, not a GUI. I have searched over the web but only found instructions on installing the MySQL server.
install MySQLWorkbench, then
export PATH=$PATH:/Applications/MySQLWorkbench.app/Contents/MacOS
This strictly installs a command line client, without the other overhead:
Install Homebrew (if you don't have it):
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
Then, install mysql-client:
brew install mysql-client
Then, add the mysql-client binary directory to your PATH:
echo 'export PATH="/usr/local/opt/mysql-client/bin:$PATH"' >> ~/.bash_profile
Finally, reload your bash profile:
source ~/.bash_profile
Then you should be able to run mysql in a terminal, if not try opening a new terminal
If you have already installed MySQL from the disk image (dmg) from http://dev.mysql.com/downloads/), open a terminal, run:
echo 'export PATH=/usr/local/mysql/bin:$PATH' >> ~/.bash_profile
then, reload .bash_profile by running following command:
. ~/.bash_profile
You can now use mysql to connect to any mysql server:
mysql -h xxx.xxx.xxx.xxx -u username -p
Credit & Reference: http://www.gigoblog.com/2011/03/13/add-mysql-to-terminal-shell-in-mac-os-x/
Best option is:
brew install mysql
Mysql has a client-only set of utilities:
Mysql client shell
https://dev.mysql.com/downloads/shell/
Other command line utilities
https://dev.mysql.com/downloads/utilities/
Mac OSX version available.
There is now a mysql-client formula.
brew install mysql-client
For installing mysql-shell with homebrew, run
brew cask install mysql-shell
you can then launch the mysql shell with
mysqlsh
if you want to enter SQL mode directly, run
mysqlsh --sql
Open the "MySQL Workbench" DMG file and
# Adjust the path to the version of MySQL Workbench you downloaded
cp "/Volumes/MySQL Workbench 6.3.9.CE/MySQLWorkbench.app/Contents/MacOS/mysql" /usr/local/bin
# Make sure it's executable
chmod +x /usr/local/bin/mysql
Eject the DMG disk
Installation command from brew:
$ brew cask install mysql-shell
Look at what you can do:
$ mysqlsh --help
Run query from mysqlsh client installed:
$ mysqlsh --host=192.x.x.x --port=3306 --user=user --password=xxxxx
MySQL Shell 8.0.18
Copyright (c) 2016, 2019, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its affiliates.
Other names may be trademarks of their respective owners.
Type '\help' or '\?' for help; '\quit' to exit.
WARNING: Using a password on the command line interface can be insecure.
Creating a session to 'user#192.x.x.x:3306'
Fetching schema names for autocompletion... Press ^C to stop.
Your MySQL connection id is 16
Server version: 8.0.18 MySQL Community Server - GPL
No default schema selected;
type \use <schema> to set one.
MySQL 192.x.x.x:3306 ssl JS >
MySQL 192.x.x.x:3306 ssl JS > `\use rafdb`
Default schema set to `rafdb`.
If you installed from the DMG on a mac, it created a mysql client but did not put it in your user path.
Add this to your .bash_profile:
export PATH="/usr/local/mysql/bin:$PATH
This will let you run mysql from anywhere as you.
As stated by the earlier answer you can get both mysql server and client libs by running
brew install mysql.
There is also client only installation. To install only client libraries run
brew install mysql-connector-c
In order to run these commands, you need homebrew package manager in your mac. You can install it by running
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
Using MacPorts you can install the client with:
sudo port install mysql57
You also need to select the installed version as your mysql
sudo port select mysql mysql57
The server is only installed if you append -server to the package name (e.g. mysql57-server)
The easiest way would be to install mysql server or workbench, copy the mysql client somewhere, update your path settings and then delete whatever you installed to get the executable in the first place.
The mysql client is available in macOS ports. If you don't have this excellent third party package manager already installed, it is available from here: https://www.macports.org/
Once you have installed macports, open a terminal and make sure everything is up to date:
sudo port selfupdate
There are multiple different versions of MySQL and mariadb (community fork of MySQL) available in the ports repos. List available versions using the following command:
port search 'mariadb*'
I recommend choosing mariadb over mysql as it is, mostly, a drop in replacement (https://mariadb.com/kb/en/mariadb-vs-mysql-compatibility/) and has excellent community support.
If applicable, choose which version of mariadb you want (a list of versions of mariadb is available here: https://downloads.mariadb.org/mariadb/+releases/). If you're not bothered, install the default version:
sudo port install mariadb
Mariadb (including the mysql-compatible command line client) is now available on your system. On my system, the CLI client resides in the following location:
$ /opt/local/bin/mysql --version
/opt/local/bin/mysql Ver 15.1 Distrib 5.5.68-MariaDB, for osx10.15 (x86_64) using readline 5.1
It's obviously a bit inconvenient to type out the full path, /opt/local/bin/mysql each time you want to use the client. Ports has already thought of this problem. To view available versions of mysql on your system, run:
$ port select mysql
Available versions for mysql:
mariadb (active)
none
Choose one from the list. For example, to use mariadb as the default mysql client:
sudo port select mysql mariadb
Now open a fresh terminal window and you should be able to start the mariadb mysql CLI client:
mysql -h <hostname> -u <username> -p
if you need a lighter solution i recommend mysql-shell, install using the command below.
brew cask install mysql-shell
To start after installation type mysqlsh.

Cannot find MySQL -- get "command not found" error when trying to access MySQL from command line

I have previously installed, connected and uploaded to a WordPress MySQL database via SSH no problem. The people who run my server made a few configuration changes, and now I get a "bash: mysql: command not found" error when I try to log into MySQL via the command line on the same server to access the same database.
I am relatively new to all of this, so I am really not sure what to do. When I run "which mysql" I get a message that says there is no MySQL executable in the /usr/bin directories. I can verify that MySQL is running between the fact that my site is still live and functioning and when I ran a command to test MySQL, I got a message that said "MySQL works!"
I find MySQL files in several directories, but I'm not really sure what I am looking for and how I connect to it when I do find it. I am also not sure if this is user error, or if somehow someone moved or hid MySQL from me -- likely user error??
A MySQL server is not a MySQL client.
Check if MySQL is running by executing this command:
ps aux | grep mysql | grep -v grep
And install the MySQL client:
sudo apt-get install mysql-client # Or your distribution command
If you have MySQL server up and running on your server, it does not mean you have a MySQL client installed on this server.
Try
ls -l /usr/bin/mysql*
Do you see MySQL binaries exactly present on system?
One more problem: Linux has very strange behaviour when you run binaries for another architecture.
For example, if you run a 32-bit executable on 64-bit system you will get an error message like "command or file not found", even if the binaries are actually present!
Now mysql-client has changed to default-mysql-client.
Hence the command:
sudo apt-get install default-mysql-client
Although MySQL is running, if you are not able to connect to the server using the mysql command, then you might be missing to provide soft links:
sudo ln -s /usr/local/mysql/bin/mysqladmin /usr/bin
sudo ln -s /usr/local/mysql/bin/mysql /usr/bin
This should enable you to connect.
In order to get MySQL CLI working or to access the mysql command from anywhere, there are a set of steps to be done to add it to the $PATH variable.
First, open a terminal, and run the following command
echo 'export PATH=/usr/local/mysql/bin:$PATH' >> ~/.bash_profile
Then to reload the Bash profile, run
. ~/.bash_profile
Now run,
mysql -u root -p
Enter the password which you gave during installation. You should see the following result:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.23 MySQL Community Server - GPL
You sometimes need to install MySQL on the local machine as well. That means, if you have a host running a MySQL Docker container, the strange thing happened to me that I needed to install mysql-server on the host as well, not only in the container.
The reason was that there was a shell script that needed to check the right database name from a query. In your case, it might be something different. On your host machine (which might even be your local computer, depending the your setup), if on Linux, try:
sudo apt-get update && sudo apt-get install mysql-server
Then you will get rid of
/home/.../some_bash_script.sh: line 123: mysql: command not found

How can I access the mysql command line tool when using XAMPP in OS X?

I've got a vanilla install of XAMPP in OS X. How can I access the mysql command line tool? I've tried typing "mysql" at the command line, and it spits back "-bash: mysql: command not found".
XAMPP is installed in Mac OS X in the following directory:
/Applications/XAMPP/
You can look what's inside that directory and run mysql command line tool providing the full path to it:
$ /Applications/XAMPP/xamppfiles/bin/mysql
If you need, you can modify your PATH environment variable to include XAMPP binaries and you won't need to specify the whole path all the time.
Open your .profile file in Mac. This can be done by entering the terminal and typing
pico ~/.profile
Add the following line to your ./profile file. Replace the path where you installed Xampp, however by default this is the route and should work:
export PATH=/opt/local/bin:/opt/local/sbin:/Applications/xampp/xamppfiles/bin:$PATH
Open a new terminal window (Recommendation is to quit all terminal windows and then reopen) and type:
mysql
That is all, isn't easy!!
Before using the mysql command, make sure that you start up the server first by running
$ mysql.server start
Then you will be able to use the commands mysqladmin and mysql.
To shut it down, run
$ mysql.server stop
and to restart just use
$ mysql.server restart
Very intuitive.
Open terminal and Follow this bellow step to add mysql to your mac environmental variable
step 1:
sudo nano ~/.bash_profile
step 2:
export PATH=/opt/local/bin:/opt/local/sbin:/Applications/xampp/xamppfiles/bin:$PATH
save it by control+x and then y and hit return. That's it!! now close the terminal and reopen
mysql --version
this will tell you which MySQL version you are using with xampp
Since I cannot comment on the accepted answer by Pablo Santa Cruz - Here's some additional info. If you're going to modify your PATH environment variable to include XAMPP binaries, make sure you add
/Applications/XAMPP/xamppfiles/bin
and not
/Applications/XAMPP/xamppfiles/bin/mysql
to the /etc/paths file. To do this run the command
sudo nano /etc/paths
then add the path to the file. Save using Ctrl+O and exit using Ctrl+X. Quit terminal and open again.