最近在搞在SQL 2005安装数据库镜像,中间遇到不少的错误,在此归纳总结,以方便有需要的朋友参考。
直接上脚本,主机部分:
---修改数据库为完整恢复模式
USE master;
ALTER DATABASE CZTest
SET RECOVERY FULL;
--------创建证书
USE master;
CREATE MASTER KEY ENCRYPTION BY PASSWORD = ‘23987hxJ#KL95234nl0zBe‘;--创建数据库密钥,并提供密钥使用密码
CREATE CERTIFICATE HOST_A_cert WITH SUBJECT = ‘HOST_A certificate‘ ,
START_DATE = ‘07/08/2014‘;
--------创建连接端口
CREATE ENDPOINT Endpoint_Mirroring
STATE = STARTED
AS
TCP ( LISTENER_PORT=5022 , LISTENER_IP = ALL )
FOR
DATABASE_MIRRORING
( AUTHENTICATION = CERTIFICATE HOST_A_cert , ENCRYPTION = REQUIRED ALGORITHM AES , ROLE = ALL );
----------备份证书
BACKUP CERTIFICATE HOST_A_cert TO FILE = ‘D:\SQLBackup\HOST_A_cert.cer‘;
----------互换证书
--操作完毕
----------添加登陆名和用户
CREATE LOGIN Login_B WITH PASSWORD = ‘2014sa‘;
CREATE USER User_B FOR LOGIN Login_B;
CREATE CERTIFICATE HOST_B_cert AUTHORIZATION User_B FROM FILE = ‘D:\SQLBackup\HOST_B_cert.cer‘;
GRANT CONNECT ON ENDPOINT::Endpoint_Mirroring TO Login_B;--准许HOST_B_login登陆名连接到端口
-----------手工同步登录名和密码
----login B的sid=0x5AA61C2F8FFA0B45AC27EEB148083CD9
select sid,name from syslogins;
----创建镜像(未成功 原因:1.数据库完整备份,事务日志也要完整备份
---- 2.创建镜像的SQL要首先在备份库中执行,然后在主库执行。顺序不可颠倒)
---- 3.在端点配置中禁用了数据库镜像传输功能:解决办法:DBCC TRACEON(1400,-1)或者安装SP1
---- 4.在安装SP1时遇到了身份验证不能通过,错误显示两个实例,其中还有一个是reporting services,打开reporting services配置,选中数据库连接,配置一下自己的 账户,
ALTER DATABASE ceshi SET PARTNER = ‘TCP://192.168.137.13:5022‘;
ALTER DATABASE HxCreditBuy SET PARTNER = ‘TCP://192.168.137.13:5022‘;
-----下面这句执行成功
ALTER DATABASE CZTest SET PARTNER = ‘TCP://192.168.137.13:5022‘;
-------------==========检查脚本======================
DROP LOGIN HOST_B_login
DROP USER HOST_B_user
DBCC TRACEON(1400,-1)
DROP MASTER KEY
DROP CERTIFICATE HOST_A_cert
--检查主机和备机的证书是否存在:
select name, start_date, expiry_date from sys.certificates where name not like ‘##%‘
--删除镜像数据库证书
--drop the old cert for the principal login
use master
DROP CERTIFICATE HOST_A_cert
GO
use master
DROP CERTIFICATE HOST_B_cert
GO
--检查镜像端口
SELECT * FROM sys.endpoints where type=4
--删除端口
use master
DROP Endpoint Endpoint_Mirroring
GO
------------------------------------------------------------------备机脚本------------------------------------------
--------------备机 登录名 sa 密码 sa
--------创建证书
USE master;
CREATE MASTER KEY ENCRYPTION BY PASSWORD = ‘23987hxJ#KL95234nl0zBe‘;
CREATE CERTIFICATE HOST_B_cert WITH SUBJECT = ‘HOST_B certificate‘ ,
START_DATE = ‘07/08/2014‘;
--------创建连接端口
CREATE ENDPOINT Endpoint_Mirroring
STATE = STARTED
AS
TCP ( LISTENER_PORT=5022 , LISTENER_IP = ALL )
FOR
DATABASE_MIRRORING
( AUTHENTICATION = CERTIFICATE HOST_B_cert , ENCRYPTION = REQUIRED ALGORITHM AES , ROLE = ALL );
---------备份证书
BACKUP CERTIFICATE HOST_B_cert TO FILE = ‘C:\SQLBackup\HOST_B_cert.cer‘;
---------互换证书
------操作完毕
------添加登录名和用户
CREATE LOGIN Login_A WITH PASSWORD = ‘2014sa‘;
CREATE USER User_A FOR LOGIN Login_A;
CREATE CERTIFICATE HOST_A_cert AUTHORIZATION User_A FROM FILE = ‘C:\SQLBackup\HOST_A_cert.cer‘;
GRANT CONNECT ON ENDPOINT::Endpoint_Mirroring TO Login_A;
-----手工同步登录名
USE master;
exec sp_addlogin
@loginame = ‘Login_B‘,
@passwd = ‘2014sa‘,
@sid = 0x5AA61C2F8FFA0B45AC27EEB148083CD9 ;
----创建镜像(未成功 原因:1.数据库完整备份,事务日志也要完整备份
---- 2.创建镜像的SQL要首先在备份库中执行,然后在主库执行,顺序不可颠倒)
ALTER DATABASE HxCreditBuy SET PARTNER = ‘TCP://lidongsh-a2628e:5022‘;
ALTER DATABASE ceshi SET PARTNER = ‘TCP://lidongsh-a2628e:5022‘;
-----下面这句执行成功
ALTER DATABASE CZTest SET PARTNER = ‘TCP://lidongsh-a2628e:5022‘;
----------===================================================================