SQL Mirroring[Hot back up with Double machine]

Background:

It‘s fairly common for businesses to want to provide some high availability for their SQL Server databases, and one option is to have two SQL Server databases on separate machines with a SQL Server database mirrored. Microsoft provides mirroring out of the
box in SQL Server 2005 and SQL Server 2008, and is a much cheaper alternative than going down the clustering/failover route, but does provide some protection. In mirroring, there is always one Principal database which serves the requests, and a standby Mirror
that is always synchronizing. If the Principal database goes down, then the Mirror can be forced to become the Principal, and will then serve the requests. Once the original Principal is available again, it will become the new Mirror.

Restrictions on Backup and Restore During Mirroring

While a database mirroring session is active, the following restrictions apply:

  • Backup and restore of the mirror database are not allowed.
  • Backup of the principal database is allowed, but BACKUP LOG WITH NORECOVERY is not allowed.
  • Restoring the principal database is not allowed.

Script in Principal:

-- Step 1
USE master;
CREATE MASTER KEY ENCRYPTION BY PASSWORD =‘Password01!‘;
CREATE CERTIFICATE HOST_pri_cert WITH SUBJECT =‘HOST_minor certificate‘ ,
START_DATE = ‘01/11/2014‘, EXPIRY_DATE = ‘01/11/2015‘;

-- Step 2
alter ENDPOINT Endpoint_Mirroring
	STATE = STARTED
	AS		TCP ( LISTENER_PORT=50221 , LISTENER_IP = ALL )
	FOR		DATABASE_MIRRORING
	( AUTHENTICATION = CERTIFICATE HOST_pri_cert , ENCRYPTION = REQUIRED ALGORITHM AES , ROLE = ALL )

--SELECT * FROM sys.endpoints;
-- drop ENDPOINT Mirroring_Endpoint2
Go

-- Step 3
BACKUP CERTIFICATE HOST_pri_cert TO FILE = ‘D:\Hot backup\HOST_pri_cert.cer‘;

-- Step 4
use master
Alter LOGIN HOST_min_login WITH PASSWORD =‘Password01!‘;
CREATE USER HOST_min_user FOR LOGIN HOST_min_login;
--drop CERTIFICATE HOST_min_cert
CREATE CERTIFICATE HOST_min_cert AUTHORIZATION HOST_min_user FROM FILE =‘D:\Hot backup\HOST_min_cert.cer‘;
GRANT CONNECT ON ENDPOINT::Endpoint_Mirroring TO [HOST_min_login]; 

-- select * from sys.certificates
SELECT * FROM SYS.DATABASE_PRINCIPALS

Alter LOGIN HOST_pro_login WITH PASSWORD =‘Password01!‘;
CREATE USER HOST_pro_user FOR LOGIN HOST_pro_login;
drop CERTIFICATE HOST_pro_cert
CREATE CERTIFICATE HOST_pro_cert AUTHORIZATION HOST_pro_user FROM FILE =‘D:\Hot backup\HOST_pro_cert.cer‘;
GRANT CONNECT ON ENDPOINT::Endpoint_Mirroring TO [HOST_pro_login];

-- select * from sys.database_principals
GO

--  Step 5
USE master;
ALTER DATABASE TestBackup01
SET RECOVERY FULL;
Go

--  Step 6
USE TestBackup01

--back up data
GO
BACKUP DATABASE [TestBackup01] TO  DISK = N‘D:\Hot backup\testbackup01.bak‘ WITH NOFORMAT, NOINIT,  NAME = N‘TestBackup01-Full Database Backup‘, SKIP, NOREWIND, NOUNLOAD,  STATS = 10
GO

BACKUP LOG [TestBackup01] TO  DISK = N‘D:\Hot backup\TestBackup01.trn‘ WITH NOFORMAT, NOINIT,  NAME = N‘TestBackup01-Transaction Log  Backup‘, SKIP, NOREWIND, NOUNLOAD,  STATS = 10
GO
 --select * from sys.database_mirroring_endpoints

Script in Mirror:

-- Step 1
USE master;
CREATE MASTER KEY ENCRYPTION BY PASSWORD =‘Password01!‘;
CREATE CERTIFICATE HOST_min_cert WITH SUBJECT =‘HOST_minor certificate‘ ,
START_DATE = ‘01/11/2014‘, EXPIRY_DATE = ‘01/11/2015‘;

-- Step 2
Alter ENDPOINT Endpoint_Mirroring
	STATE = STARTED
	AS TCP ( LISTENER_PORT=50221 , LISTENER_IP = ALL )
	FOR DATABASE_MIRRORING ( AUTHENTICATION = CERTIFICATE HOST_min_cert , ENCRYPTION = REQUIRED ALGORITHM AES , ROLE = ALL )
	-- drop endpoint Mirroring
    -- SELECT * FROM sys.endpoints;
Go

-- Step 3
BACKUP CERTIFICATE HOST_min_cert TO FILE = ‘D:\Hot backup\HOST_min_cert.cer‘;
--SELECT * FROM sys.certificates;
Go

-- Step 4
CREATE LOGIN HOST_pri_login WITH PASSWORD =‘Password01!‘;
CREATE USER HOST_pri_user FOR LOGIN HOST_pri_login;
drop CERTIFICATE HOST_pri_cert
CREATE CERTIFICATE HOST_pri_cert AUTHORIZATION HOST_pri_user FROM FILE =‘D:\Hot backup\HOST_pri_cert.cer‘;
GRANT CONNECT ON ENDPOINT::Endpoint_Mirroring TO [HOST_pri_login];      

CREATE LOGIN HOST_pro_login WITH PASSWORD =‘Password01!‘;
CREATE USER HOST_pro_user FOR LOGIN HOST_pro_login;
drop CERTIFICATE HOST_pro_cert
CREATE CERTIFICATE HOST_pro_cert AUTHORIZATION HOST_pro_user FROM FILE =‘D:\Hot backup\HOST_pro_cert.cer‘;
GRANT CONNECT ON ENDPOINT::Endpoint_Mirroring TO [HOST_pro_login];

--select * from sys.database_principals

 -- Step 5
create database TestBackup01USE master;
ALTER DATABASE TestBackup01SET RECOVERY FULL;
RESTORE DATABASE TestBackup01 FROM DISK = ‘D:\Hot backup\backup_TestBackup01.bak‘ WITH FILE = 1, NORECOVERY, NOUNLOAD;
RESTORE LOG TestBackup01 FROM DISK = ‘D:\Hot backup\backup_TestBackup01.trn‘ WITH FILE = 2, NORECOVERY, NOUNLOAD

Script in Witness:

-- Step 1
USE master;
CREATE MASTER KEY ENCRYPTION BY PASSWORD =‘Password01!‘;
CREATE CERTIFICATE HOST_pro_cert WITH SUBJECT = ‘HOST_Witness certificate‘ ,
START_DATE = ‘01/11/2014‘, EXPIRY_DATE = ‘01/11/2015‘;

-- Step 2
alter ENDPOINT Endpoint_Mirroring
	STATE = STARTED
	AS TCP ( LISTENER_PORT=50221 , LISTENER_IP = ALL )
	FOR DATABASE_MIRRORING
    ( AUTHENTICATION = CERTIFICATE HOST_pro_cert , ENCRYPTION = REQUIRED ALGORITHM AES , ROLE = ALL )
	select * from sys.endpoints
Go

-- Step 3
BACKUP CERTIFICATE HOST_pro_cert TO FILE = ‘D:\Hot backup\HOST_pro_cert.cer‘;  --Note: Full Name

-- Step 4
CREATE LOGIN HOST_min_login WITH PASSWORD =‘Password01!‘;
CREATE USER HOST_min_user FOR LOGIN HOST_min_login;
CREATE CERTIFICATE HOST_min_cert AUTHORIZATION HOST_min_user FROM FILE =‘D:\Hot backup\HOST_min_cert.cer‘;
GRANT CONNECT ON ENDPOINT::Endpoint_Mirroring TO [HOST_min_login];   

CREATE LOGIN HOST_pri_login WITH PASSWORD =‘Password01!‘;
CREATE USER HOST_pri_user FOR LOGIN HOST_pri_login;
CREATE CERTIFICATE HOST_pri_cert AUTHORIZATION HOST_pri_user FROM FILE =‘D:\Hot backup\HOST_pri_cert.cer‘;
GRANT CONNECT ON ENDPOINT::Endpoint_Mirroring TO [HOST_pri_login];
--select * from sys.database_principals
Go

-- Step 5
Create database TestBackup01

USE master;
ALTER DATABASE TestBackup01
SET RECOVERY FULL;
RESTORE DATABASE TestBackup01 FROM DISK = ‘D:\Hot backup\backup_TestBackup01.bak‘ WITH FILE = 1, NORECOVERY, NOUNLOAD;
RESTORE LOG TestBackup01 FROM DISK = ‘D:\Hot backup\backup_TestBackup01.trn‘ WITH FILE = 2, NORECOVERY, NOUNLOAD
Go

Note:

1. copy the certificates over to the other server(Mirror, Witness and Principal).

2. After coping the certificates over I setup the logins.

3. Back up and restore db with log.

  • Perform a full backup of the database on the Principal server.
  • Perform a Transaction Log backup on the Principal server.
  • Copy the backup file to the Mirror.
  • Important: Do a restore of the full backup into a new step, but before doing the restore, go to Options, then ensure you check the No Recovery option! This is vital!
  • Perform another restore of the Transaction Log, also with the No Recovery option. (This is important, otherwise you‘ll get an error when starting the mirror - See Gotchas section for explanation).
时间: 2024-12-07 05:05:36

SQL Mirroring[Hot back up with Double machine]的相关文章

Lync 2013安装中遇到的关于SQL Mirroring的一次报错的解决

Problem Description ================= Following the Lync Deployment Wizard to setup Database Mirroring with a witness for Lync 2013, it fails.   Story ================= 1. When I filled all the blanks in deployment wizard, and tried to publish topolo

[SQL in Azure] Provisioning a SQL Server Virtual Machine on Azure

http://azure.microsoft.com/en-us/documentation/articles/virtual-machines-provision-sql-server/ Provisioning a SQL Server Virtual Machine on Azure The Azure virtual machine gallery includes several images that contain Microsoft SQL Server. You can sel

sql monitor生成不了报告& FFS hint不生效两个问题思考

事情的发生就是这么偶然,一步步的深入才能汲取到更深入的知识~~ -------------------START-------------------------------------------   来了一个query running longer than 4hours的邮件,来看看里面有哪些sql: SID    SERIAL#    INST_ID SQL_ID        Run_in_sec OS_user     MACHINE       SQL_TEXT         

阿里巴巴2016笔试第一题 sql语句关键字匹配

阿里巴巴这道题和微软的笔试题比较类似,相对来讲更简单一些,可以将解决那道题的思路用在这道题上. /*题目简单描述: 根据给出的每个sql语句的执行时间,计算单个关键字的执行时间 *//* 样例输入:32.5 insert into tableA values(1,"columnA","columnB")12.5 insert into tableB values(2,"columnA","columnB","colu

SQL SERVER – Beginning of SQL Server Architecture – Terminology – Guest Post

AUGUST 30, 2012 BY PINAL DAVE SQL SERVER – Beginning of SQL Server Architecture – Terminology – Guest Post SQL Server Architecture is a very deep subject. Covering it in a single post is an almost impossible task. However, this subject is very popula

Lycn 2013 with SQL AlwaysOn 「一」建立AlwaysOn

注意:微软官方并未正式宣布Lync2013能够使用Sql Server的AlwaysOn高可用性技术,本文只是采取一些旁门左道取巧的办法达到该目的,其实施完成后还存在一些问题和风险,搭来稳定还好,如果真出了啥岔子--阅读本文也需要有一定的Lync2013实施经验,因为其中省略了一些大家熟知的步骤(主要是懒得截图凑篇幅,如果有步骤不明白可以留言交流) 目前据我们所知,Lync的后端SQL数据库高可用只能有两种办法 1.SQL clustering 2.SQL mirroring (Database

Spark SQL UDF使用

Spark1.1推出了Uer Define Function功能,用户可以在Spark SQL 里自定义实际需要的UDF来处理数据. 因为目前Spark SQL本身支持的函数有限,一些常用的函数都没有,比如len, concat...etc 但是使用UDF来自己实现根据业务需要的功能是非常方便的. Spark SQL UDF其实是一个Scala函数,被catalyst封装成一个Expression结点,最后通过eval方法计根据当前Row计算UDF的结果,源码分析见:Spark SQL源码分析之

非常全面的SQL Server巡检脚本来自sqlskills团队的Glenn Berry 大牛

-- SQL Server 2012 Diagnostic Information Queries -- Glenn Berry -- April 2015 -- Last Modified: April 27, 2015 -- http://sqlserverperformance.wordpress.com/ -- http://sqlskills.com/blogs/glenn/ -- Twitter: GlennAlanBerry -- Please listen to my Plura

[SQL in Azure] Getting Started with SQL Server in Azure Virtual Machines

This topic provides guidelines on how to sign up for SQL Server on a Azure virtual machine and how to get started creating SQL Server databases in Microsoft public cloud environment. With SQL Server in Azure Virtual Machines, you get the full benefit