1. Trang chủ
  2. » Công Nghệ Thông Tin

Tài liệu MASTERING SQL SERVER 2000- P21 doc

50 272 0
Tài liệu đã được kiểm tra trùng lặp

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Tiêu đề Replication
Trường học University of Information Technology
Chuyên ngành Database Management
Thể loại Tài liệu
Năm xuất bản 2000
Thành phố Ho Chi Minh City
Định dạng
Số trang 50
Dung lượng 2,03 MB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

On the Pull Subscription dialog box, select Northwind and click the Pull New Subscription button.. On the Choose Publication dialog box that comes up next, expand the pri-mary server not

Trang 1

company with different data-processing rules In this instance, select No andclick Next.

10 The next screen asks whether there are any subscribers that are not running SQL

Server 2000 This is because other database systems may not understand ties that are proprietary to SQL Server 2000 Also the snapshot can be stored in aformat that only SQL Servers will understand (a binary format), which willmake replication faster If there are third-party servers involved, the snapshotmust be stored in a format that they can read (character mode) Accept thedefaults here and click Next

proper-11 On the next screen, you need to choose what you will publish as an article.

Under Object Type, leave the default of Tables checked (this just limits the play to tables only) Then on the right side of the dialog box, check the box next

dis-to Employees dis-to enable it for publication

12 Click the ellipsis button next to the Employees table to bring up the properties

for the article (as tables are called in replication)

13 On the General tab of the Table Article Properties dialog box, change the

Desti-nation Table Name to Repl_Employees and click OK

Trang 2

14 Click OK to continue to the next screen, where you are warned about the

iden-tity replication issue and click Next

15 On the next screen, you are asked to select a publication name and description.

In the Publication Name box, enter Northwind Employees and leave the

description that is typed in for you Click Next

P A R TVI

Trang 3

16 You are now asked whether you would like to customize the publication further by

allowing anonymous subscribers or adding partitioning Select Yes and click Next

17 On the next screen, you are asked whether you would like to vertically or

hori-zontally partition the data in some of the articles Leave both boxes uncheckedand click Next

18 You are now asked whether you would like to allow anonymous subscribers to

access your publication If you select No, all subscribers must be registered inyour copy of Enterprise Manager (meaning that you can see them in EnterpriseManager) If you select Yes, any server can subscribe to your data Choose Yes ifyou intend to use pull subscriptions You will choose Yes here and click Next

19 On the next screen, you can change the schedule at which a snapshot is

cre-ated to refresh the subscribers This is done to make sure the subscriber isalways up-to-date You are going to use the default schedule and click Next

20 On the final screen, click Finish to create your publication.

21 You will now see a list of tasks that SQL Server must complete to create your

publication, after which you are presented with a dialog box informing you ofsuccess Click OK

Now you should look back at the Create and Manage Publications dialog box,where you should see a pink-book icon This is the icon used for snapshot replication(other types are different colors) If you click the Properties button, you will see a dia-log box that allows you to change any of the properties of the publication

This time you will pull the subscription so that you can see the difference in theprocess between pushing and pulling In this next series, you will pull the subscrip-tion to the SECOND server:

1 Please close any open dialog boxes and return to Enterprise Manager Once

there, click the SECOND server to select it

2 On the Tools menu, select Replication and click Pull Subscription to Server\second.

3 On the Pull Subscription dialog box, select Northwind and click the Pull New

Subscription button

4 On the welcome screen, check the box next to Show Advanced Options and

click Next to get started

Trang 4

5 On the next screen, you can opt to find a publication by browsing through the

available SQL Servers or searching Active Directory (assuming that you listed thepublication in Active Directory) Choose the option to Look at Publicationsfrom Registered Servers and click Next

P A R TVI

Trang 5

6 On the Choose Publication dialog box that comes up next, expand the

pri-mary server (not SECOND), select the Northwind Employees subscription,and click Next

7 You are now asked for the destination database; this is where the replicated data

will reside Because you changed the name of the destination table, you can useNorthwind as the target database and click Next

Trang 6

8 The next screen asks whether this should be an anonymous subscription.

Anonymous subscriptions are not registered at the publisher and are very usefulfor Internet subscriptions where FTP is used, because passwords are sent in cleartext and therefore are unsecured Leave the default of No, This Is a Named Sub-scription and click Next

9 You are now informed that the schema will be updated There is no choice

in the matter here, because on a pull subscription, SQL Server can detectwhether the schema exists Click Next to continue

P A R TVI

Trang 7

10 On this screen, you are asked from where to get the snapshot files This can be

the default (which is the distribution working folder) or a CD-ROM, FTP server,

or Zip drive or some other removable media In this instance, select the defaultlocation and click Next

11 On the next screen, you are asked for an update schedule Continuously means

that the server will check for updates and pull them over whenever there is achange in the data (this is a bad choice for snapshot replication, because itcopies the entire publication every time) The Schedule option allows you topick a specific time for updating, and the On Demand Only option will instructSQL Server not to replicate changes automatically—you will need to start repli-cation yourself using the Replication Monitor In this case, leave the defaultschedule and click Next

Trang 8

12 The next screen ensures that the SQLServerAgent service is running on the

pub-lisher If the service is not running, replication will fail Click Next to continue

13 On the last screen, click Finish to pull the subscription.

14 You will see a list of tasks that SQL Server must perform to pull the subscription,

after which a dialog box will inform you of success Click OK

With a snapshot publication in place and a pull subscription running, you can testthe replication In the next series of steps, you will open two copies of Query Ana-lyzer, one connected to each instance of SQL Server, and test the replication of data:

1 Open the first copy of Query Analyzer by selecting it from the SQL Server 2000

program group under Programs on the Start menu and log in to the primary

server (called first from here on out).

2 Open another copy of Query Analyzer and log in to the SECOND server (called

sec-ond from here on out) by typing server_name\SECOND in the Server Name box.

3 In the second copy of Query Analyzer, enter and execute the following code to

verify that replication worked:

USE Northwind

P A R TVI

Trang 9

INSERT Employees (LastName, FirstName, Title)VALUES (‘Frost’, ‘Jasmine’, ‘Developer’)

5 Wait for about 5 minutes (to give the server time to replicate), switch to the

sec-ond copy of Query Analyzer, and enter and execute the following code to seewhether the change replicated:

USE NorthwindSELECT * FROM Repl_EmployeesWHERE LastName = ‘Frost’

6 Close both copies of Query Analyzer once you are able to see the record in the

second copy

With that, you have successfully created and pulled a snapshot subscription Wecan now move on to merge replication

Creating and Subscribing to a Merge Publication

Merge replication is used when the publisher and all subscribers need to be able tomake changes to their local copy of the data and have those changes replicated to allother subscribers in the replication topology To demonstrate how this works, you willconfigure a merge publication on the Northwind database; then you will have the

Trang 10

SECOND server subscribe to the publication; finally you will modify the same record

in both databases and see how to deal with the subsequent conflict:

1 If you are not in Enterprise Manager, open it by selecting it from the

SQL Server 2000 group under Programs on the Start menu

2 Click your default server (not SECOND) in the contents pane and from the

Tools pull-down menu, select Replication and then click Create and ManagePublications

3 From the Create and Manage Publications dialog box, select Northwind and

click Create Publication to start the Create Publication Wizard

4 On the first screen of the Wizard, check the box next to Show Advanced

Options and click Next

5 On the second screen, you will select Northwind as the database to publish

from and click Next

6 On the next screen, you are asked whether you would like to use the existing

publication as a template This is handy if you are creating another, similartransactional subscription Because you are not, click No, I Will Define andthen click Next

7 On the next screen, you are asked what type of replication this is to be; choose

Merge and click Next

P A R TVI

Trang 11

8 The next screen asks whether all subscribers will be running SQL Server 2000 If

so, the snapshot can be stored in a format that only SQL Servers will stand (a binary format), which will make replication faster If there are third-party servers involved, the snapshot must be stored in a format that they canread (character mode) Not only that, but other database systems may notunderstand all of the proprietary properties in SQL Server 2000 Leave thedefault of servers running SQL Server 2000 and click Next

under-9 On the next screen, you need to choose what you will publish as an article.

Under Object Type, leave the default of Tables checked (this just limits the play to tables only) Then on the right side of the dialog box, check the boxnext to Customers to enable it for publication

dis-10 Click the ellipsis button next to the Customers table to bring up the properties

for the article (as tables are called in replication)

11 At the bottom of the General tab, you will see two choices; select the bottom

choice (changes to different columns in the same row will be merged) Here iswhat they do:

Treat Changes to the Same Row as a Conflict: If users at differentsubscribers make changes to the same row of a table, even if they changedata in different columns, it will be regarded as a conflict

Trang 12

Treat Changes to the Same Column as a Conflict: If users at ent subscribers make changes to the same row of a table, but differentcolumns, no conflict will occur If they make changes to the same column

differ-of the same row, a conflict will arise

12 Click the Snapshot tab.

13 You are presented with several choices if the Customers table exists already in

the subscription database You can keep it unchanged; drop it and re-create it;

keep it but delete all of the data that matches your row filter statement; or keep

it and delete all of the data You cannot simply use a different table name onthe subscriber, because merge replication needs the same table name on all sub-scribers In this case, you will use the default of dropping and re-creating theexisting table

14 Also on the Snapshot tab, there are several choices of objects to be transferred.

Leave all of the defaults (transferring all but extended properties and collation)

P A R TVI

Trang 13

15 The Resolver tab allows you to change the program used to resolve conflicts

that occur when multiple users make changes to the same data You will usethe default resolver here and click the Merging Changes tab

Trang 14

16 The Merging Changes tab is used to verify that the login used by the merge

agent has permission to perform INSERT, UPDATE, and DELETE actions Click

OK, then click Next

17 The next screen to pop up warns you that SQL Server must add a column with

a datatype of Uniqueidentifier to the Customers table Merge replication usesthis special datatype to ensure that each record is unique Click Next to createthe new column

P A R TVI

Trang 15

18 Click Next to continue to the next screen, where you are asked to select a

publi-cation name and description In the Publipubli-cation Name box, enter Northwind

Customers and leave the description that is typed in for you Click Next.

19 You are now asked whether you would like to customize the publication further

by allowing anonymous subscribers or adding partitioning Select Yes and clickNext

20 On the next screen, you are asked whether you would like to vertically or

hori-zontally partition the data in some of the articles Leave both of these optionsunchecked and click Next

21 You are now asked whether you would like to allow anonymous subscribers to

access your publication If you select No, all subscribers must be registered inyour copy of Enterprise Manager (meaning that you can see them in EnterpriseManager) If you select Yes, any server can subscribe to your data Choose Yes ifyou intend to use pull subscriptions You will choose Yes here and click Next

22 On the next screen, you can change the schedule at which a snapshot is

cre-ated to refresh the subscribers This is done to make sure the subscriber isalways up-to-date Use the default schedule and click Next

23 On the final screen, click Finish to create your publication.

24 You will now see a list of tasks that SQL Server must complete to create your

publication, after which you are presented with a dialog box informing you of

an error This error is expected in this exercise, because you are not replicatingany of the tables to which Customers is related via a foreign key Click Close todismiss the error

Now you should be back at the Create and Manage Publications dialog box, whereyou should see a yellow-book icon This is the icon used for merge replication (othertypes are different colors) If you click the Properties button, you will see a dialog boxthat allows you to change any of the properties of the publication

Let’s now push the publication to the SECOND server so that you can test mergereplication:

1 If you have opened the Properties dialog box, please close it and make sure you

are in the Create and Manage Publications dialog box

2 Select the Northwind Customers icon under Northwind and click the Push

New Subscription button

3 On the welcome screen of the Push Subscription Wizard, check the box next to

Show Advanced Options and click Next

4 On the second screen, you are asked to select a subscriber Select SECOND and

click Next

Trang 16

5 You are now asked for the target database; this is where the replicated data will

reside Because the Customers table cannot be dropped in the Northwind base on the subscriber, you need to replicate this to the pubs database There-

data-fore, enter pubs in the textbox and click Next.

6 On the next screen, you are asked to decide where the agent will run Choose

Subscriber if there are a lot of subscribers; choose Distributor if there are only afew subscribers (it is easier to manage that way) Here you will choose to runthe agent on the distributor and click Next

7 Now you can decide when to replicate changes to the subscribers

Continu-ously will replicate changes whenever one occurs Select ContinuContinu-ously andclick Next

8 You are asked whether you would like to initialize the schema at the

sub-scribers You should initialize the schema if you are replicating to a new base or have not yet created the tables on the subscriber If you have alreadycreated the schema to hold the data on the subscriber, select No In this case,select Yes and click Next

data-9 On the next screen, you are asked to set the subscription priority This is used

to resolve any conflicts that may arise when multiple users modify the samedata The server with the higher priority is given precedence The first choiceallows you to use the publisher’s priority setting to resolve conflicts, essentiallyallowing the publisher to win every time The second choice allows you to set anumber between 0.00 and 99.99 as the priority In this case, select the secondchoice and leave the setting as 75.00 Then click Next

P A R TVI

Trang 17

10 The next screen ensures that the SQLServerAgent service is running on the

pub-lisher If the service is not running, replication will fail Click Next to continue

11 On the last screen, click Finish to push the subscription.

12 You will see a list of tasks that SQL Server must perform to push the

subscrip-tion, after which a dialog box will inform you of success Click OK

With a merge publication in place and a push subscription running, you can testthe replication In the next series of steps, you will open two copies of Query Ana-lyzer, one connected to each instance of SQL Server, and test the replication of data:

1 Open the first copy of Query Analyzer by selecting it from the SQL Server 2000

program group under Programs on the Start menu and log in to the primary

server (called first from here on).

2 Open another copy of Query Analyzer and log in to the SECOND server (called

second from here on) by typing server_name\SECOND in the Server Name box.

3 In the second copy of Query Analyzer, enter and execute the following code to

verify that replication worked:

USE PubsSELECT * FROM Customers

4 Now to test merge replication, you will make a change to the same record on

both servers at the same time and see which change applies Enter the

follow-ing code in the first copy of Query Analyzer, but do not execute it yet:

USE NorthwindUPDATE CustomersSET ContactName = ‘Maria Andrews’

WHERE CustomerID = ‘ALFKI’

5 Now in the SECOND copy of Query Analyzer, enter the following code, but do

not execute it yet This will change the exact same data on the subscriber as was

changed on the publisher:

USE PubsUPDATE CustomersSET ContactName = ‘Mary Anders’

WHERE CustomerID = ‘ALFKI’

6 Execute the query in the SECOND copy of Query Analyzer.

7 Switch to the first copy of Query Analyzer and execute the query.

Trang 18

8 Wait for about 5 minutes (to give the server time to replicate), switch to the

second copy of Query Analyzer, and enter and execute the following code tosee whether the change replicated:

USE PubsSELECT * FROM CustomersWHERE CustomerID = ‘ALFKI’

9 When you see the value of Maria Andrews in the second copy of Query Analyzer,

close both copies of Query Analyzer—replication was successful

Now you have a small problem; notice that the subscriber now contains the valuethat was entered at the publisher (in the first copy of Query Analyzer) rather than the

value that was entered at the subscriber This is referred to as a conflict and can be

rec-tified in Enterprise Manager

1 In Enterprise Manager, expand your default server and select Northwind under

databases

2 Right-click Northwind, move to All Tasks, and click View Replication Conflicts.

3 The Microsoft Replication Conflict Viewer will come up and display a conflict.

P A R TVI

Trang 19

4 Click the View button.

5 In the Replication Conflict Viewer, you can see the rows that have conflicts The

winner is the server with the highest priority in this instance You have a fewchoices here to resolve the conflict:

• Keep the Winning Change will make the change from the winning serverpermanent

• Resolve with This Data will resolve the conflict with the data displayed inthe right column

• Postpone Resolution simply postpones the resolution of a conflict until alater time

• The Log Details checkbox will log the conflict for future reference

6 Make sure that Conflict Loser is displayed in the right column and click the

Resolve with This Data button

Trang 20

7 Once complete, close the Replication Conflict Viewer and switch to the second

copy of Query Analyzer

8 Enter and execute the following query to make sure the second update (Mary

Anders) is still there:

USE PubsSELECT * FROM CustomersWHERE CustomerID = ‘ALFKI’

9 Now switch to the first copy of Query Analyzer and run the following query to

make sure that the database was updated with the data from the second update:

USE NorthwindSELECT * FROM CustomersWHERE CustomerID = ‘ALFKI’

P A R TVI

Trang 21

10 Close both copies of Query Analyzer.

See what happened here? You created a merge publication and subscription thatallow users on both the subscribing and publishing servers to update the same data atthe same time When you updated the same data on both servers at once (the ALFKIrecord), the server with the highest priority tentatively won the conflict To make surethat the right data is in the databases after a conflict, you then opened the MicrosoftReplication Conflict Viewer and told SQL Server which data to keep in the databases.Now you have successfully created transactional, snapshot, and merge publications,and you have both pushed and pulled them to the subscribers However, what do you

do when replication stops working? That is the reason you have Replication Monitor

Using Replication Monitor

It has often been said that we are all at the mercy of our machines; this is especiallytrue regarding replication, because the process spans so many machines If one ofthese machines has a problem or a network link breaks down, replication will stop As

a SQL Server DBA, you will need to be able to find the cause of these problems andbring replication back online That is what Replication Monitor is for

Replication Monitor is found in Enterprise Manager on the distributor machine, and

is used to fire replication alerts (alerts are discussed in Chapter 17) and view the status

Trang 22

of each of the replication agents When you look into Replication Monitor, you willnotice two types of agents that we have not yet discussed: queue reader agents andmiscellaneous agents.

The queue reader agent is designed to read from queues that are used in queued

updat-ing, which is used when immediate updating subscribers do not have a reliable

connec-tion The miscellaneous agents are for cleaning and maintaining the distribution database,

making sure that old transactions are removed, old history is deleted, etc

For each of the agents, you can view or configure the agent profile, properties, andhistory

Agent profile: This can be used to modify how the agent runs—parameterssuch as the timeout value for connecting to the publisher or the batchsize forexporting records to a text file for a snapshot can be modified

Agent properties: Each agent that runs on the system is actually a job (asdiscussed in Chapter 17) The properties of these jobs can be changed underagent properties—details such as when the job runs or which operator gets e-mailed when the job fails are all set in the properties

Agent history: Replication jobs keep a history of events that occur duringthe process of replication This is the first place to look when there is a problemwith replication

As shown in Figure 27.8, when there is a problem with replication, you are ledright to the problem agent with a series of red X icons It is not very often that Xmarks the spot, so take advantage of it here

FIGURE 27.8

X marks the spot when there is trouble with one of the replication

agents.

P A R TVI

Trang 23

Let’s get some hands-on experience working with the Replication Monitor tools:

1 Open Enterprise Manager by selecting it from the SQL Server 2000 group under

Programs on your Start menu

2 On the first server (which is the distributor), expand Replication Monitor

3 If this is the first time you are opening the Replication Monitor, you will be

asked whether you would like the Replication Monitor to automatically refreshitself If you do not set this, you will need to manually refresh the monitor tosee changes in the status If you are presented with the Refresh Rate and Settingsscreen, check all three boxes and click OK

4 The agent profiles for the agents are almost identical, so you will look at the

snapshot agents To do this, select the snapshot agent under Agents

5 In the contents pane (on the right), right-click the Northwind Customers

publi-cation and select Agent Profile

Trang 24

6 On the Profiles screen, click the View Details button.

7 On the Details screen that comes up, notice all of the options that are available,

then click Cancel

P A R TVI

Trang 25

9 Right-click the Northwind Customers publication again and select Agent

Prop-erties This will bring up the properties for the job (for a detailed discussion ofjobs, please see Chapter 17) Click Cancel to return to Enterprise Manager

10 Right-click Northwind Customers and select Agent History This will show you

each step that was taken during replication

11 To view details about the session, click the Session Details button; this will detail

each step taken by replication If there is a problem with replication, this will

Ngày đăng: 26/01/2014, 08:20