How to restore dump without drop table before in MySql - mysql

I have a dump of a part of a table from specific date and I would like to restore this dump in a replica database in the specific table, but when I try to restore it, the mysql gives me an error: The table is already exist.
In case it helps, the way I do the dump is the next:
mysqldump --user=root my_db my_table --where="YEAR(created)='2021' AND MONTH(created)='21'" > week21.sql
I know that I can create the dump with --optoption, but this option drop first the whole table, so I would lose the current data in this table right?
Any Idea to do that?
Thanks

mysqldump (or mariadb-dump) emits a mess of SQL statements into its output file. You can read those statements by looking at the file in a text editor. And, you can edit the file if need be (but that's a brittle way to handle a workflow like yours).
You need to get it to write the correct SQL statements for your particular application. In your case the CREATE TABLE statements mess up your workflow, so leave them out.
If you use the command-line option --no-create-info mysqldump won't write CREATE TABLE statements into its output file. So that will solve your immediate problem.
If the rows you attempt to restore with your mysqldump output might already exist in your new table, you can use mysqldump's --insert-ignore command line option to get it to write INSERT IGNORE statements rather than plain INSERT statements.

Related

Does MYSQLDump create temp tables?

I'm on MySQL 5.7
I have a table that is about 150GB, the storage on the computer is only 200GB.
So I wanted to get rid of data older than 9 months on this table.
So my plan was to take a dump of the table with the where clause. Then truncate the table, and reinsert the dump.
Does creating a dump with a where clause create a temp table, where I would run out of storage before being able to export all that data?
What I ran into where I tried regular delete statement was table locking and storage filling up quickly from temp table being created to delete. At least I think this is what happened when I tried to just delete
You can make mysqldump run without using any temp space. Use the --opt switch on the command line. At a minimum use the --quick switch.
You can use a simple WHERE clause and it will still work.
And be sure to run the command on a machine with enough hard drive space to store the output .sql file.

Restore mysqldump that created using --no-create-info and want to skip tables that not present in current database

I have a mysqldump backup that was created using --no-create-info option. I want to restore it to a new database that does not have certain tables (approximately 50 tables remove from target database as they were no longer needed).
So I am getting Table 'table_name' doesn't exist for the obvious reason.
So what is the mysql way of restoring to a database that does not have all the tables present in backup file.
I may user --insert-ignore to avoid this failure but I doubt this may also ignore some genuine errors such as data type mismatch etc.
You cannot insert rows to a table that doesn't exist, obviously.
To restore the data in your dump file, you need to create those tables first. You could go back to your source MySQL instance and dump those table definitions with mysqldump --no-data
If you don't care about the data, and you only want to restore data for tables that do exist, then you could filter out the INSERT statements before trying to import that script.
You could use grep -v for example to eliminate the rows.
Or you could use sed to delete lines between "-- Dumping data for table tablename" and whatever the next table is.
If you don't want to filter the data but you don't care about restoring data for the tables that don't exist, you could create dummy tables with the right fields, but define the tables with the BLACKHOLE storage engine, so the INSERTs won't actually result in saving any data.
One more option: Import the dump file with mysql --force so it continues even if it gets errors on some of the INSERTs.

How to create a dump file when using mysqldump

I'm new to sql and trying to get to grips with some of the basics.
Say I've created a database as follows:
create database mydatabase;
and I want to back this up to a dump file. My confusion comes in here - what is this dump file? Does this automatically generate when I run the mysqldump command? Do I have to create it beforehand? If so, how? Sorry if this comes off stupid but I'm just lost here.
I know the final command would look as follows:
mysqldump -u -p mydatabase > SOMETHING;
but I don't know what to actually insert as the something
The dump file is just a file usually with a bunch of scripts you can run to recreate something in the database. Different database system have their own way of generating these, but you could effectively take a dump from an mysql database and into another rdbms as long as the scripts are compatible.
There are different options;
You could create a dump of just the schema. So that you can recreate that database later on without the data.
Create the dump with schema and data. So that you can recreate the scheme as well as the data within it.
Create the dump with just the data. So you can run those insert commands against another database of the same schema.
Basically dumps are just a way to get back to a previous state. They come in handy for different situation. Such as backing up data, replicating a data from one database to another.
As for the commands I have not used mysql in a while but looks like you are on the right track.

Trying to copy entire MySQL schema using SQL commands

After searching SO, I found answers to the following:
How to copy an entire MySQL schema using mysqldump
How to copy an entire MySQL schema using PHP
How to copy an entire MySQL schema using the enterprise edition of MySQL
How to copy an entire Microsoft SQL Server schema using the menus.
I also found a few hints about copying a MySQL schema using SQL commands.
My question: If I use the following SQL commands to copy a MySQL schema, what parts of the old schema would not be copied? Indexes? Constraints? Views? Anything else?
CREATE SCHEMA new_schema DEFAULT CHARACTER SET utf8;
CREATE TABLE new_schema.table1 LIKE old_schema.table1;
CREATE TABLE new_schema.table2 LIKE old_schema.table2;
CREATE TABLE new_schema.table3 LIKE old_schema.table3;
...;
INSERT INTO new_schema.table1 SELECT * FROM old_schema.table1;
INSERT INTO new_schema.table2 SELECT * FROM old_schema.table2;
INSERT INTO new_schema.table3 SELECT * FROM old_schema.table3;
...;
The CREATE TABLE ... LIKE will take care of indexes and constraints.
You should take care to SET FOREIGN_KEY_CHECKS=0 while you run this, because if table1 has a foreign key to table2, then creating table1 will fail. Likewise inserting data into the tables in the wrong order will fail.
Your script does not cover:
Views
Triggers
Stored procedures
Stored functions
Events
There are no CREATE... LIKE... statements for these other objects. You'll have to use SHOW CREATE... and then run it against in the context of the new schema. See the various SHOW CREATE... statements here: http://dev.mysql.com/doc/refman/5.6/en/show.html
I also caution that the way you INSERT INTO... SELECT FROM... will work, but can fill up your rollback segment if the table is very large. Tools like pt-archiver try to copy tables in batches, ascending along the primary key, to avoid this problem.
I think routines can't be copied directly with sql commands (as far as I know there's not such anything like create procedure myProc like old.myProc).
I would recommend you use mysqldump, since it takes care of copying everything, including the data (if you don't want to copy the data, you can use the -d switch to prevent creating the insert statements).
If you want to create a "template" (a database that is exactly like another database, but without the data), you can use the following:
mysqldump [connectionParameters] -d -R -v yourOldDatabase > databaseTemplate.sql
The options explained:
[connectionParameters]: host, user and password
-d: Don't copy data
-R: Include routines in the dump
-v: Output what mysqldump is doing to the console
You can open this "light" sql script to check how the objects were created.
Hope this helps

How can I update table, if table exists with MySQL info?

I have a mysql broken database and I have another one which is good and with all tables and columns.
How can I import in broken database only missing info which is in good database? I mean tables and columns and values not stored info.
I exported good database and when I try to import in broken database I get: #1060 - Duplicate column name 'id_advice'
So, what I need is to skip if duplicate items and continue to add only info which does not exist.
You can make use of Mysqldump. There is a selection to use no data.
mysqldump -uYourUserName -p=YourPassword databasename --no-data --routines > "dump.sql"
The you can import the table stucture. there is also different options to use create if not exists or drop if exists so you can tailor make it for your needs. I recommend downloading Mysql Workbench, its easily done with that tool.
Info about mysqldump
http://dev.mysql.com/doc/refman/5.6/en/mysqldump.html
You can use "IF EXIST" in your SQL statement.
You get every record/table/what-you-want with PHP or an other programmation language.
Then, you build some statement like this :
IF NOT EXISTS (SELECT what-you-want FROM BrokenTable WHERE some-test=some-value)
INSERT INTO ClearTable(value, that, you, need, to, insert);
Hope I helped you.