iT邦幫忙

0

在沒有AD、沒有加入網域的環境下建立 MSSQL Basic Availability Group(Step by Step)

  • 分享至 

  • xImage
  •  

大綱

  • 背景
  • 環境需求
  • 修改網路配置
  • 開啟防火牆
  • 安裝WSFC
  • 建立WSFC
  • 安裝MSSQL Server & SSMS
  • 建立SQL帳號
  • 建立DB
  • 建立BAG (Basic Availability Group)
  • 測試BAG
  • 總結優缺點與限制
  • Debug

背景

客戶需求每隔一段時間必須固定PM 2個小時維護系統,所以必須規劃系統HA架構,由於客戶網路環境封閉,這篇先介紹不使用AD帳號也不用把機器加入網域的做法,之後再嘗試正常有AD的架構下如何實作。


環境需求

2台主機 & 4個可用IP

https://ithelp.ithome.com.tw/upload/images/20220721/20143615zXRu3vud8S.png

自訂WorkGroup: HAGROUP(可自行變更)

自訂DNS尾碼:SQLHA(可自行變更))

*注意

兩台主機必須有相同帳號&密碼的本機使用者,並且要有管理員權限,此範例是

Account:Administrator

Password: password

兩台主機的SQL Server必須是Standard以上版本

以下正式開始安裝


修改網路配置

*兩台虛擬機都需要做一次

修改WorkGroup與自訂DNS尾碼

1.右鍵開始點選系統
https://ithelp.ithome.com.tw/upload/images/20220721/20143615GLkuaeuI7U.png

2.點選系統資訊

https://ithelp.ithome.com.tw/upload/images/20220721/20143615zgnC63Z9ra.png

3.點選變更設定
https://ithelp.ithome.com.tw/upload/images/20220721/201436156zgBcK6uUx.png

4.點選變更
https://ithelp.ithome.com.tw/upload/images/20220721/20143615ZmEisuvUtw.png

5.在工作群組輸入 HAGROUP
6.點選其他
https://ithelp.ithome.com.tw/upload/images/20220721/20143615RQ4jt1ktiN.png

7.輸入DNS尾碼 SQLHA 後按下確定

https://ithelp.ithome.com.tw/upload/images/20220721/20143615PioVA2vEsg.png

8.按下確定
https://ithelp.ithome.com.tw/upload/images/20220721/20143615vzcctuyQwW.png

9.按下確定
https://ithelp.ithome.com.tw/upload/images/20220721/20143615u6k5ZDCLON.png

10按下確定並重新啟動電腦
https://ithelp.ithome.com.tw/upload/images/20220721/201436150CPCbXDbcA.pnghttps://ithelp.ithome.com.tw/upload/images/20220721/20143615RdItgT9Quo.png

11.右鍵網路連線Icon並點擊開啟網路和網際網路設定

https://ithelp.ithome.com.tw/upload/images/20220721/20143615YPrYH5EGq7.png

12.點選變更介面卡選項
https://ithelp.ithome.com.tw/upload/images/20220721/20143615MgEo4Y4uUM.png

13.右鍵Ethernet0點選內容
https://ithelp.ithome.com.tw/upload/images/20220721/20143615mBnAsCYYNE.png

14.選擇 TCP/IPv4 開啟 內容
https://ithelp.ithome.com.tw/upload/images/20220721/20143615J9GZPkfxws.png

15.打開進階選項
https://ithelp.ithome.com.tw/upload/images/20220721/20143615bMHPtScJWH.png

16.選到DNS項目

17.取消勾選 在DNS中登入這個連線的網路位置

18.選擇 附加這些DNS尾碼

19.點擊 新增
https://ithelp.ithome.com.tw/upload/images/20220721/201436151xgzWweP2j.png

20.輸入: SQLHA 點擊新增
https://ithelp.ithome.com.tw/upload/images/20220721/201436156MOWuuXpvX.png

21.點擊確定

https://ithelp.ithome.com.tw/upload/images/20220721/20143615YeOhz3j4bf.png

22.點擊確定

https://ithelp.ithome.com.tw/upload/images/20220721/20143615HzH6y04sm3.png

23.在C:\Windows\System32\drivers\etc 開啟hosts檔案
https://ithelp.ithome.com.tw/upload/images/20220721/20143615Qn5FNmF9P0.png

24.在最下方輸入對應IP資訊並儲存檔案

192.168.1.67  WIN-K4046TOB57O.SQLHA
192.168.1.68  WIN-6K690KG06RP.SQLHA

https://ithelp.ithome.com.tw/upload/images/20220721/201436157lPWwabuNU.png

*請在虛擬機B也完成操作


開啟防火牆

*兩台虛擬機都需要做一次

1.在放大鏡輸入firewall 並開啟防火牆與網路保護
https://ithelp.ithome.com.tw/upload/images/20220721/20143615bpXRrGW4pc.png

2.開啟進階設定
https://ithelp.ithome.com.tw/upload/images/20220721/20143615LriYEuKe79.png

3.點選輸入規則
https://ithelp.ithome.com.tw/upload/images/20220721/20143615X3tIOMr8lh.png

4.點選新增規則
https://ithelp.ithome.com.tw/upload/images/20220721/201436156dnRU1Mjta.png

5.選連接阜
https://ithelp.ithome.com.tw/upload/images/20220721/20143615t9F47PsQ1N.png

6.選TCP

7.輸入特定連接阜 1433
https://ithelp.ithome.com.tw/upload/images/20220721/20143615NuhRltGZvR.png

8.允許連線
https://ithelp.ithome.com.tw/upload/images/20220721/20143615B28AI7dKdZ.png

9.下一步

https://ithelp.ithome.com.tw/upload/images/20220721/20143615bdagOHyNy4.png

10.輸入名稱1433Port
https://ithelp.ithome.com.tw/upload/images/20220721/20143615euDnTwp5DT.png

11.重複步驟4~10 建立5022Port

12.完成後看到剛剛建立好的1433Port & 5022Port
https://ithelp.ithome.com.tw/upload/images/20220721/20143615UvM8kvwRVS.png

*請在虛擬機B也完成操作


安裝WSFC

*兩台虛擬機都需要做一次

1.點選管理>新增腳色及功能
https://ithelp.ithome.com.tw/upload/images/20220721/201436150yoCt7YVYH.png

2.下一步

https://ithelp.ithome.com.tw/upload/images/20220721/20143615dScdUkTL0o.png

3.腳色型或功能安裝>下一步

https://ithelp.ithome.com.tw/upload/images/20220721/20143615JjEmbveuFx.png

4.從伺服器集區選取伺服器

https://ithelp.ithome.com.tw/upload/images/20220721/201436152WaM02dfvN.png

5.伺服器腳色 不須選取 >下一步

https://ithelp.ithome.com.tw/upload/images/20220721/20143615VKGzDkYHCf.png

6.功能選取 容錯管理叢集
https://ithelp.ithome.com.tw/upload/images/20220721/20143615ZGSXn2Axof.png

7.安裝
https://ithelp.ithome.com.tw/upload/images/20220721/20143615HA3CH3KjIa.png

*請在虛擬機B也完成操作


建立WSFC

以下在虛擬機A操作即可

若使用容錯移轉管理員建立叢集會需要經過許多驗證過程,因為正常流程是需要在有AD帳號和機器加入網域的情況下建立,因此這篇採用PowerShell下指令的方式建立

1.以管理員身分開啟Windows PowerShell
https://ithelp.ithome.com.tw/upload/images/20220721/20143615D23aZLKuvk.png

2.輸入建立叢集指令

New-Cluster sqlcluster  –Node  WIN-K4046TOB57O.SQLHA,WIN-6K690KG06RP.SQLHA –StaticAddress 192.168.1.80 -NoStorage –AdministrativeAccessPoint Dns

3.完成建立 sqlcluster

https://ithelp.ithome.com.tw/upload/images/20220721/20143615ABbRsC8fBq.png

4.從伺服器管理員開啟 容錯轉移叢集管理員

https://ithelp.ithome.com.tw/upload/images/20220721/20143615bYHx5yq3ot.png

5.點擊連線到叢集
https://ithelp.ithome.com.tw/upload/images/20220721/20143615km1bgK0evK.png

6.選擇 這部伺服器上的叢集>確定
https://ithelp.ithome.com.tw/upload/images/20220721/20143615mVpeM6kImD.png

7.確認連線到sqlcluster
https://ithelp.ithome.com.tw/upload/images/20220721/20143615LJBnwrpWrv.png

至此完成WSFC建立。


安裝MSSQL Server & SSMS

*兩台虛擬機都需要做一次

1.開啟SQL安裝

https://ithelp.ithome.com.tw/upload/images/20220721/20143615E1zlPhcEAW.png

2.略過Windows Update >Next

https://ithelp.ithome.com.tw/upload/images/20220721/20143615MxNZBUvDjq.png

3.Next
https://ithelp.ithome.com.tw/upload/images/20220721/20143615fTDw6qKhcu.png

4.我們是用SQL Server Standard的版本,所以輸入product key後>Next

https://ithelp.ithome.com.tw/upload/images/20220721/20143615Cb2r7P132s.png

5.同意使用說明 >Next

https://ithelp.ithome.com.tw/upload/images/20220721/201436152lF7zysE0p.png

6.勾選Datebase Engine Services
https://ithelp.ithome.com.tw/upload/images/20220721/20143615Mjo5emmVsH.png

7.使用 Default instance

Instance ID : MSSQLSERVER

https://ithelp.ithome.com.tw/upload/images/20220721/20143615Q7gdM3CK0B.png

SQL Server Database Engine : Startup Type: Automatic

SQL Server Agent : Startup Type: Automatic

https://ithelp.ithome.com.tw/upload/images/20220721/20143615tlN3jkN8ae.png

  1. Authentication Mode: Windows authentication mode

10.點選 Add Current User會自動加入現在的User
https://ithelp.ithome.com.tw/upload/images/20220721/201436151hHmygpoNz.png

11.點選Install

https://ithelp.ithome.com.tw/upload/images/20220721/20143615c8BParlboR.png

12.完成安裝 ,點擊Close

https://ithelp.ithome.com.tw/upload/images/20220721/20143615KVhjqwKYS7.png

13.開啟SSMS安裝
https://ithelp.ithome.com.tw/upload/images/20220721/20143615ot7pbcKwWw.png

14.Install,安裝完成後重新啟動電腦
https://ithelp.ithome.com.tw/upload/images/20220721/201436156HhlFRpXnJ.png

15.開啟SQL Server 組態管理員

https://ithelp.ithome.com.tw/upload/images/20220721/20143615vOnwiul3r1.png

16.選擇SQL Server Service

https://ithelp.ithome.com.tw/upload/images/20220721/20143615gDooCVKOkh.png

17.右鍵SQL Server開啟內容
https://ithelp.ithome.com.tw/upload/images/20220721/20143615hHO5FEgBsD.png

18.切換到Always On Availability Groups頁籤, 勾選Enable Always On Availability Groups
https://ithelp.ithome.com.tw/upload/images/20220721/20143615TlVKtAu2df.png

19.切換到Log On頁籤

輸入

Account: ./Administrator

Password: password

確定
https://ithelp.ithome.com.tw/upload/images/20220721/201436150tsMU50Tyt.png

21.需重新啟動SQL Server設定才會起作用,按確定
https://ithelp.ithome.com.tw/upload/images/20220721/20143615c62qNXOKur.png

22.Restart SQL Server

https://ithelp.ithome.com.tw/upload/images/20220721/20143615l8DUzxboFE.png

23.點選SQL Server Network Configuration>Protocols for MSSQLSERVER

右鍵 TCP/IP 設成Enable

https://ithelp.ithome.com.tw/upload/images/20220721/20143615O5nt1a621z.png

至此完成SQLServer建立與設定

*請在虛擬機B也完成操作


建立SQL帳號

*兩台虛擬機都需要做一次

1.開啟SSMS
https://ithelp.ithome.com.tw/upload/images/20220721/20143615wCtpfdI6qg.png

2.使用Windows帳號登入,按下Connect
https://ithelp.ithome.com.tw/upload/images/20220721/201436158C3ANQYEOy.png

3.右鍵Server名稱選擇Properties
https://ithelp.ithome.com.tw/upload/images/20220721/201436156nZgeiB6Dr.png

4.在Security頁面選擇 SQL Server and Windows Authentication mode
https://ithelp.ithome.com.tw/upload/images/20220721/20143615ei66whv47J.png

5.重新啟動後才設定才有效,這邊先按OK但等等在重新啟動
https://ithelp.ithome.com.tw/upload/images/20220721/201436157TTBYnDO4p.png

6.在Security>Login按右鍵選擇 New Login
https://ithelp.ithome.com.tw/upload/images/20220721/20143615RJ4kGj0ry4.png

7.建立帳號HA

選擇SQL Server authentication

下面選項都不勾選

https://ithelp.ithome.com.tw/upload/images/20220721/20143615yMCMGTlLgu.png

8.Server Roles 勾選sysadmin權限
https://ithelp.ithome.com.tw/upload/images/20220721/20143615QAj5JPHc1i.png

9.UserMapping不勾選

https://ithelp.ithome.com.tw/upload/images/20220721/20143615DF42vkEF0C.png

10.Securables不做事
https://ithelp.ithome.com.tw/upload/images/20220721/20143615v29s00dPAv.png

11.Sataus不動 按下OK

https://ithelp.ithome.com.tw/upload/images/20220721/20143615GFNnGmdvYF.png

12重啟SQL Server
https://ithelp.ithome.com.tw/upload/images/20220721/20143615AZGn8ZwNpP.png

*請在虛擬機B也完成操作


建立DB

*此步驟只需在虛擬機A操作

1.右鍵Database 選擇 New Database
https://ithelp.ithome.com.tw/upload/images/20220721/20143615u1jN6fAi71.png

2.輸入Database name: TestDB 按OK
https://ithelp.ithome.com.tw/upload/images/20220721/201436156kxY7p0f9f.png

3.重新整理Database後,對剛剛建立的TestDB右鍵>Tasks>Back up
https://ithelp.ithome.com.tw/upload/images/20220721/20143615CJot9fGBaa.png

4.按OK
https://ithelp.ithome.com.tw/upload/images/20220721/201436157CVzb7VoNr.png

以上完成測試資料庫TestDB建立

注意:不可以省略備份的步驟,否則不能建立 Availability Group


建立BAG (Basic Availability Group)

*此步驟只需在虛擬機A操作

1.右鍵Always on High Availability ,點選 New Availability Group Wizard
https://ithelp.ithome.com.tw/upload/images/20220721/20143615iLlI7kghMT.png

2.Next
https://ithelp.ithome.com.tw/upload/images/20220721/20143615k8a2gThR5B.png

3.輸入 Availability group name: AG1

Cluster type: Windows Server Failover Cluster (如果沒有這個選項可能是 WSFC建立失敗)

點擊Next
https://ithelp.ithome.com.tw/upload/images/20220721/201436157fY7gRAsuM.png

4.勾選TestDB (如果不能勾選可能是資料庫沒有備份)

https://ithelp.ithome.com.tw/upload/images/20220721/20143615LqCRI6BeBs.png

5.點擊Add Replica

https://ithelp.ithome.com.tw/upload/images/20220721/20143615Z8NFwhWKA7.png

  1. 輸入虛擬機B 的Name : WIN-6K690KG06RP

因為兩台電腦有建立相同的本機帳號Administrator所以這邊可以直接選擇Windows Authentication登入

按下 Connect
https://ithelp.ithome.com.tw/upload/images/20220721/20143615wmJB1iBAGv.png

7.如果虛擬機的Always on功能沒開會出現以下錯誤, 可去SQL組態管理員開啟即可(記得開完後要重啟SQL Server)

https://ithelp.ithome.com.tw/upload/images/20220721/20143615i1SQ3bBqH4.png

8.順利加入兩台虛擬機

勾選 Automatic Failover
https://ithelp.ithome.com.tw/upload/images/20220721/20143615LGWfq4X0pl.png

9.Endpoints不用改
https://ithelp.ithome.com.tw/upload/images/20220721/20143615JYqUVvXmhc.png

10.Backup Preferences 不用改
https://ithelp.ithome.com.tw/upload/images/20220721/20143615o0tmUmvrmo.png

11.加入Listener

點擊Create an availability group listener

Listener DNS Name : sqllistener

Port:1433

Network Mode: Static IP

按Add

https://ithelp.ithome.com.tw/upload/images/20220721/20143615uCONhmj5IJ.png

12.輸入IP: 192.168.1.99

https://ithelp.ithome.com.tw/upload/images/20220721/20143615zgGs9b72QU.png

13.Read-Only Routing不用做事,按Next
https://ithelp.ithome.com.tw/upload/images/20220721/20143615ykJx12Z0kk.png

14.這邊會跳出警告,我們可以先忽略,按Yes
https://ithelp.ithome.com.tw/upload/images/20220721/20143615Q0OV9IL8dI.png

15.選Automatic seeding 按 Next

https://ithelp.ithome.com.tw/upload/images/20220721/20143615b7CQpTpI8x.png

16.通過驗證按Next

https://ithelp.ithome.com.tw/upload/images/20220721/20143615TxmyQsl0qW.png

若出現 SQL error:[41105]

A:到SQL組態管理員關閉並重新啟動AlwaysOn功能可解決

https://ithelp.ithome.com.tw/upload/images/20220721/20143615tk98hYkESu.png

17.Finish
https://ithelp.ithome.com.tw/upload/images/20220721/201436156NVSHvgvKV.png

18.Close
https://ithelp.ithome.com.tw/upload/images/20220721/20143615vzuhAIO4WH.png

如果Listener的IP重複了會出現下列錯誤,請確認AG Listener使用的IP沒有別的機器正在使用
https://ithelp.ithome.com.tw/upload/images/20220721/20143615b7tUL1qlNv.png

19.建立完成會出現AG1
https://ithelp.ithome.com.tw/upload/images/20220721/201436152QknNd9dUH.png

20.右鍵AG1按下 Show Dashboard
https://ithelp.ithome.com.tw/upload/images/20220721/20143615nM7ML200tU.png

21確認Dashboard

https://ithelp.ithome.com.tw/upload/images/20220721/20143615LdHSq0pRTN.png

22.在 容錯移轉管理員>腳色>會看到AG1
https://ithelp.ithome.com.tw/upload/images/20220721/20143615oeA5YwnYzg.png

完成BAG建立


測試Basic Availability Group

1.找一台遠端測試PC,使用AGListener IP連線

https://ithelp.ithome.com.tw/upload/images/20220721/20143615HVqmnC0gfq.png

2.TesDB>資料表>右鍵>新增>資料表

https://ithelp.ithome.com.tw/upload/images/20220721/20143615sikHJMJjtx.png

3.建立欄位
https://ithelp.ithome.com.tw/upload/images/20220721/20143615NGnOPfmmPx.png

4.輸入Table Namehttps://ithelp.ithome.com.tw/upload/images/20220721/20143615w3QcnTHhbA.png
https://ithelp.ithome.com.tw/upload/images/20220721/20143615WzNHx5pAnN.png

6.TestDB>資料表>右鍵>重新整理
https://ithelp.ithome.com.tw/upload/images/20220721/201436151qDgKHprYJ.png

7.TestDB>資料表>dbo.Table_1>右鍵>編輯前200個資料列
https://ithelp.ithome.com.tw/upload/images/20220721/20143615Kmw2sNg5us.png

8.輸入資料
https://ithelp.ithome.com.tw/upload/images/20220721/20143615TrROeZAYfb.png

9.TestDB>資料表>dbo.Table_1>右鍵>選取前1000個資料
https://ithelp.ithome.com.tw/upload/images/20220721/20143615BS6dn50tsw.png

  1. 輸入

select @@SERVERNAME
按下F5

看到剛剛輸入的資料確認資料庫可運作

看到目前的是使用虛擬機A: WIN-K4046TOB57O當主要資料庫運作
https://ithelp.ithome.com.tw/upload/images/20220721/20143615zEHz0kl5dc.png

  1. 將虛擬機A重新啟動

過一段時間重新按下F5(轉換資料會有約5秒無法連線的空白時間),ServerName會變成虛擬機B:WIN-6K690KG06RP
https://ithelp.ithome.com.tw/upload/images/20220721/20143615lxnrUtmZhi.png

  1. 將虛擬機B重新啟動

會發現資料庫右Failover回原本的虛擬機A: WIN-K4046TOB57O

https://ithelp.ithome.com.tw/upload/images/20220721/201436154qiCGyspQY.png

至此終於大功告成
恭喜恭喜!!!


總結優缺點與限制

優點:
設定較簡單,不需要加入使用AD帳號,也不需要加入網域

缺點:
這種架構只有當遇到系統問題時才 WSFC 才會 Failover,例如關機或當機;
如果只把Primary SQL Server的Service停了,Availability Group Fail over了,但windows cluster還是在原Server。

###簡單的說就是PM時要把整台電腦關掉,不能只關SQL Server Service


Debug

1.建立AG時沒有出現Windows Server Failover Cluster的選項

A:先確認WSFC有建立成功,若有則重啟SQL Server服務
https://ithelp.ithome.com.tw/upload/images/20220721/20143615ElhSg1wwwX.png

2.建立過程中出現SQL error:[41105]

A:到SQL組態管理員關閉並重新啟動AlwaysOn功能
https://ithelp.ithome.com.tw/upload/images/20220721/20143615LHndOdNZmK.png

3. 建立Listener錯誤 SQL error: [41066]

Listener使用的IP已經有人用了,換一個IP再重建一次就可以
https://ithelp.ithome.com.tw/upload/images/20220721/20143615UGL4yTYCib.png

4.建立完成後出現錯誤訊息 1196 DNS Server失敗

https://ithelp.ithome.com.tw/upload/images/20220721/20143615tYj6dA7w5P.png

A: 變更網路介面卡 => 附加DNS尾碼、取消勾選在DNS中登入這個連線網路的位置
如此會無法用AG Listener的名稱登入SQL Server但還是可以用IP登入
https://ithelp.ithome.com.tw/upload/images/20220721/20143615PTqgdR7y2P.png


以上若有任何錯誤或疑問,請不吝指教。
謝謝!


參考資料

https://dotblogs.azurewebsites.net/rockchang/2019/03/09/161148
https://sys-blog.net/sql-server-availability-groups/
https://ithelp.ithome.com.tw/users/20119758/ironman/2957
https://techcommunity.microsoft.com/t5/failover-clustering/workgroup-and-multi-domain-clusters-in-windows-server-2016/ba-p/372059
https://docs.microsoft.com/en-us/previous-versions/windows/it-pro/windows-server-2012-R2-and-2012/dn265970(v=ws.11)?redirectedfrom=MSDN
https://www.eugenechiang.com/2020/07/01/create-failed-for-availability-group-listener-error-41066/


圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

2 則留言

0
BKY
iT邦研究生 1 級 ‧ 2022-07-22 14:52:36

請問一下:
1.SQL Server 可以建立幾個AG呢?
2.如果資料庫是在另外的storage server 中,不是在DB server,要如何設定?

s900bill iT邦新手 5 級 ‧ 2022-07-22 15:10:26 檢舉

第一個問題:
可參考微軟官網,他有寫到最多支援8個副本
https://docs.microsoft.com/zh-tw/sql/database-engine/availability-groups/windows/overview-of-always-on-availability-groups-sql-server?view=sql-server-ver16

第二個問題
可以參考這篇文章
https://mssqltaiwan.wordpress.com/2018/01/09/fci-and-always-on/
我做的是AG,你可能可以試試看FCI,但我也不確定是不是你要的

0
BKY
iT邦研究生 1 級 ‧ 2022-07-22 14:52:36

(重複 請刪除)

我要留言

立即登入留言