运维部落

首页 > 网络产品, 解决方案 > sqlserver自动配置镜像脚本

sqlserver自动配置镜像脚本

2019年7月6日
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
--环境:非域环境
 
DECLARE @DBName NVARCHAR(255) --库名
DECLARE @masterip NVARCHAR(255)  --主库IP
DECLARE @mirrorip NVARCHAR(255)  --镜像库IP
DECLARE @witness NVARCHAR(255)
DECLARE @masteriptail NVARCHAR(255)
DECLARE @mirroriptail NVARCHAR(255)
DECLARE @witnesstail NVARCHAR(255)
DECLARE @certpath NVARCHAR(MAX)  --证书路径
DECLARE @Restorepath NVARCHAR(MAX)
DECLARE @Restorepath1 NVARCHAR(MAX)
DECLARE @Restorepath2 NVARCHAR(MAX)
DECLARE @MKPASSWORD NVARCHAR(500)  --证书密码
DECLARE @LOGINPWD NVARCHAR(500)  --登录名密码
DECLARE @LISTENER_PORT  NVARCHAR(500)  --Endpoint密码
DECLARE @SQL NVARCHAR(MAX)
DECLARE @MASTERHOST_NAME NVARCHAR(50)  --主库登录名
DECLARE @SLAVEHOST_NAME NVARCHAR(50)  --镜像库登录名
DECLARE @WITNESSHOST_NAME NVARCHAR(50)
 
 
if OBJECT_ID ('tempdb..#temp')is not null 
BEGIN 
 DROP TABLE #BackupFileList
END
 
CREATE TABLE #BackupFileList 
    (
      LogicalName NVARCHAR(100) ,
      PhysicalName NVARCHAR(100) ,
      BackupType CHAR(1) ,
      FileGroupName NVARCHAR(50) ,
      SIZE BIGINT ,
      MaxSize BIGINT ,
      FileID BIGINT ,
      CreateLSN BIGINT ,
      DropLSN BIGINT NULL ,
      UniqueID UNIQUEIDENTIFIER ,
      ReadOnlyLSN BIGINT NULL ,
      ReadWriteLSN BIGINT NULL ,
      BackupSizeInBytes BIGINT ,
      SourceBlockSize INT ,
      FileGroupID INT ,
      LogGroupGUID UNIQUEIDENTIFIER NULL ,
      DifferentialBaseLSN BIGINT NULL ,
      DifferentialBaseGUID UNIQUEIDENTIFIER ,
      IsReadOnly BIT ,
      IsPresent BIT ,
      TDEThumbprint NVARCHAR(100)
    )
 
 
SET NOCOUNT ON
 
SET @masterip='192.168.56.131'  
SET @mirrorip='192.168.56.130'  
SET @witness='192.168.56.132'   
--目录后面不要带分隔符: \
SET @certpath='C:\temp\'   
SET @Restorepath='C:\temp\'   
SET @DBName='testmirror'               
SET @MKPASSWORD='master@2015key123' 
SET @LOGINPWD='User_Pass@2015key123'  
SET @LISTENER_PORT='5022'  
SET @MASTERHOST_NAME='A'  
SET @SLAVEHOST_NAME='B'  
SET @WITNESSHOST_NAME='C'  
 
 
 
 
 
 
select @masteriptail= PARSENAME(@masterip,2)+'_'+PARSENAME(@masterip,1) 
select @mirroriptail= PARSENAME(@mirrorip,2)+'_'+PARSENAME(@mirrorip,1) 
select @witnesstail= PARSENAME(@witness,2)+'_'+PARSENAME(@witness,1) 
 
 
--------------------------------------------------------------------------------
DECLARE @stat NVARCHAR(MAX)
 
SET  @stat='--自动生成镜像脚本V1 By huazai'
PRINT @stat
PRINT CHAR(13)+CHAR(13)
 
 
 
 
SET  @stat='--0、首先确定要做镜像的库的恢复模式为完整,用以下sql语句来查看'+CHAR(13)
+'--主机'+CHAR(13)
+'SELECT [name], [recovery_model_desc] FROM sys.[databases]'+CHAR(13)+CHAR(13)+CHAR(13)
 
PRINT '--主:'+@masterip
PRINT '--备:'+@mirrorip
--PRINT '--见证:'+@witness
PRINT CHAR(13)+CHAR(13)
PRINT @stat
 
--------------------------------------------------------------------
PRINT '-- ============================================='
 
SET  @stat='--1、 在主服务器和镜像服务器上和见证服务器上创建Master Key 、创建证书 '+CHAR(13)
+'--主机'+CHAR(13)
+'USE master;
CREATE MASTER KEY ENCRYPTION BY PASSWORD = '''+@MKPASSWORD+''';'
+'CREATE CERTIFICATE HOST_'
+@masteriptail
+'_cert  WITH SUBJECT = ''HOST_'
+@masteriptail
+'_certificate'','+CHAR(13)
+'START_DATE = ''09/20/2010'',EXPIRY_DATE = ''01/01/2099'';'+CHAR(13)
 
PRINT @stat
 
 
SET  @stat='--备机'+CHAR(13)
+'USE master;
CREATE MASTER KEY ENCRYPTION BY PASSWORD = '''+@MKPASSWORD+''';'
+'CREATE CERTIFICATE HOST_'
+@mirroriptail
+'_cert  WITH SUBJECT = ''HOST_'
+@mirroriptail
+'_certificate'','+CHAR(13)
+'START_DATE = ''09/20/2010'',EXPIRY_DATE = ''01/01/2099'';'+CHAR(13)
 
PRINT @stat
 
 
SET  @stat='--见证'+CHAR(13)
+'USE master;
CREATE MASTER KEY ENCRYPTION BY PASSWORD = '''+@MKPASSWORD+''';'
+'CREATE CERTIFICATE HOST_'
+@witnesstail
+'_cert  WITH SUBJECT = ''HOST_'
+@witnesstail
+'_certificate'','+CHAR(13)
+'START_DATE = ''09/20/2010'',EXPIRY_DATE = ''01/01/2099'';'+CHAR(13)+CHAR(13)+CHAR(13)+CHAR(13)
 
PRINT @stat
 
-----------------------------------------------------------
 
PRINT '-- ============================================='
 
 
SET  @stat='--2、创建镜像端点,同一个实例上只能存在一个镜像端点  '+CHAR(13)
+'--主机'+CHAR(13)
+'CREATE ENDPOINT Endpoint_Mirroring 
STATE = STARTED 
AS 
TCP ( LISTENER_PORT='+@LISTENER_PORT+' , LISTENER_IP = ALL ) 
FOR 
DATABASE_MIRRORING 
( AUTHENTICATION = CERTIFICATE HOST_'
+@masteriptail
+'_cert  , ENCRYPTION = REQUIRED ALGORITHM AES , ROLE = ALL );'+CHAR(13)
 
PRINT @stat
 
SET  @stat='--备机'+CHAR(13)
+'CREATE ENDPOINT Endpoint_Mirroring 
STATE = STARTED 
AS 
TCP ( LISTENER_PORT='+@LISTENER_PORT+' , LISTENER_IP = ALL ) 
FOR 
DATABASE_MIRRORING 
( AUTHENTICATION = CERTIFICATE HOST_'
+@mirroriptail
+'_cert  , ENCRYPTION = REQUIRED ALGORITHM AES , ROLE = ALL );'+CHAR(13)
 
PRINT @stat
 
 
SET  @stat='--见证'+CHAR(13)
+'CREATE ENDPOINT Endpoint_Mirroring
STATE = STARTED
AS
TCP ( LISTENER_PORT='+@LISTENER_PORT+' , LISTENER_IP = ALL )
FOR
DATABASE_MIRRORING
( AUTHENTICATION = CERTIFICATE HOST_'
+@witnesstail
+'_cert  , ENCRYPTION = REQUIRED ALGORITHM AES , ROLE = ALL );'+CHAR(13)+CHAR(13)+CHAR(13)
 
PRINT @stat
 
----------------------------------------------------------------------------------------
 
PRINT '-- ============================================='
 
 
SET  @stat='--3、备份证书,然后互换  '+CHAR(13)
+'--主机'+CHAR(13)
+'BACKUP CERTIFICATE HOST_'
+@masteriptail
+'_cert TO FILE = '+''''+@certpath+'\HOST_'+@masteriptail+'_cert.cer'';'+CHAR(13)
 
PRINT @stat
 
SET  @stat='--备机'+CHAR(13)
+'BACKUP CERTIFICATE HOST_'
+@mirroriptail
+'_cert TO FILE = '+''''+@certpath+'\HOST_'+@mirroriptail+'_cert.cer'';'+CHAR(13)
 
PRINT @stat
 
 
SET  @stat='--见证'+CHAR(13)
+'BACKUP CERTIFICATE HOST_'
+@witnesstail
+'_cert TO FILE = '+''''+@certpath+'\HOST_'+@witnesstail+'_cert.cer'';'+CHAR(13)+CHAR(13)+CHAR(13)
 
PRINT @stat
 
 
----------------------------------------------------------------------------------
 
PRINT '-- ============================================='
 
 
SET  @stat='--4、新增主备登陆用户  '+CHAR(13)
+'--主机'+CHAR(13)
+'CREATE LOGIN ['+@SLAVEHOST_NAME+'LoginUser] WITH PASSWORD = '''+@LOGINPWD+'''; 
CREATE USER ['+@SLAVEHOST_NAME+'User] FOR LOGIN ['+@SLAVEHOST_NAME+'LoginUser]; 
CREATE CERTIFICATE HOST_'
+@mirroriptail
+'_cert AUTHORIZATION ['+@SLAVEHOST_NAME+'User] FROM FILE ='''+@certpath+'\HOST_'+@mirroriptail+'_cert.cer'';'+CHAR(13)
+'GRANT CONNECT ON ENDPOINT::Endpoint_Mirroring TO ['+@SLAVEHOST_NAME+'LoginUser];'+CHAR(13)
 
PRINT @stat
 
 
 
 
 
 
SET  @stat='CREATE LOGIN ['+@WITNESSHOST_NAME+'LoginUser] WITH PASSWORD = '''+@LOGINPWD+'''; 
CREATE USER ['+@WITNESSHOST_NAME+'User] FOR LOGIN ['+@WITNESSHOST_NAME+'LoginUser]; 
CREATE CERTIFICATE HOST_'
+@witnesstail+
+'_cert AUTHORIZATION ['+@WITNESSHOST_NAME+'User] FROM FILE ='''+@certpath+'\HOST_'+@witnesstail+'_cert.cer'';'+CHAR(13)
+'GRANT CONNECT ON ENDPOINT::Endpoint_Mirroring TO ['+@WITNESSHOST_NAME+'LoginUser];'+CHAR(13)
 
PRINT @stat
 
 
 
 
 
SET  @stat='--备机'+CHAR(13)
+'CREATE LOGIN ['+@MASTERHOST_NAME+'LoginUser] WITH PASSWORD = '''+@LOGINPWD+'''; 
CREATE USER ['+@MASTERHOST_NAME+'User] FOR LOGIN ['+@MASTERHOST_NAME+'LoginUser]; 
CREATE CERTIFICATE HOST_'
+@masteriptail
+'_cert AUTHORIZATION ['+@MASTERHOST_NAME+'User] FROM FILE ='''+@certpath+'\HOST_'+@masteriptail+'_cert.cer'';'+CHAR(13)
+'GRANT CONNECT ON ENDPOINT::Endpoint_Mirroring TO ['+@MASTERHOST_NAME+'LoginUser];'+CHAR(13)
 
PRINT @stat
 
 
 
SET  @stat='CREATE LOGIN ['+@WITNESSHOST_NAME+'LoginUser] WITH PASSWORD = '''+@LOGINPWD+'''; 
CREATE USER ['+@WITNESSHOST_NAME+'User] FOR LOGIN ['+@WITNESSHOST_NAME+'LoginUser]; 
CREATE CERTIFICATE HOST_'
+@witnesstail+
+'_cert AUTHORIZATION ['+@WITNESSHOST_NAME+'User] FROM FILE ='''+@certpath+'\HOST_'+@witnesstail+'_cert.cer'';'+CHAR(13)
+'GRANT CONNECT ON ENDPOINT::Endpoint_Mirroring TO ['+@WITNESSHOST_NAME+'LoginUser];'+CHAR(13)
 
 
PRINT @stat
 
 
 
 
SET  @stat='--见证'+CHAR(13)
+'CREATE LOGIN ['+@MASTERHOST_NAME+'LoginUser] WITH PASSWORD = '''+@LOGINPWD+'''; 
CREATE USER ['+@MASTERHOST_NAME+'User] FOR LOGIN ['+@MASTERHOST_NAME+'LoginUser]; 
CREATE CERTIFICATE HOST_'
+@masteriptail
+'_cert AUTHORIZATION ['+@MASTERHOST_NAME+'User] FROM FILE ='''+@certpath+'\HOST_'+@masteriptail+'_cert.cer'';'+CHAR(13)
+'GRANT CONNECT ON ENDPOINT::Endpoint_Mirroring TO ['+@MASTERHOST_NAME+'LoginUser];'+CHAR(13)
 
PRINT @stat
 
 
 
 
 
 
SET  @stat='CREATE LOGIN ['+@SLAVEHOST_NAME+'LoginUser] WITH PASSWORD = '''+@LOGINPWD+'''; 
CREATE USER ['+@SLAVEHOST_NAME+'User] FOR LOGIN ['+@SLAVEHOST_NAME+'LoginUser]; 
CREATE CERTIFICATE HOST_'
+@mirroriptail
+'_cert AUTHORIZATION ['+@SLAVEHOST_NAME+'User] FROM FILE ='''+@certpath+'\HOST_'+@mirroriptail+'_cert.cer'';'+CHAR(13)
+'GRANT CONNECT ON ENDPOINT::Endpoint_Mirroring TO ['+@SLAVEHOST_NAME+'LoginUser];'+CHAR(13)
 
PRINT @stat
 
------------------------------------------------------------------------------
 
PRINT '-- ============================================='
 
 
 
SET  @stat='--5、各个机器都开放5022端口,并且用telnet测试5022端口是否开通 将下面三个脚本各自粘贴到bat文件里'+CHAR(13)
PRINT @stat
 
SET  @stat='echo 主库'+CHAR(13)
+'telnet '+@mirrorip+' 5022'+CHAR(13)
+'telnet '+@witness+' 5022'+CHAR(13)
+'pause'
 
PRINT @stat+CHAR(13)+CHAR(13)
 
SET  @stat='echo 镜像库'+CHAR(13)
+'telnet '+@masterip+' 5022'+CHAR(13)
+'telnet '+@witness+' 5022'+CHAR(13)
+'pause'
 
PRINT @stat+CHAR(13)+CHAR(13)
 
SET  @stat='echo 见证'+CHAR(13)
+'telnet '+@masterip+' 5022'+CHAR(13)
+'telnet '+@mirrorip+' 5022'+CHAR(13)
+'pause'
 
PRINT @stat+CHAR(13)+CHAR(13)+CHAR(13)
 
 
--------------------------------------------------------------
 
PRINT '-- ============================================='
 
 
 
SET  @stat='--6、备份数据库(完整备份+事务日志备份)在主机执行'+CHAR(13)
PRINT @stat
 
SET  @stat='DECLARE @FileName NVARCHAR(MAX)'+CHAR(13)+CHAR(13)
 
PRINT @stat
 
 
SET  @stat='--('+@DBName+'数据库完整备份)在主机执行'+CHAR(13)
+'SET @FileName = ''D:\DBBackup\'+@DBName+'_FullBackup_1.bak''
BACKUP DATABASE ['+@DBName+']
TO DISK=@FileName WITH FORMAT ,COMPRESSION'+CHAR(13)+CHAR(13)
 
PRINT @stat
 
 
SET  @stat='--('+@DBName+'数据库日志备份)在主机执行'+CHAR(13)
+'SET @FileName = ''D:\DBBackup\'+@DBName+'_logBackup_2.bak''
BACKUP LOG ['+@DBName+']
TO DISK=@FileName WITH FORMAT ,COMPRESSION'
 
PRINT @stat+CHAR(13)+CHAR(13)+CHAR(13)
 
------------------------------------------------------------------------------
 
PRINT '-- ============================================='
 
 
SET  @stat='--7、还原数据库(指定norecovery方式还原)在备机执行'+CHAR(13)
PRINT @stat
 
SET  @Restorepath1=''
 
SET @Restorepath2=@Restorepath+@DBName+'_FullBackup_1.bak'
SET @SQL = 'RESTORE FILELISTONLY  FROM DISK = '''+@Restorepath2+''''  
 
INSERT INTO #BackupFileList EXEC (@SQL);
 
 DECLARE @LNAME NVARCHAR(2000)
  DECLARE @PNAME NVARCHAR(2000)
 
 
        DECLARE CurTBName CURSOR
        FOR
            SELECT LogicalName,PhysicalName
            FROM    #BackupFileList  
 
        OPEN CurTBName
        FETCH NEXT FROM CurTBName INTO @LNAME,@PNAME
 
        WHILE @@FETCH_STATUS = 0
            BEGIN  
             SET  @Restorepath1=' MOVE N'''+@LNAME+''' TO N'''+@PNAME+''', '+CHAR(13)+@Restorepath1
 
 
                FETCH NEXT FROM CurTBName INTO  @LNAME,@PNAME
            END
        CLOSE CurTBName
        DEALLOCATE CurTBName
 
 
 
 
SET  @stat='USE [master]
RESTORE DATABASE '+@DBName+' FROM  DISK = N'''+@Restorepath+@DBName+'_FullBackup_1.bak'' WITH  FILE = 1,'+CHAR(13)
+@Restorepath1
+'NOUNLOAD,NORECOVERY,  REPLACE,  STATS = 5
GO'
 
SET  @stat='USE [master]
RESTORE LOG '+@DBName+' FROM  DISK = N'''+@Restorepath+@DBName+'_logBackup_2.bak'' WITH  FILE = 1,'+CHAR(13)
+'NOUNLOAD,NORECOVERY,  REPLACE,  STATS = 5
GO'
 
 
 
PRINT @stat+CHAR(13)+CHAR(13)
 
DROP TABLE #BackupFileList
 
--------------------------------------------------------------------------------
 
PRINT '-- ============================================='
 
 
 
SET  @stat='--8、增加镜像伙伴,需要先在备机上执行,再执行主机,镜像弄好之后,默认为事务安全等级为FULL'+CHAR(13)
PRINT @stat
 
 
 
 
SET  @stat='--备机上执行'+CHAR(13)
+'USE [master]
GO
 
ALTER DATABASE ['+@DBName+'] SET PARTNER = '''+'TCP://'+@masterip+':5022'';  --主机服务器的ip'+CHAR(13)+CHAR(13)
 
PRINT @stat
 
 
SET  @stat='--主机上执行'+CHAR(13)
+'USE [master]
GO
 
ALTER DATABASE ['+@DBName+'] SET PARTNER = '''+'TCP://'+@mirrorip+':5022'';  --镜像服务器的ip'+CHAR(13)+CHAR(13)
 
PRINT @stat
 
SET  @stat='ALTER DATABASE ['+@DBName+'] SET WITNESS = '''+'TCP://'+@witness+':5022'';  --见证服务器的ip'+CHAR(13)+CHAR(13)
 
PRINT @stat
本文的评论功能被关闭了.