我建议仅在宕机时间特别苛刻的环境下使用这种升级方法,在开始之前无论如何请做好数据库备份,代理作业和相关脚本的备份工作,根据我的经验,如果你花1个小时进行升级规划,那至少需要花5个小时来验证你的想法是否可行。
在SQL Server 2005实例上创建数据库,执行一次完整备份,然后备份一下事务日志,将最后的事务捕获下来,它将应用到镜像数据库上。
CREATE DATABASE NEEDTOUPGRADE ON PRIMARY
( NAME = N'NEEDTOUPGRADE', FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\DATA\NEEDTOUPGRADE.mdf' , SIZE = 2048KB , MAXSIZE = UNLIMITED, FILEGROWTH = 1024KB )
LOG ON
( NAME = N'NEEDTOUPGRADE_log', FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\DATA\NEEDTOUPGRADE_log.ldf' , SIZE = 1024KB , MAXSIZE = 2048GB , FILEGROWTH = 10%)
GO
( NAME = N'NEEDTOUPGRADE', FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\DATA\NEEDTOUPGRADE.mdf' , SIZE = 2048KB , MAXSIZE = UNLIMITED, FILEGROWTH = 1024KB )
LOG ON
( NAME = N'NEEDTOUPGRADE_log', FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\DATA\NEEDTOUPGRADE_log.ldf' , SIZE = 1024KB , MAXSIZE = 2048GB , FILEGROWTH = 10%)
GO
--执行完整备份
BACKUP DATABASE NEEDTOUPGRADE TO DISK = 'C:\NEEDTOUPGRADE_full_initial.bak'
--备份最近的事务日志
BACKUP LOG NEEDTOUPGRADE TO DISK = 'C:\NEEDTOUPGRADE_taillog_initial.trn'
接下来使用一个最基本的端点配置,启动端点为镜像做好准备。
CREATE ENDPOINT [Mirroring]
AUTHORIZATION [PMHC\tkrueger]
STATE=STARTED
AS TCP (LISTENER_PORT = 5022, LISTENER_IP = ALL)
FOR DATA_MIRRORING (ROLE = PARTNER, AUTHENTICATION = WINDOWS NEGOTIATE
, ENCRYPTION = REQUIRED ALGORITHM RC4)
AUTHORIZATION [PMHC\tkrueger]
STATE=STARTED
AS TCP (LISTENER_PORT = 5022, LISTENER_IP = ALL)
FOR DATA_MIRRORING (ROLE = PARTNER, AUTHENTICATION = WINDOWS NEGOTIATE
, ENCRYPTION = REQUIRED ALGORITHM RC4)
然后到SQL Server 2008数据库上,使用刚刚的完整备份还原数据库,接着应用备份的事务日志,完成后就可以启动数据库同步了。
--还原完整备份
RESTORE DATABASE NEEDTOUPGRADE
FROM DISK = 'C:\NEEDTOUPGRADE_full_initial.bak'
WITH NORECOVERY,
MOVE 'NEEDTOUPGRADE' TO N'C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\DATA\NEEDTOUPGRADE_mirror.mdf',
MOVE 'NEEDTOUPGRADE_log' TO N'C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\DATA\NEEDTOUPGRADE_mirror_log.ldf'
,REPLACE,NORECOVERY
GO
FROM DISK = 'C:\NEEDTOUPGRADE_full_initial.bak'
WITH NORECOVERY,
MOVE 'NEEDTOUPGRADE' TO N'C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\DATA\NEEDTOUPGRADE_mirror.mdf',
MOVE 'NEEDTOUPGRADE_log' TO N'C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\DATA\NEEDTOUPGRADE_mirror_log.ldf'
,REPLACE,NORECOVERY
GO
--应用最近的事务日志
RESTORE LOG NEEDTOUPGRADE FROM DISK = 'C:\NEEDTOUPGRADE_taillog_initial.trn' WITH NORECOVERY
GO
GO
现在SQL Server 2008上的数据库处于no recovery状态,我们可以通过端口5022的通信将其配置为镜像伙伴。
注意:我们的数据库版本现在仍然是611,SQL Server 2008的版本应该是655,我们将不能正常执行快照和其它与镜像相关的操作,我前面也提到了这是一个缺陷。
我们再在镜像(SQL Server 2008)上创建一个端点:
CREATE ENDPOINT [Mirroring]
AUTHORIZATION [PMHC\tkrueger]
STATE=STARTED
AS TCP (LISTENER_PORT = 5023, LISTENER_IP = ALL)
FOR DATA_MIRRORING (ROLE = PARTNER, AUTHENTICATION = WINDOWS NEGOTIATE
, ENCRYPTION = REQUIRED ALGORITHM RC4)
AUTHORIZATION [PMHC\tkrueger]
STATE=STARTED
AS TCP (LISTENER_PORT = 5023, LISTENER_IP = ALL)
FOR DATA_MIRRORING (ROLE = PARTNER, AUTHENTICATION = WINDOWS NEGOTIATE
, ENCRYPTION = REQUIRED ALGORITHM RC4)
现在配置数据库,使其成为一个伙伴。
ALTER DATABASE NEEDTOUPGRADE SET PARTNER= N'TCP://Servername:5022'