[英]Restore database SQL server query
I want a way to write my own query to restore the database. 我想要一种方法来编写自己的查询来恢复数据库。 The database to restore needs to have all the settings to delete the current user and re-map the same user. 要还原的数据库需要具有删除当前用户并重新映射同一用户的所有设置。 The reason for that is because when the database is restored, the user will not have the right settings to use the database and will have to re assign the user the privileges. 原因是因为在恢复数据库时,用户将没有正确的设置来使用数据库,并且必须重新为用户分配权限。
Step 1: Retrive the Logical file name of the database from backup. 步骤1:从备份中重新获取数据库的逻辑文件名。
RESTORE FILELISTONLY
FROM DISK = 'D:BackUpYourBaackUpFile.bak'
GO 走
Step 2: Use the values in the LogicalName Column in following Step. 步骤2:使用以下步骤中的LogicalName列中的值。 ----Make Database to single user Mode ----使数据库成为单用户模式
ALTER DATABASE YourDB
SET SINGLE_USER WITH
ROLLBACK IMMEDIATE
----Restore Database ----恢复数据库
RESTORE DATABASE YourDB
FROM DISK = 'D:BackUpYourBaackUpFile.bak'
WITH MOVE 'YourMDFLogicalName' TO 'D:DataYourMDFFile.mdf',
MOVE 'YourLDFLogicalName' TO 'D:DataYourLDFFile.ldf'
/ If there is no error in statement before database will be in multiuser mode. / 如果数据库处于多用户模式之前语句中没有错误。 If error occurs please execute following command it will convert database in multi user. 如果发生错误,请执行以下命令,它将在多用户中转换数据库。 / /
ALTER DATABASE YourDB SET MULTI_USER
GO
The reason for that is because when the database is restored, the user will not have the right settings to use the database and will have to re assign the user the privileges. 原因是因为在恢复数据库时,用户将没有正确的设置来使用数据库,并且必须重新为用户分配权限。
I guess that: 我猜是:
Correct? 正确?
If yes, you need to consider the following: 如果是,您需要考虑以下事项:
I guess that the second point is the reason why your user doesn't have "the right settings" after restoring. 我想第二点是你的用户在恢复后没有“正确设置”的原因。
Some background: 一些背景:
So what probably happened is this: 所以可能发生的事情是:
123456789
在旧服务器上,有一个用户“Mohammed Tahir”,SID为123456789
123456789
is allowed to read from this database" 在数据库中,有一个权限说“允许SID 123456789
从此数据库中读取” 987654321
), so the existing permissions on 123456789
don't work for him! 你在新服务器上创建了一个用户“Mohammed Tahir”,但他有一个不同的SID(比方说987654321
),所以123456789
上的现有权限对他不起作用! There is a stored procedure from Microsoft named sp_help_revlogin
, which does just that. Microsoft提供了一个名为sp_help_revlogin
的存储过程,它就是这样做的。
It generates a script with all the users from the old server. 它会为旧服务器中的所有用户生成一个脚本。 You can then run the script on the new server, and it will create the users with the same SIDs they had on the old server. 然后,您可以在新服务器上运行该脚本,它将创建具有旧服务器上相同SID的用户。
Then, you can restore the database from the old server to the new server, and all the permissions already in the database just work . 然后,您可以将数据库从旧服务器还原到新服务器,并且数据库中已有的所有权限都可以正常工作 。
You can get sp_help_revlogin
from this MSDN article: 您可以从此MSDN文章中获取sp_help_revlogin
:
How to transfer logins and passwords between instances of SQL Server . 如何在SQL Server实例之间传输登录名和密码 。
Note that there is nothing special about the actual restoring process - it's the users and their SIDs that make the difference. 请注意,实际的恢复过程没有什么特别之处 - 用户和他们的SID有所不同。
So you don't need any "special" commands to restore the database, just the standard ones, for example the one from Rahul Tripathi's answer . 因此,您不需要任何“特殊”命令来恢复数据库,只需要标准数据库,例如来自Rahul Tripathi的答案 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.