gpt4 book ai didi

php - 如何用php中的备份文件解决这个问题

转载 作者:行者123 更新时间:2023-12-04 06:16:57 32 4
gpt4 key购买 nike

我经历过大约两次 mysql 数据库崩溃并且长时间无法连接,我总是不得不联系托管服务来重新激活它......出于某种原因。所以我想创建一种机制来解决备份文件的这个问题。继承人的代码:

<?php
$query = //some source
$db_user = "data_search";
$db_password = "password";
$database = "data_search";
$backupfile = '/data_search.sql';
$db_table = "words";
$db = mysql_connect($hostname, $db_user, $db_password);
mysql_select_db($database,$db);
mysql_query("set names 'utf8'");

if ($db) {
$result = mysql_query("
SELECT
*
FROM
$db_table
WHERE
word = '$query'
LIMIT
0 , 1
");
} else {
$result = mysql_query("
LOAD DATA INFILE
'$backupfile'
FROM
'$db_table'
WHERE
word = '$query'
LIMIT
0 , 1
");
}

$num = mysql_numrows($result);
$i = 0;
while ($i < $num) {
$word = mysql_result($result,$i,"word");
echo $word;
$i++;
}

现在这只是一个示例,我 100% 准确地表明错误在备份 query($result) 中.基本上我想要的是:如果它无法连接使用备份文件。这将返回 $num = 0;有人可以帮我执行这段代码吗?什么 mysql_query()我在找吗?

最佳答案

使用 mysqldump反而。您可以从 PHP 启动它:

/* get mysql database as gzipped backup file;
* be aware, that the DB-password is stored in the ~/.bash_history,
* if you include it in the program-call;
*/
shell_exec('mysqldump --host=DB_HOST --user=DB_USER --password=DB_PASSWORD --databases DB_NAME --skip-triggers --compact --no-create-info | gzip -9 > BACKUP_FILENAME-`date +%Y-%m-%d_%Hh%Mm%Ss`.sql.gz')

或者只是通过shell脚本来做到这一点:
# !/bin/bash

REMOTE_DB_USER="my-database-user"
REMOTE_DB_PASSWORD="my-database-password"
REMOTE_DATABASE="my-database-name"
LOCAL_BACKUP_DIRECTORY="~/backup/dumps"
CURRENT_TIMESTAMP=`date +%Y-%m-%d_%Hh%Mm%Ss`

# switch to local backup directory
echo "change dir";
cd $LOCAL_BACKUP_DIRECTORY

# dump whole remote database to gzipped file
echo "dump db to local"
/usr/bin/mysqldump --user=$REMOTE_DB_USER --password=$REMOTE_DB_PASSWORD --host=127.0.0.1 --port=3306 $REMOTE_DATABASE | gzip -9 > dump-$CURRENT_TIMESTAMP.sql.gz

echo "finished"

关于php - 如何用php中的备份文件解决这个问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7111130/

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