50

I want to practice using SQL instead of phpMyAdmin.

How do I log into MAMP's MySQL from the terminal?

Don P
  • 60,113
  • 114
  • 300
  • 432

8 Answers8

92

I'm assuming the version of MAMP you're using installs itself in /Applications/MAMP. First make sure via the MAMP console that the Mysql server is on. Then connect like this from command line:

/Applications/MAMP/Library/bin/mysql -uUsername -pPassword

Obviously replace Username and Password. BTW, there is no space between -u and the Username or -p and the Password.

Good Luck learning Mysql the old fashion way!

Ray
  • 40,256
  • 21
  • 101
  • 138
  • 8
    If you give the password in this way, other processes on the system can discover the password via `ps -ef` output. Best to leave the `-p` alone with no password, so you can give it via interactive terminal input, which can only be sniffed by processes with administrative privileges. – sarnold Feb 18 '12 at 02:09
  • In general thats an excellent point, but I was assuming he was on a mac without any process reading trojans. Still, it not a practice to get used to if you one day move to shared hosts – Ray Feb 18 '12 at 03:02
  • Thanks Ray! If I were on a shared host for my live website, what is the best way to enter the password? – Don P Feb 18 '12 at 17:16
  • 1
    Just enter -p without the password. When it asks, what is your password, entering it at that point is safe. – Ray Feb 18 '12 at 23:21
  • @Donny P. dont forget to check my answer as correct if it resolves your question! Thanks – Ray Feb 19 '12 at 00:01
  • This is for MAMP standard, but how to do this with MAMP PRO ? – alvinmeimoun Nov 13 '15 at 08:22
  • In some versions, path is MAMP\bin\mysql\bin to run mysql queries. – Shiks Sep 15 '16 at 10:41
  • This answer is still valid 8 years later, I have MAMP PRO and couldn't login via --host and --port, but this worked. – Erika Jun 24 '20 at 06:57
37

If you just want to type:

mysql -u Username -p

Update for macOS Big Sur replace all ~/.bash_profile or ~/.profile with ~/.zshrc from the next commands.

Check first if you have a file named ~/.bash_profile or ~/.profile or ~/.zshrc with the following command

ls -la ~/

If one of those files exist, edit that file. Else, create a new one with what ever editor you like (here I do it with nano and have a ~/.bash_profile file)

sudo nano ~/.bash_profile

insert following line

alias mysql=/Applications/MAMP/Library/bin/mysql

Save the file and quit nano with CTRL + X and then type Y and enter

Then you need to type

source ~/.bash_profile

Now you can use

mysql -u root -p
caramba
  • 21,963
  • 19
  • 86
  • 127
  • 1
    Good tip @caramba ! The only one thing more you need to get it work immediately is to execute: $ source ~/.bash_profile – FRITZ F Mar 10 '15 at 16:50
  • 5
    Or you can add the path to PATH in your .bash_profile and then you have access to run any executable in that directory from any place you are in terminal. e.g., export PATH="/Applications/MAMP/Library/bin:${PATH}". alias works too but only gives you access to that one command. – JacquelineIO Jul 25 '15 at 19:07
11

Just simply create a symbolic link to the MAMP mysql.

sudo ln -s /Applications/MAMP/Library/bin/mysql /usr/bin/mysql

Then you can easily call it from any directory like this:

mysql -uUsername -pPassword
Timothy
  • 4,198
  • 6
  • 49
  • 59
5

I had to do a little modification, first go to the bin folder

cd /Applications/MAMP/Library/bin/

then to run mysql file a had to execute

./mysql -uUSERNAME -pPASSWORD

The reason was that: Running an executable in Mac Terminal

Community
  • 1
  • 1
TlonXP
  • 3,325
  • 3
  • 26
  • 35
3

If you want to do it on Windows type:

 c:/MAMP/bin/mysql/bin/mysql.exe -u username -p dbname < data.sql
Chris Rosenau
  • 264
  • 1
  • 7
  • 19
2

I could not get any of the above working :/ I went to http://blog-en.mamp.info/2009/08/using-mysql-command-line-with-mamp.html and it worked perfectly. Hope this helps.

Open a new terminal window, copy and paste the section below and TADA!

/Applications/MAMP/Library/bin/mysql --host=localhost -uroot -proot

2

This line works for me:

/Applications/MAMP/Library/bin/mysql -uroot -p
Pang
  • 9,564
  • 146
  • 81
  • 122
DarkSide
  • 21
  • 1
1
cd /Applications/MAMP/Library/bin

then

  ./mysql -u root -p

Enter Your Password...

coletrain
  • 2,809
  • 35
  • 43
sdk
  • 73
  • 2
  • 9