197

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.

Michael Lihs
  • 7,460
  • 17
  • 52
  • 85
hch
  • 6,030
  • 8
  • 20
  • 24
  • 1
    I did that `brew install mysql-client`. What's the next step? I was expecting to get a `mysql` cli option or something. And no mysql is not present in `/usr/local/` directory. – Prachi Aug 11 '21 at 01:13
  • 1
    The `brew install mysql-client` command doesn't install a working MySQL client. – Andrew Koster Sep 25 '21 at 18:33
  • 1
    @AndrewKoster of course it doesn't. that might actually have made sense :) – ekkis Nov 01 '21 at 21:22

16 Answers16

200

install MySQLWorkbench, then

export PATH=$PATH:/Applications/MySQLWorkbench.app/Contents/MacOS
Community
  • 1
  • 1
user1659189
  • 2,414
  • 1
  • 13
  • 6
  • restart your mac for the path changes to take effect – captainblack Nov 27 '16 at 05:53
  • 9
    How can this be the accepted answer this only works for one session and has to be redone for every single session. (and yes I tried that restarting thing but that didn't work) – jonalv Dec 05 '16 at 13:08
  • 7
    @jonalv Because shell commands can easily be added to one's .bashrc or .bash_profile if one desires any command (such as this one) to be run in every shell on startup. – jdunk Dec 26 '16 at 12:55
  • 105
    How can this be the accepted answer when the question states "Not any GUI"? – Josh J Jan 30 '17 at 18:21
  • Well this should not really be accepted answer but heck it helped me. I didn't realize Workbench comes with these tools installed. – Willa Apr 06 '17 at 16:38
  • @captainblack or just load a new terminal –  May 11 '17 at 23:01
  • 11
    Add the line in this answer to ~/.bash_profile and then run `source ~/.bash_profile` or load a new instance of terminal. – Luke Jan 15 '18 at 07:15
  • 1
    This is not an acceptable answer. First, the initial query asked for a command-line only implementation. Second, and more important, this requires requires Microsoft's .Net Framework 4.5. – JWLM Feb 12 '18 at 15:47
  • 8
    You're taking this too far. You get a GUI _and_ a CLI. Just use the CLI. Put it in your bash rc and you're done. – duality_ Aug 31 '18 at 09:00
  • I found that the current MySQLWorkbench doesn't contain mysqlsh anymore – Peiti Li Jan 28 '20 at 22:54
  • MacOS 11.2 is required – enagra May 05 '21 at 08:30
  • “MySQLWorkbench” can’t be opened because Apple cannot check it for malicious software. – Mark Locklear Aug 11 '21 at 15:52
  • This is not even close to an answer, for all of the reasons above. Can we get more reports and downvotes? – Andrew Koster Sep 25 '21 at 18:35
  • 1
    It is still working, and it is a terminal solution. iI is not just very detailed answer. You need to add the line mentioned to your bash_profile like this `echo 'export PATH=$PATH:/Applications/MySQLWorkbench.app/Contents/MacOS' >> ~/.bash_profile` then run `source ~/.bash_profile` (as mentioned in other comments) then mysql command is going to work – Daniel Dec 10 '21 at 21:30
  • As @Daniel mentioned, it's not the most detailed answer. It has already been mentioned how to add it to bash. However, If you use zsh, then simply add the line to ~/.zshrc and follow same steps (either run source ~/.zshrc or open a new terminal). – Andorkan Mar 30 '22 at 23:12
129

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

Alex W
  • 37,233
  • 13
  • 109
  • 109
  • 20
    Use `echo 'export PATH="/usr/local/opt/mysql-client/bin:$PATH"' >> ~/.zshrc` if using default zsh (default in recent macOS) – Jonny Jul 24 '20 at 13:18
  • 2
    If you use _zsh_ instead of _bash_ you should add `export PATH="/usr/local/opt/mysql-client/bin:$PATH"` to your `~/.zprofile` file. – bounav Sep 14 '20 at 15:16
  • 4
    This is the correct answer. I can't believe the other answers were even accepted. =0 – medina Apr 14 '21 at 04:16
  • 1
    On my Catalina Mac, `brew install mysql-client` put the executable into /usr/local/cellar/mysql-client/8.0.27/bin, and to avoid a conflict with the Apple-supplied mysql, it didn't create a symlink in an executable directory. So I created my own symlink: `$ ln -s /usr/local/cellar/mysql-client/8.0.27/bin/mysql /usr/local/bin/mysql8`. Having done that, I was able to invoke it with `$ mysql8 -V`. This should work in both bash and zsh. – user1527225 Nov 01 '21 at 00:29
  • this works for me on the macOS Monterey, thank you. – Lucas Liu Mar 24 '22 at 14:41
  • 1
    mac m1, mysql is in `/opt/homebrew/opt/mysql-client/bin/` – Edoardo Apr 12 '23 at 16:06
91

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/

Michael Lihs
  • 7,460
  • 17
  • 52
  • 85
karan_s438
  • 1,353
  • 1
  • 9
  • 15
  • 9
    The OP implied that he did not want to install the server, just the command-line client. – Greg Brown Mar 01 '16 at 18:17
  • @JackSparrow You should have read the question first. You answer requires or implies installing mysql server but the question specifically said he did not want to install the server – Joseph Jul 17 '16 at 21:36
  • Thanks for the answer. The installer should do that kind of stuff. – Agustí Sánchez May 20 '17 at 00:27
72

Best option is:

brew install mysql
Michael Lihs
  • 7,460
  • 17
  • 52
  • 85
Geek
  • 1,369
  • 1
  • 14
  • 25
  • 15
    That installs the server too, what about client only? – raarts Nov 01 '17 at 10:26
  • 93
    `brew install caskroom/cask/mysql-shell` will install the command line client. – Krystian Dec 18 '17 at 15:43
  • 31
    It might be worth mentioning that, if installed this way, the shell is launched with the `mysqlsh` command. – Eric Jorgensen Jan 25 '18 at 03:28
  • @Krystian how do you remove mysql-shell created with the above command? – Viraj Sep 27 '18 at 22:22
  • In the case that the MySQL Community Server was installed before: Be aware that this will make available two different servers on your system, when an upgrade is done make sure you execute the correct binaries. – Eaton Emmerich Jan 04 '19 at 11:23
  • 6
    Thank you @Krystian & @eric-jorgensen I modified command to `brew install Caskroom/cask/mysql-shell` cap C for "caskroom" and command used was mysqlsh from – il0v3d0g Mar 29 '20 at 20:48
  • 2
    @Krystian: You should have posted your comment as an answer. +1 for that comment anyway. My MacOS Mojave 10.14.6, needed to run the command `brew install homebrew/cask/mysql-shell` instead. – Romeo Sierra Oct 07 '20 at 09:05
  • This is a poor answer if the question is just asking for the mysql client. Change it. – Manachi Apr 01 '21 at 05:26
  • What is the diff between `mysql` and `mysql-shell` for `brew`? Maybe `mysql` brings all the tools - import, dump, etc and mysql-shell is for the client. I do `brew install mysql-shell`. – Timo May 09 '22 at 19:25
  • what is the difference between `brew install mysql` and `brew install mysql-client`? – A.Soliman Aug 01 '22 at 10:33
  • @A.Soliman `brew install mysql` installs both the client (used to connect to a db server) and the db server (used to host a db) – Timofey Drozhzhin Oct 17 '22 at 07:35
39

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.

troseman
  • 1,842
  • 20
  • 19
  • 3
    This is the closest to the right answer, considering the OP wanted *client only* and *command line*. However, user16... and @JackSparrow gave the most useful instructions on what to do after install. – Mike Williamson Jul 19 '17 at 21:20
  • mysql client is not installed via this method AFAICT – Rondo Oct 06 '17 at 03:31
  • 4
    looks like they broke it out as a separate tool, https://dev.mysql.com/downloads/shell/ – troseman Oct 06 '17 at 07:39
  • MySQL Shell installs `mysqlsh` which seems to be an equivalent to the classical `mysql` command. You may install it with Homebrew using `brew cask install mysql-shell`. – Bernhard Fürst Nov 21 '17 at 13:09
  • 3
    If you're expecting a `mysql` prompt, try `mysqlsh --sql` for a similar experience (and it takes mostly the same arguments, helpfully!) – Lorna Mitchell Dec 29 '17 at 11:56
31

There is now a mysql-client formula.

brew install mysql-client

egze
  • 3,200
  • 23
  • 23
  • 12
    It *does* install mysql command as something like `/usr/local/Cellar/mysql-client/x.x.x/bin/mysql`, which is probably not on PATH. Doing `brew link mysql-client` afterwards worked for me. – alx Jun 08 '19 at 13:25
  • 2
    `brew install mysql-client@5.7` for mysql 5.7 – dEll May 18 '20 at 06:23
  • thanks @alx the link got it working for me. however, i had to do `brew link mysql-client@5.7` since that was the version i installed via homebrew. – aus Jul 18 '21 at 18:27
  • 2
    Thanks, this worked! Though after install I had to add this to my .zshrc file: `export PATH="/usr/local/opt/mysql-client/bin:$PATH"` – Pavel Nov 19 '21 at 22:45
10

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
Michael Lihs
  • 7,460
  • 17
  • 52
  • 85
  • So there is the maybe silly `js` mode by default and your last code shows how to circumvent, plus one. – Timo May 09 '22 at 19:46
7

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

Michael
  • 8,362
  • 6
  • 61
  • 88
caccialdo
  • 199
  • 3
  • 12
  • 2
    This is how I solved the problem by the light your answer shed. Thanks. ```cp /Applications/MySQLWorkbench.app/Contents/MacOS/mysql /usr/local/bin``` – zzxwill Nov 09 '17 at 09:01
  • I would recommend using a soft link: `ln -s /Applications/MySQLWorkbench.app/Contents/MacOS/mysql /usr/local/bin` so that updates are reflected without having to run the copy again. – David Weber May 28 '18 at 15:36
3

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`.
brian d foy
  • 129,424
  • 31
  • 207
  • 592
shashankS
  • 1,043
  • 1
  • 11
  • 21
  • Newer `brew` syntax is `brew install mysql-shell --cask`. But why it is listed under the `cask` section? Isn't `cask` for GUI application? Also this might help: `mysqlsh --host=xxxx --port=3306 --user=xxx --password=xxxx --ssl-mode=DISABLED --sql` – Rick May 27 '22 at 07:06
2

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.

Michael Lihs
  • 7,460
  • 17
  • 52
  • 85
Paul Kenjora
  • 1,914
  • 18
  • 20
  • This worked for me. I installed the MySQL Shell from the MySQL website but still nothing. Fixing the path as directed above did the job. It seems the install did work, but didn't set the path correctly (if at all). – James Smith Dec 29 '17 at 17:48
1

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)"

Kimmo Hintikka
  • 13,472
  • 7
  • 34
  • 63
1

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)

gagarine
  • 4,190
  • 2
  • 30
  • 39
Alex G
  • 212
  • 1
  • 7
0

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.

Willa
  • 730
  • 1
  • 7
  • 14
0

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
moo
  • 1,597
  • 1
  • 14
  • 29
0

I don't know why, but for me Brew put the binaries at:

/opt/homebrew/Cellar/mysql-client/8.0.33_1/bin
halfer
  • 19,824
  • 17
  • 99
  • 186
Aku
  • 660
  • 6
  • 9
-1

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.

LandiLeite
  • 555
  • 7
  • 7