Configure read-scale for an Always On availability group
On Primary node:
On powershell : Enable-SqlAlwaysOn -ServerInstance <server\instance> -Force
SELECT @@version;
SELECT @@servername;
ALTER EVENT SESSION AlwaysOn_health ON SERVER WITH (STARTUP_STATE=ON);
GO
CREATE MASTER KEY ENCRYPTION BY Password = 'password';
CREATE CERTIFICATE dbm_certificate WITH SUBJECT = 'dbm';
BACKUP CERTIFICATE dbm_certificate
TO FILE = 'c:\Program Files\Microsoft SQL Server\MSSQL14.SQL001\MSSQL\DATA\dbm_certificate.cer'
WITH PRIVATE KEY (
FILE = 'c:\Program Files\Microsoft SQL Server\MSSQL14.SQL001\MSSQL\DATA\dbm_certificate.pvk',
ENCRYPTION BY PASSWORD = 'password'
);
Create login dbm_login with password = 'password';
Create user dbm_user for login dbm_login;
CREATE ENDPOINT [Hadr_endpoint]
AS TCP (LISTENER_IP = (0.0.0.0), LISTENER_PORT = 5022)
FOR DATA_MIRRORING (
ROLE = ALL,
AUTHENTICATION = CERTIFICATE dbm_certificate,
ENCRYPTION = REQUIRED ALGORITHM AES
);
ALTER ENDPOINT [Hadr_endpoint] STATE = STARTED;
GRANT CONNECT ON ENDPOINT::[Hadr_endpoint] TO dbm_login;
CREATE AVAILABILITY GROUP [ag1]
WITH (CLUSTER_TYPE = NONE)
FOR REPLICA ON
N'vm-sql100' WITH (
ENDPOINT_URL = N'tcp://vm-sql100:5022',
AVAILABILITY_MODE = ASYNCHRONOUS_COMMIT,
FAILOVER_MODE = MANUAL,
SEEDING_MODE = AUTOMATIC,
SECONDARY_ROLE (ALLOW_CONNECTIONS = ALL)
),
N'vm-sql200' WITH (
ENDPOINT_URL = N'tcp://vm-sql200:5022',
AVAILABILITY_MODE = ASYNCHRONOUS_COMMIT,
FAILOVER_MODE = MANUAL,
SEEDING_MODE = AUTOMATIC,
SECONDARY_ROLE (ALLOW_CONNECTIONS = ALL)
);
ALTER AVAILABILITY GROUP [ag1] GRANT CREATE ANY DATABASE;
-- Create database
CREATE DATABASE [db1];
ALTER DATABASE [db1] SET RECOVERY FULL;
BACKUP DATABASE [db1]
TO DISK = N'c:\Program Files\Microsoft SQL Server\MSSQL14.SQL001\MSSQL\DATA\db1.bak';
-- Add replica
CREATE DATABASE [dbread];
ALTER DATABASE [dbread] SET RECOVERY FULL;
BACKUP DATABASE [dbread]
TO DISK = N'c:\Program Files\Microsoft SQL Server\MSSQL14.SQL001\MSSQL\DATA\dbread.bak';
ALTER AVAILABILITY GROUP [ag1] ADD DATABASE [dbread];
--Check database
SELECT * FROM sys.databases WHERE name = 'db1';
GO
SELECT DB_NAME(database_id) AS 'database', synchronization_state_desc FROM sys.dm_hadr_database_replica_states;
BACKUP DATABASE [AdventureWorks2017]
TO DISK = N'c:\Program Files\Microsoft SQL Server\MSSQL14.SQL001\MSSQL\DATA\adv2017.bak';
ALTER AVAILABILITY GROUP [ag1] ADD DATABASE [AdventureWorks2017];
On Secondary node:
On powershell : Enable-SqlAlwaysOn -ServerInstance <server\instance> -Force
SELECT @@version;
SELECT @@servername;
CREATE MASTER KEY ENCRYPTION BY Password = 'password'
Create login dbm_login with password = 'password';
Create user dbm_user for login dbm_login;
CREATE CERTIFICATE dbm_certificate
AUTHORIZATION dbm_user
FROM FILE = 'C:\Program Files\Microsoft SQL Server\MSSQL14.SQL002\MSSQL\DATA\dbm_certificate.cer'
WITH PRIVATE KEY (
FILE = 'C:\Program Files\Microsoft SQL Server\MSSQL14.SQL002\MSSQL\DATA\dbm_certificate.pvk',
DECRYPTION BY PASSWORD = 'password'
);
-- Create enpoint
CREATE ENDPOINT [Hadr_endpoint]
AS TCP (LISTENER_IP = (0.0.0.0), LISTENER_PORT = 5022)
FOR DATA_MIRRORING (
ROLE = ALL,
AUTHENTICATION = CERTIFICATE dbm_certificate,
ENCRYPTION = REQUIRED ALGORITHM AES
);
ALTER ENDPOINT [Hadr_endpoint] STATE = STARTED;
GRANT CONNECT ON ENDPOINT::[Hadr_endpoint] TO dbm_login;
ALTER AVAILABILITY GROUP [ag1] JOIN WITH (CLUSTER_TYPE = NONE);
ALTER AVAILABILITY GROUP [ag1] GRANT CREATE ANY DATABASE;
SELECT * FROM sys.databases WHERE name = 'dbread';
GO
SELECT DB_NAME(database_id) AS 'database', synchronization_state_desc FROM sys.dm_hadr_database_replica_states;