I have a script to export the schema of my MySQL database based on which I can generate my migrations. For this process I only require the database schema, not the data itself. This is what I currently use:
mysqldump -u root --p[pass] -h localhost mydb_prod --add-drop-table --no-data > mydb_prod-`date +"%Y-%m-%d-%H:%M:%S"`.sql
The --no-data option does the trick.
However, my migrations history is also kept in a database table. This means that I do want to export the data for my migrations table. I know about the --ignore-table option to explicitly ignore specific tables, however, this would mean that I would have to explicitly list all of my tables which might lead to problems in the future since we only do migrations every once in a while.
Is there a way to export the schema of the database without table data except one (or multiple) explicitly specified tables?
I hate answering my own questions but I found a workaround so I might as well share it.
I basically first export the schema of all tables without the data and then I export just my single migrations table with the data and append this to my .sql output file:
#/bin/bash
now=$(date +%Y-%m-%d-%H:%M:%S)
output_file="mydb_prod-$now.sql"
mysqldump -u root -p[pass] -h localhost mydb_prod --add-drop-table --no-data > "$output_file"
mysqldump -u root -p[pass] -h localhost mydb_prod migration_table --add-drop-table >> "$output_file"
This gives me exactly what I need without having to manually specify every single table explicitly.
You could use a script to edit out whatever table from the dump file itself. I sometimes use sed to rename tables, etc.
Related
I need to restore a dumped database, but without discarding existing rows in tables.
To dump I use:
mysqldump -u root --password --databases mydatabase > C:\mydatabase.sql
To restore I do not use the mysql command, since it will discard all existing rows, but instead mysqlimport should do the trick, obviously. But how? Running:
mysqlimport -u root -p mydatabase c:\mydatabase.sql
says "table mydatabase.mydatabase does not exist". Why does it look for tables? How to restore dump with entire database without discarding existing rows in existing tables? I could dump single tables if mysqlimport wants it.
What to do?
If you are concerned with stomping over existing rows, you need to mysqldump it as follows:
MYSQLDUMP_OPTIONS="--no-create-info --skip-extended-insert"
mysqldump -uroot --ppassword ${MYSQLDUMP_OPTIONS} --databases mydatabase > C:\mydatabase.sql
This will do the following:
remove CREATE TABLE statements and use only INSERTs.
It will INSERT exactly one row at a time. This helps mitigate rows with duplicate keys
With the mysqldump performed in this manner, now you can import like this
mysql -uroot -p --force -Dtargetdb < c:\mydatabase.sql
Give it a Try !!!
WARNING : Dumping with --skip-extended-insert will make the mysqldump really big, but at least you can control each duplicate done one by one. This will also increase the length of time the reload of the mysqldump is done.
I would edit the mydatabase.sql file in a text editor, dropping the lines that reference dropping tables or deleting rows, then manually import the file normally using the mysql command as normal.
mysql -u username -p databasename < mydatabase.sql
The mysqlimport command is designed for dumps created with the mysql command SELECT INTO OUTFILE rather than direct database dumps.
This sounds like it is much more complicated than you are describing.
If you do a backup the way you describe, it has all the records in your database. Then you say that you do not want to delete existing rows from your database and load from the backup? Why? The reason why the backup file (the output from mysqldump) has the drop and create table commands is to ensure that you don't wind up with two copies of your data.
The right answer is to load the mysqldump output file using the mysql client. If you don't want to do that, you'll have to explain why to get a better answer.
I am doing some prototyping and so created a database with a few tables and dependencies. The project became bigger than I thought and now want to clean up the names, dependencies etc and so want to create the DB anew. But I don't want to go through the whole process of creating individual tables again, instead I want to start with what I have, clean the creation scripts up and run them if possible. Is there a way I can export all the scripts to create the DB and tables? Are there tools or mysql command line options to do this?
Thanks,
-S
This can get you started:
mysqldump -u user -ppassword -h host --no-create-db --no-data [other options] old_database > dump.sql
then you can edit the dump file for any necessary changes and import back into the new database:
mysql -u user -ppassword -h host new_database < dump.sql
More information about the mysqldump #MySQL Reference Manual
I recommend you to look at MySql WorkBench
It can do everything you need
Here's the list of all the features
Reverse Engineer from Live Database
Reverse Engineer from SQL Script
Also, good to mention that it's free (community version)
I'm trying to dump all of my mysql data for one database into a single file. That said, I don't want to include the table structures in this file (there is another file which will have the structure of the tables in it).
Is it possible to strictly extract the data and not the table structures?
What I am doing right now ...
# Extracts the database structure
mysqldump -d -hlocalhost -uusername -ppassword database -r database.sql
# Extracts each table and their data individually
mysqldump -d -hlocalhost -uusername -ppassword database --tab .
The first command will spit out a .sql file with the structure of all entities in the database whereas the second one automatically makes the .sql and .txt files with the structure and entities split out.
What I need is one copy of the entire database which is done the same way.
Thanks
Use the --no-create-info option, or its shorthand -t:
--no-create-info, -t
Do not write CREATE TABLE statements that re-create each dumped table.
Note
This option does not not exclude statements creating log file groups or tablespaces from mysqldump output; however, you can use the --no-tablespaces option for this purpose.
So I am looking for the ability to remove all products and their attributes from magento. Selecting all and then removing them from within the admin panel takes too long. Right now I am doing massive bulk imports and tweaking data until it looks right. How can I do this?
DELETE FROM catalog_product_entity;
This table is linked to by dozens of other tables, but the foreign key "CASCADE" feature built in to MySQL InnoDB should automatically delete the corresponding entries in those tables as well.
If you have SSH (or Telnet) access to a server console, I'd recommend to dump the "clean" (no products/only base attributes) database version to a .sql file:
$ mysqldump -h dbhost -u dbuser -p dbname > dump.sql
Then you can test and tweak whatever you want, without any worries. At any point you want to restore your "clean" version, you can do this by simply executing:
$ mysql -h dbhost -u dbuser -p dbname < dump.sql
Is it possible to extract a script from an MySQL database to create a copy of the tables on another MySQL database which is located on a remote server?
You can dump either the structure of the tables, or their content, using the mysqldump command-line tool.
For example, to get a dump of the structure (create table instructions) of all tables of a database, you can use something like this :
mysqldump --no-data --user=USER_NAME --password=PASSWORD --host=HOST DATABASE_NAME
And, to get this to a file, instead of getting it to the standard output, you can redirect that output :
mysqldump --no-data --user=USER_NAME --password=PASSWORD --host=HOST DATABASE_NAME > dump-file.sql
If you also want the data of the tables, and not just their structure, do not use the --no-data option.
Then, on your other server, you can import the dump, using :
mysql --user=NEW_USER_NAME --password=NEW_PASSWORD --host=NEW_HOST NEW_DATABASE_NAME < dump-file.sql
Do you mean to extract a 'dump'?
yes of course. mysqldump utility is the general choice