缩小镜像 SQL Server 2005 数据库的事务日志
我一直在互联网上查找,但找不到可以接受的解决方案来解决我的问题,我想知道是否有一个不妥协的解决方案......
我不是 DBA,但我是一个人的团队在一个巨大的网站上工作,没有额外的资金用于额外的机构,所以我正在尽我所能。
我们的备份计划很糟糕,我很难改进它。 目前,有两台服务器运行 SQL Server 2005。我有一个镜像数据库(无见证),似乎运行良好。 我在中午和午夜进行完整备份。 我们的服务提供商每晚都会将这些文件备份到磁带上,我每周都会将备份文件刻录到 DVD,以保留旧记录。 最终我想切换到日志传送,因为如果没有见证服务器,镜像似乎毫无意义。
问题是事务日志不断增长。 根据我所做的研究,我似乎无法截断镜像数据库的日志文件。 那么如何阻止文件增长!?
基于此网页,我尝试了以下操作:
USE dbname
GO
CHECKPOINT
GO
BACKUP LOG dbname TO DISK='NULL' WITH NOFORMAT, INIT, NAME = N'dbnameLog Backup', SKIP, NOREWIND, NOUNLOAD
GO
DBCC SHRINKFILE('dbname_Log', 2048)
GO
但这并没有'不工作。 我发现的其他所有内容都表明我需要在运行备份日志命令之前禁用镜像才能使其正常工作。
我的问题 (TL;DR)
如何在不禁用镜像的情况下缩小事务日志文件?
I've been looking all over the internet and I can't find an acceptable solution to my problem, I'm wondering if there even is a solution without a compromise...
I'm not a DBA, but I'm a one man team working on a huge web site with no extra funding for extra bodies, so I'm doing the best I can.
Our backup plan sucks, and I'm having a really hard time improving it. Currently, there are two servers running SQL Server 2005. I have a mirrored database (no witness) that seems to be working well. I do a full backup at noon and at midnight. These get backed up to tape by our service provider nightly, and I burn the backup files to dvd weekly to keep old records on hand. Eventually I'd like to switch to log shipping, since mirroring seems kinda pointless without a witness server.
The issue is that the transaction log is growing non-stop. From the research I've done, it seems that I can't truncate a log file of a mirrored database. So how do I stop the file from growing!?
Based on this web page, I tried this:
USE dbname
GO
CHECKPOINT
GO
BACKUP LOG dbname TO DISK='NULL' WITH NOFORMAT, INIT, NAME = N'dbnameLog Backup', SKIP, NOREWIND, NOUNLOAD
GO
DBCC SHRINKFILE('dbname_Log', 2048)
GO
But that didn't work. Everything else I've found says I need to disable the mirror before running the backup log command in order for it to work.
My Question (TL;DR)
How can I shrink my transaction log file without disabling the mirror?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
嗯,从技术上来说,缩小镜像日志是可能的。 麻烦的是用 truncate_only 备份日志。 镜像不接受它。 因此,一种方法是将日志备份到磁盘:
这是我们当前维护计划的一部分,并且它已经运行了大约 2 年,没有出现任何问题。
Well, technically it is possible to shrink a mirrored LOG. What is doing trouble is to backup log with truncate_only. Mirroring doesn't accept it. So one way is to perform a backup log to disk:
This is part of our current maintenance plan and it has been working withot problems for about 2 years.
如果镜像服务器实例落后于主体服务器实例,则活动日志空间量将会增加。 在这种情况下,您可能需要停止数据库镜像,进行日志备份以截断日志,将该日志备份应用到镜像数据库并重新启动镜像,这不是您所希望的答案,我知道=(
要缩小我们的文件,您可以尝试以下脚本:
exec sp_dboption DBName, 'trunc log on chkpt.', true
检查站
DBCC SHRINKFILE (DBNameFileName, 500);
exec sp_dboption DBName, 'trunc. 登录 chkpt。', false
希望这会有所帮助。
If the mirror server instance falls behind the principal server instance, the amount of active log space will grow. In this case, you may need to stop database mirroring, take a log backup that truncates the log, apply that log backup to the mirror database and restart mirroring, not the answer you were hoping for, I know =(
To shrink our files you could try the following script:
exec sp_dboption DBName, 'trunc. log on chkpt.', true
checkpoint
DBCC SHRINKFILE (DBNameFileName, 500);
exec sp_dboption DBName, 'trunc. log on chkpt.', false
Hope this helps.
我想我应该回答这个问题,因为它被遗忘了。
事实证明,如果数据库已镜像,则无法缩小 t-log,除非停用镜像。 如果我错了,请纠正我,但我没有找到有效的解决方案!
如果您只有两台服务器,则日志传送是可行的方法。 如果没有见证服务器,镜像几乎毫无意义,因为故障转移的唯一方法是来自主体......如果主体崩溃时无法进行故障转移,那么有点违背了拥有镜像的目的。
如果有人愿意分享有关此事的更多信息或建议,我将很高兴听到他们的声音。
I thought I should actually answer this seeing as it was left forgotten about.
Turns out, you can't shrink a t-log if the database is mirrored unless you deactivate the mirror. If I'm wrong, please correct me, but I've found no solution that works!
Log shipping is the way to go if you only have two servers. Mirroring is almost pointless without a witness server, because the only way to failover is from the principal... kinda defeats the purpose of having a mirror if you can't failover when the principal crashes.
If anyone cares to share more info or suggestions on this matter, I will be welcome to hear them.
ALTER [DatabaseName] SET PARTNER OFF
设置镜像伙伴关闭。BACKUP LOG [DatabaseName] TO DISK='Drive:\DatabaseName_log_datetime.trn'< /code>
DatabaseName_log_datetime.trn
复制到镜像服务器上的任意位置。NoRecovery
选项恢复此事务日志。RESTORE LOG [DatabaseName] FROM DISK ='Drive:\DatabaseName_log_datetime.trn'
缩小ALTER [DatabaseName] SET PARTNER OFF
BACKUP LOG [DatabaseName] TO DISK='Drive:\DatabaseName_log_datetime.trn'
DatabaseName_log_datetime.trn
to any location on Mirror Server.NoRecovery
option..on mirror database.RESTORE LOG [DatabaseName] FROM DISK ='Drive:\DatabaseName_log_datetime.trn'
测试了这篇文章中的一些建议,我发现在完整备份、检查点命令和事务日志备份之后,主体数据库事务日志大小可以缩小。
然后,镜像数据库事务日志大小与主体收缩大小同步。
使用OSQL可以在DOS下批量运行上述SQL命令,如果批量运行,WAITFOR DELAY是必须的,例如
我认为不同的备份也应该可以工作,但没有测试。 下面的 diff 备份命令将附加数据而不是覆盖。
Tested some suggestions in this post, I found that, after full backup, checkpoint command, and transaction log backup, the principal database transaction log size can be shrinked.
The mirror database transaction log size is then synchronized with principal shrinked size.
Use OSQL can run the above SQL commands in DOS batch, the WAITFOR DELAY is a must if run in batch, e.g.
I think the different backup should be worked too, but doesn't test. The below diff backup command will append the data instead of overwrite.
唯一的办法:
1) 停止镜像
2) 缩小主体文件
3)主体备份完成,+事务jrnl
4)停止镜像服务器,删除mirrorDatabase的mdf和ldf
5)启动mirrorser并删除mirrorDatabase
6) 在 mirroServer 上进行恢复,无需 3) 的恢复备份
7) 重新安装镜像
哎哟!
the only way:
1) Stop mirroring
2) shrink files on principal
3) backup complete of principal, + transaction jrnl
4) stop mirror server, delete mdf and ldf of mirrorDatabase
5) start mirrorser and delete mirrorDatabase
6) restore with no recovery backups of 3) on mirroServer
7) reinstall mirroring
Ouf !
确实,一旦数据库日志变得太大,你就无法缩小它——此时我认为你唯一的选择就是破坏镜像、缩小并重新创建。 此外,尽管存在您是否应该仅使用两台服务器使用镜像的问题,但我可以说的是,如果您这样做,则定期备份事务日志。 该空间将从其中释放,从而允许 MSSQL 重新使用日志文件中的死空间。 这不会缩小任何东西,但确实满足了阻止其增长的要求。
那么您需要做的就是定期删除文件备份。 例如,您可以这样做:
如果可以的话,请在非工作时间进行。
It's true that you can't shrink the database log once it's got too big - at that point I think your only option is to break the mirror, shrink and re-create. Further, notwithstanding the issues of whether you ought to be using mirroring with just two servers, what I can say is that if you do then regularly backup the transaction log. The space will be released from it thereby allowing MSSQL to re-use the dead space within the log file. This doesn't shrink anything but it does meet the requirement of stopping it growing.
Then all you need to do is regularly delete the file backups. For example you could do this:
Do it out of hours though if you can.
我不知道为什么会这样,只知道它确实有效。 我将其作为查询窗口中的块运行。 请自行决定使用。 如果微软能发表评论当然会很好。
I don't know why this works, only that it does indeed work. I run this as a block in a query window. Use at your own discretion. Sure would be nice if a microsoftie would comment.
只要它不是主体数据库,您就无法在不将其从镜像中取出的情况下对镜像数据库执行任何操作。
应该有效的是,如果您在主体服务器上备份数据库并随后进行收缩。 也就是说,您的维护计划应该包括一些缩减工作。 这些更改应通过同步自动传递到辅助(镜像)服务器。
如果您想要进行仅收缩事务文件的收缩,您可以使用以下 T-SQL:
然后您只需为每个数据库使用该代码片段,并在备份运行后立即运行它。
这应该使主体服务器上以及辅助/镜像服务器上的日志文件保持较小。
我希望这有帮助。
顺便提一句。 如果您已经到了磁盘上没有空间容纳日志文件的地步,唯一的选择就是将其退出镜像模式,将数据库设置为简单恢复模式,收缩日志文件,将其设置为完整再次进入恢复模式并重新设置镜像(使用数据库和日志文件的备份在镜像服务器上恢复)。
此外,如果问题在于许可,您可以使用 SQL Express 作为见证服务器。 它不需要太多资源,因此如果这是一个 Web 应用程序,您可以使用 Web 服务器。 请记住还要查看见证服务器的日志文件。
You can't really do anything to a mirrored database without taking it out of the mirror, as long as it's not the principal database.
What should work is if you backup your databases on the principal server og do a shrink afterwards. Ie your maintenance plan should include som shrinking job. Those changes should get over to the secondary (mirror) server automatically by synchronizing.
If you want to make a shrink that only shrinks the transaction file you can use the following T-SQL:
Then you just use that snippet for each database and run it right after your backup has run.
That should keep the log file small on the principal server and thereby on the secondary/mirror server.
I hope this helps.
Btw. If you've gotten to the point where there is no room left on the disk for log files, the only option is to take it out of mirror mode, set the database to simple recovery mode, shrink the log file, set it to full recovery mode again and setup the mirror again (using backups of the database og log file to restore on the mirror server).
Futhermore you can use an SQL Express as a witness server, if the issue is licensing. It shouldn't require too many ressources, so you could use a webserver, if this is a web application. Just remember to watch the log files for the witness server too.
可以通过镜像缩小数据库的事务文件,必须执行备份,因为存在活动的虚拟日志文件:
http:// /www.xoowiki.com/Article/SQL-Server/tronquer-journal-de-log-sur-base-en-miroir-499.aspx
It's possible to shrink transaction file for a database with mirror, backup must be performed as there are actives Virtual Log File :
http://www.xoowiki.com/Article/SQL-Server/tronquer-journal-de-log-sur-base-en-miroir-499.aspx
** 缩小可以在镜像中完成,我们不能做的是截断日志,因为日志是发送到镜像服务器的内容,然后主体等待确认。
解决该问题的方法是备份不截断的日志,然后收缩日志文件,或者甚至可以忽略收缩。 如果这不起作用,请在备份日志之前尝试检查点。 这应该有效...
** shrinking can be done in mirroring, what we cannot do is truncate the log, as the log is what is shipped to the mirror server and then the principal awaits the acknowledgement.
A solution to the issue is to backup the log with no truncate and then shrink the log file, or you can even ignore the shrinking. If that does not work, try a checkpoint before backing up your log. This should work...