Wednesday , April 24 2024

SQL Server AlwaysOn Availability Groups step by step

I will configure SQL Server AlwaysOn Availability Groups step by step in this article. First I want to tell about SQL Server Alwas ON.

What is AlwasyOn in SQL Server?

SQL Server AlwaysOn is a marketing term microsoft started to use with SQL Server 2012. Many people thinks that AlwaysOn is only the availability group. But it is not only the Availability Group. SQL Server Failover Cluster is also Always On. So below technogies are Always ON technologies.

  • SQL Server AlwaysOn Failover Cluster
  • SQL Server AlwaysOn Availability Groups

What is Availability Group in SQL Server

SQL Server Always On Availability Groups is based on Mirroring Technology.  Moreover, Availability Group is improved version of Mirroring.

You may want to read my article “Database Mirroring On SQL Server“.

SQL Server AlwaysOn Availability Groups can be HA (High Availability) and DR (Disaster Recovery) solution.

If you want to use Availability Group as the HA solution, you need more than one server in the same data center.

As a DR solution, you can use this technology by adding another secondary server in different locations.

You can use Availability Groups as DR by locating primary and secondary server at different data centers. If the primary data center becomes completely unserviceable(earthquake, flood, fire), you can use secondary database on secodary data center. For example, if your data center is located in a city named A, you can choose B as a disaster center or a less risky city.

To install Availability Group, you need at least 2 servers that must be in the same windows cluster. Availability Group provides HA and DR solutions with AG (Availability Group).

What is Distributed Availability Group?

SQL Server 2016 supporys Distributed Transaction for Availability Groups. Its called Distributed Availability Group.

However, it is not supported if there are multiple databases associated with the transaction on the same instance. When creating the Availability Group, we need to create it by adding WITH DTC_SUPPORT = PER_DB. In addition, the operating system must be a minimum of Windows Server 2012 R2 and the KB3090973 patch needs to be installed.

What is the difference between database mirroring and alwayson availability groups

Remember, we can use Database Mirroring database basis. So if you want to add more than one databases for Mirroring you must seperately configure them.

But, we can add more than one database to an availability group. Failover is performed on an Availability Group basis. For example, you have 50 databases on your instance. You have created 5 Availability Groups for this 50 database. You can actively operate 3 of these 5 Availability Groups in the 1st server and 2 of these 5 in the 2nd server.

Or you can change the availability groups primary server by performing failover at any time you want. It is easier for us to manage this process with Availability Group.

And we can set availability group secondary as readonly. In mirroring we must use database snapshot to read from secondary.

You can read my article “Readable  Secondary On SQL Server Always On Availability Group” to be able to read from the secondary database on Availability Group.

SQL Server Availability Groups synchronous vs asynchronous

As in Mirroring, there are Synchronous and Asynchronous databases. On the basis of AG, you can set synchronous or asynchronous.

If you set AG synchronously, all databases in AG will work synchronously. When user requests come to the primary database, transaction completion information is passed to the user after the operation is reflected on the secondary server.

This may cause some performance loss in very heavily transactional databases.

But, automatic failover is possible if we use synchronous availability groups. In the event of any problems with the servers, the AG will continue to serve automatically from the other server without anyone being aware of it.

If you set AG asynchronously, when user requests come to the primary database, transaction completion information is passed to the user before the operation is reflected on the secondary server.

The question always asked is: If we set to AG asynchronous, how far behind the secondary databases? There is no period. This time depends on the performance of the existing hardware and network. But it’s usually a few seconds.

SQL Server Availability Group Automatic Failover

You can set automatic or manual failover on the basis of AG. You need to set the AG synchronously to perform automatic failover. As long as I don’t see HADR_COMMIT waiting, I set Ag synchronously and automatically.

As the performance loss is more when rebilding indexes, I set asynchronously the AG before Index Rebuild in the systems that are experiencing distress.

The automatic failover process does not occur as a result of an error in a database that is included in the AG.

Alwayson Automatic Failover not working?

Failover operation does not occur in the following cases;

  • one of the databases was corrupt,
  • transaction log filled,
  • data file filled

SQL Server Availability Group HA or DR?

Let me give you an example to explain how HA and DR are used in Availability Group.

You have 2 servers in your data center. These two servers have made a windows cluster. You have set up Availability Group running synchronously on these two servers. This is an HA (High Availability) solution.

We have a server in your data center in another city. You have included this third server in your existing windows cluster.

Note:If you dont want to add third server to your windows cluster you can use it still. Only you need to do is creating availability group with CLUSTER_TYPE=NONE.

Then add the database on the disaster recovery center as asynchronous to the existing Availability Group. This process is called DR (Disaster Recovery) solution. On Availability Group we can make multiple secondary for the same AG.

Now let’s start the install Always On AG Availability Group for the HA solution.

SQL Server Always On Requirements

Hardware and Installation Requirements:

• You must have 2 servers included in the same windows cluster. You can request it from your system group or install it yourself.

• You need to install SQL Server as stand-alone on these 2 servers. You can find detailed information in my article “How To Install SQL Server“.

Permission Reqirements:

• Set the service accounts of the stand-alone instances that you have installed on these 2 servers to the same domain user. For example, [mydomain\myuser]. You can read the article “SQL Server Configuration Manager Settings” to change service accounts.

• The Windows Cluster Account (Windows Cluster’s name) installed on these 2 servers needs to be granted the Create Computer Object privilege in the OU (Organization Unit) where these 2 servers reside in Active Directory. You should talk with the Active Directory team in your organization and create an OU for your SQL Server Servers. After you create the OU, you must also specify the policies to be applied to this OU. You can article links about policies at the end of the section.

If you do not grant Create Computer Object permission to Windows Cluster, you will get an error as below when creating a listener.

The WSFC cluster could not bring the Network Name resource with DNS name ‘testAG’ online. The DNS name may have been taken or have a conflict with existing name services, or the WSFC cluster service may not be running or may be inaccessible. Use a different DNS name to resolve name conflicts, or check the WSFC cluster log for more information.

The attempt to create the network name and IP address for the listener failed. The WSFC service may not be running or may be inaccessible in its current state, or the values provided for the network name and IP address may be incorrect. Check the state of the WSFC cluster and validate the network name and IP address with the network administrator. (Microsoft SQL Server, Error: 19471)

Database based Requirements:

• Databases you want to add to the AG must be in the full recovery model.

• Database full backup is required. If you have a full backup after you have taken the database to Full Recovery Model, you do not need to backup again.

SQL Server Policy Articles

I recommend you read my articles about policies.

SQL Server Account Lockout Policy“,

Security Policy for SQL Server(secpol.msc)“,

SQL Server Password Policy“,

How To Create a Login On SQL Server(Manage Logins)

Once we have completed the above mentioned preparations, we can proceed to the creation of the AG.

SQL Server Always On Availability Groups Step by Step

Enable Always On Availability Groups

First, we need to enable Always ON Availability Group on two instances. If you do not activate, you will receive an error as follows.

The AlwaysOn feature must be enabled for the server instance ” before you can create an availability group on this instance..

To enable Always On Availability Group, we open SQL Server Configuration Manager with Run As Administrator. In SQL Server Services, we right-click on the instance and click properties.

In the tab that opens, select AlwaysOn High Availability, click Enable AlwaysOn Availability Groups and click OK to activate Always On Availability Group. We need to perform this process on the two servers for Always On Availability Group. This will require a service restart. You can restart your sql server services in a controlled manner.

Create Always On Availability Group

After enabling Always On Availability Group in two servers, we click New Availability Wizard from AlwaysOn High Availability on SSMS as follows.

We need to give a name to AG in the incoming screen. Our AG’s name is IlkAG. We’re moving forward by clicking next.

In the incoming screen, we select the databases that we will add to AG. The status of the databases that are suitable for receiving AG appears in the form of “Meets prerequisities”. We’re choosing TestDB.

On the next screen, in the Replicas section, click Add to connect to the second instance of Availability Group. Make sure that the instance names are the same. For example, if your primary server is “Server1\Instance1”, your secondary server should be “Server2\Instance1”. In other words, the name of the named instance on both servers is Instance1.

After the connection is complete, you should see a screen as follows.

Since we want to set the AG to be synchronous and automatic failover, we mark the required fields as follows.

For now, we leave Readable Seconday as “No”.

You can read my article “Readable  Secondary On SQL Server Always On Availability Group” to be able to read from the secondary database on Availability Group.

Create Endpoint in SQL Server Availability Group

After performing the operations on the Replicas tab, we switch to the Endpoints tab and a screen like the one below is displayed.

Change Endpoint url Availability Group

To set Always On Availability Group, if you use more than one instance on the same server, you will need to use a different endpoint port for each instance. The default endpoint port is 5022.

For example, you have 3 instances. When creating the availability group for the first instance, the default port is 5022. You must change the port from the Enpoint URL when you create a availability group for your second instance. You can use 5023 for the second instance and 5024 for the third instance. We will use port 5023 for the instance in our example.

Availability Group Backup Preferences

We go to the Backup Preferences tab and see a screen like the following. This screen asks for the preferred instance to create backups. You must choose one of them.

Prefer SecondaryIf there is an active secondary server, automated backups are performed from the secondary server. If there is no active secondary, it is performed from the primary server.
Secondary onlyAll automated backups must be performed from the secondary server.
PrimaryAll automated backups must be performed from the primary server.
Any ReplicaBackups can be performed from primary and secondary.

I just select Primary and I’m going to the Listener tab without any further changes.

What is Availability Group Listener?

There should be minimum 2 instance in Always On Availability Group architecture. The application must always go to the server where the database is active. It is the listener that provides this. Listener has a virtual name and a virtual IP. The application does not know the physical names and physical IPs of 2 servers in the Always On Availability Group architecture. The application only knows the listener name or IP.

Create an Availability Group Listener

When the Listener screen opens, we give a virtual name from the “Listener DNS Name” section as follows.

In the Port section, we give the port information that the application will connect to the databases on this AG. In Network Mode, select Static IP and click Add on the bottom and write my virtual IP. Applicants will know this IP as their database IP.

You can ask your network unit for IP. If you give IP that someone else uses, you will have trouble.

Then click next.

Select Initial Data Synchronization

In the next screen, it asks us to select initial data synchronization method.

Full:

It will automatically take the full backup and log backup of each database we selected and transfer these backups to the secondary server itself.

This requires a shared folder. Two instance’s SQL server service accounts must have read and write privileges on this shared folder.

To define a shared folder, you can read the article “How To Define a Share and Map This Share To Windows

There must be enough free space for the full and log backup of the databases you select on the disk that you define.

Join only:

We need to manually take full backup and log backup of each database we selected and transfer it to the secondary server before passing this step.

Skip initial data synchronization:

We need to manually take full backup and log backup of each database we selected and transfer it to the secondary server.

But we can do this later. I’ve never used this option until now. We’re choosing Full and click next.

On the next screen, it validates everything about configuration. If there is a problem, you can solve the problem and click Re-run validation. To solve the problem, you can go back by clicking the Back button and fix the wrong settings and click next. There was no problem with our configurations.

Click Next and then Finish. In my example, everything except the listener was created correctly.

When we click on Error near to ‘Create Availability Group Listener’ testAG ”, we can see the detail of the error as follows.

I usually set the port of the availability group to be the same as the instance’s port.

In our example, I set a different port to see what would happen if we set a different port from instance to the availability group.

You can see the error below.

Creating availability group listener resulted in an error.

Although it could not create Listener, it created AG. We can define it as described above by clicking Add Listener from the Availability Group Listeners.

The access information you will give to application developers (the database access information that they write to connection strings) is as follows:

TestAG,1435

or

“IP address you specify when defining a listener”, 1435

You can also connect via SSMS with this way. After the process is complete, you can see that the database is synchronized.

Always On Availability Group Articles

My other articles about Always On Availability Group are as follows.

You can also find the articles you need by typing in the search section of our site.

How To Backup Only Availability Group Databases in an Instance“,

Always On Availability Group Alert System“,

How to Find Time Difference Between Primary and Secondary Database in SQL Server Always On Availability Group Architecture”,

SQL Server Availability Group Failover Process“,

SQL Server Availability Group Databases Synchronization Status“,

How To Execute a Script on Multiple Instance with Registered Server

Loading

About dbtut

We are a team with over 10 years of database management and BI experience. Our Expertises: Oracle, SQL Server, PostgreSQL, MySQL, MongoDB, Elasticsearch, Kibana, Grafana.

Leave a Reply

Your email address will not be published. Required fields are marked *

Categories