Merging Two databases with the same exact structure [MYSQL] - mysql

I just had my site hacked and they were able to do some damage to my current database.
Anyway, I have a backup from few days ago. The problem is that the current database had a few thousand more posts and threads / users.
I am simply wondering how I could possibly go about merging the two databases?
The two databases have the exact structure, and I want the backup to overwrite any record from the current database, just that I want the current database to populate any new records like posts, threads, users and such.
I know you can merge two tables, if they have the exact structure, but what about two databases that have the same structure?

I assume you have a schema s1 and a schema s2.
To insert all rows of a table in s1 into a table in s2, while overwriting existing lines, you can use:
REPLACE INTO s2.table_name
SELECT * FROM s1.table_name;
If you do not want to touch existing lines:
INSERT INTO s2.table_name
SELECT * FROM s1.table_name
ON DUPLICATE KEY IGNORE;

there was some ways to do it:
1.) use Command line tools like schema Sync and mysql diff
2.) or Using SQLyog
find out more here
http://blog.webyog.com/2012/10/16/so-how-do-you-sync-your-database-schema/

In my experience ON DUPLICATE KEY IGNORE did not work. Instead I found
INSERT IGNORE ta2_table
SELECT * FROM ta1_table;
worked like a charm

Related

How to copy data from one table to another same table?

I have a master database and several child databases on the same server and all the databases have identical tables. I have to copy data from master to child databases but on each child database there's going to be different data from the tables.
Right now, I'm selecting data, comparing it and inserting / deleting it using PHP, which was working fine when there were only 2-3 child databases, but now as the child databases are growing the copying is getting slower.
I even tried to replicate the database tables using the following queries though it worked but later I realized that child dB's don't need all master data, rather they require some specific data only.
TRUNCATE master_db.papers;
INSERT INTO child_1.papers SELECT * FROM master_db.papers;
The above copies all the database based on WHERE condition. but after understanding all the requirements, I have to do the following:
I also tried replacing INSERT with UPDATE but that is causing mysql error.
Copy anything that may have updated in the master to child (UDPATE ONLY)
Copy any new data that needs to go into child.
How can I achieve that?
Thanks in advance.
I worked it out using the following:
Incase you want to copy all data from source to destination:
INSERT INTO source_db.source_table SELECT * FROM target_db.target_table WHERE some_condition = '0';
And if you want to update according to the target table:
UPDATE target_db.target_table
INNER JOIN source_db.source_table USING (some_field) SET
target_db.target_table.id = source_db.source_table.id,
target_db.target_table.name = source_db.source_table.name,
target_db.target_table.phone = source_db.source_table.phone;
Hope this helps anyone is looking to do similar task.

How to query against multiple databases on the same server

I am not sure if this has been answered before but what I found i am not sure how to make work for me but here is my problem.
I have a database used to keep track of phones for multiple clients. What needs to be done is have a query that can be ran that will run against multiple databases on the same server. each database uses the same table name that I am looking at but the names are slightly different. I came up with this..
INSERT INTO `export db`.exportinfo2
SELECT *
FROM (SELECT * FROM `export db'.tentantnames).users
WHERE name = 'Caller ID:emergency' AND value > 0
What suppose to happen is from a table that has all the database names is is to got to each database and go into the table labeled users and run a where clause on the data then export results to a different database table
I know the code needs to be dynamic but I am not sure how to make it dynamic and function. The table that has all the names for the databases is automatically created every few days.. I am not sure what else needs to be said without sounding like i repeat myself but i just need help making a dynamic query that uses a table premade as database names and run a where statement on the same named table in each database which have their name stored in a different table.
You should look into Synonyms. It can be used to fulfill your purpose

How to achieve redundancy between 2 tables of different databases?

First of all I'd like to start saying that I've checked these two questions:
Sync 2 tables of different databases - MySQL
How to synchronize two tables of different databases on the same machine (MySql)
But while similar they are not what i need.
I have 2 databases in the same server.
Db1 and Db2
Both databases have the exact copy of a single table called "user":
userid
login
name
lastname
password
level
How can I achieve some sort of redundancy between these two tables in different databases?
If db1.user gets a new record then db2.user has to have that record, if a record is modified then the other one is modified and if deleted the then the other one gets deleted too.
To be more specific, db2.user needs to be a reflection of db1.user using triggers.
EDIT: there is this question: Mysql replication on single server and that is not even remotely close to what i want to do. I updated a little bit at the very end of what i previously posted with how I'd like to achieve this thanks to a suggestion.
As proposed you can use triggers as stated in this standard documentation.
You define AFTER INSERT,AFTER UPDATE and AFTER DELETE triggers on db1.user and within this trigger you have the NEW.object to pass the information into the db2.user table.

Most efficient way to periodically delete all entries in database older than a month

I'm trying to most efficiently manage a database table and get rid of old entries that will never be accessed. Yes they could probably easily be persisted for many years but I'd just like to get rid of them. I could do this maybe once every month. Would it be more efficient to copy the entries I want to keep into a new table then simply delete the old table. Or should a query manually delete each entry after that threshhold that I set.
I'm using MySQL with JPA/JPQL JEE6 with entity annotations and Java persistence manager.
Thanks
Another solution is to design the table with range or list PARTITIONING, and then you can use ALTER TABLE to drop or truncate old partitions.
This is much quicker than using DELETE, but it may complicate other uses of the table.
A single delete query will be the most efficient solution.
Copying data from one database to another can be lengthy if you have a lot of data to keep. It means you have to retrieve all the data with a single query (or multiple, if you want to batch), and issue a lot of insert statements in the other database.
Using JPQL, you can issue a single query to delete all old statements, something like
DELETE FROM Entity e WHERE e.date = ?
This will translated to a single SQL query, and the database will take care of deleting all the unwanted records.

Setting up a master database to control the structure of other databases

I got a case where I have several databases running on the same server. There's one database for each client (company1, company2 etc). The structure of each of these databases should be identical with the same tables etc, but the data contained in each db will be different.
What I want to do is keep a master db that will contain no data, but manage the structure of all the other databases, meaning if I add, remove or alter any tables in the master db the changes will also be mirrored out to the other databases.
Example: If a table named Table1 is created in the master DB, the other databases (company1, company2 etc) will also get a table1.
Currently it is done by a script that monitors the database logs for changes made to the master database and running the same queries on each of the other databases. Looked into database replication, but from what I understand this will also bring along the data from the master database, which is not an option in this case.
Can I use some kind of logic against database schemas to do it?
So basicly what I'm asking here is:
How do I make this sync happen in the best possible way? Should I use a script monitoring the logs for changes or some other method?
How do I avoid existing data getting corrupted if a table is altered? (data getting removed if a table is dropped is okay)
Is syncing from a master database considered a good way to do what I wish (having an easy maintainable structure across several datbases)?
How will making updates like this affect the performance of the databases?
Hope my question was clear and that this is not a duplicate of some other thread. If more information and/or a better explantion of my problem is needed, let me know:)
You can get the list of tables for a given schema using:
select TABLE_NAME from information_schema.tables where TABLE_SCHEMA='<master table name>';
Use this list for a script or stored procedure ala:
create database if not exists <name>;
use <name>;
for each ( table_name in list )
create table if not exists <name>.table_name like <master_table>.table_name;
Now that Im thinking about it you might be able to put a trigger on the 'information_schema.tables' db that would call the 'create/maintain' script. Look for inserts and react accordingly.