How to make dump of all MySQL databases besides two - mysql

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

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.

MySQLDump backup and restore on different schema same server

I have a staging MySQL db where I had created intermediate tables for data load. I want to migrate this db to Prod but with only the tables needed in production (along with all the routines, events and triggers) using mysqldump.
Before I hand over the script to the platform team to carry this out in production I wanted to test it out locally, so we created a separate schema on the same server and we are trying out the following:
We tried out the mysqldump command without the --add-drop database, instead used --no-create-db because I am concerned that during the restore process, the "use stage" statements in the stored procedure definitions might change the db and wipe out my stage db. The result was that the tables got copied over, but the routines did not.
Is there a way to make this work while:
A. select subset of tables along with all routines and events
B. restore them in a different schema
C. WITHOUT risking wipeout of the original schema?
Use:
mysqldump -u username -p password --hex-blob -R -e --triggers database_name > database_name.sql
You can add or remove options from the dump commands based on your needs.
--single-transaction --- > option if you don't want or can't do table locks
-d --- > -d is --no-data for short.
-R --- > Also consider adding --routines (-R for short ) if you're database has stored procedures/functions
-B --- > Include the CREATE DATABASE command. --databases dbname (shorthand: -B dbname)
-r --- > To avoid having the character set of your shell interfere with encoding, -r schema.sql is preferred over > schema.sql. Also a good idea to specify the character set explicitly
with --default-character-set=utf8 (or whatever). You'll still want to check the set names at the top of the dump file. I've been caught in MySQL charset encoding hell before
--routines --- > Also consider adding --routines if you're database has stored procedures/functions
--hex-blob if you have blob data type in your database

How to mysqldump WITHOUT dropping any tables

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.

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.

Importing all MySQL databases

I mysqldump --all-databases nightly as a backup. But on importing this dump into a clean installation, I obviously run into a couple issues.
I obviously can't (and don't want to) overwrite the new information_schema.
All my users and permissions settings are lost, unless I overwrite the mysql database.
What is standard practice in this situation? Parse out information_schema from .sql file before uploading? And do I overwrite the mysql database or not?
you will not have problems with the info schema
http://dev.mysql.com/doc/refman/5.1/en/mysqldump.html
mysqldump does not dump the INFORMATION_SCHEMA database. If you name that database explicitly on the command line, mysqldump silently ignores it.
For excluding database, try this bash script.
for DB in $(echo "show databases" | mysql -u <username> -p'<password>' | grep -v Database grep -v <some_db_to_exclude>)
do
mysqldump -u <username> -p'<password>' ${DB}
done