简体   繁体   English

多数据库架构迁移(php/mysql)

[英]Multi database schema migration (php/mysql)

I have an application where each user has their own database and share the same schema.我有一个应用程序,每个用户都有自己的数据库并共享相同的模式。 (I have another thread discussing this, so I don't need any information on this) (我有另一个线程讨论这个,所以我不需要任何信息)

When migrating the databases, I wrote a script shown below:迁移数据库时,我编写了如下所示的脚本:

<?php
$sql = <<<SQL
ALTER TABLE xyz....;
ALTER TABLE abc.....;
SQL;
$sql_queries = explode(";", $sql);
$exclude_dbs = array();

$conn = mysql_connect("localhost", "USER", "PASSWORD");
$show_db_query = mysql_query('SHOW databases');
$databases = array();
while ($row = mysql_fetch_assoc($show_db_query))
{
        if (!in_array($row['Database'], $exclude_dbs))
        {
                $databases[] = $row['Database'];
        }
}

foreach($databases as $database)
{
        mysql_select_db($database, $conn);
        echo "Running queries on $database\n***********************************\n";
        foreach($sql_queries as $query)
        {
                if (!empty($query))
                {
                        echo "$query;";
                        if (!mysql_query($query))
                        {
                                echo "\n\nERROR: ".mysql_error()."\n\n";
                        }
                }
        }
        echo "\n\n";
}
?>

It has been working fine without any problems.它一直运行良好,没有任何问题。 I then use stepancheg / mysql-diff to make sure the database schemas are good.然后我使用stepancheg / mysql-diff来确保数据库模式是好的。

Are there any things I should be doing when migrating databases?迁移数据库时我应该做些什么? (I test on a staging server before hand also) (我也事先在登台服务器上进行了测试)

Your approach seems to be working, and the use of mysql-diff is good.您的方法似乎有效,并且 mysql-diff 的使用很好。 Have you considered using mysqldump to back up actual data?您是否考虑过使用mysqldump备份实际数据? It's a more conventional approach than a custom script.这是一种比自定义脚本更传统的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM