当前位置:文档之家› SQLServer2008数据库复制

SQLServer2008数据库复制

通过SQLServer 2008数据库复制实现数据库同步备份SQL Server 2008数据库复制是通过发布/订阅的机制进行多台服务器之间的数据同步,我们把它用于数据库的同步备份。

这里的同步备份指的是备份服务器与主服务器进行实时数据同步,正常情况下只使用主数据库服务器,备份服务器只在主服务器出现故障时投入使用。

它是一种优于文件备份的数据库备份解决方案。

在选择数据库同步备份解决方案时,我们评估了两种方式:SQL Server 2008的数据库镜像和SQL Server 2008 数据库复制。

数据库镜像的优点是系统能自动发现主服务器故障,并且自动切换至镜像服务器。

但缺点是配置复杂,镜像数据库中的数据不可见(在SQL Server Man ageme nt Studio 中,只能看到镜像数据库处于镜像状态,无法进行任何数据库操作,最简单的查询也不行。

想眼见为实,看看镜像数据库中的数据是否正确都不行。

只有将镜像数据库切换主数据库才可见)。

如果你要使用数据库镜像,强烈推荐killkill写的SQL Server 2005镜像构建手册,我们就是按照这篇文章完成了数据库镜像部署测试。

最终,我们选择了SQL Server 2008 数据库复制。

下面通过一个示例和大家一起学习一下如何部署SQL Server 2008 数据库复制。

测试环境:Win dows Server 2008 R2 + SQL Server 2008R2 (英文版),两台服务器,一台主数据库服务器CNBIogsDB1,—台备份数据库服务器CNBIogsDB2 。

复制原理:我们采用的是基于快照的事务复制。

主数据库服务器生成快照,备份库服务器读取并加载该快照,然后不停地从主数据库服务器复制事务日志。

见下图:图片来自SQL Server联机丛书安装与配置步骤:一、在两台服务器上安装好SQL Server 2008 R2 ,主要安装的组件:Database Engin e(含SQL ServerReplication) ,Management Tools 。

二、主数据库服务器(发布服务器)的配置:1.在主数据库服务器CNBIogsDBI 新建示例数据库CNBIogsDemo (注意Recovery mode要使用默认值Full,只有这个模式才能进行事务复制),然后建立一张测试表,比如:CNBIogsTest。

-.J 匚MEjIagsDema'+ _1 Database LJiagrams戸丄Tables+ _j System TablesEl □日二(ColLmne 圍ID (intj not null)Msg 们曲忙亦「(5(臨not null)IE 丄Ke/s[±]Constraints十一1 Triggers[+1 IndexesTi 二1 Statistics2.设置存放快照的文件夹:创建发布之前,先设置一下存放快照的文件夹,创建发布后会在该文件夹生成快照文件, 阅服务器需要在初始化时加载该快照文件。

选择Replication (复制)》Local Publications (本地发布)》属性,在出现的窗口中选择Publishers (发布),如下图:点击红框处的按钮,出现设置窗口:3.在主数据库服务器创建发布:在Replication (复制)》Local Publications (本地发布)中选择New Publication (新建发布),出现一个向导。

先选择要发布的数据库CNBIogsDemo,然后选择发布类型Transational publicati on (事物发布),如下图:点击Next,出现错误:原来所有要复制的表都需要有主键,刚才建CNBIogsTest表时,没有建主键。

建一下主键, 并重新启动向导就可以了。

接着选择要复制的对象:f ' New Pubfic^ion WizardArticles$el6ct tdbloi and other objects to publish B & arttl^. Select columns to filter tables.helpNext > Einish »\\ Cancel点 Next , Next ,进入 Sn apshot Age nt 窗口,选择 Create a sn apshot immediately and keep the snapshot available toinitialize subscriptions ,见下图:Objech F Qpublish;S<io(w onlv checked artcles 11 宙亡 I 戒Next,进入Age nt Security选择Security Settings,进行相应的帐户设置:Snapshot Agent SecuritySpeiifv the Jonnairi or machine atcocinf urder ^hich the Snapshot Agentprocess 蝌ill 口此Run under the follciAflng Windows jccouril:Process ^ccoi.iritPassword:Confirm Password'& Run urider the SQL Server Agent service account (Thi? is noJ a recomrriended security be址practice.]Connect to the Pubhshet ------------------------------------------------------------------ 厂创iftipsrcor-iting lhe procest account阳Using ths following SOL Server login:Login:Fa理口毗Confirim Fasswarid:OK 1 C^ncd 旦裾一个是设置运行Sn apshot Age nt的Win dows帐户,我们这里选择与SQL Server Age nt 同样的帐户。

一个是设置连接发布服务器的SQL帐户,我们这里就用主数据库服务器的sa帐户。

继续:OK,Next,Next,为这个发布起个名字:点击Finish,就开始正式创建发布,创建成功就会出现如下窗口:■ New Publication WizardCreating PublicotianClick Stop to interrupt be oper^tionL2Total3Cuccess:0 Enoi0 WamingDetails:Action I Status| Message空Creating Public时加'CNEIogs:DerriC_Pn...Su亡e«s£ Addins 胡心心1 cf 1Jewess田E tarting lhe Sna pshoJ Agent Sucres览叩| Report ▼这时查看快照文件夹,就会看到unc文件夹,快照文件就在这个文件夹中。

这里要考虑这样一个问题,如何让订阅服务器通过网络访问这个快照文件夹。

我们在这个问题上折腾了一些时间,本来想通过共享文件夹的方式,但又不想打开匿名共享,折腾了半天,没搞定订阅服务器访问共享文件夹用户验证的问题。

于是采用了FTP的方式, 所以,下面介绍一下如何让订阅服务器通过FTP访问快照文件。

4.设置快照的FTP访问首先在主数据库服务器上开通FTP服务,建立一个指向快照文件夹的FTP站点,设置好可以远程连接的FTP帐户。

然后在这台发布服务器设置一下FTP客户端配置。

配置方法如下:在Replication》Local Publications 中选择刚才创建的发布[CNBIogsDemo]:CNBIogsDemo_Publication ,选择属性》FTP Snapshot,如下图:Access si^apshot files Ihiaugh FTP -肓 Allow 5ubscr ibers to dawnload snapshot files using FTP [File ?ransJe r Pr otocoQ选中 Allow Subscribers to download snapshotfiles using FTP ,并设置一下 FTP 客户端连接参数,订阅服务器就是通过这里的设置连接FTP 服务器的(注:Path from the FTP rootfolder 的设置要和上图一样,设置为:/ftp )。

点击OK ,这时会在快照文件夹中创建一个 ftp 文件夹,并在该文件夹中生成快照文件。

这样,发布服务器就配置好了,下面配置订阅服务器。

三、备份数据库服务器(订阅服务器)的配置:进入订阅服务器 CNBIogsDB2,创建与发布服务器同名的数据库 CNBIogsDemo ,使用完全恢复模式。

在 Replication 》Local Subscriptions 中选择 NewSubscriptions ,进入向导。

Next ,进入选择发布服务器的窗口, 选择Find SQL ServerPublisher ,出现服务器连接窗口:也1 £ub 泊讪ion Options *Tj PublicaAion Access List 2 Ag&rrt SecurityFTP seirer namei9SLl6&ik1S S number21 Path from the FTP root folder ^ftpLogjnP 日滋囱ordSubscriber 冋ill use the following information to access the snapshot files:Note the Snapshot Agent can automatic all put files in this folder if you specify it a snapshot fib location on the Snapshot page of thif dialog boM.驷 GeneraJJ Arhcies5 Filler Rowsi 盘 Srtapshcit这里要注意的是Server Name中一定要填写发布服务器的计算机名,如果计算机名连接不上,要在hosts文件中加一个IP 地址解析。

成功连接发布服务器之后,就可以看到刚才在主数据库服务器上创建的发布:智New Subscription WizardPublicationChoate the publicdtiori fa ifvhichyou want ho erects one or nnore subscxipNonsPubHsher:|YcS erverOOBData bases and pidjlicatons!i CNQIagsDemoi iCN Blo^De mo_Pub lic.a lion园S-jj ITPioBlogHelp Eirrwh〉》]CancelNext,进入分发代理工作位置”的选择窗口:Distribution Agent Lo cationChoosy wher? tpiun the Di^triLutisn ^gQnt(s],For frie subscriptions I cr&^te in this wizard「Run 制 agents a\ lh& Dstributor, VCSERWE ROOG (pusli subscriphons]Thb option makes : it easier te idmiri^tei the synchronization of subscriptions centrally.,f* R un each agen^ 嬴 it $ Subset iber [p 训汕 bscri^t io ns]T his opHtinthe p-oressinrj nveihead d the PistrilTufar aid IH5 fccli5 ubscuber administer the synchronizdtiori of it* s :ubscripHon.Run the wizard more tKsri cries if you seme ag&nts to n;r mt th 曰 D rshibutar and some to run at Subscritiers.Caned------------ -----------------我们这里选择pull subscript ons ,把数据给拉过来,这样主数据库服务器的负担会轻些。

相关主题