SQL Server 遷移
【1】核心數據
DIR
【1.0】與當前版本相同、或者更高的數據庫版本,相同的實例名、實例排序規則
【1.1】登錄名及對應實例權限
【1.2】用戶數據庫,其本身的數據及庫上的觸發器、存儲過程、函數等數據庫對象信息
【1.3】作業
Content
【1.0】與當前版本相同、或者更高的數據庫版本,相同的實例名、實例排序規則
use master go SELECT SERVERPROPERTY('MachineName') AS ComputerName, SERVERPROPERTY('ServerName') AS InstanceName, -- 如果顯示的和計算機名一樣,那么實例為默認實例 SERVERPROPERTY('Edition') AS Edition, SERVERPROPERTY('ProductVersion') AS ProductVersion_number, SERVERPROPERTY('ProductLevel') AS ProductLevel, SERVERPROPERTY('Collation') as Collation; GO
【1.1】登錄名及對應實例權限
USE master GO IF OBJECT_ID ('sp_hexadecimal') IS NOT NULL DROP PROCEDURE sp_hexadecimal GO CREATE PROCEDURE sp_hexadecimal @binvalue varbinary(256), @hexvalue varchar (514) OUTPUT AS DECLARE @charvalue varchar (514) DECLARE @i int DECLARE @length int DECLARE @hexstring char(16) SELECT @charvalue = '0x' SELECT @i = 1 SELECT @length = DATALENGTH (@binvalue) SELECT @hexstring = '0123456789ABCDEF' WHILE (@i <= @length) BEGIN DECLARE @tempint int DECLARE @firstint int DECLARE @secondint int SELECT @tempint = CONVERT(int, SUBSTRING(@binvalue,@i,1)) SELECT @firstint = FLOOR(@tempint/16) SELECT @secondint = @tempint - (@firstint*16) SELECT @charvalue = @charvalue + SUBSTRING(@hexstring, @firstint+1, 1) + SUBSTRING(@hexstring, @secondint+1, 1) SELECT @i = @i + 1 END SELECT @hexvalue = @charvalue GO IF OBJECT_ID ('sp_help_revlogin') IS NOT NULL DROP PROCEDURE sp_help_revlogin GO CREATE PROCEDURE sp_help_revlogin @login_name sysname = NULL AS DECLARE @name sysname DECLARE @type varchar (1) DECLARE @hasaccess int DECLARE @denylogin int DECLARE @is_disabled int DECLARE @PWD_varbinary varbinary (256) DECLARE @PWD_string varchar (514) DECLARE @SID_varbinary varbinary (85) DECLARE @SID_string varchar (514) DECLARE @tmpstr varchar (1024) DECLARE @is_policy_checked varchar (3) DECLARE @is_expiration_checked varchar (3) DECLARE @defaultdb sysname IF (@login_name IS NULL) DECLARE login_curs CURSOR FOR SELECT p.sid, p.name, p.type, p.is_disabled, p.default_database_name, l.hasaccess, l.denylogin FROM sys.server_principals p LEFT JOIN sys.syslogins l ON ( l.name = p.name ) WHERE p.type IN ( 'S', 'G', 'U' ) AND p.name <> 'sa' ELSE DECLARE login_curs CURSOR FOR SELECT p.sid, p.name, p.type, p.is_disabled, p.default_database_name, l.hasaccess, l.denylogin FROM sys.server_principals p LEFT JOIN sys.syslogins l ON ( l.name = p.name ) WHERE p.type IN ( 'S', 'G', 'U' ) AND p.name = @login_name OPEN login_curs FETCH NEXT FROM login_curs INTO @SID_varbinary, @name, @type, @is_disabled, @defaultdb, @hasaccess, @denylogin IF (@@fetch_status = -1) BEGIN PRINT 'No login(s) found.' CLOSE login_curs DEALLOCATE login_curs RETURN -1 END SET @tmpstr = '/* sp_help_revlogin script ' PRINT @tmpstr SET @tmpstr = '** Generated ' + CONVERT (varchar, GETDATE()) + ' on ' + @@SERVERNAME + ' */' PRINT @tmpstr PRINT '' WHILE (@@fetch_status <> -1) BEGIN IF (@@fetch_status <> -2) BEGIN PRINT '' SET @tmpstr = '-- Login: ' + @name PRINT @tmpstr IF (@type IN ( 'G', 'U')) BEGIN -- NT authenticated account/group SET @tmpstr = 'CREATE LOGIN ' + QUOTENAME( @name ) + ' FROM WINDOWS WITH DEFAULT_DATABASE = [' + @defaultdb + ']' END ELSE BEGIN -- SQL Server authentication -- obtain password and sid SET @PWD_varbinary = CAST( LOGINPROPERTY( @name, 'PasswordHash' ) AS varbinary (256) ) EXEC sp_hexadecimal @PWD_varbinary, @PWD_string OUT EXEC sp_hexadecimal @SID_varbinary,@SID_string OUT -- obtain password policy state SELECT @is_policy_checked = CASE is_policy_checked WHEN 1 THEN 'ON' WHEN 0 THEN 'OFF' ELSE NULL END FROM sys.sql_logins WHERE name = @name SELECT @is_expiration_checked = CASE is_expiration_checked WHEN 1 THEN 'ON' WHEN 0 THEN 'OFF' ELSE NULL END FROM sys.sql_logins WHERE name = @name SET @tmpstr = 'CREATE LOGIN ' + QUOTENAME( @name ) + ' WITH PASSWORD = ' + @PWD_string + ' HASHED, SID = ' + @SID_string + ', DEFAULT_DATABASE = [' + @defaultdb + ']' IF ( @is_policy_checked IS NOT NULL ) BEGIN SET @tmpstr = @tmpstr + ', CHECK_POLICY = ' + @is_policy_checked END IF ( @is_expiration_checked IS NOT NULL ) BEGIN SET @tmpstr = @tmpstr + ', CHECK_EXPIRATION = ' + @is_expiration_checked END END IF (@denylogin = 1) BEGIN -- login is denied access SET @tmpstr = @tmpstr + '; DENY CONNECT SQL TO ' + QUOTENAME( @name ) END ELSE IF (@hasaccess = 0) BEGIN -- login exists but does not have access SET @tmpstr = @tmpstr + '; REVOKE CONNECT SQL TO ' + QUOTENAME( @name ) END IF (@is_disabled = 1) BEGIN -- login is disabled SET @tmpstr = @tmpstr + '; ALTER LOGIN ' + QUOTENAME( @name ) + ' DISABLE' END PRINT @tmpstr END FETCH NEXT FROM login_curs INTO @SID_varbinary, @name, @type, @is_disabled, @defaultdb, @hasaccess, @denylogin END CLOSE login_curs DEALLOCATE login_curs RETURN 0 GO exec sp_help_revlogin
--查看登錄名服務器角色 SELECT sp.name AS [login_name] ,CASE WHEN sp.[type]='S' THEN 'SQL 登錄名' WHEN sp.[type]='U' THEN 'Windows 登錄名' WHEN sp.[type]='G' THEN 'Windows 組' WHEN sp.[type]='R' THEN '服務器角色' WHEN sp.[type]='C' THEN '映射到證書的登錄名' WHEN sp.[type]='K' THEN '映射到非對稱密鑰的登錄名' END AS [principal_type] ,sp.is_disabled ,ISNULL(sp.default_database_name,'') as [default_database_name] ,ISNULL(rsp.name,'') AS [server_role] ,STUFF((SELECT ','+permission_name FROM sys.server_permissions spp where sp.principal_id=spp.grantee_principal_id for xml path('')),1,1,'') as [permissions] FROM sys.server_principals sp LEFT JOIN sys.server_role_members srm ON sp.principal_id=srm.member_principal_id LEFT JOIN sys.server_principals rsp ON srm.role_principal_id=rsp.principal_id where rsp.name is not null ORDER BY [principal_type],sp.principal_id --授權服務器角色 select N'EXEC sp_addsrvrolemember N''' +sp.name+ ''' ,N''' + rsp.name+''' ' FROM sys.server_principals sp LEFT JOIN sys.server_role_members srm ON sp.principal_id=srm.member_principal_id LEFT JOIN sys.server_principals rsp ON srm.role_principal_id=rsp.principal_id where rsp.name is not null -- 將登錄名添加為某個服務器級角色的成員 EXEC sp_addsrvrolemember @loginame= 'kk' ,@rolename = 'sysadmin' master庫中的用戶名及權限可以用如下腳本進行遷移。 -- 授予【數據庫角色成員身份】權限 SELECT 'exec sp_addrolemember N'''+g.name+''', N'''+u.name+'''' FROM sys.database_principals u inner join sys.database_role_members m on u.principal_id = m.member_principal_id inner join sys.database_principals g on g.principal_id = m.role_principal_id ORDER BY g.name,u.name -- 授予【安全對象】權限 SELECT N'grant '+B.permission_name collate chinese_prc_ci_ai_ws+N' on ['+A.name+N'] to ['+C.name+N']' FROM sys.sysobjects A(NOLOCK) INNER JOIN sys.database_permissions B(NOLOCK) ON A.id=B.major_id INNER JOIN sys.database_principals C(NOLOCK) ON B.grantee_principal_id=C.principal_id --WHERE C.name='kk' --A.name='objectName'
【1.2】用戶數據庫,其本身的數據及庫上的觸發器、存儲過程、函數等數據庫對象信息
【method-1】備份還原(利用用戶數據庫備份還原)
【method-2】生成腳本(利用對象資源管理器詳細信息(快捷鍵F7),批量生成數據庫對象的創建腳本)
【method-3】故障轉移(利用復制、鏡像、日志傳送等技術,以最小停機時間為標准來解決)
【1.3】作業
【method-1】利用對象資源管理器詳細信息(快捷鍵F7),批量生成作業的創建SQL腳本)
【method-2】自己根據msdb相關系統表、視圖等數據,寫腳本生產創建作業SQL
【2】步驟列表
一定要有一個准備好的計划,我下面列出了所有的遷移過程需要做的工作,如下列表:
| 序號 |
SQL Server遷移步驟 |
| 1 |
必要環境的准備(比如高版本的服務器操作系統) |
| 2 |
研究棄用和停用的功能、特性 |
| 3 |
運行數據遷移助手(DMA)了解哪些改變不被允許或者會影響遷移。 |
| 4 |
確認SQL Server 服務,數據引擎,SSIS,SSAS,SSRS等等可用 |
| 5 |
排序規則注意一致或者有變更的仔細核對 |
| 6 |
確保應用程序的連接需求 |
| 7 |
日志、聚集、數據庫鏡像、復制、全文索引、分布式服務等服務類的都需要有計划的去管理遷移。 |
| 8 |
管理有效的連接服務器,遷移 |
| 9 |
備份策略和計划的完整遷移,包含了完整、差別、事務日志備份。 |
| 10 |
規划需要的磁盤空間 |
| 11 |
管理遷移各個服務的賬號 |
| 12 |
檢查數據一致性 |
| 13 |
預升級--升級前后比較性能指標 |
| 14 |
評估宕機時間及影響 |
| 15 |
定稿升級流程 |
| 16 |
制定升級、遷移的驗收標准 |
| 17 |
最終驗收 |
| 18 |
回滾計划以及測試 |
| 19 |
務必通知所有涉及和影響的負責人 |
| 20 |
向所有負責人發送升級、遷移的步驟 |
| 21 |
准備新的、或者遷移舊的數據庫維護計划 |
