Fully implement the SQL Server image Function

Source: Internet
Author: User

After one day of tossing the SQL Server image, I finally got some results. Now I have shared some of the problems I have encountered on the Internet. Now try to share all the problems encountered.

I did not configure the results in the domain environment. Maybe it is the reason for the domain user. Because I did it in the production environment, changing the domain user requires restarting the SQL Server, so this method is abandoned, only certificates can be used.

Environment:

HOST: 192.168.10.2 (Code)

Image: 192.168.10.1 (codenamed B for convenience)

(I have not witnessed the server with limited conditions .) Both servers use SQL Server 2005

First configure the host

Run the following SQL statement on the host:

 
 
  1. Create master key encryption by password = 'Password ';
  2. GO
  3. -- Create a certificate for the database instance on 10.2
  4. Create certificate As_A_cert
  5. With subject = 'as _ A_cert ',
  6. START_DATE = '2014/1/123 ',
  7. EXPIRY_DATE = '2017/123 ';
  8. GO
  9. -- Use the certificate created above to create an image endpoint for the database instance on 10.2
  10. Create endpoint Endpoint_As
  11. STATE = STARTED
  12. As tcp (
  13. LISTENER_PORT = 5022,
  14. LISTENER_IP = ALL
  15. )
  16. FOR database_grouping ing (
  17. AUTHENTICATION = CERTIFICATE As_A_cert,
  18. ENCRYPTION = required algorithm RC4,
  19. ROLE = ALL
  20. );
  21. GO

Note: Pay attention to setting the Image Port of the database. 5022.

 
 
  1. -- Back up the certificate on 10.2 and copy it to 10.1
  2. Backup certificate As_A_cert to file = 'd: \ As_A_cert.cer ';
  3. GO

Note: Back up Certificate A and copy certificate A to image server B.

Configure the backup storage

 
 
  1. USE master;
  2. Create master key encryption by password = 'Password ';
  3. GO
  4. -- Create a certificate for the database instance on 10.1 B
  5. Create certificate As_ B _cert
  6. With subject = 'as _ B _cert ',
  7. START_DATE = '2014/1/123 ',
  8. EXPIRY_DATE = '2017/123 ';
  9. GO
  10. -- Use the certificate created above on 10.1 B to create an image endpoint for the database instance
  11. Create endpoint Endpoint_As
  12. STATE = STARTED
  13. As tcp (
  14. Listener_ports = 5022
  15. , LISTENER_IP = ALL
  16. )
  17. FOR database_grouping ing (
  18. AUTHENTICATION = CERTIFICATE As_ B _cert
  19. , ENCRYPTION = REQUIRED ALGORITHM AES
  20. , ROLE = ALL
  21. );
  22. GO
  23. -- Back up the certificate on 10.1 B and copy it to 10.2.
  24. Backup certificate As_ B _cert to file = 'd: \ As_ B _cert.cer ';
  25. GO

Copy the backed up certificate B to server.

Create an account for image Login

Run

 
 
  1. -- Exchange certificates,
  2. -- Synchronize Login
  3. Create login B _login WITH PASSWORD = 'Password ';
  4.  
  5. Create user B _user for login B _login;
  6.  
  7. Create certificate As_ B _cert AUTHORIZATION B _user from file = 'd: \ As_ B _cert.cer ';
  8.  
  9. Grant connect on endpoint: Endpoint_Bs TO [B _login];

Execute on B

 
 
  1. -- Exchange certificates,
  2. -- Synchronize Login
  3. Create login A_login with password = 'Password ';
  4.  
  5. Create user A_user for login A_login;
  6.  
  7. Create certificate As_A_cert AUTHORIZATION A_user from file = 'd: \ As_A_cert.cer ';
  8.  
  9. Grant connect on endpoint: Endpoint_As TO [A_login];

Remember that port 5022 of the two servers is not occupied and the two servers can be connected.

There will be no problem in subsequent steps, and the image is half done.

NextFull backupTest Database on server

 
 
  1. -- Complete backup of the host
  2. USE master;
  3. Alter database Test set recovery full;
  4. GO
  5. Backup database Test
  6. To disk = 'd: \ SQLServerBackups \ Test. bak'
  7. With format;
  8. GO
  9. Backup log Test to disk = 'd: \ SQLServerBackups \ Test. bak ';
  10. GO
  11. -- Copy the backup file to B.

Make sure to complete the backup.

Completely restore the database on server B

There are many problems here. One by one.

If we directly execute the following SQL statement.

 
 
  1. RESTORE DATABASE Test  
  2.     FROM DISK = 'D:\Back\Test.bak'  
  3.     WITH NORECOVERY  
  4. GO  
  5. RESTORE LOG Test  
  6.     FROM DISK = 'D:\Back\Test_log.bak'  
  7.     WITH FILE=1, NORECOVERY  
  8. GO 

May report:

Message 3154, level 16, status 4, 1st rows
The database backup in the backup set is different from the existing 'test' database.
Message 3013, level 16, state 1, 1st rows

It may be because the backup set names of the two databases are different. The reason for finding the backup set for half a day is unsuccessful. Therefore, use the sp_addumpdevice method below.

Use sp_addumpdevice to create a restoration device. This ensures that the backup file belongs to the database.

 
 
  1. exec sp_addumpdevice 'disk','Test_backup', 
  2.         'E:\backup\Test.bak' 
  3. exec sp_addumpdevice 'disk','Test_log_backup', 
  4.         'E:\backup\Test_log.bak' 
  5. go 

After successful execution, we will executeRecovery completed

 
 
  1. RESTORE DATABASE Test  
  2.   FROM Test_backup 
  3.   WITH DBO_ONLY,  
  4.     NORECOVERY,STATS; 
  5. go 
  6. RESTORE LOG Test  
  7.   FROM Test_log_backup 
  8.   WITH file=1, 
  9.     NORECOVERY; 
  10. GO 

If the database has been backed up multiple times, multiple backup sets will be generated. Therefore, the file here cannot be specified as 1.

This error may be:

Message 4326, level 16, state 1, 1st rows
Logs in this backup set are terminated at LSN 36000000014300001, which is too early to be applied to the database.

. You can restore a new log backup that contains LSN 36000000018400001.

You can use this statement to query the backup set of the backup file.

Restore headeronly from disk = 'e: \ backup \ Test_log.bak'

Find the last sequence number and specify it to the file.

Note that NORECOVERY must be specified for the first full recovery.

So far, all preparations have been completed. We have enabled the image.

Run

 
 
  1. ALTER DATABASE Test SET PARTNER = 'TCP://192.168.10.2:5022'; 

Run the command on the host.

 
 
  1. ALTER DATABASE Test SET PARTNER = 'TCP://192.168.10.2:5022'; 

In this way, the images of the two servers are synchronized.

Delete image:

 
 
  1. ALTER DATABASE Test SET PARTNER OFF 

If a problem occurs on the host, run

 
 
  1. USE MASTER  
  2.  
  3. Go  
  4.  
  5. ALTER DATABASE Test SET PARTNER FAILOVER  
  6.  
  7. Go 

Summary:

If there is a problem with the process during image creation, you must delete the deleted items when you need to re-execute them.

 
 
  1. -- Query Images
  2. Select * from sys. endpoints
  3. -- Delete a port
  4. Drop endpoint Endpoint_As
  5. -- Query the certificate
  6. Select * from sys. tricric_keys
  7. -- Delete the certificate. Delete the certificate before deleting the primary key.
  8. Drop certificate As_A_cert
  9. -- Delete a primary key
  10. DROP MASTER KEY
  11. -- Delete an image
  12. Alter database <dbname> set partner off
  13. -- Delete Login Name
  14. Drop login <login_name>

Sp_addumpdevice syntax

 
 
  1. Sp_addumpdevice [@ devtype =] 'device _ type'
  2.  
  3. , [@ Logicalname =] 'logical _ name'
  4.  
  5. , [@ Physicalname =] 'physical _ name'
  6.  
  7. ]
  8. The parameters include:
  9. @ Devtype: device type. The supported values are disk and tape, where disk is the disk file and tape is
  10. Any tape device supported by windows.
  11. @ Logicalname: Logical name of the backup device.
  12. @ Physicalname: Physical name and path of the Backup Device

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.