設定 SQL Server 資料庫鏡像的範例指令碼 (SharePoint Server 2010)

 

適用版本: SharePoint Foundation 2010, SharePoint Server 2010

上次修改主題的時間: 2016-11-30

本文包含一系列範例指令碼,您可以使用它們為測試 Microsoft SharePoint Server 2010 環境設定 Microsoft SQL Server 鏡像。建議 SQL Server 資料庫管理員為實際作業環境設定鏡像。

若要使用 SharePoint Server 2010 設定資料庫鏡像,您必須單獨處理每一個要鏡像的資料庫。

本文內容:

下列章節中的步驟適用於下列伺服器陣列拓撲:

  • 一或多部前端網頁伺服器

  • 執行 SQL Server 2008 的伺服器有三種:主體伺服器、鏡像伺服器及見證伺服器

  • 一個設定資料庫

  • 多個內容資料庫

  • 一或多個服務應用程式資料庫

設定憑證及完整復原的資料庫鏡像

每個步驟都列出了應在其上執行該步驟的伺服器。使用 Transact-SQL 傳送這些命令至 SQL Server。預留位置資訊由角括號 (<>) 表示;將其替換為專屬於您的部署的資訊。

設定輸出連線的主體伺服器

  1. 在主體伺服器上,建立憑證並開啟鏡像連接埠。

    --On the master database, create the database master key, if needed
    CREATE MASTER KEY ENCRYPTION BY PASSWORD = '<test1234->';
    GO
    -- Make a certificate for this server instance.
    USE master;
    CREATE CERTIFICATE <MASTER_HostA_cert> 
       WITH SUBJECT = '<Master_HostA certificate>';
    GO
    --Create a mirroring endpoint for server instance by using the certificate
    CREATE ENDPOINT Endpoint_Mirroring
       STATE = STARTED
       AS TCP (
          LISTENER_PORT=5024
          , LISTENER_IP = ALL
       ) 
       FOR DATABASE_MIRRORING ( 
          AUTHENTICATION = CERTIFICATE <MASTER_HostA_cert>
          , ENCRYPTION = REQUIRED ALGORITHM RC4
          , ROLE = ALL
       );
    GO
    
  2. 在主體伺服器上,備份憑證。

    --Back up the HOST_A certificate.
    BACKUP CERTIFICATE MASTER_HostA_cert TO FILE = '<c:\MASTER_HostA_cert.cer>';
    GO
    
  3. 在主體伺服器上,備份資料庫。此範例使用設定資料庫。對所有資料庫重複此作業。

    USE master;
    --Ensure that SharePoint_Config uses the full recovery model.
    ALTER DATABASE SharePoint_Config
       SET RECOVERY FULL;
    GO
    USE SharePoint_Config
    BACKUP DATABASE SharePoint_Config 
        TO DISK = '<c:\SharePoint_Config.bak>' 
        WITH FORMAT
    GO
    BACKUP Log SharePoint_Config 
        TO DISK = '<c:\SharePoint_Config_log.bak>' 
        WITH FORMAT
    GO
    
  4. 複製備份檔案至鏡像伺服器。對所有資料庫重複此作業。

  5. 使用任何的安全複製方法,複製備份憑證檔案 (例如,C:\HOST_HostA_cert.cer) 至鏡像伺服器。

  6. 在主體伺服器上,建立鏡像伺服器的登入與使用者、建立憑證與使用者的關聯,並為合作關係授與登入連線權限。

    --Create a login on HOST_A for HOST_B
    USE master;
    CREATE LOGIN <HOST_HostB_login> WITH PASSWORD = '<1234-test>';
    GO
    --Create a user for that login.
    CREATE USER <HOST_HostB_user> FOR LOGIN <HOST_HostB_login>;
    GO
    --Associate the certificate with the user
    CREATE CERTIFICATE <HOST_HostB_cert>
       AUTHORIZATION <HOST_HostB_user>
       FROM FILE = '<c:\HOST_HostB_cert.cer>' --do not use a network path, SQL Server will give an error about the key not being valid
    GO
    --Grant CONNECT permission on the login for the remote mirroring endpoint.
    GRANT CONNECT ON ENDPOINT::Endpoint_Mirroring TO [<HOST_HostB_login>];
    GO
    

設定輸出連線的鏡像伺服器

  1. 在鏡像伺服器上,建立憑證並開啟鏡像連接埠。

    --On the master database, create the database master key, if needed.
    USE master;
    CREATE MASTER KEY ENCRYPTION BY PASSWORD = '<1234-test>';
    GO
    -- Make a certificate on the HOST_B server instance.
    CREATE CERTIFICATE <HOST_HostB>
       WITH SUBJECT = '<HOST_HostB certificate for database mirroring>';
    GO
    --Create a mirroring endpoint for the server instance on HOST_B.
    CREATE ENDPOINT Endpoint_Mirroring
       STATE = STARTED
       AS TCP (
          LISTENER_PORT=5024
          , LISTENER_IP = ALL
       ) 
       FOR DATABASE_MIRRORING ( 
          AUTHENTICATION = CERTIFICATE <HOST_HostB>
          , ENCRYPTION = REQUIRED ALGORITHM RC4
          , ROLE = ALL
       );
    GO
    
  2. 在鏡像伺服器上,備份憑證。

    --Back up the HOST_B certificate.
    BACKUP CERTIFICATE <HOST_HostB> TO FILE = '<C:\HOST_HostB_cert.cer>';
    GO 
    
  3. 使用任何的安全複製方法,複製備份憑證檔案 (例如,C:\HOST_HostB_cert.cer) 至主體伺服器。

  4. 在鏡像伺服器上,從備份檔案還原資料庫。此範例使用設定資料庫。對所有資料庫重複此作業。

    RESTORE DATABASE SharePoint_Config 
        FROM DISK = '<c:\SharePoint_Config.bak>' 
        WITH NORECOVERY
    GO
    RESTORE log SharePoint_Config 
        FROM DISK = '<c:\SharePoint_Config_log.bak>' 
        WITH NORECOVERY
    GO
    

設定輸入連線的鏡像伺服器

  1. 在鏡像伺服器上,為主體伺服器建立登入和使用者、將憑證與使用者相關聯以及針對合作關係授與登入連線權限。

    --Create a login on HOST_B for HOST_A
    USE master;
    CREATE LOGIN <MASTER_HostA_login> WITH PASSWORD = '<test1234->';
    GO
    --Create a user for that login.
    CREATE USER <MASTER_HostA_user> FOR LOGIN <MASTER_HostA_login>;
    GO
    --Associate the certificate with the user
    CREATE CERTIFICATE <MASTER_HostA_cert>
       AUTHORIZATION <MASTER_HostA_user>
       FROM FILE = '<c:\MASTER_HostA_cert.cer>' --do not use a network path, SQL Server will give an error about the key not being valid
    GO
    --Grant CONNECT permission on the login for the remote mirroring endpoint.
    GRANT CONNECT ON ENDPOINT::Endpoint_Mirroring TO [<MASTER_HostA_login>];
    GO
    

設定輸入連線的主體伺服器

  1. 在主體伺服器上,建立鏡像伺服器的登入與使用者、建立憑證與使用者的關聯,並為合作關係授與登入連線權限。

    --Create a login on HOST_A for HOST_B
    USE master;
    CREATE LOGIN <HOST_HostB_login> WITH PASSWORD = '<1234-test>';
    GO
    --Create a user for that login.
    CREATE USER <HOST_HostB_user> FOR LOGIN <HOST_HostB_login>;
    GO
    --Associate the certificate with the user
    CREATE CERTIFICATE <HOST_HostB_cert>
       AUTHORIZATION <HOST_HostB_user>
       FROM FILE = '<c:\HOST_HostB_cert.cer>' --do not use a network path, SQL Server will give an error about the key not being valid
    GO
    --Grant CONNECT permission on the login for the remote mirroring endpoint.
    GRANT CONNECT ON ENDPOINT::Endpoint_Mirroring TO [<HOST_HostB_login>];
    GO
    

設定鏡像合作夥伴

  1. 在主體伺服器上,設定鏡像合作關係。此範例使用設定資料庫。對所有資料庫重複此作業。

    --At HOST_A, set the server instance on HOST_B as a partner (mirror server).
    ALTER DATABASE SharePoint_Config
        SET PARTNER = '<TCP://databasemirror.adatum.com:5024>';
    GO
    
  2. 在鏡像伺服器上,設定鏡像合作關係。此範例使用設定資料庫。對所有資料庫重複此作業。

    --At HOST_B, set the server instance on HOST_A as a partner (principal server):
    ALTER DATABASE SharePoint_Config 
        SET PARTNER = '<TCP://databasemaster.adatum.com:5024>';
    GO
    

設定見證伺服器

每個步驟都列出了應在其上執行該步驟的伺服器。使用 Transact-SQL 傳送這些命令至 SQL Server。預留位置資訊由角括號 (<>) 表示;將其替換為專屬於您的部署的資訊。

  1. 在見證伺服器上,設定憑證並開啟連接埠。

    --On the master database, create the database master key, if needed
    CREATE MASTER KEY ENCRYPTION BY PASSWORD = '<1234test->';
    GO
    -- Make a certificate for this server instance.
    USE master;
    CREATE CERTIFICATE <WITNESS_HostC_cert> 
       WITH SUBJECT = '<Witness_HostC certificate>';
    GO
    --Create a mirroring endpoint for server instance by using the certificate
    CREATE ENDPOINT Endpoint_Mirroring
       STATE = STARTED
       AS TCP (
          LISTENER_PORT=5024
          , LISTENER_IP = ALL
       ) 
       FOR DATABASE_MIRRORING ( 
          AUTHENTICATION = CERTIFICATE <WITNESS_HostC_cert
          , ENCRYPTION = REQUIRED ALGORITHM RC4
          , ROLE = ALL
       );
    GO
    
  2. 在見證伺服器上,備份憑證。

    --Back up the HOST_C certificate 
    BACKUP CERTIFICATE <WITNESS_HostC_cert> TO FILE = '<c:\ WITNESS_HostC_cert.cer>';
    GO
    
  3. 使用任何的安全複製方法,複製備份憑證檔案 (例如,C:\WITNESS_HOSTC_cert.cer) 至主體伺服器與鏡像伺服器。

  4. 在見證伺服器上,為主體伺服器和鏡像伺服器建立登入和使用者、將憑證與使用者相關聯以及針對合作關係授與登入連線權限。

    --Create a login on witness HOST_C for principal HOST_A
    USE master;
    CREATE LOGIN <MASTER_HostA_login> WITH PASSWORD = '<test1234->';
    GO
    --Create a user for that login.
    CREATE USER <MASTER_HostA_user> FOR LOGIN <MASTER_HostA_login>;
    GO
    --Associate the certificate with the user
    CREATE CERTIFICATE <MASTER_HostA_cert>
       AUTHORIZATION <MASTER_HostA_user>
       FROM FILE = '<c:\MASTER_HostA_cert.cer>' --do not use a network path, SQL Server will give an error about the key not being valid
    GO
    --Grant CONNECT permission on the login for the remote mirroring endpoint.
    GRANT CONNECT ON ENDPOINT::Endpoint_Mirroring TO [<MASTER_HostA_login>];
    GO
    --Create a login for the mirror Host_B
    CREATE LOGIN <HOST_HostB_login> WITH PASSWORD = '<1234-test>';
    GO
    --Create a user for that login.
    CREATE USER <HOST_HostB_user> FOR LOGIN <HOST_HostB_login>;
    GO
    --Associate the certificate with the user
    CREATE CERTIFICATE <HOST_HostB_cert>
       AUTHORIZATION <HOST_HostB_user>
       FROM FILE = '<c:\HOST_HostB_cert.cer>' --do not use a network path, SQL Server will give an error about the key not being valid
    GO
    --Grant CONNECT permission on the login for the remote mirroring endpoint.
    GRANT CONNECT ON ENDPOINT::Endpoint_Mirroring TO [<HOST_HostB_login>];
    GO
    
  5. 在主體伺服器上,建立見證伺服器的登入與使用者、建立憑證與使用者的關聯,並為合作關係授與登入連線權限。對鏡像伺服器重複此作業。

    --Create a login on master HostA for witness HostC
    USE master;
    CREATE LOGIN <WITNESS_HostC_login> WITH PASSWORD = '<1234test->';
    GO
    --Create a user for that login.
    CREATE USER <WITNESS_HostC_user> FOR LOGIN <WITNESS_HostC_login>;
    GO
    --Associate the certificate with the user
    CREATE CERTIFICATE <WITNESS_HostC_cert>
       AUTHORIZATION <WITNESS_HostC_user>
       FROM FILE = '<c:\WITNESS_HostC_cert.cer>' --do not use a network path, SQL Server will give an error about the key not being valid
    GO
    --Grant CONNECT permission on the login for the remote mirroring endpoint.
    GRANT CONNECT ON ENDPOINT::Endpoint_Mirroring TO [<WITNESS_HostC_login>];
    GO
    
  6. 在主體伺服器上,附加見證伺服器。此範例使用設定資料庫。對所有資料庫重複此作業。

    --Set up the witness server
    ALTER DATABASE SharePoint_Config
        SET WITNESS = 
        '<TCP://databasewitness.adatum.com:5024>'
    GO
    

傳輸權限至鏡像伺服器

設定鏡像資料庫時,並不會自動在鏡像伺服器上的 mastermsdb 資料庫中,設定將與 SharePoint 伺服器陣列一起使用之資料存放庫的 SQL Server 登入與權限。相反地,您必須設定必要登入的權限。

建議您執行指令碼,將登入與權限從主體伺服器傳輸至鏡像伺服器。如需建議使用的指令碼,可從下列位置獲得:知識存放存放庫文章 918992:如何在 SQL Server 2005 執行個體之間傳送登入和密碼 (https://go.microsoft.com/fwlink/?linkid=122053&clcid=0x404)。

從伺服器中刪除鏡像

若要從伺服器中刪除鏡像,請參閱如何:移除資料庫鏡像 (Transact-SQL) (https://go.microsoft.com/fwlink/?linkid=185070&clcid=0x404)。