Sqlserver 2012 for windows 2012 镜像库构建与读操作

目录

 

1.测试验证环境

2、建立互信

2.1、主库操作

2.2、镜像库操作

3、主库数据同步

3.1、主库

3.2、镜像库

4、建立镜像map关系

4.1、镜像库

4.2、主库

5、使用快照读数据库

6、拆除镜像库


1.测试验证环境

 

服务器角色

机器名

IP

SQL Server Ver(select @@VERSION)

db

WIN-82QUK19NNLH 192.168.68.137 Microsoft SQL Server 2012 (SP1) - 11.0.3128.0 (X64) 
mirror db

Winbak

192.168.68.139 Microsoft SQL Server 2012 (SP1) - 11.0.3128.0 (X64) 

2、建立互信

2.1、主库操作

--创建数据库主密钥
USE master   
GO   
CREATE MASTER KEY ENCRYPTION BY PASSWORD = 'Pa$$w0rd';

select top 100 is_master_key_encrypted_by_server,* from sys.databases;



--创建证书,并用主密钥加密
USE master   
GO   
CREATE CERTIFICATE Host_A_Cert    
WITH Subject = 'Host_P Certificate',   
Expiry_Date = '2050-1-1'; --过期日期 


--创建端点,并复制到对端
IF NOT EXISTS ( SELECT  1   
                FROM    sys.database_mirroring_endpoints )   
    BEGIN   
        CREATE ENDPOINT [DatabaseMirroring] STATE = STARTED AS TCP ( LISTENER_PORT = 5022,   
            LISTENER_IP = ALL ) FOR DATABASE_MIRRORING ( AUTHENTICATION =   
            CERTIFICATE Host_A_Cert, ENCRYPTION = REQUIRED Algorithm AES, ROLE =   
            ALL );   
    END 

--备份证书
BACKUP CERTIFICATE Host_A_Cert   
TO FILE = 'C:\mirror\Host_A_Cert.cer';
   
--创建登录账号

CREATE LOGIN Host_B_Login WITH PASSWORD = 'Pa$$w0rd';


--创建用户,并映射到Step 5中创建的登录账号中
CREATE USER Host_B_User For Login Host_B_Login;

--使用证书授权用户
CREATE CERTIFICATE Host_B_Cert   
AUTHORIZATION Host_B_User   
FROM FILE = 'C:\mirror\Host_S_Cert.cer';  


select * from sys.certificates;

--把Step 5中的登录账号授权访问端口
GRANT CONNECT ON ENDPOINT::[DatabaseMirroring] TO [Host_B_Login];  





2.2、镜像库操作

--创建数据库主密钥
USE master   
GO   
CREATE MASTER KEY ENCRYPTION BY PASSWORD = 'Pa$$w0rd';

select top 100 is_master_key_encrypted_by_server,* from sys.databases;



--创建证书,并用主密钥加密
USE master   
GO   
CREATE CERTIFICATE Host_S_Cert    
WITH Subject = 'Host_S Certificate',   
Expiry_Date = '2050-1-1'; --过期日期 


--创建端点
IF NOT EXISTS ( SELECT  1   
                FROM    sys.database_mirroring_endpoints )   
    BEGIN   
        CREATE ENDPOINT [DatabaseMirroring] STATE = STARTED AS TCP ( LISTENER_PORT = 5022,   
            LISTENER_IP = ALL ) FOR DATABASE_MIRRORING ( AUTHENTICATION =   
            CERTIFICATE Host_S_Cert, ENCRYPTION = REQUIRED Algorithm AES, ROLE =   
            ALL );   
    END 

--创建端点,并复制到对端
BACKUP CERTIFICATE Host_A_Cert   
TO FILE = 'C:\miroor\Host_S_Cert.cer';
   
--创建登录账号

CREATE LOGIN Host_A_Login WITH PASSWORD = 'Pa$$w0rd';


--创建用户,并映射到Step 5中创建的登录账号中
CREATE USER Host_A_User For Login Host_A_Login;

--使用证书授权用户
CREATE CERTIFICATE Host_A_Cert   
AUTHORIZATION Host_A_User   
FROM FILE = 'C:\miroor\Host_A_Cert.cer';  

--把Step 5中的登录账号授权访问端口
GRANT CONNECT ON ENDPOINT::[DatabaseMirroring] TO [Host_A_Login];  





 

3、主库数据同步

3.1、主库

BACKUP DATABASE test TO DISK='c:\bak\db.bak' WITH FORMAT 
GO 


CREATE TABLE test.dbo.TB_test(ID int) 
GO

BACKUP LOG test TO DISK='c:\bak\db_log.bak' WITH FORMAT 
GO 

3.2、镜像库

RESTORE DATABASE test FROM DISK='c:\bak\db.bak' WITH REPLACE,NORECOVERY 
GO 
--将事务日志还原到删除操作前(这里的时间对应上面的删除时间,并比删除时间略早 

RESTORE LOG test FROM DISK='c:\bak\db_log.bak' WITH NORECOVERY 
GO 

4、建立镜像map关系


4.1、镜像库

ALTER DATABASE [test] 
    SET PARTNER = 'TCP://192.168.68.137:5022';
GO

4.2、主库

ALTER DATABASE [test] 
    SET PARTNER = 'TCP://192.168.68.138:5022';
GO

 

5、使用快照读数据库

快照库只能读当时创建时间点的数据。

create database snap_test2
on ( name = test ,filename = 'C:\Program Files\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\DATA\test_data_1902.ss')
AS SNAPSHOT OF test;
go

6、拆除镜像库

 

alter database test set partner off;

restore database test with recovery;

 

你可能感兴趣的:(sqlserver,sqlserver,镜像,mirror,只读打开)