You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/database-engine/availability-groups/windows/create-an-availability-group-transact-sql.md
+34-42Lines changed: 34 additions & 42 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -81,7 +81,7 @@ ms.author: mathoma
81
81
82
82
1. The following [!INCLUDE[tsql](../../../includes/tsql-md.md)] example creates these databases and alters them to use the full recovery model:
83
83
84
-
```
84
+
```sql
85
85
-- Create sample databases:
86
86
CREATEDATABASEMyDb1;
87
87
GO
@@ -96,18 +96,17 @@ ms.author: mathoma
96
96
97
97
2. The following code example creates a full database backup of *MyDb1*and*MyDb2*. This code example uses a fictional backup share, \\\\*FILESERVER*\\*SQLbackups*.
98
98
99
-
```
99
+
```sql
100
100
-- Backup sample databases:
101
101
BACKUP DATABASE MyDb1
102
102
TO DISK = N'\\FILESERVER\SQLbackups\MyDb1.bak'
103
-
WITH FORMAT
103
+
WITH FORMAT;
104
104
GO
105
105
106
106
BACKUP DATABASE MyDb2
107
107
TO DISK = N'\\FILESERVER\SQLbackups\MyDb2.bak'
108
-
WITH FORMAT
108
+
WITH FORMAT;
109
109
GO
110
-
111
110
```
112
111
113
112
[[TopOfExample]](#ExampleConfigAGWinAuth)
@@ -124,26 +123,24 @@ ms.author: mathoma
124
123
125
124
1. Create a database mirroring endpoint named *dbm_endpoint*on the server instance on which you plan to create the availability group (this is an instance named `AgHostInstance`on`COMPUTER01`). This endpoint uses port 7022. Note that the server instance on which you create the availability group will host the primary replica.
126
125
127
-
```
126
+
```sql
128
127
-- Create endpoint on server instance that hosts the primary replica:
129
128
CREATE ENDPOINT dbm_endpoint
130
129
STATE=STARTED
131
130
AS TCP (LISTENER_PORT=7022)
132
-
FOR DATABASE_MIRRORING (ROLE=ALL)
131
+
FOR DATABASE_MIRRORING (ROLE=ALL);
133
132
GO
134
-
135
133
```
136
134
137
135
2. Create an endpoint *dbm_endpoint*on the server instance that will host the secondary replica (this is the default server instance on`COMPUTER02`). This endpoint uses port 5022.
138
136
139
-
```
137
+
```sql
140
138
-- Create endpoint on server instance that hosts the secondary replica:
141
139
CREATE ENDPOINT dbm_endpoint
142
140
STATE=STARTED
143
141
AS TCP (LISTENER_PORT=5022)
144
-
FOR DATABASE_MIRRORING (ROLE=ALL)
142
+
FOR DATABASE_MIRRORING (ROLE=ALL);
145
143
GO
146
-
147
144
```
148
145
149
146
3. > [!NOTE]
@@ -153,7 +150,7 @@ ms.author: mathoma
153
150
154
151
The following code example shows the [!INCLUDE[tsql](../../../includes/tsql-md.md)] statements for creating a login and granting it permission on an endpoint. The domain account of the remote server instance is represented here as*domain_name*\\*user_name*.
155
152
156
-
```
153
+
```sql
157
154
-- If necessary, create a login for the service account, domain_name\user_name
158
155
-- of the server instance that will host the other replica:
159
156
USE master;
@@ -170,9 +167,8 @@ ms.author: mathoma
170
167
171
168
The following code example creates an availability group named *MyAG*on the server instance on which the sample databases, *MyDb1*and*MyDb2*, were created. The local server instance, `AgHostInstance`, on*COMPUTER01* is specified first. This instance will host the initial primary replica. A remote server instance, the default server instance on*COMPUTER02*, is specified to host a secondary replica. Both availability replica are configured to use asynchronous-commit mode with manual failover (for asynchronous-commit replicas manual failover means forced failover with possible data loss).
172
169
173
-
```
174
-
175
-
-- Create the availability group, MyAG:
170
+
```sql
171
+
-- Create the availability group, MyAG:
176
172
CREATE AVAILABILITY GROUP MyAG
177
173
FOR
178
174
DATABASE MyDB1, MyDB2
@@ -198,7 +194,7 @@ ms.author: mathoma
198
194
199
195
The following code example joins the secondary replica on`COMPUTER02` to the `MyAG` availability group.
200
196
201
-
```
197
+
```sql
202
198
-- On the server instance that hosts the secondary replica,
203
199
-- join the secondary replica to the availability group:
204
200
ALTER AVAILABILITY GROUP MyAG JOIN;
@@ -209,19 +205,18 @@ ms.author: mathoma
209
205
210
206
The following code example creates the *MyDb1*and*MyDb2* secondary databases by restoring database backups using RESTORE WITH NORECOVERY.
211
207
212
-
```
208
+
```sql
213
209
-- On the server instance that hosts the secondary replica,
214
210
-- Restore database backups using the WITH NORECOVERY option:
215
211
RESTORE DATABASE MyDb1
216
212
FROM DISK = N'\\FILESERVER\SQLbackups\MyDb1.bak'
217
-
WITH NORECOVERY
213
+
WITH NORECOVERY;
218
214
GO
219
215
220
216
RESTORE DATABASE MyDb2
221
217
FROM DISK = N'\\FILESERVER\SQLbackups\MyDb2.bak'
222
-
WITH NORECOVERY
223
-
GO
224
-
218
+
WITH NORECOVERY;
219
+
GO
225
220
```
226
221
227
222
7. On the server instance that hosts the primary replica, back up the transaction log on each of the primary databases.
@@ -231,19 +226,18 @@ ms.author: mathoma
231
226
232
227
The following code example creates a transaction log backup on MyDb1 andon MyDb2.
233
228
234
-
```
229
+
```sql
235
230
-- On the server instance that hosts the primary replica,
236
231
-- Backup the transaction log on each primary database:
237
232
BACKUP LOG MyDb1
238
233
TO DISK = N'\\FILESERVER\SQLbackups\MyDb1.bak'
239
-
WITH NOFORMAT
234
+
WITH NOFORMAT;
240
235
GO
241
236
242
237
BACKUP LOG MyDb2
243
238
TO DISK = N'\\FILESERVER\SQLbackups\MyDb2.bak'
244
-
WITHNOFORMAT
245
-
GO
246
-
239
+
WITHNOFORMAT;
240
+
GO
247
241
```
248
242
249
243
> [!TIP]
@@ -256,32 +250,31 @@ ms.author: mathoma
256
250
> [!IMPORTANT]
257
251
> When you are preparing a real secondary database, you need to apply every log backup taken since the database backup from which you created the secondary database, starting with the earliest and always using RESTORE WITH NORECOVERY. Of course, if you restore both full and differential database backups, you would only need to apply the log backups taken after the differential backup.
258
252
259
-
```
253
+
```sql
260
254
-- Restore the transaction log on each secondary database,
261
255
-- using the WITH NORECOVERY option:
262
256
RESTORE LOG MyDb1
263
257
FROM DISK = N'\\FILESERVER\SQLbackups\MyDb1.bak'
264
-
WITH FILE=1, NORECOVERY
258
+
WITH FILE=1, NORECOVERY;
265
259
GO
266
260
RESTORE LOG MyDb2
267
261
FROM DISK = N'\\FILESERVER\SQLbackups\MyDb2.bak'
268
-
WITH FILE=1, NORECOVERY
262
+
WITH FILE=1, NORECOVERY;
269
263
GO
270
264
```
271
265
272
266
9. On the server instance that hosts the secondary replica, join the new secondary databases to the availability group.
273
267
274
268
The following code example, joins the *MyDb1* secondary database and then the *MyDb2* secondary databases to the *MyAG* availability group.
275
269
276
-
```
270
+
```sql
277
271
-- On the server instance that hosts the secondary replica,
278
272
-- join each secondary database to the availability group:
279
273
ALTER DATABASE MyDb1 SET HADR AVAILABILITY GROUP = MyAG;
280
274
GO
281
275
282
276
ALTER DATABASE MyDb2 SET HADR AVAILABILITY GROUP = MyAG;
283
277
GO
284
-
285
278
```
286
279
287
280
### <a name="CompleteCodeExample"></a> Complete Code Example for Sample Configuration Procedure
@@ -307,7 +300,7 @@ ms.author: mathoma
307
300
> [!NOTE]
308
301
> For additional [!INCLUDE[tsql](../../../includes/tsql-md.md)] code examples of creating an availability group, see [CREATE AVAILABILITY GROUP (Transact-SQL)](../../../t-sql/statements/create-availability-group-transact-sql.md).
309
302
310
-
```
303
+
```sql
311
304
-- on the server instance that will host the primary replica,
312
305
-- create sample databases:
313
306
CREATE DATABASE MyDb1;
@@ -323,26 +316,26 @@ GO
323
316
-- Backup sample databases:
324
317
BACKUP DATABASE MyDb1
325
318
TO DISK = N'\\FILESERVER\SQLbackups\MyDb1.bak'
326
-
WITH FORMAT
319
+
WITH FORMAT;
327
320
GO
328
321
329
322
BACKUP DATABASE MyDb2
330
323
TO DISK = N'\\FILESERVER\SQLbackups\MyDb2.bak'
331
-
WITH FORMAT
324
+
WITH FORMAT;
332
325
GO
333
326
334
327
-- Create the endpoint on the server instance that will host the primary replica:
335
328
CREATE ENDPOINT dbm_endpoint
336
329
STATE=STARTED
337
330
AS TCP (LISTENER_PORT=7022)
338
-
FOR DATABASE_MIRRORING (ROLE=ALL)
331
+
FOR DATABASE_MIRRORING (ROLE=ALL);
339
332
GO
340
333
341
334
-- Create the endpoint on the server instance that will host the secondary replica:
342
335
CREATE ENDPOINT dbm_endpoint
343
336
STATE=STARTED
344
337
AS TCP (LISTENER_PORT=7022)
345
-
FOR DATABASE_MIRRORING (ROLE=ALL)
338
+
FOR DATABASE_MIRRORING (ROLE=ALL);
346
339
GO
347
340
348
341
-- If both service accounts run under the same domain account, skip this step. Otherwise,
@@ -400,18 +393,18 @@ GO
400
393
-- Restore database backups onto this server instance, using RESTORE WITH NORECOVERY:
401
394
RESTORE DATABASE MyDb1
402
395
FROM DISK = N'\\FILESERVER\SQLbackups\MyDb1.bak'
403
-
WITH NORECOVERY
396
+
WITH NORECOVERY;
404
397
GO
405
398
406
399
RESTORE DATABASE MyDb2
407
400
FROM DISK = N'\\FILESERVER\SQLbackups\MyDb2.bak'
408
-
WITH NORECOVERY
401
+
WITH NORECOVERY;
409
402
GO
410
403
411
404
-- Back up the transaction log on each primary database:
412
405
BACKUP LOG MyDb1
413
406
TO DISK = N'\\FILESERVER\SQLbackups\MyDb1.bak'
414
-
WITH NOFORMAT
407
+
WITH NOFORMAT;
415
408
GO
416
409
417
410
BACKUP LOG MyDb2
@@ -423,11 +416,11 @@ GO
423
416
-- using the WITH NORECOVERY option:
424
417
RESTORE LOG MyDb1
425
418
FROM DISK = N'\\FILESERVER\SQLbackups\MyDb1.bak'
426
-
WITH FILE=1, NORECOVERY
419
+
WITH FILE=1, NORECOVERY;
427
420
GO
428
421
RESTORE LOG MyDb2
429
422
FROM DISK = N'\\FILESERVER\SQLbackups\MyDb2.bak'
430
-
WITH FILE=1, NORECOVERY
423
+
WITH FILE=1, NORECOVERY;
431
424
GO
432
425
433
426
-- On the server instance that hosts the secondary replica,
@@ -437,7 +430,6 @@ GO
437
430
438
431
ALTER DATABASE MyDb2 SET HADR AVAILABILITY GROUP = MyAG;
0 commit comments