如何从 SQL Server 2008 中删除 .ldf 文件?
如果我停止 SQL-server 然后删除数据库中的 .LDF 文件(事务日志文件),会发生什么?数据库会被标记为可疑还是 SQL 服务器会自动创建一个新数据库? SQL Server 2008 R2 我的 .LDF 文件太大,那么如何管理它,是否可以缩小或删除它 请在查询表中提出建议。
If I stop SQL-server and then delete the .LDF file (transactionlog file) to the database, what will happen ? Will the database be marked suspect or will SQL-server just create a new automatically ? SQL Server 2008 R2
And My .LDF file Size is Too Big, So how to manage it, whether I can Shrink it or delete
Please Suggest in the Query Form.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您不应该删除任何数据库文件,因为它会严重损坏您的数据库!
如果磁盘空间不足,您可能需要将数据库拆分为多个部分。这可以在数据库的属性中完成。因此,您可以将数据库的每个部分放入不同的存储卷。
如果您使用以下命令将恢复模式从完整更改为简单,您还可以收缩事务日志文件:
也可以切换回完全恢复:
有关 SHRINKDATABASE 的更新 - 或者我不知道的内容在回答这个问题时:
虽然上面的方法摆脱了一些未使用的空间,但它对数据库文件(MDF)有一些严重的缺点 - 它会通过碎片化索引来损害你的索引,从而恶化数据库的性能。因此,您需要随后重建索引以消除收缩命令造成的碎片。
如果您只想缩小日志文件,可能需要使用 SHRINKFILE反而。我从 MSDN 复制了这个例子:
You should not delete any of the database files since it can severely damage your database!
If you run out of disk space you might want to split your database in multiple parts. This can be done in the database's properties. So you are able to put each part of the database to a different storage volume.
You also can shrink the transaction log file if you change the recovery mode from full to simple, using following commands:
Switching back to full recovery is possible as well:
Update about SHRINKDATABASE - or what I did not know when answering this question:
Although the method above gets rid off some unused space it has some severe disadvantages on database files (MDF) - it will harm your indexes by fragmenting them worsening the performance of your database. So you need to rebuild the indexes afterwards to get rid off the fragmentation the shrink command caused.
If you want to shrink just the log file only might want to use SHRINKFILE instead. I copied this example from MSDN:
不要冒险手动删除 LDF 文件!如果您不需要交易文件或希望将其缩小到您选择的任何大小,请按照以下步骤操作:
(请注意,这将影响您的备份,因此在执行此操作之前请确保)
Do not risk deleting your LDF files manually! If you do not need transaction files or wish to reduce them to any size you choose, follow these steps:
(Note this will affect your backups so be sure before doing so)
我通过
在SQL 2012中对4个不同的数据库执行此操作,对于SQL 2008我应该是相同的
I did it by
Did it for 4 different databases in SQL 2012, i should be the same for SQL 2008
正如您可以阅读评论一样,删除日志并不是一个好的解决方案。但是,如果您确定没有丢失任何内容,则可以将数据库恢复模式更改为简单,然后使用
DBCC Shrinkdatabase('这里是您的数据库名称')
来清除日志。
最糟糕的事情就是从磁盘删除日志文件。如果您的服务器在服务器停止时有未完成的事务,这些事务在重新启动后将不会回滚,并且您将获得损坏的数据。
As you can read comments, it is not good solution to remove log. But if you are sure that you do not lose anything, you can just change your DB recovery mode to simple and then use
DBCC shrinkdatabase ('here your database name')
to clear your log.
The worst thing that you can do is to delete log file from disk. If your server had unfinished transactions at moment of server stop, those transactions will not roll back after restart and you will get corrupted data.
您应该备份事务日志,然后就有可用空间来缩小它。更改为简单模式然后收缩意味着您将丢失在恢复时有用的所有事务数据。
You should back up your transaction log, then there will be free space to shrink it. Changing to simple mode then shrinking means you will lose all the transaction data which would be useful in the event of a restore.
清除 MS SQL Server 中所有数据库中的所有 ldf 文件(事务日志文件)的最佳方法,如果所有数据库都已提前备份的话:
这是一个改进的代码,基于: https://www.sqlservercentral.com/Forums/Topic1163961-357-1.aspx
我建议阅读这篇文章: https://learn .microsoft.com/en-us/sql/relational-databases/backup-restore/recovery-models-sql-server
有时,在某些数据库上永久启用 RECOVERY MODEL = SIMPLE 是值得的,因此一劳永逸摆脱日志问题。特别是当我们每天备份数据(或服务器)时,从安全角度来看,白天的更改并不重要。
The best way to clear ALL ldf files (transaction log files) in all databases in MS SQL server, IF all databases was backed up earlier of course:
This is an improved code, based on: https://www.sqlservercentral.com/Forums/Topic1163961-357-1.aspx
I recommend reading this article: https://learn.microsoft.com/en-us/sql/relational-databases/backup-restore/recovery-models-sql-server
Sometimes it is worthwhile to permanently enable RECOVERY MODEL = SIMPLE on some databases and thus once and for all get rid of log problems. Especially when we backup data (or server) daily and daytime changes are not critical from a security point of view.