今天刚在学数据库的读写分离(主从分离),原本电脑上安装的SQL Server2012是Express版本的,在数据库-->复制下,只有本地订阅,没有本地发布,所以只能把这个版本的数据库删了,装上了Enterprise版本。
等待数据库装完之后,进入数据库就显示如下页面了

本地发布和本地订阅都有,为了实现数据库主从分离这个功能,我们还需要开启SQL Server代理服务器,打开本地电脑的服务,启动SQL Server代理服务(默认是没有启动的)

启动完服务就可以了开始实现我们要的功能了,首先,我们创建两个数据库TestA(做为主数据库)和TestB(做为从数据库),
主数据库的作用一般用于新增、删除、更改操作
从数据库可以有多个,一般用于查询操作,因为一般网站可能80%的访问是查询。
本次演示在同一台机器上进行。
1、新建本地发布

2、选中需要发布的数据库,以TestA为例

3、发布类型选择事务发布

4、把我们需要的表选上(示例只创建了一个表)

5、如果有需要,可以进行筛选表行

6、创建快照代理

7、选中快照代理安全性进行设置


8、填写发布名称,会在本地发布中显示

9、至此本地发布新建完成(会在本地发布中显示所写的发布名称)

10、接下来新建本地订阅(右键新建订阅,以TestB为例)

11、选中发布服务器(有几种选择,本机如下)

12、如果不是选择本机的(例如查找SQL Server 发布服务器)

13、分发代理位置

注意:这里我们选中推送订阅
当数据库的数据有变更的时候,就会通过订阅和发布系统通知各个从数据库更新
1、从数据库隔一段时间轮询主数据库,将主数据库中增量的脚本获取后执行(性能低下)
2、当主数据库有数据更新时,主数据库将增量的数据脚本自动推送给各个数据库执行(性能高效,也是我们使用的这种)
14、选择订阅服务器,也就是以我们的TestB为例

15、设置安全性


16、至此本地订阅发布成功
然而,却出现问题了,在本地订阅中并没有显示发布成功的数据库
通过一步步查看,在我们刚创建的本地发布中,右键‘查看快照代理状态’

发现ReplData文件夹下,访问被拒绝,因为需要给它设置一个Everyone的权限

这时候重新启动代理,就可以顺利启动了

本地订阅也可以显示了,并且数据顺利同步过去了

 |