如何将数据库及其用户从一个SQL Server传输到另一个SQL Server?

时间:2021-08-16 23:07:36

we have one db in an SQLServer instance that should be transferred to another instance (let's say dbname="testdb" with a user testuser).

我们在一个SQLServer实例中有一个数据库应该转移到另一个实例(假设dbname =“testdb”与用户testuser)。

This transfer works easily when we use SQL Server Management Studio (backup the database on the source machine and restore it on the target machine).

当我们使用SQL Server Management Studio(在源计算机上备份数据库并在目标计算机上还原它)时,此传输很容易。

The problem is now that I can't connect with the testuser on the target machine. It's part of the db but not part of the SQL Server wide logins.

现在的问题是我无法与目标机器上的testuser连接。它是数据库的一部分,但不是SQL Server范围登录的一部分。

Now my question. How can I add the user from the db to the SQL Server logins?

现在我的问题。如何将用户从db添加到SQL Server登录?

Thanks in advance for any comments!

提前感谢您的任何意见!

Cheers, Helmut

干杯,赫尔穆特

2 个解决方案

#1


3  

What really helped me in the end was running the following SQL script (after restore)

最后真正帮助我的是运行以下SQL脚本(恢复后)

USE testdb;
GO
EXEC sp_change_users_login 'Auto_Fix', 'testuser', NULL, 'testpwd';
GO

This "connects" the database user "testuser" to the server login "testuser". If no server login "testuser" exists a new one is created.

这将数据库用户“testuser”“连接”到服务器登录“testuser”。如果没有服务器登录“testuser”,则创建一个新的。

For documentation please see http://msdn.microsoft.com/en-us/library/ms174378.aspx

有关文档,请参阅http://msdn.microsoft.com/en-us/library/ms174378.aspx

#2


1  

I used to face the same problem dear. The Solution for his is You have to create login in server with the Statement below

我曾经面对同样的问题亲爱的。他的解决方案是您必须使用下面的语句在服务器中创建登录

Use [testdb] --Your Database name

使用[testdb] - 您的数据库名称

CREATE USER [testuser] FOR LOGIN [testuser]

CREATE USER [testuser] FOR LOGIN [testuser]

here your [testuser] is your database user and the second [testuser] your server login name and with this statement we mapped each other.

这里你的[testuser]是你的数据库用户,第二个[testuser]是你的服务器登录名,我们用这个语句相互映射。

#1


3  

What really helped me in the end was running the following SQL script (after restore)

最后真正帮助我的是运行以下SQL脚本(恢复后)

USE testdb;
GO
EXEC sp_change_users_login 'Auto_Fix', 'testuser', NULL, 'testpwd';
GO

This "connects" the database user "testuser" to the server login "testuser". If no server login "testuser" exists a new one is created.

这将数据库用户“testuser”“连接”到服务器登录“testuser”。如果没有服务器登录“testuser”,则创建一个新的。

For documentation please see http://msdn.microsoft.com/en-us/library/ms174378.aspx

有关文档,请参阅http://msdn.microsoft.com/en-us/library/ms174378.aspx

#2


1  

I used to face the same problem dear. The Solution for his is You have to create login in server with the Statement below

我曾经面对同样的问题亲爱的。他的解决方案是您必须使用下面的语句在服务器中创建登录

Use [testdb] --Your Database name

使用[testdb] - 您的数据库名称

CREATE USER [testuser] FOR LOGIN [testuser]

CREATE USER [testuser] FOR LOGIN [testuser]

here your [testuser] is your database user and the second [testuser] your server login name and with this statement we mapped each other.

这里你的[testuser]是你的数据库用户,第二个[testuser]是你的服务器登录名,我们用这个语句相互映射。