How to mysqldump WITHOUT dropping any tables - mysql

Normally, when I backup the database, I run a command like this:
mysqldump -uuser -p -hhost -Ddatabase > C:\TEMP\db_2018-04-05.sql
Inside that file, there are DROP table statements. This is normally fine, but I've modified my localhost to have a different schema than the production database.
If I execute this file, it will blow away the important changes to the database schema on my localhost.
All I need is the INSERT statements. Is there any flag I can pass mysqldump to achieve this?

Include the command for the mysqldump ignore the structure.
mysqldump --no-create-info ...

All you need is add --skip-add-drop-table option when using mysqldump.
$ mysqldump -uuser -p -hhost -Ddatabase --skip-add-drop-table > C:\TEMP\db_2018-04-05.sql
Now no DROP TABLE IF EXISTS in SQL files.
see docs of mysql on --skip-add-drop-table.

Related

Is importing a table from dump will delete my old table data? [duplicate]

Normally, when I backup the database, I run a command like this:
mysqldump -uuser -p -hhost -Ddatabase > C:\TEMP\db_2018-04-05.sql
Inside that file, there are DROP table statements. This is normally fine, but I've modified my localhost to have a different schema than the production database.
If I execute this file, it will blow away the important changes to the database schema on my localhost.
All I need is the INSERT statements. Is there any flag I can pass mysqldump to achieve this?
Include the command for the mysqldump ignore the structure.
mysqldump --no-create-info ...
All you need is add --skip-add-drop-table option when using mysqldump.
$ mysqldump -uuser -p -hhost -Ddatabase --skip-add-drop-table > C:\TEMP\db_2018-04-05.sql
Now no DROP TABLE IF EXISTS in SQL files.
see docs of mysql on --skip-add-drop-table.

Can I delete all data in Database MySQL without deleting tables or relationships?

I am trying to empty the database of any data, while keeping the relationships and tables as they are
I have no idea if my thinking is right or wrong
Yes, you just run mysqldump with --no-data
mysqldump --no-data -u someuser -p mydatabase
You can save it to a .sql file and then drop your database
Then you restore it from the dump
truncate table_name;
table_name is the table you want to delete all data in it.
truncate only works on tables, so you need to execute truncate table one by one.
Use truncate to all table :
mysqldump -d -uuser -ppass --add-drop-table databasename > databasename.sql
mysql -uuser -ppass databasename < databasename.sql
or you can read this, similar problem.

How to make dump of all MySQL databases besides two

this is probably massively simple, however I will be doing this for a live server and don't want to mess it up.
Can someone please let me know how I can do a mysqldump of all databases, procedures, triggers etc except the mysql and performance_schema databases?
Yes, you can dump several schemas at the same time :
mysqldump --user=[USER] --password=[PASS] --host=[HOST] --databases mydb1 mydb2 mydb3 [...] --routines > dumpfile.sql
OR
mysqldump --user=[USER] --password=[p --host=[HOST] --all-databases --routines > dumpfile.sql
concerning the last command, if you don't want to dump performance_schema (EDIT: as mentioned by #Barranka, by default mysqldump won't dump it), mysql, phpMyAdmin schema, etc. you just need to ensure that [USER] can't access them.
As stated in the reference manual:
mysqldump does not dump the INFORMATION_SCHEMA or performance_schema database by default. To dump either of these, name it explicitly on the command line and also use the --skip-lock-tables option. You can also name them with the --databases option.
So that takes care of your concern about dumping those databases.
Now, to dump all databases, I think you should do something like this:
mysqldump -h Host -u User -pPassword -A -R > very_big_dump.sql
To test it without dumping all data, you can add the -d flag to dump only database, table (and routine) definitions with no data.
As mentioned by Basile in his answer, the easiest way to ommit dumping the mysql database is to invoke mysqldump with a user that does not have access to it. So the punch line is: use or create a user that has access only to the databases you mean to dump.
There's no option in mysqldump that you could use to filter the databases list, but you can run two commands:
# DATABASES=$(mysql -N -B -e "SHOW DATABASES" | grep -Ev '(mysql|performance_schema)')
# mysqldump -B $DATABASES

What is the SQL command that 'resets' the database in MySQL?

Is there a SQL command that 'resets' the database in MySQL?
By reset I mean, all rows are deleted and auto increment are reset.
You're looking for TRUNCATE, as in TRUNCATE TABLE mystuff
More info: http://dev.mysql.com/doc/refman/5.0/en/truncate-table.html
mysqldump -uuser -hhost -p --no-data name_of_database > backup_file_name.sql
The above command will keep the entire structure of your database but without the data. (that's what the --no-data param does).
Now when you want to reset your database you just:
mysql -uuser -hhost -p < backup_file_name.sql
View the .sql file created to see what its doing. There are a bunch of options you can add to the mysqldump command.
Enjoy.

Mysqldump question

I am trying to dump a large database using mysqldump command. I would like to avoid 'use database' command in the generated sql file.
This is because I want to create the same database with a different name. Since the sql file size is large I am unable to open the sql file and edit it.
I tried --no-create-db but still I am getting use command in the dump file
Please help.
Maybe you used something like this:
mysqldump -u -p <other options> --database your_database > file.sql
I discovered that when you use --database, the script is generated with that 'use your_database' line. So, don't use that option and the line is gone:
mysql -u -p <other options> your_database > file.sql
You should maybe post this on serverfault, but if you are on a linux box, you could consider sed (or perl/python scripts) to replace the name of the database, or remove the "use " line.
The way to do this is to run mysqldump once for each database. They way I did it is mysqldump -u user -p --tables databasename. This dumps all the tables for a database and removes the USE database statement.
--no-create-db is your friend:
http://dev.mysql.com/doc/refman/5.1/en/mysqldump.html#option_mysqldump_no-create-db