Architecture & Performance
Technology22 December 20232 min de lectureArticle en anglais

SQL Server AlwaysOn Feature Tutorial : Part 5 - Activate AlwaysOn on SQL Server

In these articles, we’ll look at how to set up high availability on one or more databases with the AlwaysOn feature, using the « dbatools » PowerShell library. To do this, we’ll…

Pierre-Antoine Collet
Pierre-Antoine Collet
Expert en Performance IT
Sommaire

In these articles, we’ll look at how to set up high availability on one or more databases with the AlwaysOn feature, using the « dbatools » PowerShell library. To do this, we’ll need to set up an active directory, two SQL server instances on two different servers, using Failover Clustering and activate AlwaysOn on SQL Server. In this article you will learn :

  • How to configure the SQL server environment for AlwaysOn.
  • How to create an availability group.
  • How to test the availability group.

Configuration of SQL Server for AlwaysOn

First of all, we're going to create a "Test" database on the first SQL server instance, which is on our "Server1" VM.

Screenshot 2023-10-25 140027

We now need to create a shared directory.

Screenshot 2023-10-26 084114

To do this, create a "DBShare" directory on the first VM, "Server1".Then right-click on it, "Properties" -> "Sharing" -> "Share" -> "Find People" and put in the service account we've created.

Screenshot 2023-10-26 084331

Finally, in the "Security" tab, select our SQL service account, then click on "Edit" and set it to "Write".

Screenshot 2023-10-26 084523

Creation of an Availibility Group

In PowerShell, we'll run these commands to initialize the two instances we'll be using.

$sqlinstance1=Connect-DbaInstance -SqlInstance SERVER1\EVALUATIONS1 -TrustServerCertificate
$sqlinstance2=Connect-DbaInstance -SqlInstance SERVER2\EVALUATIONS2 -TrustServerCertificate

Finally, to create the Availibility Group, run this PowerShell command, naming your AvailibilityGroup and the database you wish to replicate. Then remember to give the listener a valid IP address

New-DbaAvailabilityGroup -Primary $sqlinstance1 -Secondary $sqlinstance2 -Name AG-Test -Database Organisation -ClusterType Wsfc -SeedingMode Automatic -FailoverMode Automatic -SharedPath "\\Server1\DBShare" -ConnectionModeInSecondaryRole "Yes"
Add-DbaAgListener -SqlInstance $sqlinstance1 -AvailabilityGroup AG-Test -IPAddress 172.168.1.31

As you can see, our "Test" database has been replicated on SERVER2 and the two databases are "Synchronized".

Screenshot 2023-10-27 094905

Testing the Availibility Group

To test the availability group, you need to connect to the listener which has the same name as the availability group, as we didn't specify it when we created it.

Screenshot 2023-11-07 133523

As you can see, when you connect to the listener, you have access to the "Test" database.

Screenshot 2023-11-07 133928

You have successfully activate AlwaysOn on SQL Server. You can now have fun switching off one of the two VMs to see that, when connecting to the listener, you still have access to your database.

Conclusion

In these articles, you'll have learned to activate AlwaysOn on SQL server to set up a high-availability solution, configuring the entire environment for its proper operation.

Besoin d'aide sur ce sujet ?

Notre équipe d'experts est à votre disposition pour vous accompagner.

Contactez-nous