gpt4 book ai didi

php - 来自 php 脚本的 mysqldump 的内存要求

转载 作者:行者123 更新时间:2023-11-29 05:25:39 24 4
gpt4 key购买 nike

我有以下脚本,非常适合数据库备份。但是我会遇到超大型数据库的内存问题吗?

//Set large amount of memory to prevent fatal errors and don't let it timeout
ini_set('memory_limit', '1024M');
set_time_limit(0);

$database = escapeshellarg($this->db->database);
$db_hostname = escapeshellarg($this->db->hostname);
$db_username= escapeshellarg($this->db->username);
$db_password = escapeshellarg($this->db->password);

$backup_command = "/usr/local/mysql/bin/mysqldump -h $db_hostname -u $db_username -p$db_password $database";
$dump_output = shell_exec($backup_command);

force_download('database.sql', $dump_output);

最佳答案

我不会使用 shell_exec()。使用 passthru() 代替,它不需要额外的内存,因为它将 mysqldump 的输出原封不动地传递给 PHP 的输出:

// set appropriate headers for download ...  
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="download.sql"');
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');

// then directly output using passthru()
passthru('/usr/local/mysql/bin/mysqldump -h $db_hostname -u $db_username -p$db_password $database"');

但是,此解决方案无法设置 Content-Length header ,因为 PHP 的输出大小未知。在大多数情况下,它应该没问题,您可以接受。 (phpmyadmin 也是这样做的)


此处不需要使用 passthru() 甚至 set_time_limit()(除非您的服务器运行 Windows(!))。来自documenation :

Note: The set_time_limit() function and the configuration directive max_execution_time only affect the execution time of the script itself. Any time spent on activity that happens outside the execution of the script such as system calls using system(), stream operations, database queries, etc. is not included when determining the maximum time that the script has been running. This is not true on Windows where the measured time is real.

关于php - 来自 php 脚本的 mysqldump 的内存要求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20665609/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com