MySQLDump backup and restore on different schema same server - mysql

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

Related

mysqldump include "use db_name" statement with single table dump

I'm trying to use mysqldump to programmatically create exports of single tables for a system of exporting and importing tables from our production environment into local development environments.
I need the .sql file to include a "USE database_name" statement at the top. The production db name is different than our local db name (we prefix test_ on development). It is imperative I preserve the .sql dump files as they are used on docker container initialization.
I can achieve this by including the --databases database_name flag in the mysqldump command. The problem is, this dumps the entire database. I only want a single table.
Does anyone know if this is possible somehow - to dump a single table but also include the "USE data_basename" line?
Here's what I have in a bash script, so far - where $1 is the specific table name. This works, but I have to manually enter the "USE database_name" statement at the top of the generated .sql file.
mysqldump -h {host i.p.} -u {username} --password="{password}" db_name $1 > docker/local_mysql_db/files/docker-entrypoint-initdb.d/$1.sql
docker exec -i db_container_name mysql {test_db_name} < docker/local_mysql_db/files/docker-entrypoint-initdb.d/$1.sql

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

Export mysql schema (data only) except for one table

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.

How to duplicate MySQL Datatabase + include Stored Procedure (Routines)

if I want copy/backup database to the same server, I try:
mysqldump -u root -pfirdi --opt fdev | mysql -u root -pfirdi fdev2
but NOT include the Stored Procedure.
if I try to run:
mysqldump -u root -pfirdi -R --opt fdev | mysql -u root -pfirdi fdev2
(I add option '-R' in the line), it still can not (hang).
Does anyone can help me?
Thank You.
I frequently use something like this:
mysqldump ... -v -R yourDatabase | mysql ...
If your database is too large, you may need to dump it to a file first, and then load it into your server:
mysqldump ... > sqlFile.sql
mysql ... < sqlFile.sql
In Linux, something like this may work:
mysqldump ... > sqlFile.sql && mysql ... < sqlFile.sql && rm sqlFile.sql
This will:
Dump the database to sqlFile.sql
Load sqlFile.sql to your new database (only if the dump was successful)
Delete sqlFile.sql (only if the load operation was successful).
If any of these steps fails, the instruction will stop.
Some useful options for mysqldump:
--delayed-insert: Usues insert delayed instead of normal insert.
--disable-keys: Encloses the insert instructions for each table between alter table ... disable keys and alter table ... enable keys. That may improve the insert speed
-v: Output what mysqldump is doing to Standard output. This is useful if you want to find out what table is hanging your dump.
-R: Also export stored procedures and functions.
-d: Only dump data structures... it is useful if you want to create a database "template". Using -R and -d together creates an empty database dump (no data), with routines.
If you are having problems exporting the routines, you can try something like this:
Create a "no data" dump of your database: mysql ... -d -R > emptyDbDump.sql`
Edit the emptyDbDump.sql file with a text editor to remove all table and view definitions (leave only the stored functions and procedures definitions).
Load this new emptyDbDump.sql file to your new database.
Take a look to the reference manual for additional information.
Hope this helps

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.