It's possible to ignore tables in mysqldump using:
mysqldump -u username -p database --ignore-table=database.table1 > database.sql
Is it possible to ignore certain records in tables while dumping?
I need to dump a table for daily transfer.
mysqldump requires LOCK TABLES privilege. Unfortunately, LOCK TABLES does not apply at the table level, only at the database level, and I don't want to give the MySQL user that much access.
Is there a way to do something like ...
/usr/bin/mysql -uusername -ppassword -D daterbase -e "select * from table" > outfile.sql
... but have it output in SQL format instead of query result format?
If the table is an InnoDB table, you can use mysqldump --single-transaction dbname tablename.
By using a transaction, it doesn't need to lock anything.
Or you can use SELECT ... INTO OUTFILE but that dumps in a tab-delimited text format, not in SQL format. See on mysql for details.
To dump a single database I can do:
mysqldump -u root files
To dump a single table I can do:
mysqldump -u root files path
How would I dump two tables combined? Something like:
mysqldump -u root files path&path_updated
Note that I have about 50 tables, only two of which need to be exported, so I don't want to do a --skip-table on this.
Just keep adding table names
mysqldump -u root files path path_updated ...
Check the documentation
http://dev.mysql.com/doc/refman/5.5/en/mysqldump.html
mysqldump databasename table1 table2 (Reference)
Is it possible, using mysql dump to export the entire database structure, but exclude certain tables data from export.
Say the database has 200 tables, I wish to export the structure of all 200 tables, but i want to ignore the data of 5 specific tables.
If this is possible, how is it done?
This will produce export.sql with structure from all tables and data from all tables excluding table_name
mysqldump --ignore-table=db_name.table_name db_name > export.sql
mysqldump --no-data db_name table_name >> export.sql
I think that AmitP's solution is great already - to improve it even further, I think it makes sense to create all tables (structure) first and then fill it with data, except the ones "excluded"
mysqldump --no-data db_name > export.sql
mysqldump --no-create-info --ignore-table=db_name.table_name db_name >> export.sql
if you want to exclude more than 1 table, simply use the --ignore-tabledirective more often (in the 2nc command) - see mysqldump help:
--ignore-table=name Do not dump the specified table. To specify more than one
table to ignore, use the directive multiple times, once
for each table. Each table must be specified with both
database and table names, e.g.,
--ignore-table=database.table
I am a new user, and do not have enough reputation to vote or comment on answers, so I am simply sharing this as an answer.
#kantholy clearly has the best answer.
#AmitP's method dumps all structure and data to a file, and then a drop/create table statement at the end. The resulting file will still require you to import all of your unwanted data before simply destroying it.
#kantholy's method dumps all structure first, and then only data for the table you do not ignore. This means your subsequent import will not have to take the time to import all the data you do not want - especially important if you have very large amounts of data you want to ignore to save time.
To recap, the most efficient answer is:
mysqldump --no-data db_name > export.sql
mysqldump --no-create-info --ignore-table=db_name.table_name1 [--ignore-table=db_name.table_name2, ...] db_name >> export.sql
As per the mysqldump docs:
mysqldump name_of_db --ignore-table=name_of_db.name_of_table
In mysqldump from MariaDB in version 10.1 or higher you can use --ignore-table-data:
mysqldump --ignore-table-data="db_name.table" db_name > export.sql
For multiple tables repeat the --ignore-table-data option:
mysqldump --ignore-table-data="db_name.table_1" --ignore-table-data="db_name.table_2" db_name > export.sql
From MariaDB mysqldump docs:
--ignore-table-data=name
Do not dump the specified table data (only the structure). To specify more than one table to ignore, use the directive multiple times, once for each table. Each table must be specified with both database and table names. From MariaDB 10.1.46, MariaDB 10.2.33, MariaDB 10.3.24, MariaDB 10.4.14 and MariaDB 10.5.3. See also --no-data.
Previous answers don't fix the issue with the AUTO_INCREMENT when we export the structure and don't show how to export some specific data in tables.
To go further, we must do :
1/ Export the structure
mysqldump --no-data db_name | sed 's/ AUTO_INCREMENT=[0-9]*\b//g' > export-structure.sql
2/ Export only data and ignores some tables
mysqldump --no-create-info --ignore-table=db_name.table_name1 [--ignore-table=db_name.table_name2, ...] db_name >> export-data.sql
3/ Export specific data in one table
mysqldump --no-create-info --tables table_name --where="id not in ('1', '2', ...)" > export-table_name-data.sql
I tried to use the --skip-opt option to reset AUTO_INCREMENT but this also delete the AUTO_INCREMENT definition on the field, the CHARSET and other things
Another possibility that I use is to avoid the lines inserting data into the wanted table.
The principle is to filter out the INSERT INTO lines using grep -v
mysqldump name_of_db | grep -v 'INSERT INTO \`name_of_table\` VALUES'
or
mysqldump name_of_db | grep -v 'INSERT INTO \`name_of_db\`.\`name_of_table\` VALUES'
That you can easily get into a gziped file and a separated error file
mysqldump name_of_db | grep -v 'INSERT INTO \`name_of_db\`.\`name_of_table\`' | gzip > /path/dumpfile.sql.gz 2> /path/name_of_db.err
and therefore get a nice backup of what you want and know what failed if any :-)
To further improve on kantholy's answer, adding compression and removing most of the disk writes by not writing uncompressed data:
#!/bin/bash
echo -n "db name:"
read -r db_name
echo -n "username:"
read -r username
echo -n "Exclude data from table:"
read -r exclude_table_data
{
mysqldump "$db_name" --user="$username" --password --no-tablespaces --no-data \
&& \
mysqldump "$db_name" --user="$username" --password --no-tablespaces --no-create-info \
--ignore-table="${db_name}.${exclude_table_data}";
} \
| bzip2 -c9 \
> "${db_name}_$(date +%y%m%d_%H%M).sql.bz2"
In my opinion the best answer is from Steak, the only answer really working on any case.
All the answers suggesting two dumps are wrong, or at least they can work just under certain premises.
As many have pointed above you can have problems with sequences.
But I find more critical that the database can have triggers that validate or process information (suppose a trigger that insert records on table B when inserting on table A) - in this case, the sequence of creating the full schema (including triggers) and then inserting the data will create a different set of results.
The below command will export the database structure and ignore the data
mysqldump --no-data --databases -u[db_user] -p[db_password] [schema] > File.sql
Then export the data ignoring the table
mysqldump --ignore-table=[schema.table_name] --databases -u[db_user] -p[db_password] [schema] >> File.sql
I'd like to be able to make a backup of tables with less than 100K rows. What I'm trying to do is clone a development database to my local machine that has many log tables that I don't need the data for, and tables with "legitimate" content.
So I'm going to have one dump that just copies the structures of the tables, and another, that copies the relevant data from these tables with less than 100k rows.
If I have to use an intermediary language like Python or PHP, I'm fine with that.
edit: So the question is, how do I create a mysql dump of data from tables with less than 100k rows?
USe something like this
mysql databasename -u [root] -p[password] —disable-column-names -e
'select table_name from information_schema.tables where table_rows < 100000;'
| xargs mysqldump [databasename] -u [root] -p[password] > [target_file]
p.s. all this will need to be in a single line
To dump only the schema
mysqldump --user=dbuser --password --no-data --tab=/tmp dbname
or try to export schema and data seperately for each table with below command
mysqldump --user=dbuser --password --tab=/tmp dbname
Or
mysqldump --opt --where="1 limit 100000" database > fileName.sql
that would give you the 100K rows from every table.
To Ignore some tables
mysqldump --opt --where="1 limit 100000" --ignore-table=database.table1
--ignore-table=database.table2 database > fileName.sql