3. Creating an Availability Group
Connect to the SQL-WEST
instance using SSMS and create the sample databases as follows:
USE MASTER
GO
CREATE DATBASE [DatabaseA]
GO
CREATE DATBASE [DatabaseB]
GO
CREATE DATABASE [DatabaseC]
GO
Next, we need to take a full database backup of
these databases. If we do not do this first, the Availability Group
Wizard will not allow us to create the availability group with these
databases. On the SQL-WEST
server backup all three databases. For simplicity, we can create a folder, C:\Backup
, and issue the following statements within SSMS:
USE MASTER
GO
BACKUP DATABASE DatabaseA TO DISK='C:\backup\DatabaseA.bak'
GO
BACKUP DATABASE DatabaseB TO DISK='C:\backup\DatabaseB.bak'
GO
BACKUP DATABASE DatabaseC TO DISK='C:\backup\DatabaseC.bak'
GO
Next, launch the New Availability Group wizard
by selecting it from the context menu of the Availability Groups node
within the Management node in Object Explorer. The first question the
wizard will ask is for an Availability Group name. Type TestAG, and click Next.
The Select Databases page shown in Figure 5
is where we can select one or more databases to be part of the
Availability Group. Select DatabaseA, DatabaseB, and DatabaseC, and
click Next.
Figure 5. Select Databases page
The next page is where we define the replicas. This page is shown in Figure 6,
and here, we can specify each secondary availability replica and
whether or not the data will flow asynchronously (i.e., high safety) or
synchronously (i.e., high performance). Click the Add Replica button,
and add SQL-EAST
and SQL-SOUTH
. Next, change the Replica mode for SQL-SOUTH
to “High performance” by clicking the Replica Mode drop-down for SQL-SOUTH
.
Notice that there are three options for Replica Mode: Automatic
Failover, High performance, and High safety. You can only have two
server instances involved with automatic failover in this example that
would be SQL-WEST
, the primary, and SQL-EAST
, the secondary automatic failover partner.
Figure 6. Specify Replicas page
The fourth column, “Connection Mode in
Secondary Role,” determines if you want give users the ability to
connect directly to the secondary availability replica. “Disallow
connections” is the default and does not allow any active connections
to that database on the secondary server. Note that this is for the
database itself, not for the SQL Server instance that is hosting the
database. Users can still connect to the SQL Server instance; they just
can’t use the database that is part of this availability group. The
next option is “Allow all connections,” which, as the name implies,
will allow users to connect to and use the database.
However, if the user issues a write query, the query will fail. To
mitigate the end users’ frustration with their application periodically
erroring out, there is another option called, “Allow only read-intent
connections.” When this option is selected, an extra parameter is
required on the connection string to signal to the user who connects
that he or she will be issuing read-only queries. This is mainly to
force the users to know the expected behavior.
For this example, we will select “Allow all connections” for the SQL-SOUTH
server instance, and click Next.
From a client perspective, it’s easy to enable
automatic failover. While using database mirroring, we had to specify
the failover partner in the connection string. In this case, you can
define an availability group listener, which is essentially a virtual
IP that users connect to. In the event of a failure, the users still
connect to this virtual IP, but now, they are being served by the
secondary instance. The clients do not have to change or add anything
to the connection string to achieve this behavior. The next page in the
wizard, Figure 7 allows you to specify this availability group listener.
Figure 7. Specify the Availabilty Group Listener dialog
The wizard can also handle the initial data synchronization for you. The next page in the wizard, shown in Figure 8, allows you to specify a network share on SQL-WEST
to obtain the backup from or to skip the initial data synchronization altogether.
Figure 8. Data Synchronization page
The wizard will then perform a validation check
to see if it can access the network share, if there is enough free disk
space, and so on. Once this validation checks out, the wizard will
provide you a summary of your selections. When you click Finish, the
wizard will create your availability group.
If you go back to Object Explorer, you
will see a new node under the Availability Groups node within the
Management node of Object Explorer. By right-clicking and selecting
Show Dashboard, you can see the overall health of the group including
synchronization status and any issues that need to be addressed.