AbyssalSwamp  ActivaUser
» Guest:  Register | Login | 会员列表

RSS subscription to this AbyssalSwamp  

Previous thread Next thread
     
Title: php如何备份mysql  
 
xander
超级版主
Rank: 12Rank: 12Rank: 12


UID 1
Digest 2
Points 2
Posts 169
码币MB 309 Code
黄金 0 Catty
钻石 903 Pellet
Permissions 200
Register 2022-2-7
Status offline
php如何备份mysql

// 数据库连接信息
$host = 'localhost';
$user = 'db_username';
$pass = 'db_password';
$db_name = 'db_name';

// 连接到 MySQL 数据库服务器
$mysqli = new mysqli($host, $user, $pass, $db_name);

// 检查连接是否成功
if ($mysqli->connect_errno) {
    echo "连接 MySQL 数据库失败:" . $mysqli->connect_error;
    exit();
}

// 构建备份查询
$query = "mysqldump --user=$user --password=$pass --host=$host $db_name > backup.sql";

// 发送查询到 MySQL 数据库服务器执行
$mysqli->query($query);

// 将备份数据写入文件
$file = 'backup.sql';
$fh = fopen($file, 'w');
fwrite($fh, $mysqli->query($query));
fclose($fh);

// 关闭 MySQL 数据库连接
$mysqli->close();

echo "MySQL 数据库备份已完成。";


2023-3-30 13:55#1
View profile  Blog  Send a short message  Top
     


  Printable version | Recommend to a friend | Subscribe to topic | Favorite topic  


 


All times are GMT+8, and the current time is 2026-1-14 00:06 Clear informations ->sessions/cookies - Contact Us - CAFFZ - ZAKE