Friday, June 9, 2017

Script to add database in existing availability group by Tsql.

This is the script to include database in existing Always on availability group by passing below parameters.

1. Primary server name, port number

2.Secodnary server name , port number

3. Database name

4.Availability group name

5.Shared path on primary server with read and write access for dba id required to take backup from primary server for HADR configuration.

6. This script to be executed in SQLCMD only.


--- YOU MUST EXECUTE THE FOLLOWING SCRIPT IN SQLCMD MODE.
:Connect Primary-Server_Name,5008

USE [master]

GO

ALTER AVAILABILITY GROUP [Availability group NAME]
ADD DATABASE [Database Name];

GO

:Connect Primary-Server_Name,Port_Number

BACKUP DATABASE [Database Name] TO  DISK = N'\\Primary-Server_Name\Always_on_Backup
\Database Name.bak' WITH  COPY_ONLY, FORMAT, INIT, SKIP, REWIND, NOUNLOAD,
COMPRESSION,  STATS = 5

GO

:Connect Secondary-Server_Name,Port_Number

RESTORE DATABASE [Database Name] FROM  DISK = N'\
\Primary-Server_Name\Always_on_Backup\Database Name.bak' WITH  NORECOVERY,  NOUNLOAD,
 STATS = 5

GO

:Connect Primary-Server_Name,Port_Number

BACKUP LOG [Database Name] TO  DISK = N'\\Primary-Server_Name\Always_on_Backup
\Database Name_20170609165021.trn' WITH NOFORMAT, NOINIT, NOSKIP, REWIND,
NOUNLOAD, COMPRESSION,  STATS = 5

GO

:Connect Secondary-Server_Name,Port_Number

RESTORE LOG [Database Name] FROM  DISK = N'\\Primary-Server_Name\Always_on_Backup
\Database Name_20170609165021.trn' WITH  NORECOVERY,  NOUNLOAD,  STATS = 5

GO

:Connect Secondary-Server_Name,Port_Number


-- Wait for the replica to start communicating
begin try
declare @conn bit
declare @count int
declare @replica_id uniqueidentifier 
declare @group_id uniqueidentifier
set @conn = 0
set @count = 30 -- wait for 5 minutes 

if (serverproperty('IsHadrEnabled') = 1)
                and (isnull((select member_state from master.sys.dm_hadr_cluster_members where
upper(member_name COLLATE Latin1_General_CI_AS) = upper(cast(serverproperty
('ComputerNamePhysicalNetBIOS') as nvarchar(256)) COLLATE Latin1_General_CI_AS)), 0) <>
0)
                and (isnull((select state from master.sys.database_mirroring_endpoints), 1) = 0)
begin
    select @group_id = ags.group_id from master.sys.availability_groups as ags where name
= N'Availability group NAME'
                select @replica_id = replicas.replica_id from master.sys.availability_replicas as
replicas where upper(replicas.replica_server_name COLLATE Latin1_General_CI_AS) = upper
(@@SERVERNAME COLLATE Latin1_General_CI_AS) and group_id = @group_id
                while @conn <> 1 and @count > 0
                begin
                                set @conn = isnull((select connected_state from
master.sys.dm_hadr_availability_replica_states as states where states.replica_id =
@replica_id), 1)
                                if @conn = 1
                                begin
                                                -- exit loop when the replica is connected, or if the query
cannot find the replica status
                                                break
                                end
                                waitfor delay '00:00:10'
                                set @count = @count - 1
                end
end
end try
begin catch
                -- If the wait loop fails, do not stop execution of the alter database statement
end catch
ALTER DATABASE [Database Name] SET HADR AVAILABILITY GROUP = [Availability group NAME];

GO


GO

 Hope adding database to an existing Always on availability group can be done in TSQL script.
 If more number of databases to be added in Always on availability group, We can do it by repeating above steps in Sql server management studio new query window .

Tuesday, June 6, 2017

Steps to apply patches in Always on availability group replica servers

How do apply patch in always on availability group replica servers?

1. Make sure to connect the instances using 'Multisubnetfailover=TRUE' as this required to perform failover.

2.perform the patch apply in the secondary server replica .

3.Reboot the secondary server replica and perform health check .

4.Ensure databases are in synchronising mode in the secondary server replica .

-- select * from sys. Dm_hadr_database_replica_states.

Just check the synchronisation_state_desc and synchronisation_health_desc  column value for health values.

5.If Always on is configured in Asynchronous mode , then make sure to change it to Synchronous mode to avoid data loss.( Revert back to asynchronous mode once all steps for patch completed in both servers).

Alter  availability group group_name
Modify replica on 'server name '

With

({ availability _mode =synchronous_commit|asynchronous_commit})

6.Failover the Always on group to the Secondary server replica.

Use master
Go
Alter availability group availability _group Failover
Go


7.Ensure databases are in synchronised mode , if no , execute the following script for all availability group databases .

Use master
Go
Alter database database_name
Set HADR resume

8.Apply the patch in the old primary server(Current secondary replica)

9.Reboot the old primary server and perform health check .

10.Failover from old secondary server to old primary server(production)

Execute below script

Use master
Go
Alter availability group availability _group Failover
Go

11.Perform health check .

To verify everything in place , view the dashboard of The availability group in sql server management studio.

Make sure New patch got applied in both servers successfully .





Resume data movement for availability group databases # Resume HADR

while I noticed that on the secondary replica there was a pause icon next to the Availability Databases. The primary had shown both were "green", but there was an option on the secondary to Resume Data Movement. I resumed the first database, and immediately the In Recovery status message was removed. 

A minute later it changed from Not Synchronizing to Synchronized, and everything worked as expected.

Note you can also use TSQL on the secondary to resume replication on multiple database at the same time:

ALTER DATABASE [Patch] SET HADR RESUME;
ALTER DATABASE [test] SET HADR RESUME;

GO

Always on availability group -HADR Questions &Answers

What about a sync mode secondary replica of always on availability group goes offline for 3 hours and come back online it self?

Answer:

Once the secondary replica server goes offline ,data movement between replica would not happen and hence the transaction log on primary grows and keep on accumulating transactions.
This would cause log file to grow;So please make sure to you have enough space in place for the log file drive in the primary replica server.

Once secondary server comes online , always on group will synchronise itself to make sure primary and secondary data in SYNC Without data loss.

No need to resume manually Always on availability session .MANUAL RESUME is used in manual/ planned failover.

MYSQL::Setting Validate_Password componet for MySQL Database to ensure password policy settings

Inadequate Password Settings for MySQL Database We observed that the `validate_password%` settings on hostname `<insert hostname>` a...