从多个数据库导出表并导入到单个数据库中

时间:2016-06-18 03:23:44

标签: php mysql joomla

我想从多个数据库中导出具有相同名称的三个表中的特定数据,并将它们导入到单个数据库SUPPORT_DATABASE中,表格为XXX_usersXXX_usergroups,{{ 1}}。

用户XXX-user_usergroup_map是所有表中的主键。

以下是我到目前为止的脚本

的MySQL-Export.php

id

的MySQL-Import.php

<?php
//ENTER THE RELEVANT INFO BELOW
$mysqlDatabaseName ='db1';
$mysqlUserName ='db1owner';
$mysqlPassword ='Password';
$mysqlHostName ='db1.mywebhost.net';
$mysqlExportPath ='db1backup.sql';

//DONT EDIT BELOW THIS LINE
//Export the database and output the status to the page
$command='mysqldump --opt -h' .$mysqlHostName .' -u' .$mysqlUserName .' -p'    .$mysqlPassword .' ' .$mysqlDatabaseName .'
SELECT id, name, username, password, email FROM `xxx_users` WHERE `id` in (SELECT `user_id` FROM `xxx_user_usergroup_map` WHERE `group_id` = (SELECT `id`     FROM `xxx_usergroups` WHERE `title` = 'Administrator'));
SELECT id, name, username, password, email FROM `xxx_users` WHERE `id` in (SELECT `user_id` FROM `xxx_user_usergroup_map` WHERE `group_id` = (SELECT `id` FROM `xxx_usergroups` WHERE `title` = 'Engineer'));
> ~/' .$mysqlExportPath;
exec($command,$output=array(),$worked);
switch($worked){
case 0:
echo 'Database <b>' .$mysqlDatabaseName .'</b> successfully exported to <b>~/' .$mysqlExportPath .'</b>';
break;
case 1:
echo 'There was a warning during the export of <b>' .$mysqlDatabaseName .'</b> to <b>~/' .$mysqlExportPath .'</b>';
break;
case 2:
echo 'There was an error during export. Please check your values:<br/><br/><table><tr><td>MySQL Database Name:</td><td><b>' .$mysqlDatabaseName .'</b></td></tr><tr><td>MySQL User Name:</td><td><b>' .$mysqlUserName .'</b></td></tr><tr><td>MySQL Password:</td><td><b>NOTSHOWN </b></td></tr><tr><td>MySQL Host Name:</td><td><b>' .$mysqlHostName .'</b></td></tr></table>';
break;
}
?>

表结构

$mysqlDatabaseName ='db2';
$mysqlUserName ='db2owner';
$mysqlPassword ='Password';
$mysqlHostName ='db2.mywebhost.net';
$mysqlImportFilename ='db1backup.sql';

//DONT EDIT BELOW THIS LINE
//Export the database and output the status to the page
$command='mysql -h' .$mysqlHostName .' -u' .$mysqlUserName .' -p' .$mysqlPassword .' ' .$mysqlDatabaseName .' < ' .$mysqlImportFilename;
exec($command,$output=array(),$worked);
switch($worked){
case 0:
echo 'Import file <b>' .$mysqlImportFilename .'</b> successfully imported to    database <b>' .$mysqlDatabaseName .'</b>';
break;
case 1:
echo 'There was an error during import. Please make sure the import file is saved in the same folder as this script and check your values:<br/><br/><table><tr><td>MySQL Database Name:</td><td><b>' .$mysqlDatabaseName .'</b></td></tr><tr><td>MySQL User Name:</td><td><b>' .$mysqlUserName .'</b></td></tr><tr><td>MySQL Password:</td><td><b>NOTSHOWN</b></td></tr><tr><td>MySQL Host Name:</td><td><b>' .$mysqlHostName .'</b></td></tr><tr><td>MySQL Import Filename:</td><td><b>' .$mysqlImportFilename .'</b></td></tr></table>';
break;
}
?> 

我认为我可以做的是使用XXX_user user_id, name, username, email, password 300, john smtih, jsmith, jsmith@test.com, password123@ XXX_usergroups group_id, type 8, administrator XXX_user_group_map user_id, group_id 300, 8 从三个数据库导出数据并将数据库存储在服务器上,然后使用MySQL-Export.php将数据库从所有三个数据库导入辅助数据库,一旦数据MySQL-import.php MODIFY group_id中的用户XXX-user_usergroup_mapSUPPORT_DATABASE

中的用户组相匹配

此时我有点卡住,因为从多个数据库导入用户,用户可能共享相同的user_id,所以我不确定该怎么做,我在哪里以及如何更改{{1} }。

user_id

之后user_id是什么并不重要

任何机构都可以提出一些建议。

1 个答案:

答案 0 :(得分:0)

这里有一种方法,一步一步

  1. 使用此查询转储每个数据库
  2. SELECT u.name, u.username, u.email, u.password, g2.type FROM "XXX_user" u, "XXX_user_group_map" g, "XXX_usergroups" g2 WHERE g.user_id = u.user_id AND g.group_id = g2.group_id

    在此查询中,您可以将用户组类型作为字符串获取,以便与SUPPORT_DATABASE组ID匹配

    1. 现在编写一个脚本将sql导入SUPPORT_DATABASE,设置SUPPORT_DATABASE以使user_id上的AUTO INCREMENT和电子邮件中的唯一。