最近几天,公司的技术维护人员频繁让我恢复数据库,因为他们总是少了where条件,导致update、delete出现了无法恢复的后果,加上那些库都是几十G。恢复起来少说也要十几分钟。为此,找了一些资料和工作总结,给出一下几个方法,用于快速恢复表,而不是库, 但
最近几天,公司的技术维护人员频繁让我恢复数据库,因为他们总是少了where条件,导致update、delete出现了无法恢复的后果,加上那些库都是几十G。恢复起来少说也要十几分钟。为此,找了一些资料和工作总结,给出一下几个方法,用于快速恢复表,而不是库,但是切记,防范总比亡羊补牢好。
在生产环境或者开发环境,往往都有某些非常重要的表。这些表存放了核心数据。当这些表出现数据损坏时,需要尽快还原。但是,正式环境的数据库往往都是非常大的,统计数据表明,1T的数据库还原时间接近24小时,所以因为一个表而还原一个库,不单空间,甚至时间上都是一个很大的挑战。本文介绍如何恢复单表,而不需要恢复整个库。
现在假设一个表:TEST_TABLE。我们需要尽快恢复这个表,并且把恢复过程中对其他表和用户的影响降到最低。
SQLServer(特别是2008以后),具有很多备份及恢复功能:完整、部分、文件、差异和事务备份。而恢复模式的选择严重影响备份策略和备份类型。
下面是几个可供参考的方案,但是记住,各有好坏,应该按照实际需要选择:
方案1:恢复到一个不同的数据库:
这对于小数据库来说不失为一种好的办法,用备份还原一个新的库,并把新库中的表数据同步回去。你可以做完整恢复,或者时间点恢复。但是对于大数据库,是非常耗时和耗费磁盘空间的。这个方法仅仅用于还原数据,在还原数据(就是同步数据)的时候,你要考虑触发器、外键等因素。
方案2:使用STOPAT来还原日志:
你可能想恢复最近的数据库备份,并回滚到某个时间点,即发生意外前的某个时刻。此时可以使用STOPAT子句,但是前提是必须为完整或大容量日志恢复模式。下面是例子:
RESTORE DATABASE 需要恢复的数据库 FROM 数据库备份 WITH FILE=3, NORECOVERY ; RESTORE LOG需要恢复的数据库 FROM数据库备份 WITH FILE=4, NORECOVERY, STOPAT = 'Oct 22, 2012 02:00 AM' ; RESTORE DATABASE 需要恢复的数据库 WITH RECOVERY ;
注意:这种方法的主要缺点是会覆盖掉从stopat指定时间点之后所修改的所有数据。所以要衡量好得失。
方案3:数据库快照:
创建数据库快照。当发生意外时,可以从快照中直接获取原来的数据。但是必须是在发生意外之前创建的快照。这在核心表不经常更新,特别是有规律更新时很有用。但是当表经常、不定期被更新,或者很多用户在访问时,这种方法就不可取了。当需要使用这种方法时,记得在每次更新前先创建快照。
方案4:使用视图:
你可以创建一个新的数据库,并把TEST_TABLE移动到这个库里面。当你需要恢复的时候,你只需要恢复这个非常小的数据库即可。访问源数据库的数据时,最简单的方法就是创建一个视图,选择TEST_TABLE表中所有列的所有数据。但是注意这个方法需要在创建视图前,重命名或者删除源数据库的表:
USE 需要恢复的数据库 ; GO CREATE VIEW TEST_TABLE AS SELECT * FROM 备份数据库.架构名.TEST_TABLE ; GO
使用这种方法,可以对视图使用SELECT /INSERT/UPDATE/DELETE语句,就像直接操作实体表似得。当TEST_TABLE更改时,要使用SP_REFRESHVIEW存储过程来更新元数据。
方案5:创建同义词(Synonym):
和方案4类似,把表移到另外一个数据库,然后对源数据库的这个表创建一个同义词:
USE 需要恢复的数据库 ; GO CREATE SYNONYM TEST_TABLE FOR 新数据库.架构名.TEST_TABLE ; GO
这个方法的有点就是你不需要担心元数据更新所带来的结构变更不及时。但是这个方法的问题就是不能在DDL语句中引用同义词,或者不能在链接服务器中找到。
方案6:使用BCP保存数据:
你可以创建一个作业,使用BCP定期导出数据。但是这种方法的缺点和方案1类似,需要找到哪天的文件并导进去,同时要考虑触发器和外键问题。
各种方法的对比:
方法 | 优点 | 缺点 |
还原数据库 | 快且容易 | 适用于小库,且要注意触发器和外键等 |
还原日志 | 能指定时间点 | 所有时间点后的新数据会被覆盖 |
数据库快照 | 当表不是经常更新时很有用 | 当表并行更新时,快照容易出现问题 |
视图 | 把表的数据于库分开,没有数据丢失 | 元数据需要周期性更新,并要定期维护新数据库 |
同义词 | 把表的数据于库分开,没有数据丢失 | 在链接服务器上不能用,并要定期维护新数据库 |
BCP | 拥有表的专用备份 | 需要额外的空间、还会出现触发器、外键等问题 |
总结:
良好的编程习惯和良好的备份机制才是解决问题的根本,以上的措施都仅仅是一个亡羊补牢的办法。可能有人说SQLServer 新版本不是有部分还原吗?我们来看看联机丛书的说明:

TograntpermissionstonewMySQLusers,followthesesteps:1)AccessMySQLasauserwithsufficientprivileges,2)CreateanewuserwiththeCREATEUSERcommand,3)UsetheGRANTcommandtospecifypermissionslikeSELECT,INSERT,UPDATE,orALLPRIVILEGESonspecificdatabasesortables,and4)

ToaddusersinMySQLeffectivelyandsecurely,followthesesteps:1)UsetheCREATEUSERstatementtoaddanewuser,specifyingthehostandastrongpassword.2)GrantnecessaryprivilegesusingtheGRANTstatement,adheringtotheprincipleofleastprivilege.3)Implementsecuritymeasuresl

ToaddanewuserwithcomplexpermissionsinMySQL,followthesesteps:1)CreatetheuserwithCREATEUSER'newuser'@'localhost'IDENTIFIEDBY'password';.2)Grantreadaccesstoalltablesin'mydatabase'withGRANTSELECTONmydatabase.TO'newuser'@'localhost';.3)Grantwriteaccessto'

The string data types in MySQL include CHAR, VARCHAR, BINARY, VARBINARY, BLOB, and TEXT. The collations determine the comparison and sorting of strings. 1.CHAR is suitable for fixed-length strings, VARCHAR is suitable for variable-length strings. 2.BINARY and VARBINARY are used for binary data, and BLOB and TEXT are used for large object data. 3. Sorting rules such as utf8mb4_unicode_ci ignores upper and lower case and is suitable for user names; utf8mb4_bin is case sensitive and is suitable for fields that require precise comparison.

The best MySQLVARCHAR column length selection should be based on data analysis, consider future growth, evaluate performance impacts, and character set requirements. 1) Analyze the data to determine typical lengths; 2) Reserve future expansion space; 3) Pay attention to the impact of large lengths on performance; 4) Consider the impact of character sets on storage. Through these steps, the efficiency and scalability of the database can be optimized.

MySQLBLOBshavelimits:TINYBLOB(255bytes),BLOB(65,535bytes),MEDIUMBLOB(16,777,215bytes),andLONGBLOB(4,294,967,295bytes).TouseBLOBseffectively:1)ConsiderperformanceimpactsandstorelargeBLOBsexternally;2)Managebackupsandreplicationcarefully;3)Usepathsinst

The best tools and technologies for automating the creation of users in MySQL include: 1. MySQLWorkbench, suitable for small to medium-sized environments, easy to use but high resource consumption; 2. Ansible, suitable for multi-server environments, simple but steep learning curve; 3. Custom Python scripts, flexible but need to ensure script security; 4. Puppet and Chef, suitable for large-scale environments, complex but scalable. Scale, learning curve and integration needs should be considered when choosing.

Yes,youcansearchinsideaBLOBinMySQLusingspecifictechniques.1)ConverttheBLOBtoaUTF-8stringwithCONVERTfunctionandsearchusingLIKE.2)ForcompressedBLOBs,useUNCOMPRESSbeforeconversion.3)Considerperformanceimpactsanddataencoding.4)Forcomplexdata,externalproc


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

Dreamweaver Mac version
Visual web development tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Chinese version
Chinese version, very easy to use

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software
