Unable to attach database in Amazon RDS SQL Server - sql-server

I'm trying to detach and attach a database in AWS RDS. I'm able to detach the database without any issues. But while I try to attach the database, I'm getting few errors.
When I run the attach command for the first time, I get the following error:
Unable to create database: User, group, or role 'sa' already exists in the current database.
When I execute for the second time, I get this:
Msg 1802, Level 16, State 7.
CREATE DATABASE failed. Some file names listed could not be created. Check related errors.
Msg 5120, Level 16, State 101.
Unable to open the physical file "D:\RDSDBDATA\DATA\DBattachTest.mdf". Operating system error 2: "2(The system cannot find the file specified.)". (Line 6)
Please advise

I'm not sure what you are trying to do but you should be using backup and restore functionality to move DBs around. Attach and detach will not work as they do in a traditional SQL environment: RDS is a managed service and you have extremely limited access to the underlying OS.
From AWS docs:
Restoring a Database
To restore your database, you call the rds_restore_database stored procedure.
The following parameters are required:
#restore_db_name – The name of the database to restore.
#s3_arn_to_restore_from – The Amazon S3 bucket that contains the backup file, and the name of the file.
The following parameters are optional:
#kms_master_key_arn – If you encrypted the backup file, the key to use to decrypt the file.
Example Without Encryption
exec msdb.dbo.rds_restore_database
#restore_db_name='database_name',
#s3_arn_to_restore_from='arn:aws:s3:::bucket_name/file_name_and_extension';
Example With Encryption
exec msdb.dbo.rds_restore_database
#restore_db_name='database_name',
#s3_arn_to_restore_from='arn:aws:s3:::bucket_name/file_name_and_extension',
#kms_master_key_arn='arn:aws:kms:region:account-id:key/key-id';

Related

Error when trying to load database into backup server

Im trying to load a database dump into my Sybase backup server.
Running sybase ASE-16_0 on both my primary and backup machine.
Import is done in isql cli via
load database DB from ./dumps/data_dump
Error message is the following:
Backup Server session id is: 22. Use this value when executing the
'sp_volchanged' system stored procedure after fulfilling any volume change
request from the Backup Server.
Backup Server: 4.141.2.40: [11] The 'open' call failed for database/archive
device while working on stripe device
'/opt/dumps/data_dump' with error number
13 (Permission denied). Refer to your operating system documentation for further
details.
Also found this SAP Knowledge Base article, but it's hidden behind a paywall:
https://userapps.support.sap.com/sap/support/knowledge/en/3140989
Setting up a new Server fixed the issue for me. Im still working on a possible answer for this problem.

How can I rename an RDS SQL Server Web Edition's DB name (Not the RDS instance name, but one of the DB's inside the RDS instance)?

I have tried to rename the DB name in a RDS with SSMS but its giving below error.
User does not have permission to alter database 'DB name', the
database does not exist, or the database is not in a state that allows
access checks. (Microsoft SQL Server, Error: 5011)
Can some one please help me on this?
I have went through many articles and aws docs but did not find straight away solution so I am writing this, hope it helps someone. After some amendments to the findings, I was able to fix this issue.
Run the below queries as master on new query terminal.
use master
ALTER DATABASE YourOldDBname
SET SINGLE_USER WITH ROLLBACK IMMEDIATE
GO
EXEC rdsadmin.dbo.rds_modify_db_name N'YourOldDBname', N'YourNewDBname'
GO
ALTER DATABASE YourNewDBname
SET MULTI_USER
GO
If you have any active connection, you might get below error. Mostly you will not get any error as you are running above queries, if you still seeing below error please shut down any server that might be trying to establish a connection to same RDS DB.
Msg 50000, Level 16, State 2, Procedure rds_modify_db_name, Line 95 https://forums.aws.amazon.com/
The database could not be exclusively locked to perform the operation.
Hope its helped you :)

Create Database Snapshot for remote database on local machine

I am having a subtle problem in creating Database snapshot. I am connected to a remote database. I want to create a snapshot on my local machine. Is it possible or allowed?
I am typing the query below
> CREATE DATABASE SampleSnapshot ON ( NAME = 'Sample', FILENAME =
> 'C:\SampleFile' ) AS SNAPSHOT OF Sample; GO
and getting the exception
Msg 262, Level 14, State 1, Line 1
CREATE DATABASE permission denied in database 'master'.
I am having following questions:
When i specify the path in query the snapshot will be created on local machine or server machine? If local machine then how to create the snapshot on server machine itself.
No. Generally Client/Server means you send instructions to the server. Which execute on the server. Which means that your C:\ is relative TO THE SERVER, not your client machine.

SQL Server production server - all databases are in recovery pending state

All the databases in my SQL Server production server are in recovery pending state. I tried to execute different queries but they were of no use. Please help me as production work has been stopped at client side.
Tried to execute alter commands - but show error as following:
Msg 5120, Level 16, State 101, Line 1 Unable to open the physical file
"G:\Data\MSSQL\Database.mdf". Operating system error 3: "3(The system
cannot find the path specified.)". File activation failure. The
physical file name "G:\Data\MSSQL\Data\Database_log.ldf" may be
incorrect. Msg 945, Level 14, State 2, Line 1 Database 'Database'
cannot be opened due to inaccessible files or insufficient memory or
disk space. See the SQL Server errorlog for details
Msg 5069, Level 16, State 1, Line 1
Recovery pending means that for some reason SQL cannot run restart recovery on the database. Usually this is because the log is missing or corrupt.
Check to see if you can find the Database.mdf and Database_log.ldf files in the folder specified.
Check your system has not run out of disk space.
This could be caused by a hard drive failure. You may need to do a full restore of your last full back, any differentials and then restore the logs up until the log error occurred.
See similar issue here
My team encountered this error many times for my clients & I know, It is not easy to manage in the Production server. In your case Error 5120 –This error comes when the database is in Read Only Mode.
To fix this you can below code
USE [master]
GO
ALTER DATABASE [SQLAuthority] SET READ_WRITE WITH NO_WAIT
GO
After fixing 5120, you can process to fix "databases are in recovery pending state"
Recovery Pending – If the SQL Server knows that database recovery needs to be run but something is preventing it from starting, the Server marks the db in ‘Recovery Pending’ state. This is different from the SUSPECT state because it cannot be said that recovery is going to fail – it just hasn’t started yet.
Execute the following set of queries:
ALTER DATABASE [DBName] SET EMERGENCY; GO
ALTER DATABASE [DBName] set single_user GO
DBCC CHECKDB ([DBName], REPAIR_ALLOW_DATA_LOSS) WITH ALL_ERRORMSGS; GO
ALTER DATABASE [DBName] set multi_user GO
Note: You can also read the Microsoft Warning on DBCC CHECKDB REPAIR ALLOW DATA LOSS.
It might be because of following possible causes:
Permissions
Find your SQL Server instance in the services list and double-click it, then select the Log On tab. It is this log on account that must have sufficient permissions to write to the temporary backup folder location. Check the permissions on the temporary backup folder by right-clicking it in Windows Explorer, selecting Properties, then navigating to the Security tab. Make sure that the account SQL Server is using has explicit read/write permissions for this folder.
Mapped Drives
Use a fully qualified UNC path instead of a mapped drive letter.
Lack Of Domain Trust
You can resolve this issue by ensuring that a trust between the two domains is established. You may also need to configure the SQL Server service account with pass-through authentication between the domains.
Please refer more here for recovery db
Execute these queries to fix SQL server database in recovery pending state:
ALTER DATABASE [DBName] SET EMERGENCY
GO
ALTER DATABASE [DBName] SET single_user
GO
DBCC CHECKDB ([DBName], REPAIR_ALLOW_DATA_LOSS) WITH ALL_ERRORMSGS
GO
ALTER DATABASE [DBName] SET multi_user
GO
EMERGENCY mode marks the SQL Server database as READ_ONLY, deactivates logging, and gives the permission to system admin only. This method is capable of resolving any technical issue and bringing the database back to the accessible state. The database will automatically come out of the EMERGENCY mode.

MSSQL mirroring error 1499

I`ve got a problem with my mirroring. I have already uses this feature to make 2 db work.
But it was on test bases.
Now when I use my scripts on work DB it throws an error:
Msg 1499, Level 16, State 1, Line 1
Database mirroring error: status 1488, severity 16, state 1, string db_name.
Any suggestions?
you database may be in single user mode
right click the database -> options and select MULTI USER under restrict access
try to check the principal and mirror server names, dtabase name etc. It is possible that there is misprints.
This happened to us recently. We found that SQL set the restored database to single user mode during the restore process.
After some research, we simply restarted SQL server services on the mirror. When SQL started back up, the restored database was in multi-user mode and we were able to initialize mirroring without any issues.
There are other sites out there with answers as well, but they involve taking down the principle database. That was not really an option for us at the time. Here is one example in case restarting the mirror does not help the next person who runs into this.
http://blog.armgasys.com/?p=259
I had some what similar issue this is how I solved it. When I ran wizard for mirroring it created endpoints(first part) successfully but when i tried to turn on mirroring it gave me error.
So I solved it by running below command on both server
USE [master]
alter database [yourdatabase] set partner off;
go
--Secondary already had partner but primary did not
--- then i ran below commands on each server.
USE [master]
ALTER DATABASE [yourdatabase]
SET PARTNER = 'TCP://OtherMachineSQLname.domain.LOCAL:5022';
GO
-- then mirroring started to work.

Resources