Copy MySql DB between servers

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • code green
    Recognized Expert Top Contributor
    • Mar 2007
    • 1726

    Copy MySql DB between servers

    How do I update the same MySql database from server 1 to server 2 using a php script.
    I can make the connection between the two and 'see' the tables using SHOW TABLES.
    Now I want to replace the data of certain tables in server 2 with server 1.
    I've looked at mysqldump and mysqlimport but can't find any PHP examples between two servers.
  • code green
    Recognized Expert Top Contributor
    • Mar 2007
    • 1726

    #2
    Hello. Anybody there?.OK. I'll go some way to solving my own problem.
    Found this chunk of code
    [PHP] // Source server settings
    $from_user = "Put User Here";
    $from_password = "Put password here";
    $from_database = "Put Database name here";
    $from_server = "Put Source Server here";
    $from_41 = False;

    // Destination Server Settings
    $to_user = "Put User here";
    $to_password = "Put Password here";
    $to_database = "Put Database name Here";
    $to_server = "Put Source Server here";
    // $to_server = "localhost" ;

    // Don't Edit below here!!!! (Unless you know what your doing).
    if ($from_41) {
    $theOpt = "--skip-opt ";
    } else {
    $theOpt = "";
    }

    $Results = system( "mysqldump $theOpt -h$from_server -u$from_user -p$from_password $from_database | mysql -u$to_user -p$to_password -h$to_server $to_database", $ResultErr );

    echo "Results from MySQL import of $DatabaseName with User $User with file $TheFile:\r\n $Results\r\n $ResultErr";[/PHP]At the following address. Will try to make it work then report back!
    Code:
    http://wiki.i40.com/MigratingmySQL

    Comment

    • code green
      Recognized Expert Top Contributor
      • Mar 2007
      • 1726

      #3
      Struggled with mysqldump all day.
      Tried implementing with system() and exec() by creating a backup file (mysqldumper.sq l).
      The code runs, creates the file but it is empty.
      [PHP]$res = exec($dump,$res ult,$err)[/PHP];exec() returns an empty value, the array $result is empty and $err returns 1;
      system() produces similar results.
      Have used the full path to mysqldump.exe and tested it is there.
      [PHP]$dumpfile = '../../../../mysql/bin/';
      if(file_exists( $dumpfile))
      {
      $dumpfile .= 'mysqldump';
      $dump = "$dumpfile --opt $this->name -h $this->host -u $this->user > $to_name.sql"; [/PHP]
      echoing out $dump gives
      Code:
      ../../../../mysql/bin/mysqldump --opt db198743945 -h localhost -u root > mysqldumper.sql
      Can anybody help?
      Last edited by code green; Jul 12 '07, 04:32 PM. Reason: mistake

      Comment

      Working...