【问题标题】:How clone all data from one database to another one in Codeigniter如何在 Codeigniter 中将所有数据从一个数据库克隆到另一个数据库
【发布时间】:2020-09-15 08:20:37
【问题描述】:

我将把所有数据从实时舞台数据库移到另一个新数据库中。 该项目基于 Codeignitor。 目前我正在使用转储文件,但处理它们真的很慢。

$temp_path = './db/dump/old_backup.sql';

// Create temporary table
echo "Preparing temporary database ..... \n";
$this->createTemporarDB();

// Make Dump from live db
echo "Preparing backup from live database ..... \n";
$backup = $this->exportDumpFromDB('default', 'old_backup.sql');
write_file($temp_path, $backup, 'w+');

$this->new_db = $this->load->database('new_db', true);
$temp_line = '';
$lines = file('./db/dump/old_backup.sql');

foreach ($lines as $line) {
    if (substr($line, 0, 2) == '--' || $line == '' || substr($line, 0, 1) == '#') {
        continue;
    }
    $temp_line .= $line;
    // Display percentage of temp data
    if (substr(trim($line), -1, 1) == ';') {
        $this->temp_db->query($temp_line);
        $temp_line = '';
    }
    $index++;
}

有没有最好的方法来解决我的问题(延迟问题,我需要加快速度)。 谢谢。

【问题讨论】:

  • 您是否尝试过直接在数据库服务器上创建带有mysqldump 的转储文件,或者使用您的数据库管理软件从那里创建转储?
  • 我正在构建一个cli来转储文件,因此需要使用php通过代码实现它。有可能吗?
  • 是的,很有可能。您可以使用 php 调用 mysqldump 而不是自己构建转储(这需要更长的时间)。检查this answer了解详细信息(这是一个旧答案,从旧的 PHP5 天开始,但基础仍然相同)
  • 这能回答你的问题吗? Using a .php file to generate a MySQL dump
  • 感谢您的帮助,我可以解决此问题,但您的方法对我来说很好,可以帮助我更进一步。

标签: php mysql codeigniter


【解决方案1】:

我解决了这个问题。 在这里附上我的方法。

$tables = $this->db->query("SHOW TABLES FROM [old db name]")->result_array();
$this->temp_db = $this->load->database[new db name]', true);
foreach ($tables as $key => $val) {
   $this->temp_db->query('CREATE TABLE ' . $val['Tables_in_squirrel'] . ' LIKE [old db name].' . $val['Tables_in_squirrel']);
   $this->temp_db->query('INSERT ' . $val['Tables_in_[old db name]'] . ' SELECT * FROM squirrel.' . $val['Tables_in_squirrel']);
  }
}

这是基于 Codeigniter 的代码。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多