[英]how to copy mysql DB from localhost to server using php?
I have a php project running in my local machine(angular,php,mysql).Same copy of project running in online. 我有一个在本地计算机上运行的php项目(angular,php,mysql)。在线运行的项目的相同副本。 My Aim is to Sync(copy local db to server db) every one hour by running any PHP Script using angular 'set Interval' function.
我的目标是每隔一小时通过使用角度“设置间隔”功能运行任何PHP脚本来同步(将本地数据库复制到服务器数据库)。 What is the IDEA behind this functionality should i use?
我应该使用此功能背后的IDEA是什么? or how i will achieve this ?
或者我将如何实现这一目标? Any suggestions will be great help for me, and Thanks in advance.
任何建议对我都会有很大的帮助,在此先谢谢您。
If your database tables not gonna change what you can do is create a function select all the data from your local database and pass that data to online function to update your online database with new or updated records. 如果您的数据库表不会改变,您可以做的是创建一个函数,从本地数据库中选择所有数据,然后将该数据传递给联机功能,以使用新记录或更新记录来更新联机数据库。
For ex: 例如:
If you have a table called users. 如果您有一个称为用户的表。 From AJAX you will select all local data and create JSON Object pass the data to script function.
从AJAX中,您将选择所有本地数据并创建JSON对象,将数据传递给脚本函数。 From that JSON Object you will pass data to online php file and update your online database from it.
从该JSON对象,您将把数据传递到在线php文件并从中更新您的在线数据库。
Note: You have to careful with giving lot of conditions to check whether data get missing or override. 注意:您必须谨慎处理大量条件,以检查数据是否丢失或覆盖。
You'll have to write a service and some code to dump your database (if you want to sync complete database every time) follow this answer 您必须编写服务和一些代码来转储数据库(如果您想每次都同步完整的数据库),请遵循以下答案
After dumping your sql next you have to upload the file to your server via the service. 在转储sql之后,您必须通过该服务将文件上传到服务器。 Upon receiving you can load the data again
mysql -u username -p database_name < file.sql
收到后,您可以再次加载数据
mysql -u username -p database_name < file.sql
However I won't recommend this, try exploring database approach of Master-Slave Database, where your local server's database will be a Master and your remote server will be slave. 但是,我不建议这样做,请尝试探索Master-Slave数据库的数据库方法,其中本地服务器的数据库将为主服务器,而远程服务器为从属服务器。 Your data will automatically be synchronized.Please see this tutorial
您的数据将自动同步。请参阅本教程
You can implement interface to select tables you want to import in live. 您可以实现接口以选择要实时导入的表。 Use below code generate CSV files of selected tables and prepare array.
使用以下代码生成所选表的CSV文件并准备数组。
<?php
$file_name_flat = 'TABLE-NAME.csv'; // Replace TABLE-NAME with your selected table name.
$fpointer = fopen(FOLDER-LOCATION.$file_name_flat, 'w+'); // Open CSV file. Replace
FOLDER-LOCATION with your local folder path where you want to save this CSV files.
//Execute query to get all columns data
$query = "select * FROM TABLE-NAME WHERE 1"; // Replace TABLE-NAME with your selected
table name. You can set other conditions based on your requirement.
//Execute query as per your CMS / Framework coding standards and write CSV file.
$result_flat = $DB_Connection->query($query)->fetchAll('assoc');
foreach ($result_flat as $fields) {
fputcsv($fpointer, $fields);
}
//Prepare Array of CSVs to create ZIP file
$files = array($file_name_flat);
fclose($fpointer); // close CSV file after successfully write.
?>
//Create ZIP
$zipname = 'tables_'.date('Y-m-d-H-i-s').'.zip';
createZipFile($files,$zipname,FOLDER_LOCATION); //Replace FOLDER-LOCATION with your
local folder path where you saved CSV files.
/* createZipFile Funcation to create zip file Both params are mandatory */
function createZipFile($files_names = array(),$zipfileName, $files_path=""){
$zip = new \ZipArchive;
$zip->open(TMP.$zipfileName, \ZipArchive::CREATE);
foreach ($files_names as $file) {
$zip->addFile($files_path.$file,$file);
}
$zip->close();
foreach ($files_names as $file) {
unlink($files_path.$file);
}
///Then download the zipped file.
header('Content-Type: application/zip');
header('Content-disposition: attachment; filename='.$zipfileName);
header('Content-Length: ' . filesize(FOLDER_LOCATION.$zipfileName));
readfile(TMP.$zipfileName);
unlink(TMP.$zipfileName);
die;
}
Now Implement a form to upload this zip file on live server. 现在,实现一个表单以将该zip文件上传到实时服务器上。 In Post action of this form add code to get zip file.
在此表单的发布操作中,添加代码以获取zip文件。
$filename = $_FILES['filename']['name'];
$source = $_FILES["filename"]["tmp_name"];
//Upload zip file to server location. Replace SERVER_FOLDER_PATH to server's location
where you want to save uploaded zip.
if(move_uploaded_file($source, SERVER_FOLDER_PATH)) {
//Extract ZIP file
$zip = new \ZipArchive();
$x = $zip->open($target_path);
if($x === true) {
$zip->extractTo(PATH_TO_SAVE_EXTRACTED_ZIP); // change this to the correct site path
$zip->close();
$cdir = scandir(PATH_TO_SAVE_EXTRACTED_ZIP); // Read DIR
$fieldSeparator = ",";
$lineSeparator = '\n';
foreach ($cdir as $key => $value)
{
if (!in_array($value,array(".","..")))
{
$fileName = PATH_TO_SAVE_EXTRACTED_ZIP.$value; // replace
PATH_TO_SAVE_EXTRACTED_ZIP with your server path
$tableName = SET_TABLE_NAME; // You have to set the logic to get the table name.
if (is_file($fileName))
{
// User MYSQL "LOAD DATA LOCAL INFILE" to IMPORT CSVs into particular tables. There are option available for this LOAD DATA process. It will import your CSV to particular table. No need to execute loop to insert data one by one.
$q = 'LOAD DATA LOCAL INFILE "'.$fileName.'" REPLACE INTO TABLE '.$tableName.' FIELDS TERMINATED BY "' .$fieldSeparator. '" Enclosed BY '.'\'"\''.' LINES TERMINATED BY "'.$lineSeparator.'"';
$DB_Connection->query($q);
}
}
}
}
You can check LOAD DATA of MySQL from - MYSQL 您可以从-MYSQL检查MySQL的LOAD DATA
您可以在本地计算机上运行cron作业,该作业使用mysqldump导出MySQL数据,然后使用rsync和sshpass将其上传到服务器。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.