MySQL数据库中备份/恢复的两方法介绍
下面介绍MySQL数据库备份/恢复的两种方法。 方法一: query($sql); } //将生成的临时备份文件合在一起 $outfile = date(Y-m-d)..sql; if(file_exists($dbdir.$outfile)) @unlink($dbdir.$outfile); $fpr = fopen($dbdir.$outfile, a); foreach($txtname as $
下面介绍MySQL数据库备份/恢复的两种方法。
方法一:
query($sql); } //将生成的临时备份文件合在一起 $outfile = date("Y-m-d").".sql"; if(file_exists($dbdir.$outfile)) @unlink($dbdir.$outfile); $fpr = fopen($dbdir.$outfile, "a"); foreach($txtname as $txt){ if(file_exists($dbdir.$txt)){ //读取临时备份文件 $tdata = readfiles($dbdir.$txt); //生成备份文件 $tbl = explode(".", $txt); $str = "`".$tbl[0]."`{{".$tdata."}}"; if(fwrite($fpr, $str)){ echo $tbl[0]."...写入 $outfile 成功!
n"; }else{ echo $tbl[0]."...写入 $outfile 失败!
n"; } @unlink($dbdir.$txt); } } fclose($fpr); }else{//恢复数据 $tdata = readfiles($dbdir.$_POST["sqlfile"]); preg_match_all("/`(.*)`{{(.*)}}/isU", $tdata, $data_ar); foreach($data_ar[1] as $k => $tt){ if(empty($data_ar[2][$k])) continue; $tfile = $dbdir.$tt.".txt"; $fp = fopen($tfile, "w"); if(fwrite($fp, $data_ar[2][$k])){ //清空表 $sql = "TRUNCATE TABLE `$tt`"; $db->query($sql); //重新装入数据 $sql = "LOAD DATA LOW_PRIORITY INFILE '".$dbdir.$tt.".txt"."' INTO TABLE `$tt`"; if($db->query($sql)){ fclose($fp); echo $tt."表数据恢复成功!
n"; unlink($dbdir.$tt.".txt"); }else{ echo $tt."表数据恢复失败!
n"; } } } //echo $tdata; //print_r($data_ar); //exit; }} /* * 读取文件内容 * 参数 $file 为文件名及完整路径 * 返回文件内容 */ function readfiles($file){ $tdata = ""; $fp = fopen($file, "r"); if(filesize($file)
方法二:
想在PHP后台管理直接能够备份数据库,于是想呀想,一直没有什么思路,一开始是考虑用php来访问服务器安装mysql的目录,比如 /usr/local/mysql/data目录,直接把下面对应的文件进行备份,但是出现了问题:
第一、运行php的是apche的用户,比如是nobody,那么它一般是没有权限访问/usr/local/mysql/data目录的。
第二、就算能够访问,那么你如何能够把/usr/local/mysql/data目录下的文件拷贝出来呢?因为mysql在运行的时候是不运行访问的,那么nobody用户有权限停止mysql的服务,不可能!
越想越不对劲,没有办法,看能不能从PHP操作数据库入手,,于是就去看了下phpMyadmin和Discuz!的代码,呵呵,于是偷抄了Discuz!的代码,形成了如下备份数据库的方法。
注:更多精彩文章请关注三联编程教程栏目。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











MySQL and phpMyAdmin are powerful database management tools. 1) MySQL is used to create databases and tables, and to execute DML and SQL queries. 2) phpMyAdmin provides an intuitive interface for database management, table structure management, data operations and user permission management.

Oracle is not only a database company, but also a leader in cloud computing and ERP systems. 1. Oracle provides comprehensive solutions from database to cloud services and ERP systems. 2. OracleCloud challenges AWS and Azure, providing IaaS, PaaS and SaaS services. 3. Oracle's ERP systems such as E-BusinessSuite and FusionApplications help enterprises optimize operations.

In MySQL, the function of foreign keys is to establish the relationship between tables and ensure the consistency and integrity of the data. Foreign keys maintain the effectiveness of data through reference integrity checks and cascading operations. Pay attention to performance optimization and avoid common errors when using them.

The main difference between MySQL and MariaDB is performance, functionality and license: 1. MySQL is developed by Oracle, and MariaDB is its fork. 2. MariaDB may perform better in high load environments. 3.MariaDB provides more storage engines and functions. 4.MySQL adopts a dual license, and MariaDB is completely open source. The existing infrastructure, performance requirements, functional requirements and license costs should be taken into account when choosing.

SQL is a standard language for managing relational databases, while MySQL is a database management system that uses SQL. SQL defines ways to interact with a database, including CRUD operations, while MySQL implements the SQL standard and provides additional features such as stored procedures and triggers.

Redis is a memory data structure storage system, mainly used as a database, cache and message broker. Its core features include single-threaded model, I/O multiplexing, persistence mechanism, replication and clustering functions. Redis is commonly used in practical applications for caching, session storage, and message queues. It can significantly improve its performance by selecting the right data structure, using pipelines and transactions, and monitoring and tuning.

MySQL and phpMyAdmin can be effectively managed through the following steps: 1. Create and delete database: Just click in phpMyAdmin to complete. 2. Manage tables: You can create tables, modify structures, and add indexes. 3. Data operation: Supports inserting, updating, deleting data and executing SQL queries. 4. Import and export data: Supports SQL, CSV, XML and other formats. 5. Optimization and monitoring: Use the OPTIMIZETABLE command to optimize tables and use query analyzers and monitoring tools to solve performance problems.

MySQL is suitable for rapid development and small and medium-sized applications, while Oracle is suitable for large enterprises and high availability needs. 1) MySQL is open source and easy to use, suitable for web applications and small and medium-sized enterprises. 2) Oracle is powerful and suitable for large enterprises and government agencies. 3) MySQL supports a variety of storage engines, and Oracle provides rich enterprise-level functions.
