Creating the Database on the Mirror Server
When the endpoints are
configured and roles are established, you can create the database on the
mirror server and get it to the point of being able to mirror. You must
first make a backup copy of the principal database (AdventureWorks,
in this example). This backup will be used to create the database on
the mirror server. You can use SSMS tasks or use SQL scripts to do this.
The SQL scripts (DBBackupAW2008.sql), which are easily repeatable, are used here.
On the principal server, you make a complete backup as follows:
BACKUP DATABASE [AdventureWorks]
TO DISK = N'C:\Program Files\Microsoft SQL
Server\MSSQL.2\MSSQL\Backup\AdventureWorks4Mirror.bak'
WITH FORMAT
GO
Next, you copy this backup file
to a place where the mirror server can reach it on the network. When
that is complete, you can issue the following database RESTORE command to create the AdventureWorks database on the mirror server (using the WITH NORECOVERY option):
-- use this restore database(with NoRecovery option)
to create the mirrored version of this DB --
RESTORE FILELISTONLY
FROM DISK = 'C:\Program Files\Microsoft SQL
Server\MSSQL.2\MSSQL\Backup\AdventureWorks4Mirror.bak'
go
RESTORE DATABASE AdventureWorks
FROM DISK = 'C:\Program Files\Microsoft SQL
Server\MSSQL.2\MSSQL\Backup\AdventureWorks4Mirror.bak'
WITH NORECOVERY,
MOVE 'AdventureWorks_Data' TO 'C:\Program Files\Microsoft SQL
Server\MSSQL.4\MSSQL\Data\AdventureWorks_Data.mdf',
MOVE 'AdventureWorks_Log' TO 'C:\Program Files\Microsoft SQL
Server\MSSQL.4\MSSQL\Data\AdventureWorks_Log.ldf'
GO
Because you don’t necessarily have the same directory structure on the mirror server, you use the MOVE option as part of this restore to place the database files in the location you desire.
The restore process should yield something that looks like the following result set when restoring the AdventureWorks database that is shipped with SQL Server 2008:
-- Processed 21200 pages for database 'AdventureWorks',
file 'AdventureWorks_Data' on file 1.
-- Processed 2 pages for database 'AdventureWorks',
file 'AdventureWorks_Log' on file 1.
-- RESTORE DATABASE successfully processed 21202 pages
in 14.677 seconds (11.833 MB/sec).
Basically, this result set says
you are not ready to get into the mirroring business yet. You must now
apply at least one transaction log dump to the mirror database. This
brings the mirror database to a point of synchronization with the
principal and leaves the mirror database in the Restoring state. At this
database recovery point, you can run through the mirroring wizard and
start mirroring for high availability.
From the principal server, you dump (that is, back up) a transaction log as follows:
BACKUP LOG [AdventureWorks] TO
DISK = N'C:\Program Files\Microsoft SQL
Server\MSSQL.2\MSSQL\Backup\AdventureWorksLog.bak'
Go
Processed 8 pages for database 'AdventureWorks', file 'AdventureWorks_Log' on file 2.
Then you move this backup to a
place where it can be reached by the mirror server. When that is done,
you restore the log to the mirror database. From the mirror server, you
restore the transaction log as follows. Note the following WITH FILE = statement; the file number must match the value in the backup log results (see the on file 2 reference in the previous code):
RESTORE LOG [AdventureWorks]
FROM DISK = N'C:\Program Files\Microsoft SQL
Server\MSSQL.4\MSSQL\Backup\AdventureWorksLog.bak'
WITH FILE = 2, NORECOVERY
GO
The restore log process should yield something that looks like the following result set:
RESTORE LOG successfully processed 8 pages
in 0.034 seconds (9.396 MB/sec).
Note
You might need to update the FILE = x entry in the RESTORE LOG command to correspond to the “on file” value given during the log backup.
You are now ready to mirror the database in high-availability mode.
Identifying the Other Endpoints for Database Mirroring
To
get each node in the topology to see each other, you have to identify
the endpoints and listener port values to the databases involved in the
database mirroring configuration (the principal and mirror). This also
activates database mirroring. This process requires altering the
database by using either the SET PARTNER or SET WITNESS statements within the ALTER DATABASE command. The Database Mirroring Wizard can also do this step for you, but doing it manually is easy.
We identify the
unique endpoint listening port values for each endpoint that are unique
within the server. They are port values 1430, 1440, and 1450 in our
example.
Remember, you will be doing this after you create the AdventureWorks database on the mirror server side. After creating that database, you can run the following ALTER DATABASE command on the mirror server to identify the principal for the mirror to partner with:
-- From the Mirror Server Database: identify the principal server endpoint --
ALTER DATABASE AdventureWorks
SET PARTNER = ' TCP://REM1237433.ads.autodesk.com:1430'
GO
Now, you are ready for the
final step. From the principal server, you identify the mirror and
witness. After you complete these step, the database mirroring topology
tries to synchronize itself and begin database mirroring. The following
statements identify the mirror server endpoint and witness server
endpoint to the principal server’s database:
-- From the Principal Server Database: identify the mirror server endpoint --
ALTER DATABASE AdventureWorks
SET PARTNER = 'TCP://REM1237433.ads.autodesk.com:1440'
GO
-- From the Principal Server Database: identify the witness server endpoint --
ALTER DATABASE AdventureWorks
SET WITNESS = 'TCP://REM1237433.ads.autodesk.com:1450'
GO
You do not have to alter any database from the witness server.
When this process completes
successfully, you are mirroring! Yes, in fact, with this configuration,
you are in automatic failover mode.
If you have issues or just want
to start over, you can drop an endpoint or alter an endpoint quite
easily. To drop and existing endpoint, you use the DROP ENDPOINT command. In this example, the following command would drop the endpoint you just created:
-- To DROP an existing endpoint --
DROP ENDPOINT EndPoint4DBMirroring1430;
Altering an endpoint (for example, to change the listener_port value) is just as easy as dropping one. The following example shows how to alter the currently defined endpoint to a new listener_port value of 1435 because of a conflict at the network level. (However, because
we use the port in the endpoint name, it might have been best to just
drop and create a new endpoint to fit the naming convention. Either way,
you can easily manipulate these endpoints to fit your networking
needs.)
-- To ALTER an existing endpoint --
ALTER ENDPOINT EndPoint4DBMirroring1430
STATE = STARTED
AS TCP( LISTENER_PORT = 1435 )
FOR DATABASE_MIRRORING (ROLE = PARTNER)