Jump to content

one mysql server to another


The Little Guy

Recommended Posts

Can you use the mysql command line tools?

 

($ = command prompt)

$ mysqldump your-database > your-db.sql
$ bzip2 your-db.sql
$ sftp remote.computer.net
> cd /whatever/dir
> put your-db.sql.bz2
> quit
$ ssh remote.computer.net
$ cd /whatever/dir
$ bzip2 -d your-db.sql.bz2
$ mysql > your-db.sql
$ rm your-db.sql
$ ^D

I don't want to dump it into a sql file.

 

I just want to take my dead database and send certain rows from that database, if it meets the requirements, over to a live database.

 

My dead database has thousands of URLs, and each URL has a page title. Each page also has context.

 

I currently have 3 spiders,

- one retrieves URLS

- one retrieves titles

- one retrieves body content and file size

 

my 4th spider will crawl my database looking for rows that don't have NULL values for:

- URL

- title

- body content

 

I need to find these, then send a mass amount of rows to the remote database with as few queries as possible.

If you're only doing this once or a great once in a while, dumping and uploading is likely the most efficient solution.  You can still use a WHERE clause when dumping:

 

mysqldump database --where="URL IS NOT NULL AND title IS NOT NULL AND content IS NOT NULL" > outfile.sql
...etc

 

If you have your heart set on doing this through PHP, create two database connections, write a query that select the matching rows from the source database and assemble a query with the results for inclusion into the target database.  You can use a loop as usual.  This method, however, will be more taxing on both systems; I don't know if it will be significant, though.

 

$source = mysql_connect('hostname','user','pass');
$target = mysql_connect('hostname2','user','pass');

mysql_select_db('database1',$source);
mysql_select_db('database2',$target);

$result = mysql_query('SELECT * FROM table WHERE URL IS NOT NULL AND title IS NOT NULL AND content IS NOT NULL',$source);
while ($row = mysql_fetch_assoc($result)) mysql_query(sprintf('INSERT INTO table (URL,title,content) VALUES(%s,%s,%s,%s,etc)',$row['URL'],$row['title'],$row['content']),$target);

mysql_close($source);
mysql_close($target);

 

I left out error checking for simplicity's sake.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.