Importing MySQL Database in Mac Terminal

Few days back I have posted “How to Dump MySQL Database to a file on Windows”. Exporting large database in phpMyAdmin is not much difficult (however the “command line” method is better), but importing large database in phpMyAdmin is a real headache. First you need to allow to upload a large file. For this you need to change your php settings (you need to modify php.ini file). Your application may not need to upload such large files, but you need to modify this for uploading large database dump file. Then it’ll take a bit long time to upload the huge database file. Then when phpMyAdmin will execute huge number of lines SQL, crossing the maximum execution time and again starting from the point, whether it just crossed maximum execution time. During this process, if you encounter any other problem, then you will have a bigger headache. So you should always prefer importing large MySQL database using command line, i.e. Terminal in Mac. You will find many resources on web discussing about how to Import MySQL Database in linux. But you will find very less resource on Importing MySQL Database in Mac Terminal. So, I decided to write about this here.

Open Terminal.

Then change directory to where MySQL is installed. If you have installed MAMP, then MySQL is installed in “/Applications/MAMP/Library/bin/” directory. So enter the following command:

cd /Applications/MAMP/Library/bin/

Change the path, if your installation is different.

Now execute the following command:

./mysql -u database_username -p database_name < /database_file.sql

In the above example, I have my database file on the root folder (Macintosh HD). Depending on where you have kept your database file, change that part of the command. It'll ask for the password of database user. Provide that and the database will be exported in few seconds, depending upon the size of the database. And everything is done now.





2 responses to “Importing MySQL Database in Mac Terminal”

  1. Gregor McKelvie Avatar

    Thanks for this article. Saved me a good bunch of time. My db was fairly large and via terminal it only took a few seconds to import.


  2. Macrox Avatar

    I am not a regular here but i would just like to say that your article helped a lot! I used to only work with the windows command line and had no idea how to do it in terminal. So thanks again and keep up the good work!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.