简体   繁体   English

如何在启用镜像的情况下缩小 SQL Server 日志文件?

[英]How to shrink a SQL Server Log file with Mirroring enabled?

I have several databases for my applications that use SQL Server 2005 mirroring to keep a nice copy of the data somewhere else.我有几个数据库用于我的应用程序,它们使用 SQL Server 2005 镜像来在其他地方保留一份不错的数据副本。 Works like a charm, however, the log file just seems to be growing and growing, one is at 15GB for a 3GB database.工作起来很迷人,然而,日志文件似乎一直在增长,一个 3GB 的数据库是 15GB。

Normally, I can just shrink it - however an error pops up that this specifically cannot be done.通常,我可以缩小它 - 但是会弹出一个错误,表明这无法完成。 But, it seems eventually if unchecked would just expand to use all the space on the drive.但是,似乎最终如果取消选中只会扩展以使用驱动器上的所有空间。

I see that I can set a maximum file size for the log file, is that the answer here?我看到我可以为日志文件设置最大文件大小,这就是答案吗? Will the log just roll when it hits the max, or will the DB just stop functioning?日志会在达到最大值时滚动,还是数据库会停止运行?

Thanks谢谢

We ran into the same issue after switching from log shipping to mirroring. 从日志传送切换到镜像后,我们遇到了同样的问题。 You have to create a job that regularly backs up the transaction log (every 15 or 30 minutes or so) to keep the log size from getting out of hand. 您必须创建一个定期备份事务日志的作业(每隔15或30分钟左右),以防止日志大小失控。

If it is already out of hand, run BACKUP LOG TO DISK = 'Nul', then run a DBCC SHRINKFILE command. 如果已经失控,请运行BACKUP LOG TO DISK ='Nul',然后运行DBCC SHRINKFILE命令。 Then you can get your job set up. 然后你可以设置你的工作。

Note that The 'Nul' is not a misspelling, it is an old DOS trick that behaves as if you are writing a file, but really just dumps the information off into the ether so it doesn't take up space on the machine. 请注意,'Nul'不是拼写错误,它是一个旧的DOS技巧,其行为就像您正在编写文件一样,但实际上只是将信息转储到以太网中,因此它不会占用机器上的空间。

Also, your log will grow until you run out of space, then the whole thing stops working. 此外,您的日志将增长,直到您的空间不足,然后整个过程停止工作。 Your app will receive an error that the transaction log is full. 您的应用将收到事务日志已满的错误。

EDIT: David correctly pointed out that this action will break the log chain and reduce the ability to recover from failure. 编辑:David正确地指出,此操作将破坏日志链并降低从故障中恢复的能力。 Be sure to use the backup log to 'nul' command as a last resort. 请务必将备份日志用作'nul'命令作为最后的手段。 If you have space on the drive, you should take a proper log backup and setup your log backup plan. 如果驱动器上有空间,则应进行适当的日志备份并设置日志备份计划。 Make sure you also include regular full backups and a cleanup task to remove old files. 确保还包括常规完整备份和清除任务以删除旧文件。

I would look at why your logfile is growing, then you can define a strategy for solving your issue. 我会看看你的日志文件增长的原因,然后你可以定义一个解决问题的策略。 It might be worth checking your mirroring state 可能值得检查您的镜像状态

When a principal database is exposed, it is active with user connections and processing transactions. 公开主体数据库时,它与用户连接和处理事务一起处于活动状态。 However, no log records are being sent to the mirror database, and if the principal should fail, the mirror will not have any of the transactions from the principal from the point the principal entered the exposed state. 但是,没有日志记录被发送到镜像数据库,并且如果主体应该失败,则镜像将不会从主体进入暴露状态的点开始具有任何来自主体的事务。 Also, the principal's transaction log cannot be truncated, so the log file will be growing indefinitely. 此外,主体的事务日志不能被截断,因此日志文件将无限增长。 ref http://www.microsoft.com/technet/prodtechnol/sql/2005/dbmirror.mspx 参考http://www.microsoft.com/technet/prodtechnol/sql/2005/dbmirror.mspx

I had the same problem on mirrored databases the log file grew 40GB where main Db file was around 700 Mb I found this solution In microsoft it works but only shrinked my Database about 4 percent 我在镜像数据库上遇到了同样的问题,日志文件增长了40GB,其中主Db文件大约700 Mb我发现这个解决方案在微软它可以工作,但只收缩了我的数据库大约4%

1- just execute this to create a stored procedure 1-只需执行此操作即可创建存储过程

   use master
   go
   if object_id ('sp_shrink_mirrored_database', 'P') is not null 
     drop proc sp_shrink_mirrored_database 
   go
   create procedure sp_shrink_mirrored_database @dbname sysname, @target_percent int = null
   as
   begin
     declare @filename sysname
     declare @filesize int
     declare @sql nvarchar(4000)

     if @target_percent is null
       dbcc shrinkdatabase (@dbname)
     else 
       dbcc shrinkdatabase (@dbname, @target_percent)
     declare c cursor for 
     select [name], [size] from sys.master_files where type=0 and database_id = db_id (@dbname)
     open c
     fetch next from c into @filename, @filesize
     while @@fetch_status=0
     begin
       set @filesize=(@filesize+1)*8
       set @sql='alter database [' + @dbname + '] modify file ( name=' 
         + @filename + ', size=' + cast(@filesize as nvarchar) + 'kb )'
       execute sp_executesql @sql
       fetch next from c into @filename, @filesize
     end
     close c
     deallocate c
   end
   go

2- execute the stored procedure like this in a new query window For example, if you want to shrink the mydb database, run the following statement. 2-在新的查询窗口中执行这样的存储过程例如,如果要收缩mydb数据库,请运行以下语句。

  EXEC sp_shrink_mirrored_database 'mydb'

http://support.microsoft.com/kb/937531 http://support.microsoft.com/kb/937531

The shrink operation is not duplicated on the mirror database when you use database mirroring in SQL Server 2005 在SQL Server 2005中使用数据库镜像时,镜像数据库上的收缩操作不会重复

CAUSE 原因
Database mirroring will change the physical file sizes only after a checkpoint. 数据库镜像仅在检查点之后才会更改物理文件大小。

WORKAROUND 替代方法
Method 1 方法1
To work around this problem, run the following statements to create a new stored procedure in the master database. 要解决此问题,请运行以下语句以在master数据库中创建新的存储过程。 Then, use this stored procedure to shrink the principal database instead of running the DBCC SHRINKDATABASE statement or the DBCC SHRINKFILE statement. 然后,使用此存储过程收缩主体数据库,而不是运行DBCC SHRINKDATABASE语句或DBCC SHRINKFILE语句。
[stored procedure too long to post here] [存储过程太长,无法在此处发布]

Method 2 方法2
Issue a manual checkpoint after shrinking the files on the principal. 缩小主体上的文件后发出手动检查点。

Some good ideas can be found here: http://yukonspace ... transaction-log-growth . 可以在这里找到一些好的想法: http:// yukonspace ... transaction-log-growth

From my experience with full-recovery mode databases (should behave in somewhat similar way) you need to make regular full backups at least, otherwise logs only grow. 根据我对完全恢复模式数据库的经验(应该采用类似的方式),您至少需要定期进行完全备份,否则日志只会增长。

Simply you can shrink the log file by using the following command 您只需使用以下命令缩小日志文件即可

USE DBNAME
GO
DBCC SHRINKFILE(DBNAME_log, 1)
BACKUP LOG DBNAME WITH TRUNCATE_ONLY
DBCC SHRINKFILE(DBNAME_log, 1)
GO

Are you sure the Mirror did not fall behind? 你确定镜子没落后吗? The principal server instance can't truncate the log until they have been successfully applied to the mirror. 主服务器实例在成功应用于镜像之前无法截断日志。 After a certain point I believe you may have to backup the transaction log and apply it to the mirror and restart mirroring. 在某一点之后,我相信您可能必须备份事务日志并将其应用于镜像并重新启动镜像。

要收缩事务文件,必须执行备份,因为存在活动虚拟日志文件: http//www.xoowiki.com/Article/SQL-Server/tronquer-journal-de-log-sur-base-en-miroir-499的.aspx

  1. You have to backup the log regularly otherwise your log file will grow indefinitely. 您必须定期备份日志,否则您的日志文件将无限增长。 If you can't write to the log (ie out of disk space or hit max size) the DB will fail 如果您无法写入日志(即磁盘空间不足或达到最大大小),则DB将失败
  2. SQL Server 2005 has an issue where the shrink isn't reflected on the mirror side. SQL Server 2005存在一个问题,即镜像端没有反映缩小。 The solution is here http://support.microsoft.com/kb/937531 解决方案在这里http://support.microsoft.com/kb/937531

This article has some good info on How Log Files work, you should also read Factors that can delay log truncation 本文提供了有关日志文件如何工作的一些很好的信息,您还应该阅读可以延迟日志截断的因素

Configure Transaction Log backup and create a job or run direct the following command.配置事务日志备份并创建作业或直接运行以下命令。

use DatabaseName GO DBCC SHRINKFILE (LogicalFileName_log, 1);使用 DatabaseName GO DBCC SHRINKFILE (LogicalFileName_log, 1); GO

and if you want to shrink a Data file then follow the bellow steps.如果您想缩小数据文件,请按照以下步骤操作。

  1. go to your database properties.转到您的数据库属性。
  2. select option in the left.选择左侧的选项。
  3. change auto Shrink property to true, by default it is false.将 auto Shrink 属性更改为 true,默认为 false。 and wait for sql server to shrink it automatically.并等待sql server自动缩小它。

I hope it will resolve both log and data file shrining problem.我希望它可以解决日志和数据文件收缩问题。

Yousaf Khan.优素福汗。

Please make sure to perform a full backup after performing log backup with TRUNCATE_ONLY. 使用TRUNCATE_ONLY执行日志备份后,请确保执行完整备份。 This breaks the log backup chain. 这会破坏日志备份链。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM