%3CLINGO-SUB%20id%3D%22lingo-sub-981679%22%20slang%3D%22en-US%22%3EBuild%20a%20SQL%20Cluster%20Lab%20Part%203%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-981679%22%20slang%3D%22en-US%22%3E%3CP%3E%3C!--%20wp%3Aparagraph%20--%3E%3C%2FP%3E%0A%3CP%3EYou%20are%20going%20to%20create%20a%20multi-subnet%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fsql%2Fdatabase-engine%2Favailability-groups%2Fwindows%2Foverview-of-always-on-availability-groups-sql-server%22%20target%3D%22_blank%22%20rel%3D%22noreferrer%20noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%20aria-label%3D%22Availability%20Group%20(opens%20in%20a%20new%20tab)%22%3EAvailability%20Group%3C%2FA%3E%20in%20Part%203%20of%20our%20series%20on%20how%20to%20build%20a%20SQL%20Cluster%20Lab.%20First%20you%20give%20the%20Cluster%20Name%20Object%20rights%20in%20Active%20Directory%20and%20then%20install%20your%20AG.%20You%20will%20also%20ensure%20the%20AG%20endpoints%20are%20using%20the%20dedicated%20network%20created%20for%20them%20in%20Part%201.%20Here%20are%20links%20to%20the%20other%20articles%20in%20the%20series.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2FPremier-Field-Engineering%2FBuild-a-SQL-Cluster-Lab-Part-1%2Fba-p%2F917572%22%20target%3D%22_blank%22%20rel%3D%22noopener%22%3EPart%201%20-%20Building%20the%20environment.%3C%2FA%3E%20Hyper-v%20VMs%2C%20Active%20Directory%2C%20DNS%2C%20and%20Routing%20and%20Remote%20Access%20Services%3C%2FLI%3E%0A%3CLI%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2FPremier-Field-Engineering%2FBuild-a-SQL-Cluster-Lab-Part-2%2Fba-p%2F917579%22%20target%3D%22_blank%22%20rel%3D%22noopener%22%3EPart%202%20-%20Windows%20Failover%20Clustering%3C%2FA%3E.%20Cluster%20validation%2C%20networks%2C%20and%20quorum%3C%2FLI%3E%0A%3CLI%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2FPremier-Field-Engineering%2FBuild-a-SQL-Cluster-Lab-Part-3%2Fba-p%2F981679%22%20target%3D%22_blank%22%20rel%3D%22noopener%22%3EPart%203%20-%20Availability%20Group%3C%2FA%3E.%20Dedicated%20network%20and%20AD%20permissions%3C%2FLI%3E%0A%3CLI%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2FPremier-Field-Engineering%2FBuild-a-SQL-Cluster-Lab-Part-4%2Fba-p%2F917599%22%20target%3D%22_blank%22%20rel%3D%22noopener%22%3EPart%204%20-%20Failover%20Cluster%20Instance%3C%2FA%3E.%20Creating%20shared%20storage%20using%20iSCSI%20and%20adding%20the%20storage%20to%20the%20cluster.%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CP%3E%3C!--%20%2Fwp%3Aparagraph%20--%3E%20%3C!--%20wp%3Ablock%20%7B%22ref%22%3A2264%7D%20%2F--%3E%20%3C!--%20wp%3Aheading%20--%3E%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId-1853573696%22%20id%3D%22toc-hId-1853573696%22%3E%26nbsp%3B%3C%2FH2%3E%0A%3CH2%20id%3D%22toc-hId--698583265%22%20id%3D%22toc-hId--698583265%22%3E%3C!--%20wp%3Aheading%20--%3E%3C%2FH2%3E%0A%3CH2%20id%3D%22toc-hId-1044227070%22%20id%3D%22toc-hId-1044227070%22%3ECreate%20Databases%20Code%3C%2FH2%3E%0A%3CH2%20id%3D%22toc-hId--1507929891%22%20id%3D%22toc-hId--1507929891%22%3E%3C!--%20%2Fwp%3Aheading%20--%3E%20%3C!--%20wp%3Asyntaxhighlighter%2Fcode%20--%3E%3C%2FH2%3E%0A%3CPRE%20class%3D%22wp-block-syntaxhighlighter-code%22%3EUSE%20%5Bmaster%5D%3B%0AGO%0A%0ADROP%20DATABASE%20IF%20EXISTS%20App1AG_DB1%3B%0AGO%0A%0ADROP%20DATABASE%20IF%20EXISTS%20App1AG_DB2%3B%0AGO%0A%0ADROP%20DATABASE%20IF%20EXISTS%20App1AG_DB3%3B%0AGO%0A%0ACREATE%20DATABASE%20%5BApp1AG_DB1%5D%0A%20ON%20%20PRIMARY%20%0A(%20NAME%20%3D%20N'App1AG_DB1'%2C%20FILENAME%20%3D%20N'C%3A%5CProgram%20Files%5CMicrosoft%20SQL%20Server%5CMSSQL14.MSSQLSERVER%5CMSSQL%5CDATA%5CApp1AG_DB1.mdf'%20%2C%20SIZE%20%3D%203072KB%20%2C%20MAXSIZE%20%3D%20UNLIMITED%2C%20FILEGROWTH%20%3D%201024KB%20)%0A%20LOG%20ON%20%0A(%20NAME%20%3D%20N'App1AG_DB1_log'%2C%20FILENAME%20%3D%20N'C%3A%5CProgram%20Files%5CMicrosoft%20SQL%20Server%5CMSSQL14.MSSQLSERVER%5CMSSQL%5CDATA%5CApp1AG_DB1_log.ldf'%20%2C%20SIZE%20%3D%201024KB%20%2C%20MAXSIZE%20%3D%202048GB%20%2C%20FILEGROWTH%20%3D%201024KB)%0AGO%0A%0ACREATE%20DATABASE%20%5BApp1AG_DB2%5D%0A%20ON%20%20PRIMARY%20%0A(%20NAME%20%3D%20N'App1AG_DB2'%2C%20FILENAME%20%3D%20N'C%3A%5CProgram%20Files%5CMicrosoft%20SQL%20Server%5CMSSQL14.MSSQLSERVER%5CMSSQL%5CDATA%5CApp1AG_DB2.mdf'%20%2C%20SIZE%20%3D%203072KB%20%2C%20MAXSIZE%20%3D%20UNLIMITED%2C%20FILEGROWTH%20%3D%201024KB%20)%0A%20LOG%20ON%20%0A(%20NAME%20%3D%20N'App1AG_DB2_log'%2C%20FILENAME%20%3D%20N'C%3A%5CProgram%20Files%5CMicrosoft%20SQL%20Server%5CMSSQL14.MSSQLSERVER%5CMSSQL%5CDATA%5CApp1AG_DB2_log.ldf'%20%2C%20SIZE%20%3D%201024KB%20%2C%20MAXSIZE%20%3D%202048GB%20%2C%20FILEGROWTH%20%3D%201024KB)%0AGO%0A%0ACREATE%20DATABASE%20%5BApp1AG_DB3%5D%0A%20ON%20%20PRIMARY%20%0A(%20NAME%20%3D%20N'App1AG_DB3'%2C%20FILENAME%20%3D%20N'C%3A%5CProgram%20Files%5CMicrosoft%20SQL%20Server%5CMSSQL14.MSSQLSERVER%5CMSSQL%5CDATA%5CApp1AG_DB3.mdf'%20%2C%20SIZE%20%3D%203072KB%20%2C%20MAXSIZE%20%3D%20UNLIMITED%2C%20FILEGROWTH%20%3D%201024KB%20)%0A%20LOG%20ON%20%0A(%20NAME%20%3D%20N'App1AG_DB3_log'%2C%20FILENAME%20%3D%20N'C%3A%5CProgram%20Files%5CMicrosoft%20SQL%20Server%5CMSSQL14.MSSQLSERVER%5CMSSQL%5CDATA%5CApp1AG_DB3_log.ldf'%20%2C%20SIZE%20%3D%201024KB%20%2C%20MAXSIZE%20%3D%202048GB%20%2C%20FILEGROWTH%20%3D%201024KB)%0AGO%0A%0A%0A--Full%20backup%20required%20to%20create%20an%20AG%0ABACKUP%20DATABASE%20%5BApp1AG_DB1%5D%20TO%20%20DISK%20%3D%20N'C%3A%5CProgram%20Files%5CMicrosoft%20SQL%20Server%5CMSSQL14.MSSQLSERVER%5CMSSQL%5CBackup%5CApp1AG_DB1.bak'%20WITH%20FORMAT%2C%20INIT%2C%20%20SKIP%2C%20NOREWIND%2C%20NOUNLOAD%2C%20%20STATS%20%3D%2010%2C%20CHECKSUM%0AGO%0A%0ABACKUP%20DATABASE%20%5BApp1AG_DB2%5D%20TO%20%20DISK%20%3D%20N'C%3A%5CProgram%20Files%5CMicrosoft%20SQL%20Server%5CMSSQL14.MSSQLSERVER%5CMSSQL%5CBackup%5CApp1AG_DB2.bak'%20WITH%20FORMAT%2C%20INIT%2C%20%20SKIP%2C%20NOREWIND%2C%20NOUNLOAD%2C%20%20STATS%20%3D%2010%2C%20CHECKSUM%0AGO%0A%0ABACKUP%20DATABASE%20%5BApp1AG_DB3%5D%20TO%20%20DISK%20%3D%20N'C%3A%5CProgram%20Files%5CMicrosoft%20SQL%20Server%5CMSSQL14.MSSQLSERVER%5CMSSQL%5CBackup%5CApp1AG_DB3.bak'%20WITH%20FORMAT%2C%20INIT%2C%20%20SKIP%2C%20NOREWIND%2C%20NOUNLOAD%2C%20%20STATS%20%3D%2010%2C%20CHECKSUM%0AGO%0A%3C%2FPRE%3E%0A%3CH2%20id%3D%22toc-hId-234880444%22%20id%3D%22toc-hId-234880444%22%3E%3C!--%20%2Fwp%3Asyntaxhighlighter%2Fcode%20--%3E%20%3C!--%20wp%3Aheading%20--%3E%3C%2FH2%3E%0A%3CH2%20id%3D%22toc-hId-1977690779%22%20id%3D%22toc-hId-1977690779%22%3E%26nbsp%3B%3C%2FH2%3E%0A%3CH2%20id%3D%22toc-hId--574466182%22%20id%3D%22toc-hId--574466182%22%3ECreate%20Availability%20Group%20Code%3C%2FH2%3E%0A%3CH2%20id%3D%22toc-hId-1168344153%22%20id%3D%22toc-hId-1168344153%22%3E%3C!--%20%2Fwp%3Aheading%20--%3E%20%3C!--%20wp%3Asyntaxhighlighter%2Fcode%20%7B%22language%22%3A%22sql%22%7D%20--%3E%3C%2FH2%3E%0A%3CPRE%20class%3D%22wp-block-syntaxhighlighter-code%22%3E%2F*%0AAuthor%3A%20Ryan%20Adams%0AWebsite%3A%20https%3A%2F%2Fwww.ryanjadams.com%0ATwitter%3A%20%40ryanjadams%0A%0AThis%20script%20must%20be%20executed%20in%20SQLCMD%20mode.%20%20This%20script%20was%20designed%20to%20setup%20an%20AlwaysOn%20Availability%20Group%0Ain%20a%20custom%20lab%20environment.%20%20Use%20at%20your%20own%20risk%20and%20DO%20NOT%20run%20this%20in%20production.%20%20Make%20sure%20you%20read%20and%20understand%0Ait%20thoroughly.%0A*%2F%0A%0A%2F*%20First%20we%20connect%20to%20each%20replica%20in%20order%20and%20create%20logins%20for%20the%20account%20running%20the%20SQL%20Server%20Service%20on%20the%20other%20replicas%20*%2F%0A%3AConnect%20NODE1%0A%0AUSE%20%5Bmaster%5D%0AGO%0A%0ACREATE%20LOGIN%20%5Bcontoso%5Csvc-sql2%5D%20FROM%20WINDOWS%0AGO%0A%0ACREATE%20LOGIN%20%5Bcontoso%5Csvc-sql3%5D%20FROM%20WINDOWS%0AGO%0A%0A%0A%3AConnect%20NODE2%0A%0AUSE%20%5Bmaster%5D%0AGO%0A%0ACREATE%20LOGIN%20%5Bcontoso%5Csvc-sql1%5D%20FROM%20WINDOWS%0AGO%0A%0ACREATE%20LOGIN%20%5Bcontoso%5Csvc-sql3%5D%20FROM%20WINDOWS%0AGO%0A%0A%0A%3AConnect%20NODE3%0A%0AUSE%20%5Bmaster%5D%3B%0AGO%0A%0ACREATE%20LOGIN%20%5Bcontoso%5Csvc-sql1%5D%20FROM%20WINDOWS%3B%0AGO%0A%0ACREATE%20LOGIN%20%5Bcontoso%5Csvc-sql2%5D%20FROM%20WINDOWS%3B%0AGO%0A%0A%2F*%20Next%20we%20connect%20to%20each%20replica%20and%20create%20the%20endpoint.%0AWe%20also%20grant%20connect%20permission%20on%20the%20endpoint%20to%20all%20of%20the%20other%20replica%20service%20accounts.%20*%2F%0A%3AConnect%20NODE1%0A%0AUSE%20%5Bmaster%5D%3B%0AGO%0A%0ACREATE%20ENDPOINT%20%5BAlwaysOn_EP%5D%0A%20STATE%20%3D%20STARTED%0A%20AS%20TCP%20(LISTENER_PORT%20%3D%205022%2C%20LISTENER_IP%20%3D%20(10.0.1.1))%20%2F*%20Specify%20an%20IP%20value%20for%20LISTENER_IP%20to%20dedicate%20AlwaysOn%20traffic%20to%20its%20own%20NIC%20card%20*%2F%0A%20FOR%20DATABASE_MIRRORING%20(ROLE%20%3D%20ALL%20%2F*%20Cannot%20use%20Witness%20as%20that%20is%20for%20mirroring%20only.%20%20Partner%20will%20work%20but%20that's%20really%20just%20for%20mirroring%20terminology%20*%2F%0A%20%20%2C%20AUTHENTICATION%20%3D%20WINDOWS%20NEGOTIATE%20%2F*%20Authentication%20can%20be%20Windows%20or%20Certificate%20of%20a%20combination%20in%20preference%20order.%20%20Authorization%20can%20force%20NTLM%20or%20Kerberos.%20These%20are%20default%20values%20*%2F%0A%20%20%2C%20ENCRYPTION%20%3D%20REQUIRED%20ALGORITHM%20AES)%3B%20%2F*%20Default%20algorithm%20is%20RC4%2C%20however%20RC4%20is%20marked%20depracated.%20Use%20AES%20(GUI%20uses%20AES%20by%20default%2C%20but%20TSQL%20does%20not)%20*%2F%0AGO%0A%0AGRANT%20CONNECT%20ON%20ENDPOINT%3A%3A%5BAlwaysOn_EP%5D%20TO%20%5Bcontoso%5Csvc-sql2%5D%3B%0AGO%0A%0AGRANT%20CONNECT%20ON%20ENDPOINT%3A%3A%5BAlwaysOn_EP%5D%20TO%20%5Bcontoso%5Csvc-sql3%5D%3B%0AGO%0A%0A%3AConnect%20NODE2%0A%0AUSE%20%5Bmaster%5D%3B%0AGO%0A%0ACREATE%20ENDPOINT%20%5BAlwaysOn_EP%5D%0A%20STATE%20%3D%20STARTED%0A%20AS%20TCP%20(LISTENER_PORT%20%3D%205022%2C%20LISTENER_IP%20%3D%20(10.0.1.2))%20%2F*%20Specify%20an%20IP%20value%20for%20LISTENER_IP%20to%20dedicate%20AlwaysOn%20traffic%20to%20it's%20own%20NIC%20card%20*%2F%0A%20FOR%20DATABASE_MIRRORING%20(ROLE%20%3D%20ALL%20%2F*%20Cannot%20use%20Witness%20as%20that%20is%20for%20mirroring%20only.%20%20Partner%20will%20work%20but%20that's%20really%20just%20for%20mirroring%20terminology%20*%2F%0A%20%20%2C%20AUTHENTICATION%20%3D%20WINDOWS%20NEGOTIATE%20%2F*%20Authentication%20can%20be%20Windows%20or%20Certificate%20of%20a%20combination%20in%20preference%20order.%20%20Authorization%20can%20force%20NTLM%20or%20Kerberos.%20These%20are%20default%20values%20*%2F%0A%20%20%2C%20ENCRYPTION%20%3D%20REQUIRED%20ALGORITHM%20AES)%3B%20%2F*%20Default%20algorithm%20is%20RC4%2C%20however%20RC4%20is%20marked%20depracated.%20Use%20AES%20(GUI%20uses%20AES%20by%20default%2C%20but%20TSQL%20does%20not)%20*%2F%0AGO%0A%0AGRANT%20CONNECT%20ON%20ENDPOINT%3A%3A%5BAlwaysOn_EP%5D%20TO%20%5Bcontoso%5Csvc-sql1%5D%3B%0AGO%0A%0AGRANT%20CONNECT%20ON%20ENDPOINT%3A%3A%5BAlwaysOn_EP%5D%20TO%20%5Bcontoso%5Csvc-sql3%5D%3B%0AGO%0A%0A%0A%3AConnect%20NODE3%0A%0AUSE%20%5Bmaster%5D%3B%0AGO%0A%0ACREATE%20ENDPOINT%20%5BAlwaysOn_EP%5D%0A%20STATE%20%3D%20STARTED%0A%20AS%20TCP%20(LISTENER_PORT%20%3D%205022%2C%20LISTENER_IP%20%3D%20(172.18.0.3))%20%2F*%20Specify%20an%20IP%20value%20for%20LISTENER_IP%20to%20dedicate%20AlwaysOn%20traffic%20to%20it's%20own%20NIC%20card%20*%2F%0A%20FOR%20DATABASE_MIRRORING%20(ROLE%20%3D%20ALL%20%2F*%20Cannot%20use%20Witness%20as%20that%20is%20for%20mirroring%20only.%20%20Partner%20will%20work%20but%20that's%20really%20just%20for%20mirroring%20terminology%20*%2F%0A%20%20%2C%20AUTHENTICATION%20%3D%20WINDOWS%20NEGOTIATE%20%2F*%20Authentication%20can%20be%20Windows%20or%20Certificate%20of%20a%20combination%20in%20preference%20order.%20%20Authorization%20can%20force%20NTLM%20or%20Kerberos.%20These%20are%20default%20values%20*%2F%0A%20%20%2C%20ENCRYPTION%20%3D%20REQUIRED%20ALGORITHM%20AES)%3B%20%2F*%20Default%20algorithm%20is%20RC4%2C%20however%20RC4%20is%20marked%20depracated.%20Use%20AES%20(GUI%20uses%20AES%20by%20default%2C%20but%20TSQL%20does%20not)%20*%2F%0AGO%0A%0AGRANT%20CONNECT%20ON%20ENDPOINT%3A%3A%5BAlwaysOn_EP%5D%20TO%20%5Bcontoso%5Csvc-sql1%5D%3B%0AGO%0A%0AGRANT%20CONNECT%20ON%20ENDPOINT%3A%3A%5BAlwaysOn_EP%5D%20TO%20%5Bcontoso%5Csvc-sql2%5D%3B%0AGO%0A%0A%2F*%20Here%20we%20connect%20to%20each%20replica%20and%20ensure%20that%20the%20AlwaysOn%20extended%20events%20session%20is%20set%20to%20run%20at%20startup%20and%20that%20it%20is%20also%20started%20*%2F%0A%3AConnect%20NODE1%0A%0AIF%20EXISTS(SELECT%20*%20FROM%20sys.server_event_sessions%20WHERE%20name%3D'AlwaysOn_health')%0ABEGIN%0A%20%20ALTER%20EVENT%20SESSION%20%5BAlwaysOn_health%5D%20ON%20SERVER%20WITH%20(STARTUP_STATE%3DON)%3B%0AEND%0A%0AIF%20NOT%20EXISTS(SELECT%20*%20FROM%20sys.dm_xe_sessions%20WHERE%20name%3D'AlwaysOn_health')%0ABEGIN%0A%20%20ALTER%20EVENT%20SESSION%20%5BAlwaysOn_health%5D%20ON%20SERVER%20STATE%3DSTART%3B%0AEND%0A%0AGO%0A%0A%3AConnect%20NODE2%0A%0AIF%20EXISTS(SELECT%20*%20FROM%20sys.server_event_sessions%20WHERE%20name%3D'AlwaysOn_health')%0ABEGIN%0A%20%20ALTER%20EVENT%20SESSION%20%5BAlwaysOn_health%5D%20ON%20SERVER%20WITH%20(STARTUP_STATE%3DON)%3B%0AEND%0A%0AIF%20NOT%20EXISTS(SELECT%20*%20FROM%20sys.dm_xe_sessions%20WHERE%20name%3D'AlwaysOn_health')%0ABEGIN%0A%20%20ALTER%20EVENT%20SESSION%20%5BAlwaysOn_health%5D%20ON%20SERVER%20STATE%3DSTART%3B%0AEND%0A%0AGO%0A%0A%3AConnect%20NODE3%0A%0AIF%20EXISTS(SELECT%20*%20FROM%20sys.server_event_sessions%20WHERE%20name%3D'AlwaysOn_health')%0ABEGIN%0A%20%20ALTER%20EVENT%20SESSION%20%5BAlwaysOn_health%5D%20ON%20SERVER%20WITH%20(STARTUP_STATE%3DON)%3B%0AEND%0A%0AIF%20NOT%20EXISTS(SELECT%20*%20FROM%20sys.dm_xe_sessions%20WHERE%20name%3D'AlwaysOn_health')%0ABEGIN%0A%20%20ALTER%20EVENT%20SESSION%20%5BAlwaysOn_health%5D%20ON%20SERVER%20STATE%3DSTART%3B%0AEND%0A%0AGO%0A%0A%2F*%0AThere%20will%20be%203%20databases%20included%20in%20our%20AG%20and%20they%20currently%20exist%20on%20NODE1.%0APrior%20to%20SQL%202016%20we%20would%20have%20to%20do%20the%20following%3A%0A%20Backup%20each%20database%20data%20file%20to%20a%20share%20and%20then%20restore%20it%20on%20each%20replica%20making%20sure%20to%20use%20the%20%22NORECOVERY%22%20clause%0A%20Backup%20each%20database%20log%20file%20to%20a%20share%20and%20then%20restore%20it%20on%20each%20replica%20making%20sure%20to%20use%20the%20%22NORECOVERY%22%20clause%0A%0AWith%20SQL%202016%2B%20we%20can%20use%20Direct%20Seeding%20instead.%0A*%2F%0A%0A%2F*%20Here%20we%20connect%20to%20our%20primary%20replica%20(NODE1)%20and%20create%20our%20AG.%20*%2F%0A%3AConnect%20NODE1%0A%0A%2F*%20We%20can%20use%20trace%20flag%209567%20to%20enable%20compression%20for%20the%20VDI%20backup%20for%20the%20seeding%20process%20*%2F%0ADBCC%20TRACEON%20(9567%2C%20-1)%3B%0AGO%0A%0AUSE%20%5Bmaster%5D%3B%0AGO%0A%0ACREATE%20AVAILABILITY%20GROUP%20%5BApp1AG%5D%0AWITH%20(AUTOMATED_BACKUP_PREFERENCE%20%3D%20SECONDARY%2C%20FAILURE_CONDITION_LEVEL%20%3D%203%2C%20HEALTH_CHECK_TIMEOUT%20%3D%2030000%2C%20DB_FAILOVER%20%3D%20ON%2C%20DTC_SUPPORT%20%3D%20PER_DB%2C%20CLUSTER_TYPE%20%3D%20WSFC)%20%2F*%20You%20can%20also%20use%20SECONDARY_ONLY%2C%20PRIMARY%2C%20or%20NONE%0AFailure%20condition%20levels%20are%20from%20the%20least%20restrcitive%20of%201(SQL%20Service%20is%20down)%20to%205(Exhaustion%20of%20worker%20threads).%203%20is%20the%20default.%0AHealth%20check%20default%20is%2030%20seconds%2C%20represented%20in%20milliseconds.%20This%20is%20how%20long%20we%20wait%20for%20sp_server_diagnostics%20to%20return.%20*%2F%0AFOR%20DATABASE%20%5BApp1AG_DB1%5D%2C%20%5BApp1AG_DB2%5D%2C%20%5BApp1AG_DB3%5D%0AREPLICA%20ON%20N'NODE1'%20WITH%20(ENDPOINT_URL%20%3D%20N'TCP%3A%2F%2FNODE1.contoso.com%3A5022'%2C%20FAILOVER_MODE%20%3D%20AUTOMATIC%2C%20AVAILABILITY_MODE%20%3D%20SYNCHRONOUS_COMMIT%2C%20BACKUP_PRIORITY%20%3D%2050%2C%20SECONDARY_ROLE(ALLOW_CONNECTIONS%20%3D%20ALL)%2C%20SESSION_TIMEOUT%20%3D%2010%2C%20SEEDING_MODE%20%3D%20AUTOMATIC)%2C%0A%20N'NODE2'%20WITH%20(ENDPOINT_URL%20%3D%20N'TCP%3A%2F%2FNODE2.contoso.com%3A5022'%2C%20FAILOVER_MODE%20%3D%20AUTOMATIC%2C%20AVAILABILITY_MODE%20%3D%20SYNCHRONOUS_COMMIT%2C%20BACKUP_PRIORITY%20%3D%2060%2C%20SECONDARY_ROLE(ALLOW_CONNECTIONS%20%3D%20ALL)%2C%20SESSION_TIMEOUT%20%3D%2010%2C%20SEEDING_MODE%20%3D%20AUTOMATIC)%2C%0A%20N'NODE3'%20WITH%20(ENDPOINT_URL%20%3D%20N'TCP%3A%2F%2FNODE3.contoso.com%3A5022'%2C%20FAILOVER_MODE%20%3D%20MANUAL%2C%20AVAILABILITY_MODE%20%3D%20SYNCHRONOUS_COMMIT%2C%20BACKUP_PRIORITY%20%3D%2070%2C%20SECONDARY_ROLE(ALLOW_CONNECTIONS%20%3D%20READ_ONLY)%2C%20SESSION_TIMEOUT%20%3D%2010%2C%20SEEDING_MODE%20%3D%20AUTOMATIC)%3B%0AGO%0A%0A%2F*%20Here%20we%20create%20a%20listener%20for%20our%20AG.%20If%20you%20have%20issues%20creating%20the%20listener%20check%20permissions%20in%20AD.%0A%20You%20might%20also%20have%20to%20turn%20the%20AG%20networks%20to%20client%20and%20cluster%20and%20then%20turn%20them%20back%20to%20none%20post-listener%20creation*%2F%0AALTER%20AVAILABILITY%20GROUP%20%5BApp1AG%5D%0AADD%20LISTENER%20N'App1AG'%20(%0AWITH%20IP%0A((N'10.0.0.11'%2C%20N'255.255.255.0')%2C('172.16.0.11'%2C'255.240.0.0'))%0A%2C%20PORT%3D1433)%3B%0AGO%0A%0A%2F*%20Now%20that%20the%20AG%20exists%20we%20connect%20to%20each%20secondary%20replica%20and%20join%20it%20to%20the%20group%0A%20We%20also%20have%20to%20grant%20the%20CREATE%20ANY%20DATABASE%20permission%20so%20Seeding%20can%20create%20the%20DBs%20*%2F%0A%3AConnect%20NODE2%0A%0AALTER%20AVAILABILITY%20GROUP%20%5BApp1AG%5D%20JOIN%3B%0AGO%0AALTER%20AVAILABILITY%20GROUP%20%5BApp1AG%5D%20GRANT%20CREATE%20ANY%20DATABASE%3B%0AGO%0A%0A%3AConnect%20NODE3%0A%0AALTER%20AVAILABILITY%20GROUP%20%5BApp1AG%5D%20JOIN%3B%0AGO%0AALTER%20AVAILABILITY%20GROUP%20%5BApp1AG%5D%20GRANT%20CREATE%20ANY%20DATABASE%3B%0AGO%0A%0A--This%20query%20allows%20us%20to%20view%20seeding%20performance%20statistics%0ASELECT%20*%20FROM%20sys.dm_hadr_physical_seeding_stats%3B%0A%0A%2F*%20Now%20we%20need%20to%20turn%20our%20trace%20flag%20back%20off%20*%2F%0A%3AConnect%20NODE1%0ADBCC%20TRACEOFF%20(9567%2C%20-1)%3B%0AGO%3C%2FPRE%3E%0A%3CH2%20id%3D%22toc-hId--1383812808%22%20id%3D%22toc-hId--1383812808%22%3E%3C!--%20%2Fwp%3Asyntaxhighlighter%2Fcode%20--%3E%3C%2FH2%3E%0A%3CH2%20id%3D%22toc-hId-358997527%22%20id%3D%22toc-hId-358997527%22%3E%26nbsp%3B%3C%2FH2%3E%0A%3CH2%20id%3D%22toc-hId--1307882869%22%20id%3D%22toc-hId--1307882869%22%3ENetwork%20Configuration%3C%2FH2%3E%0A%3CP%3E%3C!--%20%2Fwp%3Aheading%20--%3E%20%3C!--%20wp%3Aparagraph%20--%3E%3C%2FP%3E%0A%3CP%3EHere%20is%20the%20list%20of%20IP%20addresses%20from%20%3CA%20href%3D%22https%3A%2F%2Fwww.ryanjadams.com%2F2019%2F10%2Fbuild-a-sql-cluster-lab-part-1%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3EPart%201%3C%2FA%3E%20that%20you%20will%20need%20to%20configure%20the%20AG.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CTABLE%20class%3D%22wp-block-table%22%3E%0A%3CTBODY%3E%0A%3CTR%3E%0A%3CTD%20class%3D%22%22%3E%0A%3CDIV%20class%3D%22editor-rich-text%20block-editor-rich-text%22%3E%0A%3CDIV%3E%0A%3CDIV%3E%0A%3CDIV%20class%3D%22components-autocomplete%22%3E%0A%3CDIV%20class%3D%22block-editor-rich-text__editable%20editor-rich-text__editable%20wp-block-table__cell-content%22%20role%3D%22textbox%22%20contenteditable%3D%22false%22%20aria-multiline%3D%22true%22%20aria-autocomplete%3D%22list%22%20data-is-placeholder-visible%3D%22false%22%3E%26nbsp%3B%3C%2FDIV%3E%0A%3C%2FDIV%3E%0A%3C%2FDIV%3E%0A%3C%2FDIV%3E%0A%3C%2FDIV%3E%0A%3C%2FTD%3E%0A%3CTD%20class%3D%22%22%3E%0A%3CDIV%20class%3D%22editor-rich-text%20block-editor-rich-text%22%3E%0A%3CDIV%3E%0A%3CDIV%3E%0A%3CDIV%20class%3D%22components-autocomplete%22%3E%0A%3CDIV%20class%3D%22block-editor-rich-text__editable%20editor-rich-text__editable%20wp-block-table__cell-content%22%20role%3D%22textbox%22%20contenteditable%3D%22false%22%20aria-multiline%3D%22true%22%20aria-autocomplete%3D%22list%22%20data-is-placeholder-visible%3D%22false%22%3E%3CSTRONG%3EData%20Center%201%20IP%3C%2FSTRONG%3E%3C%2FDIV%3E%0A%3C%2FDIV%3E%0A%3C%2FDIV%3E%0A%3C%2FDIV%3E%0A%3C%2FDIV%3E%0A%3C%2FTD%3E%0A%3CTD%20class%3D%22%22%3E%0A%3CDIV%20class%3D%22editor-rich-text%20block-editor-rich-text%22%3E%0A%3CDIV%3E%0A%3CDIV%3E%0A%3CDIV%20class%3D%22components-autocomplete%22%3E%0A%3CDIV%20class%3D%22block-editor-rich-text__editable%20editor-rich-text__editable%20wp-block-table__cell-content%22%20role%3D%22textbox%22%20contenteditable%3D%22false%22%20aria-multiline%3D%22true%22%20aria-autocomplete%3D%22list%22%20data-is-placeholder-visible%3D%22false%22%3E%3CSTRONG%3EData%20Center%202%20IP%3C%2FSTRONG%3E%3C%2FDIV%3E%0A%3C%2FDIV%3E%0A%3C%2FDIV%3E%0A%3C%2FDIV%3E%0A%3C%2FDIV%3E%0A%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%20class%3D%22%22%3E%0A%3CDIV%20class%3D%22editor-rich-text%20block-editor-rich-text%22%3E%0A%3CDIV%3E%0A%3CDIV%3E%0A%3CDIV%20class%3D%22components-autocomplete%22%3E%0A%3CDIV%20class%3D%22block-editor-rich-text__editable%20editor-rich-text__editable%20wp-block-table__cell-content%22%20role%3D%22textbox%22%20contenteditable%3D%22false%22%20aria-multiline%3D%22true%22%20aria-autocomplete%3D%22list%22%20data-is-placeholder-visible%3D%22false%22%3ECluster%3C%2FDIV%3E%0A%3C%2FDIV%3E%0A%3C%2FDIV%3E%0A%3C%2FDIV%3E%0A%3C%2FDIV%3E%0A%3C%2FTD%3E%0A%3CTD%20class%3D%22%22%3E%0A%3CDIV%20class%3D%22editor-rich-text%20block-editor-rich-text%22%3E%0A%3CDIV%3E%0A%3CDIV%3E%0A%3CDIV%20class%3D%22components-autocomplete%22%3E%0A%3CDIV%20class%3D%22block-editor-rich-text__editable%20editor-rich-text__editable%20wp-block-table__cell-content%22%20role%3D%22textbox%22%20contenteditable%3D%22false%22%20aria-multiline%3D%22true%22%20aria-autocomplete%3D%22list%22%20data-is-placeholder-visible%3D%22false%22%3E10.0.0.10%3C%2FDIV%3E%0A%3C%2FDIV%3E%0A%3C%2FDIV%3E%0A%3C%2FDIV%3E%0A%3C%2FDIV%3E%0A%3C%2FTD%3E%0A%3CTD%20class%3D%22%22%3E%0A%3CDIV%20class%3D%22editor-rich-text%20block-editor-rich-text%22%3E%0A%3CDIV%3E%0A%3CDIV%3E%0A%3CDIV%20class%3D%22components-autocomplete%22%3E%0A%3CDIV%20class%3D%22block-editor-rich-text__editable%20editor-rich-text__editable%20wp-block-table__cell-content%22%20role%3D%22textbox%22%20contenteditable%3D%22false%22%20aria-multiline%3D%22true%22%20aria-autocomplete%3D%22list%22%20data-is-placeholder-visible%3D%22false%22%3E172.16.0.10%3C%2FDIV%3E%0A%3C%2FDIV%3E%0A%3C%2FDIV%3E%0A%3C%2FDIV%3E%0A%3C%2FDIV%3E%0A%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%20class%3D%22%22%3E%0A%3CDIV%20class%3D%22editor-rich-text%20block-editor-rich-text%22%3E%0A%3CDIV%3E%0A%3CDIV%3E%0A%3CDIV%20class%3D%22components-autocomplete%22%3E%0A%3CDIV%20class%3D%22block-editor-rich-text__editable%20editor-rich-text__editable%20wp-block-table__cell-content%22%20role%3D%22textbox%22%20contenteditable%3D%22false%22%20aria-multiline%3D%22true%22%20aria-autocomplete%3D%22list%22%20data-is-placeholder-visible%3D%22false%22%3EAG%20Listener%3C%2FDIV%3E%0A%3C%2FDIV%3E%0A%3C%2FDIV%3E%0A%3C%2FDIV%3E%0A%3C%2FDIV%3E%0A%3C%2FTD%3E%0A%3CTD%20class%3D%22%22%3E%0A%3CDIV%20class%3D%22editor-rich-text%20block-editor-rich-text%22%3E%0A%3CDIV%3E%0A%3CDIV%3E%0A%3CDIV%20class%3D%22components-autocomplete%22%3E%0A%3CDIV%20class%3D%22block-editor-rich-text__editable%20editor-rich-text__editable%20wp-block-table__cell-content%22%20role%3D%22textbox%22%20contenteditable%3D%22false%22%20aria-multiline%3D%22true%22%20aria-autocomplete%3D%22list%22%20data-is-placeholder-visible%3D%22false%22%3E10.0.0.11%3C%2FDIV%3E%0A%3C%2FDIV%3E%0A%3C%2FDIV%3E%0A%3C%2FDIV%3E%0A%3C%2FDIV%3E%0A%3C%2FTD%3E%0A%3CTD%20class%3D%22%22%3E%0A%3CDIV%20class%3D%22editor-rich-text%20block-editor-rich-text%22%3E%0A%3CDIV%3E%0A%3CDIV%3E%0A%3CDIV%20class%3D%22components-autocomplete%22%3E%0A%3CDIV%20class%3D%22block-editor-rich-text__editable%20editor-rich-text__editable%20wp-block-table__cell-content%22%20role%3D%22textbox%22%20contenteditable%3D%22false%22%20aria-multiline%3D%22true%22%20aria-autocomplete%3D%22list%22%20data-is-placeholder-visible%3D%22false%22%3E172.16.0.11%3C%2FDIV%3E%0A%3C%2FDIV%3E%0A%3C%2FDIV%3E%0A%3C%2FDIV%3E%0A%3C%2FDIV%3E%0A%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%20class%3D%22%22%3E%0A%3CDIV%20class%3D%22editor-rich-text%20block-editor-rich-text%22%3E%0A%3CDIV%3E%0A%3CDIV%3E%0A%3CDIV%20class%3D%22components-autocomplete%22%3E%0A%3CDIV%20class%3D%22block-editor-rich-text__editable%20editor-rich-text__editable%20wp-block-table__cell-content%22%20role%3D%22textbox%22%20contenteditable%3D%22false%22%20aria-multiline%3D%22true%22%20aria-autocomplete%3D%22list%22%20data-is-placeholder-visible%3D%22false%22%3EFCI%3C%2FDIV%3E%0A%3C%2FDIV%3E%0A%3C%2FDIV%3E%0A%3C%2FDIV%3E%0A%3C%2FDIV%3E%0A%3C%2FTD%3E%0A%3CTD%20class%3D%22%22%3E%0A%3CDIV%20class%3D%22editor-rich-text%20block-editor-rich-text%22%3E%0A%3CDIV%3E%0A%3CDIV%3E%0A%3CDIV%20class%3D%22components-autocomplete%22%3E%0A%3CDIV%20class%3D%22block-editor-rich-text__editable%20editor-rich-text__editable%20wp-block-table__cell-content%22%20role%3D%22textbox%22%20contenteditable%3D%22false%22%20aria-multiline%3D%22true%22%20aria-autocomplete%3D%22list%22%20data-is-placeholder-visible%3D%22false%22%3E10.0.0.15%3C%2FDIV%3E%0A%3C%2FDIV%3E%0A%3C%2FDIV%3E%0A%3C%2FDIV%3E%0A%3C%2FDIV%3E%0A%3C%2FTD%3E%0A%3CTD%20class%3D%22%22%3E%0A%3CDIV%20class%3D%22editor-rich-text%20block-editor-rich-text%22%3E%0A%3CDIV%3E%0A%3CDIV%3E%0A%3CDIV%20class%3D%22components-autocomplete%22%3E%0A%3CDIV%20class%3D%22block-editor-rich-text__editable%20editor-rich-text__editable%20wp-block-table__cell-content%22%20role%3D%22textbox%22%20contenteditable%3D%22false%22%20aria-multiline%3D%22true%22%20aria-autocomplete%3D%22list%22%20data-is-placeholder-visible%3D%22false%22%3EN%2FA%3C%2FDIV%3E%0A%3C%2FDIV%3E%0A%3C%2FDIV%3E%0A%3C%2FDIV%3E%0A%3C%2FDIV%3E%0A%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3C%2FTBODY%3E%0A%3C%2FTABLE%3E%0A%3CP%3E%3C!--%20%2Fwp%3Aparagraph%20--%3E%20%3C!--%20wp%3Ablock%20%7B%22ref%22%3A2261%7D%20%2F--%3E%20%3C!--%20wp%3Aheading%20--%3E%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId-434927466%22%20id%3D%22toc-hId-434927466%22%3E%26nbsp%3B%3C%2FH2%3E%0A%3CP%3E%3C!--%20%2Fwp%3Aparagraph%20--%3E%20%3C!--%20wp%3Ablock%20%7B%22ref%22%3A2261%7D%20%2F--%3E%20%3C!--%20wp%3Aheading%20--%3E%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId--2117229495%22%20id%3D%22toc-hId--2117229495%22%3EArchitecture%3C%2FH2%3E%0A%3CP%3E%3C!--%20%2Fwp%3Aheading%20--%3E%20%3C!--%20wp%3Aparagraph%20--%3E%3C%2FP%3E%0A%3CP%3EAs%20a%20refresher%2C%20here%20is%20the%20diagram%20of%20the%20lab%20you%20are%20building.%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F154745i4B45D4B6B3A29602%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20alt%3D%22Lab%20Architecture.png%22%20title%3D%22Lab%20Architecture.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%3C!--%20%2Fwp%3Aparagraph%20--%3E%20%3C!--%20wp%3Aimage%20%7B%22id%22%3A2274%2C%22linkDestination%22%3A%22media%22%7D%20--%3E%3C%2FP%3E%0A%3CP%3E%3C!--%20%2Fwp%3Aimage%20--%3E%20%3C!--%20wp%3Aheading%20--%3E%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId--374419160%22%20id%3D%22toc-hId--374419160%22%3EBuild%20Availability%20Group%20Video%3C%2FH2%3E%0A%3CP%3E%3C!--%20%2Fwp%3Aheading%20--%3E%20%3C!--%20wp%3Acore-embed%2Fyoutube%20%7B%22url%22%3A%22https%3A%2F%2Fyoutu.be%2FBn4cSfazwg0%22%2C%22type%22%3A%22video%22%2C%22providerNameSlug%22%3A%22youtube%22%2C%22className%22%3A%22wp-embed-aspect-16-9%20wp-has-aspect-ratio%22%7D%20--%3E%3C%2FP%3E%0A%3CFIGURE%20class%3D%22wp-block-embed-youtube%20wp-block-embed%20is-type-video%20is-provider-youtube%20wp-embed-aspect-16-9%20wp-has-aspect-ratio%22%3E%0A%3CDIV%20class%3D%22wp-block-embed__wrapper%22%3E%3CDIV%20class%3D%22video-embed-center%20video-embed%22%3E%3CIFRAME%20class%3D%22embedly-embed%22%20src%3D%22https%3A%2F%2Fcdn.embedly.com%2Fwidgets%2Fmedia.html%3Fsrc%3Dhttps%253A%252F%252Fwww.youtube.com%252Fembed%252FBn4cSfazwg0%253Ffeature%253Doembed%26amp%3Burl%3Dhttps%253A%252F%252Fwww.youtube.com%252Fwatch%253Fv%253DBn4cSfazwg0%26amp%3Bimage%3Dhttps%253A%252F%252Fi.ytimg.com%252Fvi%252FBn4cSfazwg0%252Fhqdefault.jpg%26amp%3Bkey%3Dfad07bfa4bd747d3bdea27e17b533c0e%26amp%3Btype%3Dtext%252Fhtml%26amp%3Bschema%3Dyoutube%22%20width%3D%22600%22%20height%3D%22337%22%20scrolling%3D%22no%22%20frameborder%3D%220%22%20allow%3D%22autoplay%3B%20fullscreen%22%20allowfullscreen%3D%22true%22%20title%3D%22Video%22%3E%3C%2FIFRAME%3E%3C%2FDIV%3E%3C%2FDIV%3E%0A%3C%2FFIGURE%3E%0A%3CP%3E%3C!--%20%2Fwp%3Acore-embed%2Fyoutube%20--%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-981679%22%20slang%3D%22en-US%22%3E%3CP%3EIn%20Part%203%20you%20create%20a%20multi-subnet%20Availability%20Group.%20You%20learn%20CNO%20permissions%20and%20also%20how%20to%20dedicate%20your%20AG%20network%20traffic.%3C%2FP%3E%3C%2FLINGO-TEASER%3E%3CLINGO-LABS%20id%3D%22lingo-labs-981679%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3ERyanAdams%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Microsoft

You are going to create a multi-subnet Availability Group in Part 3 of our series on how to build a SQL Cluster Lab. First you give the Cluster Name Object rights in Active Directory and then install your AG. You will also ensure the AG endpoints are using the dedicated network created for them in Part 1. Here are links to the other articles in the series.

 

 

Create Databases Code

USE [master];
GO

DROP DATABASE IF EXISTS App1AG_DB1;
GO

DROP DATABASE IF EXISTS App1AG_DB2;
GO

DROP DATABASE IF EXISTS App1AG_DB3;
GO

CREATE DATABASE [App1AG_DB1]
 ON  PRIMARY 
( NAME = N'App1AG_DB1', FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL14.MSSQLSERVER\MSSQL\DATA\App1AG_DB1.mdf' , SIZE = 3072KB , MAXSIZE = UNLIMITED, FILEGROWTH = 1024KB )
 LOG ON 
( NAME = N'App1AG_DB1_log', FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL14.MSSQLSERVER\MSSQL\DATA\App1AG_DB1_log.ldf' , SIZE = 1024KB , MAXSIZE = 2048GB , FILEGROWTH = 1024KB)
GO

CREATE DATABASE [App1AG_DB2]
 ON  PRIMARY 
( NAME = N'App1AG_DB2', FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL14.MSSQLSERVER\MSSQL\DATA\App1AG_DB2.mdf' , SIZE = 3072KB , MAXSIZE = UNLIMITED, FILEGROWTH = 1024KB )
 LOG ON 
( NAME = N'App1AG_DB2_log', FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL14.MSSQLSERVER\MSSQL\DATA\App1AG_DB2_log.ldf' , SIZE = 1024KB , MAXSIZE = 2048GB , FILEGROWTH = 1024KB)
GO

CREATE DATABASE [App1AG_DB3]
 ON  PRIMARY 
( NAME = N'App1AG_DB3', FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL14.MSSQLSERVER\MSSQL\DATA\App1AG_DB3.mdf' , SIZE = 3072KB , MAXSIZE = UNLIMITED, FILEGROWTH = 1024KB )
 LOG ON 
( NAME = N'App1AG_DB3_log', FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL14.MSSQLSERVER\MSSQL\DATA\App1AG_DB3_log.ldf' , SIZE = 1024KB , MAXSIZE = 2048GB , FILEGROWTH = 1024KB)
GO


--Full backup required to create an AG
BACKUP DATABASE [App1AG_DB1] TO  DISK = N'C:\Program Files\Microsoft SQL Server\MSSQL14.MSSQLSERVER\MSSQL\Backup\App1AG_DB1.bak' WITH FORMAT, INIT,  SKIP, NOREWIND, NOUNLOAD,  STATS = 10, CHECKSUM
GO

BACKUP DATABASE [App1AG_DB2] TO  DISK = N'C:\Program Files\Microsoft SQL Server\MSSQL14.MSSQLSERVER\MSSQL\Backup\App1AG_DB2.bak' WITH FORMAT, INIT,  SKIP, NOREWIND, NOUNLOAD,  STATS = 10, CHECKSUM
GO

BACKUP DATABASE [App1AG_DB3] TO  DISK = N'C:\Program Files\Microsoft SQL Server\MSSQL14.MSSQLSERVER\MSSQL\Backup\App1AG_DB3.bak' WITH FORMAT, INIT,  SKIP, NOREWIND, NOUNLOAD,  STATS = 10, CHECKSUM
GO

 

Create Availability Group Code

/*
Author: Ryan Adams
Website: https://www.ryanjadams.com
Twitter: @ryanjadams

This script must be executed in SQLCMD mode.  This script was designed to setup an AlwaysOn Availability Group
in a custom lab environment.  Use at your own risk and DO NOT run this in production.  Make sure you read and understand
it thoroughly.
*/

/* First we connect to each replica in order and create logins for the account running the SQL Server Service on the other replicas */
:Connect NODE1

USE [master]
GO

CREATE LOGIN [contoso\svc-sql2] FROM WINDOWS
GO

CREATE LOGIN [contoso\svc-sql3] FROM WINDOWS
GO


:Connect NODE2

USE [master]
GO

CREATE LOGIN [contoso\svc-sql1] FROM WINDOWS
GO

CREATE LOGIN [contoso\svc-sql3] FROM WINDOWS
GO


:Connect NODE3

USE [master];
GO

CREATE LOGIN [contoso\svc-sql1] FROM WINDOWS;
GO

CREATE LOGIN [contoso\svc-sql2] FROM WINDOWS;
GO

/* Next we connect to each replica and create the endpoint.
We also grant connect permission on the endpoint to all of the other replica service accounts. */
:Connect NODE1

USE [master];
GO

CREATE ENDPOINT [AlwaysOn_EP]
	STATE = STARTED
	AS TCP (LISTENER_PORT = 5022, LISTENER_IP = (10.0.1.1)) /* Specify an IP value for LISTENER_IP to dedicate AlwaysOn traffic to its own NIC card */
	FOR DATABASE_MIRRORING (ROLE = ALL /* Cannot use Witness as that is for mirroring only.  Partner will work but that's really just for mirroring terminology */
		, AUTHENTICATION = WINDOWS NEGOTIATE /* Authentication can be Windows or Certificate of a combination in preference order.  Authorization can force NTLM or Kerberos. These are default values */
		, ENCRYPTION = REQUIRED ALGORITHM AES); /* Default algorithm is RC4, however RC4 is marked depracated. Use AES (GUI uses AES by default, but TSQL does not) */
GO

GRANT CONNECT ON ENDPOINT::[AlwaysOn_EP] TO [contoso\svc-sql2];
GO

GRANT CONNECT ON ENDPOINT::[AlwaysOn_EP] TO [contoso\svc-sql3];
GO

:Connect NODE2

USE [master];
GO

CREATE ENDPOINT [AlwaysOn_EP]
	STATE = STARTED
	AS TCP (LISTENER_PORT = 5022, LISTENER_IP = (10.0.1.2)) /* Specify an IP value for LISTENER_IP to dedicate AlwaysOn traffic to it's own NIC card */
	FOR DATABASE_MIRRORING (ROLE = ALL /* Cannot use Witness as that is for mirroring only.  Partner will work but that's really just for mirroring terminology */
		, AUTHENTICATION = WINDOWS NEGOTIATE /* Authentication can be Windows or Certificate of a combination in preference order.  Authorization can force NTLM or Kerberos. These are default values */
		, ENCRYPTION = REQUIRED ALGORITHM AES); /* Default algorithm is RC4, however RC4 is marked depracated. Use AES (GUI uses AES by default, but TSQL does not) */
GO

GRANT CONNECT ON ENDPOINT::[AlwaysOn_EP] TO [contoso\svc-sql1];
GO

GRANT CONNECT ON ENDPOINT::[AlwaysOn_EP] TO [contoso\svc-sql3];
GO


:Connect NODE3

USE [master];
GO

CREATE ENDPOINT [AlwaysOn_EP]
	STATE = STARTED
	AS TCP (LISTENER_PORT = 5022, LISTENER_IP = (172.18.0.3)) /* Specify an IP value for LISTENER_IP to dedicate AlwaysOn traffic to it's own NIC card */
	FOR DATABASE_MIRRORING (ROLE = ALL /* Cannot use Witness as that is for mirroring only.  Partner will work but that's really just for mirroring terminology */
		, AUTHENTICATION = WINDOWS NEGOTIATE /* Authentication can be Windows or Certificate of a combination in preference order.  Authorization can force NTLM or Kerberos. These are default values */
		, ENCRYPTION = REQUIRED ALGORITHM AES); /* Default algorithm is RC4, however RC4 is marked depracated. Use AES (GUI uses AES by default, but TSQL does not) */
GO

GRANT CONNECT ON ENDPOINT::[AlwaysOn_EP] TO [contoso\svc-sql1];
GO

GRANT CONNECT ON ENDPOINT::[AlwaysOn_EP] TO [contoso\svc-sql2];
GO

/* Here we connect to each replica and ensure that the AlwaysOn extended events session is set to run at startup and that it is also started */
:Connect NODE1

IF EXISTS(SELECT * FROM sys.server_event_sessions WHERE name='AlwaysOn_health')
BEGIN
  ALTER EVENT SESSION [AlwaysOn_health] ON SERVER WITH (STARTUP_STATE=ON);
END

IF NOT EXISTS(SELECT * FROM sys.dm_xe_sessions WHERE name='AlwaysOn_health')
BEGIN
  ALTER EVENT SESSION [AlwaysOn_health] ON SERVER STATE=START;
END

GO

:Connect NODE2

IF EXISTS(SELECT * FROM sys.server_event_sessions WHERE name='AlwaysOn_health')
BEGIN
  ALTER EVENT SESSION [AlwaysOn_health] ON SERVER WITH (STARTUP_STATE=ON);
END

IF NOT EXISTS(SELECT * FROM sys.dm_xe_sessions WHERE name='AlwaysOn_health')
BEGIN
  ALTER EVENT SESSION [AlwaysOn_health] ON SERVER STATE=START;
END

GO

:Connect NODE3

IF EXISTS(SELECT * FROM sys.server_event_sessions WHERE name='AlwaysOn_health')
BEGIN
  ALTER EVENT SESSION [AlwaysOn_health] ON SERVER WITH (STARTUP_STATE=ON);
END

IF NOT EXISTS(SELECT * FROM sys.dm_xe_sessions WHERE name='AlwaysOn_health')
BEGIN
  ALTER EVENT SESSION [AlwaysOn_health] ON SERVER STATE=START;
END

GO

/*
There will be 3 databases included in our AG and they currently exist on NODE1.
Prior to SQL 2016 we would have to do the following:
	Backup each database data file to a share and then restore it on each replica making sure to use the "NORECOVERY" clause
	Backup each database log file to a share and then restore it on each replica making sure to use the "NORECOVERY" clause

With SQL 2016+ we can use Direct Seeding instead.
*/

/* Here we connect to our primary replica (NODE1) and create our AG. */
:Connect NODE1

/* We can use trace flag 9567 to enable compression for the VDI backup for the seeding process */
DBCC TRACEON (9567, -1);
GO

USE [master];
GO

CREATE AVAILABILITY GROUP [App1AG]
WITH (AUTOMATED_BACKUP_PREFERENCE = SECONDARY, FAILURE_CONDITION_LEVEL = 3, HEALTH_CHECK_TIMEOUT = 30000, DB_FAILOVER = ON, DTC_SUPPORT = PER_DB, CLUSTER_TYPE = WSFC) /* You can also use SECONDARY_ONLY, PRIMARY, or NONE
Failure condition levels are from the least restrcitive of 1(SQL Service is down) to 5(Exhaustion of worker threads). 3 is the default.
Health check default is 30 seconds, represented in milliseconds. This is how long we wait for sp_server_diagnostics to return. */
FOR DATABASE [App1AG_DB1], [App1AG_DB2], [App1AG_DB3]
REPLICA ON N'NODE1' WITH (ENDPOINT_URL = N'TCP://NODE1.contoso.com:5022', FAILOVER_MODE = AUTOMATIC, AVAILABILITY_MODE = SYNCHRONOUS_COMMIT, BACKUP_PRIORITY = 50, SECONDARY_ROLE(ALLOW_CONNECTIONS = ALL), SESSION_TIMEOUT = 10, SEEDING_MODE = AUTOMATIC),
	N'NODE2' WITH (ENDPOINT_URL = N'TCP://NODE2.contoso.com:5022', FAILOVER_MODE = AUTOMATIC, AVAILABILITY_MODE = SYNCHRONOUS_COMMIT, BACKUP_PRIORITY = 60, SECONDARY_ROLE(ALLOW_CONNECTIONS = ALL), SESSION_TIMEOUT = 10, SEEDING_MODE = AUTOMATIC),
	N'NODE3' WITH (ENDPOINT_URL = N'TCP://NODE3.contoso.com:5022', FAILOVER_MODE = MANUAL, AVAILABILITY_MODE = SYNCHRONOUS_COMMIT, BACKUP_PRIORITY = 70, SECONDARY_ROLE(ALLOW_CONNECTIONS = READ_ONLY), SESSION_TIMEOUT = 10, SEEDING_MODE = AUTOMATIC);
GO

/* Here we create a listener for our AG. If you have issues creating the listener check permissions in AD.
	You might also have to turn the AG networks to client and cluster and then turn them back to none post-listener creation*/
ALTER AVAILABILITY GROUP [App1AG]
ADD LISTENER N'App1AG' (
WITH IP
((N'10.0.0.11', N'255.255.255.0'),('172.16.0.11','255.240.0.0'))
, PORT=1433);
GO

/* Now that the AG exists we connect to each secondary replica and join it to the group
	We also have to grant the CREATE ANY DATABASE permission so Seeding can create the DBs */
:Connect NODE2

ALTER AVAILABILITY GROUP [App1AG] JOIN;
GO
ALTER AVAILABILITY GROUP [App1AG] GRANT CREATE ANY DATABASE;
GO

:Connect NODE3

ALTER AVAILABILITY GROUP [App1AG] JOIN;
GO
ALTER AVAILABILITY GROUP [App1AG] GRANT CREATE ANY DATABASE;
GO

--This query allows us to view seeding performance statistics
SELECT * FROM sys.dm_hadr_physical_seeding_stats;

/* Now we need to turn our trace flag back off */
:Connect NODE1
DBCC TRACEOFF (9567, -1);
GO

 

Network Configuration

Here is the list of IP addresses from Part 1 that you will need to configure the AG.

 

 
Data Center 1 IP
Data Center 2 IP
Cluster
10.0.0.10
172.16.0.10
AG Listener
10.0.0.11
172.16.0.11
FCI
10.0.0.15
N/A

 

Architecture

As a refresher, here is the diagram of the lab you are building.

Lab Architecture.png

Build Availability Group Video