Replication Overview


1- What is SQL Replication?

SQL replication is a technology designed to allow storing identical data in multiple locations. First lets examine why replication may be useful and how it solves common data distribution problems. There are several classical examples in which SQL replication solves business problems. One of the most popular ones is the case when a business has mobile employees, who need to access data from their portable computers while they are away from they office. Another example is when the workforce of a business is distributed around the world and all employees need to access one and the same set of data, but network connectivity has poor quality. In both the above examples using SQL replication is the right thing to do. Replication is used in many other scenarios as well for example as a backup solution, and for offloading database intensive processing like reporting and data mining from main live databases.

2- Types of SQL Replication:

  • Snapshot replication

The publisher simply takes a snapshot of the entire replicated database and shares it with the subscribers.  Of course, this is a very time and resource-intensive process.  For this reason, most administrators don’t use snapshot replication on a recurring basis for databases that change frequently

  • Transactional replication

With transactional replication, the replication agent monitors the publisher for changes to the database and transmits those changes to the subscribers.  This transmission can take place immediately or on a periodic basis.

  • Merge replication

Merge replication, like transactional replication, typically starts with a snapshot of the publication database objects and data. Subsequent data changes and schema modifications made at the Publisher and Subscribers are tracked with triggers. The Subscriber synchronizes with the Publisher when connected to the network and exchanges all rows that have changed between the Publisher and Subscriber since the last time synchronization occurred.

3- Replication Overview

  • Publisher

Publisher Is database instance that’ allowed the Replicate data to the Subscribers Databases instances Publisher can sent to one or more Subscriber Database

  • Distributor

The Distributor is a database instance that acts as a store for replication specific data associated with one or more Publishers The distribution database stores replication status data, metadata about the publication, and, in some cases, acts as a queue for data moving from the Publisher to the Subscribers

  • Subscribers

A Subscriber is a database instance that receives replicated data. A Subscriber can receive data from multiple Publishers and publications. Depending on the type of replication chosen, the Subscriber can also pass data changes back to the Publisher or republish the data to other Subscribers.

  • Article

database object that is included in a publication this Publication Contain difference type like (table , View , Stored ).

********************************************************************************************************************************

I hope that this will be useful topic

Regards

Mostafa Elmasry

References

http://technet.microsoft.com/en-us/library/ms151314(v=sql.105).aspx

MSSQL 2008R2 Mirroring in Workgroup


Problem

Now if I have 3 Servers with 3 SQl server Instance but this servers not trusted = Not in domain this servers in workgroup but now I won’t to create mirroring but the last step in Mirroring Configuration need from the Services Account What I will write and the servers with no trusted . so what we can do to make this servers is trusted .

Solution

We will make link between this SQL Server instance this link not the linked server feature but that’s meaning we will make master key and Certificate on all Servers then Create users in the 3 Servers then give grant Connection to this Users on this Mirroring Endpoint Example

Server name                            Certification                user

Server A(Principle)                 CERT_A                       User_A

Server B (Mirror)                     CERT_B                       User_B

Server C (Witness)                   CERT_C                       User_C

now I create difference Certificate  and user on the servers then I will

Create Certificate Server B and C  on server A

Create Certificate Server A and C on Server B

Create Certificate Server A and B on Server C

Also I make this Steps on user then give the 3 user in the Server A and B and C grant Connection on Mirroring Endpoint by this Way I make Bridge between the 3 Servers

Step 1 On Principle Server :

USE MASTER

GO

CREATE MASTER KEY ENCRYPTION BY PASSWORD =‘Admin123’

GO

CREATE CERTIFICATE Principle_cert

WITH SUBJECT = ‘Principle certificate’

GO

CREATE ENDPOINT End_Mirroring

STATE = STARTED

ASTCP (LISTENER_PORT = 5025 , LISTENER_IP=ALL)

FOR DATABASE_MIRRORING

(AUTHENTICATION = CERTIFICATE Principle_cert , ENCRYPTION=REQUIRED ALGORITHM RC4 , ROLE=ALL)

GO

BACKUP CERTIFICATE Principle_cert

TO FILE =‘C:\certificate\Principle_cert.cer’

GO

Step no 2 On Mirror Server :

USE master

GO

CREATE MASTER KEY ENCRYPTION BY PASSWORD = ‘Admin123’

GO

CREATE CERTIFICATE Mirror_cert WITH SUBJECT = ‘Mirror certificate’

GO

CREATE ENDPOINT End_Mirroring

STATE=STARTED

ASTCP (LISTENER_PORT= 5026,LISTENER_IP=ALL)

FOR DATABASE_MIRRORING

(AUTHENTICATION=CERTIFICATE Mirror_cert , ENCRYPTION = REQUIRED ALGORITHM RC4 , ROLE=ALL)

GO

BACKUP CERTIFICATE Mirror_cert

TO FILE=‘D:\certificate\Mirror_cert.cer’;

GO

Step No 3 on Principle Server : 

Copy the Certificate ” Mirror_cert  ” From Mirror Server to the Principle Server because I will Create the Certificate Mirroring Server on Principle Server With the User name Mirror_user

USE MASTER

GO

CREATE LOGIN Mirror_login WITH PASSWORD=‘Admin123’

GO

CREATE USER Mirror_user FOR LOGIN Mirror_login

GO

CREATE CERTIFICATE Mirror_cert 

AUTHORIZATION Mirror_cert 

FROM FILE=‘C:\certificate\Mirror_cert .cer’  —–the Path of your Certificate After you Copy it

GO

GRANT CONNECT ON ENDPOINT ::End Mirroring TO [Mirror_login] ——-Give privilege to the User Mirror_login to Connect to the Mirroring Endpoint ” End_Mirroring “

GO

Step No 4 on Mirror Server : 

Copy the Certificate ” Principle_cert  ” From Principle Server to the Mirror Server because I will Create the Certificate Principle Server on Mirroring Server With the User name Principle_user

USE MASTER

GO

CREATE LOGIN Principle_login WITH PASSWORD=‘Admin123’

GO

CREATE USER Principle_user FOR LOGIN Principle_login

GO

CREATE ERTIFICATE Principle_cert

AUTHORIZATION Principle_cert

FROM FILE=‘C:\certificate\Mirror_cert .cer’  —–the Path of your Certificate After you Copy it

GO

GRANT CONNECT ON ENDPOINT :: End Mirroring TO [Principle_login] ——-Give privilege to the User Principle_login to Connect to the Mirroring Endpoint ” End_Mirroring “

Step No 5 on Witness Server : Create master Key and Certificate Witness_Cert

USE MASTER

GO

CREATE MASTER KEY ENCRYPTION BY PASSWORD=‘Admin123’

GO

CREATE CERTIFICATE Witness_cert

WITH SUBJECT=‘Witness certificate’

GO

CREATE ENDPOINT End_Mirroring

STATE=STARTED

ASTCP (LISTENER_PORT= 5027,LISTENER_IP=ALL)

FOR DATABASE_MIRRORING

(AUTHENTICATION=CERTIFICATE Witness_cert,

ENCRYPTION = REQUIRED ALGORITHM RC4 , ROLE = Witness)

GO

BACKUP ERTIFICATE witness_cert

TO FILE=‘C:\certificate\Witness_cert.cer’

GO

Step No 6 on Principle Server : 

Copy the Certificate ” ًWitness_cert  ” From Witness Server to the Principle Server because I will Create the Certificate Witness Server on Principle Server With the User name Witness_user

USE MASTER

GO

CREATE LOGIN Witness_login WITH PASSWORD=‘Admin123’

GO

CREATE USER Witness_user FOR LOGIN Witness_login

GO

CREATE CERTIFICATE Witness_cert

AUTHORIZATION Witness_user FROM FILE=‘C:\certificate\Witness_cert.cer’

GO

GRANT CONNECT ON ENDPOINT::End_Mirroring TO [Witness_login]

GO

Step No 7 on Mirror Server : 

Copy the Certificate ” ًWitness_cert  ” From Witness Server to the Mirror Server because I will Create the Certificate Witness Server on Mirror Server With the User name Witness_user

USE MASTER

GO

CREATE LOGIN Witness_login WITH PASSWORD=‘Admin123’

GO

CREATE USER Witness_user FOR LOGIN Witness_login

GO

CREATE CERTIFICATE Witness_cert

AUTHORIZATION Witness_user FROM FILE=‘C:\certificate\Witness_cert.cer’

GO

GRANT CONNECT ON ENDPOINT::End_Mirroring TO [Witness_login]

GO

Step No 8 on Witness Server :

Copy the Certificate ” Principle_cert  ” From Principle Server and  ” Mirror_cert  ” From the Mirror Server

” Principle_cert  “

USE MASTER

GO

CREATE LOGIN Principle_login WITH PASSWORD=‘Admin123’

GO

CREATE USER Principle_user FOR LOGIN Principle_login

GO

CREATE CERTIFICATE Principle_cert

AUTHORIZATION Principle_user

FROM FILE=‘D:\certificate\Principle_cert.cer’

GO

GRANT CONNECT ON ENDPOINT::End_Mirroring TO [Principle_login]

GO

” Mirror_cert  “

CREATE LOGIN Mirror_login WITH PASSWORD=‘Admin123’

GO

CREATE USER Mirror_user FOR LOGIN Mirror_login

GO

CREATE CERTIFICATE Mirror_cert

AUTHORIZATION Mirror_user

FROM FILE=‘C:\certificate\Mirror_cert.cer’

GO

GRANTCONNECT ON ENDPOINT::End_Mirroring TO [Mirror_login]

GO

Step No 8 :

take Full backup + Log backup from your Database Mirroring on Principle Server then Go to Mirroring Server and restore this Backup With NORECOVERY Option

Step No 9 :

GO TO the principle Server in make your Easy mirroring configuration but in the last Step don’t write any Service Account let it Empty then After this Step Mirroring will be Complete Successfully

See this post if you don’t know how to Configure mirroring Database With SQL management Studio

http://wp.me/p1Oidq-6w  Mirroring part 1

http://wp.me/p1Oidq-7h  Mirroring part 2

http://wp.me/p1Oidq-7w  Mirroring part 3

 

Connect With windows authentication Between 2 SQL Instance in 2 PC in Workgroup


Problem

How Can I Connect With windows authentication from SQL Server instance in PC name (SQL-PC) to another SQl Server instance in Another PC name (SQL2-PC) although this two PC in Workgroup not in Domain  ?????

Solution

It’s very Easy on PC no 1 Create Windows User name Admin With password 123 for Example then Go to the another Server PC no 2 then Create Windows user Name Admin With same password 123

then Add this user in SQl Server instance in PC no 1 and PC no 2  the Try to Connect Form PC no1 to PC no 2 By SQL Server it Will be Connect Direct  but if you Change the User name or the password in any PC SQL Server will give you Error

Connected

Connected

Not Connected

Not Connected

Log Shipping in SQL Server 2008R2 Step by Step


Introudaction

SQL Server Log Shipping Allow to Transfare Data Form one Database  (Primary) to a lot of Database (Secondry) So in Log Shipping we have three Srrvers (Primary , Secondry (optinal) monitor ) . the Secondry Server can be More than one Database

Log shipping involves copying a database backup and subsequent transaction log backups from the primary (source) server and restoring the database and transaction log backups on one or more secondary (Stand By / Destination) servers

#######################################################################################

1-       Create Folder in Primary Server (LogShipping_Primary) aslo in Secondry Server Create Folder Name (LogShipping_Secondry) this two Folders must be sharing .

2-      Take Full and transaction log backup from the database (Log Shipping) in Primary Server.

3-      Restore this Database on Secondry Server with Stand By option.

logShippingDB after Restore

4-      On primary Server Write Click >>> properties  >>> Log Shipping n >>>> Enable log Shipping.

Enable Log Shipping

5-      Configure the backup Setting for the Transaction Log backups

  • Network backup path Folder : the Folder name LogShipping_Primary in Primary Server put the network path for this Folder \\Servername\Foldername
  • Local path : the local path for the folder in primary Server like C:\users\DBA\Desctop\LogShipping_Primary

PrimaryServer backup path

6-      Configure Sechudel job to the backup database

4

7-      Add Secondry Server

Add Secondry Server

8-      Connect to Select your Secondry Server

6

Select Secondry Server

9-      Initialize Secondry Database

  • First option         : if you want to take Full backup the restore it to the Secondry Server
  • Seconde Option : If you won’t to Restore the backup from the primary Server to the Secondry Server
  • Theird option      : if you Take the backup and already restore it to the secondry server   (like my example)

8-	Initialize Secondry Database

10-      Copy files : in this tabe you will put the path of the Folder in Secondry Server put you will put it like this \\Servername\Foldername

Copy Files

10-      Configure the Sehedule to take the backup files copy from the primary server to the secondry server

10-	Configure the Sehedule to take the backup

11-      Restore transaction log : we will select Standby option and Disconnect all user in restore operation

Restore transaction log

12-      Configure the Sehedule to restore the backup taken from the primary Server

12

13-      Then ok

14-      Optinal (Check on monitor Server instance to monitor the backup Status  and the job Status  for the Jobs in primary Server and Secondry Server

Configure the Sehedule to restore the backup

14

14-      last Step in Log Shiping Configuration

Finish

14-      After you finish from the Log Shipping go to the Primary Server to Check the new jobs

16

15-      Return to the Secondry Server to Check the new Jobs

17

How to monitor Database mirroring


monitor Database mirroring by SQL Management:

1- I will Stop mirroring Server to input transaction into Principle Server and See the log what happened in It Go to your database Mirroring in Principle Server >> Write Click >>> tasks >>>Lunch Database mirroring monitor

Database monitore

2- Create Table on Principle Server  and insert 10000 Record into New table

CREATE TABLE TEST( A INT, B INT)

DECLARE @I INT=0

WHILE @I <100000

BEGIN

INSERTINTO TEST VALUES(@I,@I)

SET @I=@I+1

END

SELECT COUNT(*) FROM TEST

T-SQL

3- now run the Script and insert the Data then let’s go to Check the monitor to See what happened

3

 

4

4- Then Start the mirroring Server and Check the Monitor again

5

6

*********************************************************************************************************************

Check the database mirroring information :

SELECT M.database_id , D.Name , M.mirroring_state_desc  ,

M.mirroring_role_desc,M.mirroring_partner_name , M.mirroring_partner_instance,

M.mirroring_witness_name , M.mirroring_witness_state_desc

FROM sys.database_mirroring as M inner join Sys.databases as D

on M.database_id = D.database_id

Where D.database_id > 4

7