简体   繁体   English

还原数据库SQL Server查询

[英]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. 原因是因为在恢复数据库时,用户将没有正确的设置来使用数据库,并且必须重新为用户分配权限。

Check this out:- 看看这个 : -

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: 我猜是:

  1. You are using mixed mode authentication and the user is a SQL Server user (not a Windows user) 您正在使用混合模式身份验证,用户是SQL Server用户(不是Windows用户)
  2. You are restoring the database to a different server than the one where the backup was made 您正在将数据库还原到与进行备份的服务器不同的服务器

Correct? 正确?

If yes, you need to consider the following: 如果是,您需要考虑以下事项:

  1. The user must exist on the second server as well, it's not created automatically when you restore the database there 用户也必须存在于第二台服务器上,当您在那里恢复数据库时,它不会自动创建
  2. It's not enough to just create a new user with the same name on the second server - to SQL Server, this would be a different user! 仅仅在第二台服务器上创建一个具有相同名称的新用户是不够的 - 对于SQL Server,这将是一个不同的用户!

I guess that the second point is the reason why your user doesn't have "the right settings" after restoring. 我想第二点是你的用户在恢复后没有“正确设置”的原因。

Some background: 一些背景:

  • Internally, all SQL Server users are represented by a SID (something unique and unreadable - similar to a GUID . SQL Server doesn't care about the actual user name internally) . 在内部,所有SQL Server用户都由SID表示(某些内容是唯一且不可读的 - 类似于GUID.SQL Server并不关心内部的实际用户名)
  • The permissions that each user has on a database are saved inside the database, using the SID and not the username 每个用户对数据库的权限都使用SID而不是用户名保存在数据库中
  • When you restore the database to a different server, the permissions are restored with the database...but they only work when there's a user with the exact SID on the new server 将数据库还原到其他服务器时,将使用数据库还原权限...但只有当用户在新服务器上具有确切的SID时,它们才有效
  • As I said before: when you just create a new user with the same name, he gets a new SID. 正如我之前所说:当你刚刚创建一个具有相同名称的新用户时,他会得到一个新的SID。

So what probably happened is this: 所以可能发生的事情是:

  • on the old server, there's a user "Mohammed Tahir" with the SID 123456789 在旧服务器上,有一个用户“Mohammed Tahir”,SID为123456789
  • inside the database, there's a permission that says "SID 123456789 is allowed to read from this database" 在数据库中,有一个权限说“允许SID 123456789从此数据库中读取”
  • you restored the database on the new server 您在新服务器上还原了数据库
  • you created a user "Mohammed Tahir" on the new server, but he has a different SID (let's say 987654321 ), so the existing permissions on 123456789 don't work for him! 你在新服务器上创建了一个用户“Mohammed Tahir”,但他有一个不同的SID(比方说987654321 ),所以123456789上的现有权限对他不起作用!

So what you need is a way to "copy" the user from the old server to the new server, with the exact same SID. 所以你需要的是一种将用户从旧服务器“复制”到新服务器的方法,具有完全相同的SID。

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.

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