LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices


Reply
  Search this Thread
Old 10-25-2017, 07:18 AM   #1
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,671
Blog Entries: 4

Rep: Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945
MySQL database copy/sync in less than 6 hours per table?


I've been trying to use Percona Toolkit's pt-table-sync tool to synchronize a test database against a production one, but it's just too damned slow.

A single, albeit-large table (880,000 rows) took over six hours to sync. I literally don't have the days that it would take to use this approach.

(It also routinely causes a slave to stop syncing with its master, when I use it as a source. But I have to do that, because it brings the production database to its knees.)

My original process consisted of doing a mysqldump on the host, splitting the file into per-table files (for restartability), then reloading the tables one at a time using a script, but (for various purely political reasons that need not be discussed here), the remote server no longer has sufficient disk-space to hold these dumps. However, when it worked, it did the complete job in a few hours.

So, I basically need a script that will do table-copies between databases. (Dropping and rebuilding the tables in the remote, please). And, let it be one that I do not have to write!

I'd like for it to be able to know that if the number-of-rows are the same already, the table should not be automatically copied.

What might you suggest?

Last edited by sundialsvcs; 10-25-2017 at 07:23 AM.
 
Old 10-25-2017, 09:29 AM   #2
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Rep: Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043
I don't have script for you but it seems very simple.
Assuming:
The "remote" server is your target
The local server is your source
You don't want to store the dumps on the remote

Then a few commands like this would produce your script:
Code:
tables="footable bartable baztable"
for table in $tables 
do 
   mysqldump localdatabase $table > /tmp/${table}.sql
   mysql -h remotehost remotedatabase < /tmp/${table}.sql
done
Untested code, consider it pseudo code.

900,000 records is nothing for MySQL, provided you don't have multi-megabytes of blobs. Imports of several GB and 9,000,000 record take a few minutes on a 1 CPU VPS for me.

If your tables contain relations to other tables be careful with the per-table copying.

jlinkels
 
Old 10-25-2017, 10:23 AM   #3
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,681

Rep: Reputation: 7971Reputation: 7971Reputation: 7971Reputation: 7971Reputation: 7971Reputation: 7971Reputation: 7971Reputation: 7971Reputation: 7971Reputation: 7971Reputation: 7971
Quote:
Originally Posted by sundialsvcs View Post
I've been trying to use Percona Toolkit's pt-table-sync tool to synchronize a test database against a production one, but it's just too damned slow.

A single, albeit-large table (880,000 rows) took over six hours to sync. I literally don't have the days that it would take to use this approach. (It also routinely causes a slave to stop syncing with its master, when I use it as a source. But I have to do that, because it brings the production database to its knees.)

My original process consisted of doing a mysqldump on the host, splitting the file into per-table files (for restartability), then reloading the tables one at a time using a script, but (for various purely political reasons that need not be discussed here), the remote server no longer has sufficient disk-space to hold these dumps. However, when it worked, it did the complete job in a few hours.

So, I basically need a script that will do table-copies between databases. (Dropping and rebuilding the tables in the remote, please). And, let it be one that I do not have to write!

I'd like for it to be able to know that if the number-of-rows are the same already, the table should not be automatically copied. What might you suggest?
First, I'd go with what jlinkels suggested; I'm doing something similar with good results. I'm doing it to/from a hosted site, so the link isn't the greatest in the world, but I can dump a pretty good sized DB in a few minutes, and load speed is similarly good. Political issues are another story.

That said, if you want to know if the number of rows is the same, you could just do a fairly simple script, and do a "select max(RecordID)" (or whatever you have as your primary key/record ID/SOMETHING that increments), on both tables and compare them. If less, dump..if not, carry on.

Last edited by TB0ne; 10-25-2017 at 10:24 AM.
 
Old 10-26-2017, 08:39 AM   #4
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,671

Original Poster
Blog Entries: 4

Rep: Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945
I'm basically looking for a simple script – e.g. "if the number of records is the same, skip this table, and if the number of records has simply increased, copy the new ones."

I could write the damn thing, of course, but I'm a lazy sot.
 
Old 10-26-2017, 05:47 PM   #5
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Rep: Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043
Even a sample script requires testing and debugging so I am giving you the idea:
Code:
$remoterows=$(echo "SELECT COUNT(*) FROM $remotetable" | mysql -h remotehost -s -q remotedb)
$localrows=$(echo "SELECT COUNT(*) FROM $localtable" | mysql -h localhost -s -q localdb)
if [ $localrows -gt $remoterows ]
then
  #do the tabledump & copy
fi
jlinkels
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem to copy values from one table to another in Mysql abhishekgit Linux - General 2 03-28-2014 09:20 AM
[SOLVED] MySQL Database Triggers: Inserting or updating the same table rm_-rf_windows Linux - General 4 03-18-2012 06:54 PM
fixing mysql database table without phpmyadmin c0c0deuz Linux - Server 3 12-01-2007 08:17 PM
interesting question about mysql database sync shadow.blue Linux - Software 0 11-15-2003 08:24 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Server

All times are GMT -5. The time now is 02:14 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration