Setting up database mirroring on SQL 2005 can be tricky and reading this MSDN article can be helpful. I am going to run through a few simple commands that might help you get things running if this is your first time trying to setup a mirrored database. It may not be obvious if this is your first time setting up mirroring so I want to mention that both databases need to be running the same version of SQL 2005 in order for mirroring to work. Namely, you can't mirror between Standard and Enterprise editions, both servers either need to be standard edition or they need to be enterprise edition.
1) Your first step is to back up the database that you want to mirror (the principle database). You could use a command like:
Backup Database
[DatabaseName]
To Disk='C:\Backup\DatabaseName_Log.bak'
You will also need to backup the log. (I've done it without the log backup/restore but might as well follow MS instructions). The following command is similar to the former:
Backup Log
[DatabaseName]
To Disk='C:\Backup\DatabaseName_Log.bak'
You might find the following commands useful in the process:
2) Restore the database probably using WITH MOVE and you must use WITH NORECOVER. I have this command posted below, scroll down to view it. Next you are going to need to restore the log file. I have the syntax for the log file restore below as well.
3) Configure Security for Mirroring through Management Studio. In order to accomplish this step, right-click on the database you wish to mirror (not the database you just restored, the original) and click on properties. In the left panel navigation there will be a page listed as "Mirroring". Click on the mirroring page and then click "Configure Security". Walk through the steps, since this is our first time setting up a mirrored database, let's select No to the first question, "Do you want to configure security to include a witness server instance?", Next, Next. Now you need to pick your mirrored server and give it some connection settings, Next. Now enter in your Service Accounts so that the database can create the logins to be used for the principle and mirrored databases. If you don't know what to do on this step just leave them both blank and we'll get back to it later. Click Finish.
4) Start Mirroring. You will be prompted to Start Mirroring after SQL Server has finished creating the logins and the endpoints. Click Start Mirroring.
5) Error Messages: At this point, you'll get an error message if there are configuration issues. Some of the errors you might get are listed below:
An error occurred while starting mirroring.
Database does not exist on the mirror server instance. You must restore a backup of the principal database on the mirror server instance before starting mirroring. (SqlManagerUI) Solution: Make sure that your database is restored correctly on the mirrored server
Database mirroring is disabled by default. Database mirroring is currently provided for evaluation purposes only and is not to be used in production environments. Solution: Modify SQL 2005 Database Configuration allow startup option -t1400. Follow the instructions from this link.
The ALTER DATABASE command could not be sent to the remote server instance 'TCP://<ServerName>:<port>' Solution: Check the server's ERRORLOG, more than likely this is a login problem. Create the login (Database->Security) from the error message that was written in the database log.
If you receive an error message other than those listed above be sure to check the error logs on the principle and mirrored servers. The location of the error logs is typically C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\LOG\ERRORLOG. The error log should be able to help you determine what configuration settings are wrong.
If you find the following error in your log you need to setup a user on the server that has the credentials below (make the user a sysadmin for the purposes of getting it working)
Database Mirroring login attempt by user 'DOMAIN\USERNAME.' failed with error: 'Connection handshake failed. The login 'DOMAIN\USERNAME' does not have CONNECT permission on the endpoint. State 84.
6) Mirroring is Working! If you've got it working you'll know it, but the following command will give you the details just in case you want to verify everything worked correctly
SELECT
DB_NAME(database_id) AS 'DatabaseName'
, mirroring_role_desc
, mirroring_safety_level_desc
, mirroring_state_desc
, mirroring_safety_sequence
, mirroring_role_sequence
, mirroring_partner_instance
, mirroring_witness_name
, mirroring_witness_state_desc
, mirroring_failover_lsn
FROM sys.database_mirroring
WHERE mirroring_guid IS NOT NULL;
Other helpful commands to create database mirrors and/or diagnose mirroring problems:
-- ### Determine If Mirroring Endpoint is Established ### ---
SELECT
name,
role_desc,
state_desc
FROM
sys.database_mirroring_endpoints
-- ### Remove Mirroring From A Database ### --
ALTER DATABASE
[DatabaseName]
SET
PARTNER OFF
-- ### Restore Database With Move ### --
RESTORE DATABASE
[DatabaseName]
FROM DISK =
'C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\DATA\DatabaseName_FULL.bak'
WITH
NORECOVERY,
MOVE
'DatabaseName'
TO
'C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\DATA\DatabaseName.mdf',
MOVE
'DatabaseName'
TO
'C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\DATA\DatabaseName.ldf'
-- ### Restore Log File ### --
RESTORE LOG
[DatabaseName]
FROM DISK =
'C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\DATA\DatabaseName_Log.bak'
WITH
NORECOVERY
-- ### Drop and Create Endpoints ### --
DROP ENDPOINT [Mirroring]
GO
CREATE ENDPOINT [Mirroring]
AUTHORIZATION [Domain\User]
STATE=STARTED
AS TCP (LISTENER_PORT = 5022, LISTENER_IP = ALL)
FOR DATA_MIRRORING (ROLE = PARTNER, AUTHENTICATION = WINDOWS NEGOTIATE
, ENCRYPTION = DISABLED)
-- ### Restore the Mirrored Database ### --
RESTORE DATABASE
[DatabaseName]
WITH
RECOVERY