SQL Server 2008 -- User not able to access restored Database - sql-server

I'm trying to move a database from one SQL Server database running on one machine to a another machine that is the test server and copy of the original.
On the main machine, I took a backup of database myDB.
On the test machine, I deleted the existing older database myDB.
On the test machine, I restored the new database myDB.
The data seems to have come across successfully.
But I have a problem accessing the database.
The owner of all tables is 'user1' and user1 exists with the same login on both DB's.
After trying to access the restored database on the test machine though as user1 there are problems.
First it says that the password for user1 isn't correct.
After resetting the password, it says the user1 doesn't have a default database set. But it is set already to myDB. It is the same name as the restored db -- myDB.
What have I done wrong in restoring the DB?
Do I need to have deleted in addition to the DB the user user1 before attempting to restore the backup? I tried that. How do I handle updating a restore and maintaining user access?

after doing the restore do a
ALTER USER user1 WITH LOGIN = user1
inside the restored DB, that will fix the mismapped SID
See also Do you still use sp_change_users_login instead of ALTER USER UserName WITH LOGIN = UserName

To avoid this issue add your user ( the user who is taking backup) as a user of the Backup database, and your user should have (login) access in the new instance.

Related

AWS RDS SQL Server Backup Restore Issue

I have setup the RDS instance for SQL Server 2019 SE. Option group also created with major version=15. SQL Server backup and restore option is added along with S3 settings and IAM role. The option group is also attached with instance.
I executed the command;
exec msdb.dbo.rds_restore_database #restore_db_name='MyDatabaseOnRDS_Staging',
#s3_arn_to_restore_from='arn:aws:s3:::MyBucketName/MyDatabaseOnRDS_Staging.bak';
The above statement created the task which is visible in;
exec msdb.dbo.rds_task_status;
The % complete goes to 100%. But then it shows ERROR in 'lifecycle' column. When I check the details in task_info column, it shows the error at the end;
The proposed new database owner is already a user or aliased in the database. Changed database context to 'master'.
What am I doing wrong?
Ok, the issue was sa user. It was added as a db user. It was in disabled state, but for some reason AWS RDS was not allowing rdsa to be the DB owner of the DB in restore as sa was the part of db users.
I did to following steps;
Changed the sa schema owner to dbo
Removed sa user (just user and not the sa schema) from the DB Users.
Took the backup, copied the backup to S3 and restored it.
You're attempting to restore the database from within a connection to that database. Change the context you're executing the restore operation from to the master database, as the error suggested.

SQL Server - new user has access to master database

I created a new SQL server with a database in Azure and after logging in with the admin account I added a new user that I wanted to only have access to one database.
In SSMS I right clicked on mynewdatabase database and selected New Query.
I executed
CREATE USER mynewuser WITH PASSWORD ='good password here';
ALTER ROLE db_owner ADD MEMBER mynewuser ;
Now after logging in as mynewuser I can still see the master database.
The question is how do I restrict mynewuser to only access mynewdatabase?
I'm sure this is something basic.
In SQL Server, including Azure SQL DB, all users can can see all system databases in sys.databases. This is not the same as being able to connect to them or run queries in them. This does not disclose any sensitive information as these are system databases and whether you saw them listed or not you would know they were there. See https://msdn.microsoft.com/en-us/library/ms178534.aspx#Anchor_0.
Based on the steps you describe, you have created a contained user that should not be able to connect to the master database or run queries in Azure SQL DB.

Change the user identity in MS SQL

I have the following scenario:
On one PC I have SQL db where the owner is local user e.g. \xyz. This db has a lot of users and they all are domain user. Now I have to make a copy of the db on another PC. I'm planning to backup it up and restore on the second computer. I think that domain users will be OK but local user \xyz likely will not be OK because is different and etc.
My question is: how could I change it and make \xyz valid on the second PC? Could I delete this user and create a new one with correct and likely assign ownership and roles it had on the first PC?
I have to do this procedure on 5 PC and I'm thinking of a way to save time and it came to my mind that backup/restore would be the best and simple approach ... but I am concern about local user.
Thanks
When you restore a db from one server to another, you may have "orphaned" database users. You'll need to re-map the db user to an existing (or new) server login. For instance:
ALTER USER \xyz
WITH LOGIN = loginName

can't change the password for a user

I have downloaded database and attached it to my local sql server, however I can't seem to change the password of one the existing user's on the db.
Using the following command:
ALTER LOGIN [NotificationsUser] WITH PASSWORD = 'password';
I get the error:
Msg 15151, Level 16, State 1, Line 1
Cannot alter the login
'NotificationsUser', because it does
not exist or you do not have
permission.
Is this possible?, what access permissions do I need to change user permissions anyway ?
If you've attached this database to your local SQL server then you'll need to do a couple of things:
If you haven't already done so, create user logins on your SQL server to match the ones that exist in the attached database. It's simpler to do this before attaching the database. But it can be done after the DB has been attached.
Because the SID's of the users in the newly attached database won't be the same as the newly created logins you'll need to resolve this using the sp_change_users_login stored procedure. The database user's are in effect orphaned.
For example if you have:
SQL Login: bob Attached database user: bob
Open a new query in SQL Management Studio for the attached database then run:
sp_change_users_login #action='report'
If you have "orphaned" users in your database then you'll see a result set that looks like:
UserName UserSID
bob 0x57F6DFA5D5D7374A97769856E7CB5610
To reconnect this user to a SQL login execute:
sp_change_users_login #action='update_one',
#loginname='bob',
#usernamepattern='bob'
I think you're confusing a database user with a server login.
Your database may have a user in it called NotificationUser but this needs to be associated with a server login, which is the object you're trying to alter with the script. A database restore from a different server won't have created this server login so there's a good chance it doesn't exist on your server. More info here

Change Login Name

i have a problem when i restore DB_1 database backup into DB_2 database. but Login Name for user dbo is missing (). how i can re-create the Login Name?? i use SQL 2000
What you have here is called an orphaned user. i.e. the user exists in the database you've restored, but it isn't setup as a login on the database server.
If the user is a windows integrated login, the adding the login to the database server is all you need to do. If its a SQL Server login, then it's a little tricker:
To get a report of that orphaned users are in your restored database, run:
USE restored_database
GO
exec sp_change_users_login #Action='Report';
GO
To re-create the database at login & have it linked to the restored database run:
EXEC sp_change_users_login 'Auto_Fix', 'user', 'login', 'password'
Call sp_dropuser 'user' on the restored database.
If the user already exist on the server, give the user permission to the restored database, otherwise re-create the user on the server and then give permission to the restored database.
Add 'sa' as the databaseowner so add 'sa' to the db owner role of the DB_2 catalog before you restore the backup to that catalog.
Btw, sorry if I sound rude, but stackoverflow is mainly for programming questions, not sysadmin questions, I think if you ask your question on a sqlserver oriented board you'll get more responses.

Resources