Light Mode

MySQL Dump Table from query condition

You can add a where clause in your sqldump command
By
Published ~ 3 minutes read

In order to test a project locally I needed access to the production website database. Unfortunately copying billions of records to my small development VM was not an option. I needed only the latest records but doing a sqldump would get me data from as far back as 2004. The cool thing however is that mysql allows you to add a condition on your mysqldump command to extract whatever you see fit. Let's break it down.

MySQLDump command

By default mysqldump command will make a copy of whatever database you pass it.

$> mysqldump -umy_user_name -p database_name > database_dump.sql

This will include the database schema and all the current data. This is usually what you are looking for. But when you only need the schema, the same command can do the trick with this flag:

$> mysqldump --no-data -umy_user_name -p database_name > database_dump.sql

The --no-data flag, as the name implies, will limit the dump to the schema only and no data.

MySQLDump a table

If you don't want a copy of the whole database, you have the options to dump specific tables. For that you only need to name the table list after the database name.

$> mysqldump -umy_user_name -p database_name my_table1 > mytable1_dump.sql

Note that you can pass multiple tables if you like:

$> mysqldump -umy_user_name -p database_name my_table1 my_table2 my_table3 > mytables_dump.sql

If you want only the schema of the tables you can pass the --no-data flag as usual or simply -d.

MySQLDump a table with a condition (--where)

This is the problem I was having. I didn't want to dump a billion records. I only wanted the records for this year and luckily mysqldump has a flag for that. So let's say I want to dump from my_table1 where the field date_created is greater than '2016-01-01'. Here is how I would write it.

$> mysqldump -umy_user_name -p database_name --tables my_table1 --where="date_created > '2016-01-01' " > mytable1_filtered_dump.sql

I used the --tables flag to let mysql know that everything I pass after is a table name, then the --where flag allows me to pass a string for the condition. Note that the condition can be as complex as you want. All it cares about is that it is syntactically correct.

How do I figure out all I can do with mysqldump.

If you are reading this on my blog, it means you skipped the very first step you should have followed before googling. There is a in-depth documentation you can read before you google. And that documentation is available at your fingertips... no internet needed.

$> man mysqldump

The default manual contains all the information I have just told you and then some. Sometimes reading a little bit from it can go a long way. In the meanwhile enjoy exporting your database with a condition.


Did you like this article? You can buy me a coffee.
Share your insightful comments here.

Join my newsletter

Follow me on Twitter, Spotify, or RSS Feed

On a related note, here are some interesting articles.

Using MySQLi and PDO instead of MySQL_*

It is time to deal with mysql_* functions once and for all. These methods are deprecated and slow. The time to upgrade has long passed yet we still see it everywhere. Since I cannot force every author to update their tutorial and blogs, I decided to write a post to hopefully rank better and provide the essential information to help new comers.

SQL injection and how to protect your website

Experienced developers are expensive. In a world where cutting cost seems like the best option, companies try to maximize their profit by spending less and less on good talent. It is much cheaper to hire someone who just learned php a few weeks ago then a seasoned developer. But it becomes a very bad investment when the newbie introduces insecure code. The problem is, a lot of things learned from those LAMP CRUD application tutorial do not focus much on security. When this code is introduced to a commercial application, the damage can be very expensive. I like how stackoverflow users are fighting very hard to eradicate SQL injection, I am doing my part too but it seems like it is much easier to find insecure code on-line. That said, I will attempt to scare you off your feet so you know better what is the cost of SQL injection.

Abstracting MySql results in PHP

If there is one thing a web server does everyday it iss connecting to the database. I have been using PHP for many years now, but if you ask me to write a script to fetch data from the database I couldn't do it without going back to the Ultimate PHP manual to find a few examples first.

View all articles

Comments

There are no comments added yet.

Let's hear your thoughts

For my eyes only