Importing sql database using the command line

This is definitely worth a post. Just when I want to disregard this utility as trivial – every now and then I get a consulting gig where I need to run a full backup – yes of everything, files, database, the works. You see, sometimes I get a task that isn’t as black and white as it may seem at first glance – something is a bit unconventional, but you know – there is nothing wrong with that. Sites come in all shapes and sizes, in all sorts of architectures. It’s good to get exposed to a variety even really weird ones. Look I’d rather poke around a dev site over production no matter how tempting it is – I’ve seen way too many sites go down in my life time, it’s not worth the gamble. I tell every junior developer – the first thing you should do is create a back up. First thing you should do. Then you can show off your programming skills later and you know – be that “rockstar” developer you were meant to be.

So this one isn’t that weird, just a good example of where I had to use the import utility via the command line. I figure it would be a good time to showcase this powerful tool.

So if you ever run into a situation where you are importing a gigantic sql file that either (1) results in a connection time out or (2) gateway error, or (3) some error that didn’t result in the a successful import – here’s what you need to know:


$ mysql -h mysql.example.com -u username -p dbname < databasefile.sql

Yup this will work – you’ve successfully imported the database through the command line.