Showing posts with label Database Mirroring. Show all posts
Showing posts with label Database Mirroring. Show all posts

Friday, 26 October 2012

Database Mirroring Error


I have been setting up multi instance database mirroring for the last couple of days along with some other DR related processes.  I will do a detailed post about these at a later date.  I came across this particular error again (for about the sixth or seventh time) Error Message below;

Msg 1416, Level 16, State 31, Line “LineNumberHere” Database "DatabaseNameHere" is not configured for database mirroring

The reason for me posting this is because each time I have seen this error it has been at the same point in the process of setting up database mirroring.  As always I prefer to avoid using the GUI and Wizard and instead have a set of scripts to setup mirroring.  I had created my endpoints, granted permissions, taken full and log backups of the database(s) in question on the PRINCIPAL Server and restored them to the MIRROR server WITH NORECOVERY.  The next step in my process is to run the below to enable the mirroring partnership;

ALTER DATABASE "DatabaseNameHere" SET PARTNER = 'TCP://FullyQualifiedNameHere:PortNumberHere';
GO

The reason for the failure is that I ran the script on the PRINCIPAL and it should be run on the MIRROR server first, Doh!

I remember the first time I saw this error I followed what a lot of people in various forums advised and amongst other solutions (which also didn’t work) recreated the backups and restored them.  This can be a time consuming process if you are working with large databases so I would advise you to first check that you are running this step on the MIRROR server first as it could save you a considerable amount of time and effort.

Enjoy!

Chris

Tuesday, 21 February 2012

A Script A Day - Day 14 - Upgrading to SQL 2012

Today’s script is one I have used to test one possible upgrade method from SQL Server 2008 to SQL Server 2012. If truth be told this would be my prefered upgrade method I’ll explain why…

I have database mirroring in production on SQL Server 2008 two physical servers in an active passive cluster configuration as the PRINCIPAL and the FAILOVER PARTNER is a third physical server.  My plan is to create another active passive cluster with SQL Server 2012 installed and configured then break the existing mirroring partnership and setup a new mirroring partnership to the new cluster.  All this work can be done without any downtime to the current environment!  Once the new mirroring partnership is setup I can schedule a failover and a few seconds later I’m on SQL Server 2012 in production.

I can then rebuild the old SQL 2008 PRINCIPAL and FAILOVER PARTNER servers with SQL Server 2012 and create availability groups, Wohooo!  I'm way to excited about availability groups, it opens up so many possibilities!!!

/*
      -----------------------------------------------------------------
      Test upgrading SQL Server 2008 to SQL Server 2012
     
      Server1 is the PRINCIPAL and Server2 is the FAILOVER PARTNER
      The test database is called DenaliHA
      The test table is called HATest
     
      You will need to specify a login to have permissions granted on
      the endpoints
      -----------------------------------------------------------------
     
      For more SQL resources, check out SQLServer365.blogspot.co.uk

      -----------------------------------------------------------------

      You may alter this code for your own purposes.
      You may republish altered code as long as you give due credit.
      You must obtain prior permission before blogging this code.
 
      THIS CODE AND INFORMATION ARE PROVIDED "AS IS"
     
      -----------------------------------------------------------------
*/

--    *** RUN AT THE PRINCIPAL ***

-- Create test database
USE [master]
GO
CREATE DATABASE [DenaliHA] ON  PRIMARY
( NAME = N'DenaliHA_Data', FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL10.MSSQLSERVER\MSSQL\DATA\DenaliHA_Data.mdf' , SIZE = 1048576KB , MAXSIZE = UNLIMITED, FILEGROWTH = 1024KB )
 LOG ON
( NAME = N'DenaliHA_Log', FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL10.MSSQLSERVER\MSSQL\DATA\DenaliHA_Log.ldf' , SIZE = 1048576KB , MAXSIZE = 2048GB , FILEGROWTH = 10%)
GO
ALTER DATABASE [DenaliHA] SET COMPATIBILITY_LEVEL = 100
GO
IF (1 = FULLTEXTSERVICEPROPERTY('IsFullTextInstalled'))
begin
EXEC [DenaliHA].[dbo].[sp_fulltext_database] @action = 'enable'
end
GO

-- Create test table
USE DenaliHA
GO
CREATE TABLE HATest (
                              HATestID INT IDENTITY(1,1),
                              Forename VARCHAR (100),
                              Surname VARCHAR (100)
                         );

-- Insert some data
INSERT INTO HATest (Forename, Surname) VALUES ('Chris','McGowan')
GO 10000
CREATE CLUSTERED INDEX [IDX_HATest:Composite1] ON HATest (HATestID);
GO

-- Backup database and transaction log
BACKUP DATABASE DenaliHA TO DISK  = 'C:\DenaliHA\DenaliHA.bak';
GO
BACKUP LOG DenaliHA TO DISK = 'C:\DenaliHA\DenaliHA.trn';
GO

-- Create endpoint
USE master
GO
IF NOT EXISTS (   SELECT *
                        FROM sys.endpoints
                        WHERE name = 'DenaliHADatabaseMirroringEndpoint'      )
      CREATE ENDPOINT [DenaliHADatabaseMirroringEndpoint]
      STATE = STARTED
      AS TCP (LISTENER_PORT = 1430, LISTENER_IP = ALL)
      FOR DATA_MIRRORING (ROLE = PARTNER, AUTHENTICATION = WINDOWS NEGOTIATE,
      ENCRYPTION = REQUIRED ALGORITHM AES);
GO
           
-- Grant permissions on endpoint
IF EXISTS ( SELECT  name
                  FROM    sys.server_principals
                  WHERE   name = '' )                                                                                         -- Must add Login Name
      GRANT CONNECT ON ENDPOINT::DenaliHADatabaseMirroringEndpoint TO [Login Name Here];  -- Must add Login Name
GO

--    *** RUN AT THE FAILOVER PARTNER ***

USE master
GO
-- Create endpoint
IF NOT EXISTS (   SELECT *
                        FROM sys.endpoints
                        WHERE type_desc = 'DATABASE_MIRRORING'    )
      CREATE ENDPOINT [DenaliHADatabaseMirroringEndpoint]
      STATE = STARTED
      AS TCP (LISTENER_PORT = 1440, LISTENER_IP = ALL)
      FOR DATA_MIRRORING (ROLE = PARTNER, AUTHENTICATION = WINDOWS NEGOTIATE,
      ENCRYPTION = REQUIRED ALGORITHM AES);
GO

-- Grant permissions on endpoint
IF EXISTS ( SELECT  name
                  FROM    sys.server_principals
                  WHERE   name = '' )                                                                                         -- Must add Login Name
      GRANT CONNECT ON ENDPOINT::DenaliHADatabaseMirroringEndpoint TO [Login Name Here];  -- Must add Login Name
GO

-- Copy backup files from server1

-- Get file locations for the restore
USE DenaliHA
GO
sp_helpfile
GO

-- Restore backups
USE master
GO
RESTORE DATABASE DenaliHA FROM DISK  = 'C:\DenaliHA\DenaliHA.bak' WITH REPLACE, MOVE 'DenaliHA_Data' TO 'C:\Program Files\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\DATA\DenaliHA_Data.mdf', MOVE 'DenaliHA_Log' TO 'C:\Program Files\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\DATA\DenaliHA_Log.ldf', NORECOVERY;
GO
RESTORE LOG DenaliHA FROM DISK = 'C:\DenaliHA\DenaliHA.trn' WITH REPLACE, MOVE 'DenaliHA_Data' TO 'C:\Program Files\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\DATA\DenaliHA_Data.mdf', MOVE 'DenaliHA_Log' TO 'C:\Program Files\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\DATA\DenaliHA_Log.ldf', NORECOVERY;
GO

-- Enable database for mirroring
ALTER DATABASE DenaliHA SET PARTNER = 'TCP://Server1.GPGROUP.COM:1430';

--    *** RUN AT THE PRINCIPAL ***

-- Enable database for mirroring
ALTER DATABASE DenaliHA SET PARTNER = 'TCP://Server2.GPGROUP.COM:1440';

-- Insert some more data to prove the database mirroring session is working
USE DenaliHA
GO
INSERT INTO HATest (Forename, Surname) VALUES ('Chris2','McGowan2');
GO 10000

-- Failover!!!
ALTER DATABASE DenaliHA SET PARTNER FAILOVER;

/*
      It is at this point where the database will be online on the SQL 2012 instance
      NOTE - Databasebase Mirroring will be suspsended and errors like the below will be received;

      'TCP://Server1.GPGROUP.COM:1430', the remote mirroring partner for database 'DenaliHA', encountered error 948, status 2, severity 20. Database mirroring has been suspended.  Resolve the error on the remote server and resume mirroring, or remove mirroring and re-establish the mirror server instance.
      Error: 1453, Severity: 16, State: 1.

      This is beacuse Database Mirroring works from SQL 2008 to SQL 2012 for upgrades only!  Mirroring SQL 2012 to SQL 2008 will not work!!!
*/

--    *** RUN AT THE PRINCIPAL ***

-- Remove mirroring
ALTER DATABASE DenaliHA SET PARTNER OFF;

--    *** RUN AT THE FAILOVER PARTNER ***

-- Bring original database online
RESTORE DATABASE DenaliHA WITH RECOVERY;

-- Drop Databases
DROP DATABASE DenaliHA;
GO

--    *** RUN AT THE PRINCIPAL ***
DROP DATABASE DenaliHA;
GO

Enjoy!

Chris