Configuring terminal to work with MAMP mysql on Leopard

I've decided to try MAMP on new Mac OS X 10.5. Mostly because PHP bundled with Leopard doesn't have GD library (otherwise it has quite cool set of modules). The other reason that is better Marc Liyanage's PHP installer is not yet ready for Leopard.

But after backing up all my MySQL databases (around 200Mb dump) it turned out that you can't acces MAMP's mysql via terminal to restore it (or I was something terribly missing from FAQs).

Here is a hint. You have to create symlink from local mysql.sock to MAMP:

sudo mkdir /var/mysql
sudo ln -s /Applications/MAMP/tmp/mysql/mysql.sock /var/mysql/mysql.sock
sudo chown _mysql /var/mysql/mysql.sock
sudo chmod 777 /var/mysql/mysql.sock

Hope it'll save somebody's few minutes.

Thank you so much for posting this solution. You saved me more than a few minutes.

This not only saved me a few minutes but a huge headache as well. This was the big show stopper when it came to creating a local development environment.

Thanks for taking the time to post this, it really helped me out.

Hey Andrey,

I did what you wrote in your post but still can't get mysql to work. After completing the described commands, where do I need to navigate to and by which command can I execute mysql?

Thanks a lot by a total Mac-Newbie :)

Regards

Frederik

After applying this little hint you can use terminal to access the MySQL under MAMP. Searching say for "mysql commands" in google will give you more info on how to use MySQL from command line interface (read terminal).

A quick tip:

mysql -u yourusername -p yourpassword Databasename < DatabasenameBackupFile.sql
This will import single database backup back to MySQL (the empty database should already exist on the your MAMP)

mysql -u yourusername -p yourpassword < AllDatabaseBackupFile.sql
This will import all databases backup back to MySQL and create all the db. It will help only if you saved complete sql dump from all your databases.

And of course you still can use phpMyAdmin supplied with MAMP.

You'll also probably need to change directories to where the mysql binary file is located,

cd /Applications/MAMP/Library/bin/

And by default the "mysql" file won't be set to execute as a command and so you'll probably see this error:
-bash: mysql: command not found

So just add a "./" in front of it, and try this:

./mysql -u yourusername -p yourpassword < AllDatabaseBackupFile.sql

This should also work:

./mysql --user=yourusername --password=yourpassword < AllDatabaseBackupFile.sql

You'll of course need to probably point to where ever you're download file is located. Perhaps something like: /Users/kent/Sites/AllDatabaseBackupFile.sql;

If you run more than one version of MySQL on your machine, a solution to this problem is to add the "--socket=" directive to your mysql command like so:

mysql --socket=/Applications/MAMP/tmp/mysql/mysql.sock -u username -p

This will make sure the right version of the sock is used and you won't need to overwrite any other versions of MySQL you already have installed.

thanks for the tips, i'll try.

Are you running with some other instance of mysql installed?

You can specify the socket on the command line like:

/Applications/MAMP/Library/bin/mysql -uroot -proot --socket=/Applications/MAMP/tmp/mysql/mysql.sock

I actually switched back to Mark's PHP for 10.5 distro as soon as it was available. It's still in beta but I didn't have any troubles with it yet.

thanks a lot!
but before running you tutorial, you have to mention that MAMP is running, so we can get an active mysql.sock

thanks again :)

Tested it just now & it works great.

Do you have any idea how do this?

Not really unfortunately. As I mentioned earlier I'm back to Entropy PHP distribution for a while. In this case svn works just fine.

Don't forget to check Versions application by the way. It is amazing.

Hi, I think this solution is the one that I'm looking for.

Though, I can't get it to work. I'm a newb in Unix scripting i Terminal, and when i type "sudo ... " it asks for my password, of course. Nothing wrong with that, but say if you don't have any password on your Mac? I just hit 'Enter' and nothing seem to happen. No sign at all.

This is the first time I try MySQL-development on a Mac; I'm used to the Windows way. In WAMP, all I have to do is to click on the small icon, and choose 'MySQL Command Line' or something like that. I've found NONE documentation of this in MAMP. On top of that, I want to be able to access a MySQL command line in Coda too ..

Thanks in ahead.

I had the same problem (it may not a prob) but I just try one of above line. (even easier - pwolanin | Mon, 2008-06-09 21:09 command line

/Applications/MAMP/Library/bin/mysql -uroot -proot --socket=/Applications/MAMP/tmp/mysql/mysql.sock

It works!

##### CREATE NEW EMPTY DATABASE VIA MAMP PHPMYADMIN #####

##### COPY SQL DUMP TO: #####

/Applications/MAMP/Library/bin/

##### CREATE A SOCKET TO MAMP MYSQL IN TERMINAL ######

/Applications/MAMP/Library/bin/mysql -uroot -proot --socket=/Applications/MAMP/tmp/mysql/mysql.sock

##### AT MYSQL PROMPT SET MYSQL TO 'USE' NEW DB ######

\u yourNewDatabase

##### AT MYSQL PROMPT RESTORE SQL DUMP ######

\. yourMysqlFile.sql

I agree, I don't want to disturb the mysql that comes with Leopard ... I just want to ignore it. MAMP is the only mysql that I really *use*.

So, this works for me.

I delete the link to the built-in mysql and then create a new symbolic link in /usr/local/bin for the mysql that comes with MAMP. That way I can execute mysql from any path and don't have to type a long path. Ofcourse, this only works when MAMP is running.

1) cd /usr/local/bin/
2) sudo rm mysql
3) sudo ln -s /Applications/MAMP/Library/bin/mysql mysql

Oh, that password you have to enter ... that's the root or default users password. The same one you enter to install software.

You saved me a lot of time. THANKS, THANKS, THANKS... :)

Hello, I'm getting closer but could use a tip. No problem connecting to mysql following the above instructions using Terminal. However, following "so the full process then for me is:" from Kayless, I enter "u yourNewDatabase" (using the proper name), mysql responds "database changed", then I enter "\. mydatabase.sql" (which is a .sql file in MAMP/Library/bin. Now I get the error "Failed to open file 'foilive.sql', error: 2".
Thanks for any advice.

BTW, the reason I'd like to be able to import into MAMP from the command line is I'm always getting "max_packet_errors" when using php my admin. The file is usually around 10mb but sometimes there are cache tables of around 1000kb; if I empty them I can import through phpmy admin.
Thanks again.

Thanks for this, i was struggling to connect mysql administrator gui to mamp's mysql db, but of course you just paste the socket into the option (click advanced options)

/Applications/MAMP/tmp/mysql/mysql.sock

easy peasy!

I <3 you

This is perfect! Thank you ever so much for saving me a whole lot of headaches.

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.

More information about formatting options