让SQL Server永不宕机:无域环境下的AlwaysOn高可用实战

Words 4360Read Time 11 min
2026-2-11
cover
type
Post
status
Published
date
Apr 8, 2020 12:54
slug
summary
本文详细介绍如何在无域环境下搭建SQL Server群集高可用性架构。从环境配置、SQL Server安装、故障转移群集创建,到AlwaysOn可用性组的完整配置流程,涵盖主从热备、自动故障转移、数据同步等核心技术。通过分步骤的图文教程和实战测试,帮助你构建企业级的数据库高可用解决方案,确保业务连续性和数据安全。
tags
SQL Server
Infrastructure
category
Database
icon
password
wordCount
4247

前言

数据库宕机对于企业来说可能意味着灾难。想象一下:你的核心业务系统突然停摆,订单无法处理,客户数据消失......这不是危言耸听,而是每个技术团队都可能面临的真实挑战。
💡
本文核心要点
  • 构建SQL Server无域群集高可用性架构
  • 实现主从热备与自动故障转移
  • 详细的配置步骤与实战测试
  • 适用于无域环境的企业级解决方案
今天,我们将一起搭建一套SQL Server无域群集可用性组,让你的数据库系统在面对故障时能够自动切换,确保业务连续性。这套方案不需要复杂的域环境,特别适合中小型企业或特殊网络环境。

实验环境

  • PBA-1 Server:4x core, 8GB RAM, 50GB HDD, Win2017 server OS
  • PBA-2 Server:4x core, 8GB RAM, 50GB HDD, Win2017 server OS

核心概念:先搞懂这些术语

什么是数据灾备

简单来说,数据灾备就是给你的数据买一份保险。当主服务器出现问题时,备份服务器能立即接管,确保业务不中断。这不仅仅是简单的数据复制,而是一套完整的容错机制。
🛡️
数据灾备:将全系统或部分数据从应用主机复制到其他存储介质的过程,防止操作失误或系统故障导致数据丢失。

我们要实现什么

这次实验的目标很明确:
主从热备:两台服务器协同工作,一主一备
自动故障转移:主服务器宕机后,备份服务器秒级接管
零域依赖:无需Active Directory域环境
数据同步:实时或准实时的数据复制

完整实施路线图

  1. 配置Host Name
  1. 安装SQL Server
  1. 创建SQL Server实例
  1. 配置SQL Server与防火墙
  1. 安装SSMS
  1. 创建备份与清理计划
  1. 创建故障转移群集
  1. 创建无域群集高可用组

第一步:配置主机名与网络

  1. 服务器配置
    1. Server
      Host Name
      domain
      SQL Server端口
      PBA-1
      PBA-1
      test.com
      14331
      PBA-1
      PBA-1
      test.com
      14331
  1. 右击我的电脑,点击属性
    1. notion image
  1. 输入主机名与域名
    1. notion image
  1. 重启电脑可以看见主机名变化
    1. notion image
  1. 修改C:文件,添加ip,服务器名,域名 101.241.50.134 PBA-1 test.com 101.241.50.138 PBA-2 test.com
  1. Ctrl+R,输入cmd回车。两台服务器用ping命令测试服务器名,例如
    1. notion image
      notion image

第二步:安装与配置SQL Server

关于SQL Server

SQL Server是微软的旗舰级数据库产品,从小型应用到企业级系统都能胜任。它的强大之处在于与Windows生态的深度集成,以及丰富的企业级特性——比如我们今天要用到的AlwaysOn高可用性组。

安装

两台服务器都需要安装
  1. 打开SQL Server 安装器
    1. notion image
  1. 选择安装
    1. notion image
  1. 选择新建一个示例
    1. notion image
  1. 输入激活码,这里使用试用版
    1. notion image
  1. 选择接受
    1. notion image
  1. 选择自动更新
    1. notion image
  1. 下一步
    1. notion image
  1. 安装下列插件,指定安装目录,点击下一步 -SQL Server Replication -Client Tools Connectivity -Client Tools SDK -SQL Client Connectivity SDK
    1. notion image
  1. 输入实列名(数据库名)
    1. notion image
  1. 选择自动启动服务
    1. notion image
  1. 添加目前用户,下一步
    1. notion image
  1. 给SQL Server授予文件系统权限
    1. notion image
  1. 点击安装
    1. notion image
  1. 等待安装完成后,点击开始,打开SQL Server配置器
    1. notion image
  1. 检测是否存在新创建的示例
    1. notion image

网络配置

两台服务器都需要配置
  1. 打开SQL Server配置器
    1. notion image
  1. 点击SQL Server Services,双击需要修改的实列
    1. notion image
  1. 选择Local System,点击apply
    1. notion image
  1. 点击yes
    1. notion image
  1. 点击SQL Server Network Configuration
    1. notion image
  1. 双击一个我们需要修改的示例,双击Named Pipes
    1. notion image
  1. 设置Enable为Yes
    1. notion image
  1. 点击IP Address,配置实例端口,点击apply
    1. notion image
  1. 点击OK
    1. notion image
  1. 现在SQL Server能建立TCP/IP连接了
    1. notion image
  1. 重启实例
    1. notion image

第三步:打开防火墙通道

  1. 配置防火墙规则如下
    1. Fire Rule Name
      Port
      Host Name
      Protocol
      Profile
      Remarks
      sql-instance
      14331
      PBA-1
      TCP
      Domain,Private
      Primary Server SQL Server实例端口
      sql-instance
      14331
      PBA-2
      TCP
      Domain,Private
      DR Server SQL Server实例端口
      sql-endpoint
      5022
      PBA-1
      TCP
      Domain,Private
      Primary Server SQL Server高可用组通信端口
      sql-endpoint
      5022
      PBA-2
      TCP
      Domain,Private
      DR Server SQL Server高可用组通信端口
      sqlserver-browser
      1434
      PBA-1
      UDP
      Domain,Private
      Primary Server SQL Server browser 通信端口
      sqlserver-browser
      1434
      PBA-2
      UDP
      Domain,Private
      DR Server SQL Server browser 通信端口
  1. 打开windows设置
    1. notion image
  1. 选择Update & Security
    1. notion image
  1. Windows Security > Firewall & network protection
    1. notion image
  1. 点击Advanced settings
    1. notion image
  1. 创建防火墙规则
    1. notion image
  1. 选择port
    1. notion image
  1. 输入要开启的端口
    1. notion image
  1. 选择 Allow the connection option
    1. notion image
  1. 选择 Domain选项和Private选项
    1. notion image
  1. 输入防火墙规则名和描述
    1. notion image
  1. 检测防火墙规则是否打开
    1. notion image
  1. 重复上面步骤创建所有防火墙规则

第四步:安装管理工具SSMS

SQL Server Management Studio (SSMS) 是用于管理任何 SQL 基础结构的集成环境。 使用 SSMS,可以访问、配置、管理和开发 SQL Server、Azure SQL 数据库和 SQL 数据仓库的所有组件。 SSMS 在一个综合实用工具中汇集了大量图形工具和丰富的脚本编辑器,为各种技能水平的开发者和数据库管理员提供对 SQL Server 的访问权限。

安装

  1. 打开SSMS安装器
    1. notion image
  1. 点击安装
    1. notion image
  1. 安装成功
    1. notion image

创建数据仓库

  1. 打开SSMS
    1. notion image
  1. 连接到数据仓库实例
    1. notion image
  1. 新建数据库
    1. notion image
  1. 输入数据库名,点击创建
    1. notion image

手动备份

  1. 选择目标数据库,右键选择tasks,选择Back up
    1. notion image
  1. 选择备份类型,点击OK
    1. notion image

自动备份任务

  1. 连接数据库后选择Maintenance Plan Wizard
    1. notion image
  1. 下一步
    1. notion image
  1. 输入计划名和描述,点击change,选择备份时间和频率,下一步
    1. notion image
  1. 选择 back up Database
    1. notion image
  1. next
    1. notion image
  1. 选择需要备份的数据库
    1. notion image
  1. 选择备份文件保存的目录
    1. notion image
  1. 下一步
    1. notion image
  1. 选择备份日志的文件目录
    1. notion image
  1. 完成
    1. notion image
  1. 备份成功
    1. notion image
  1. 检查是否已经创建备份任务
    1. notion image

自动清理任务

  1. 连接数据库后,点击Management -> Maintenance Plans-> Manintenance Plan Wizard
    1. notion image
  1. next
    1. notion image
  1. 输入计划名和描述,点击change,选择清理时间和频率,下一步
    1. notion image
  1. 选择Maintenance Cleanup Task
    1. notion image
  1. next
    1. notion image
  1. 在13编号那里输入清理目录,14指删除超过多少天的文件
    1. notion image
  1. 选择存放清理任务log日志文件夹
    1. notion image
  1. 完成
    1. notion image
  1. 创建成功
    1. notion image

第五步:搭建故障转移群集

无域群集:不需要AD域环境

故障转移群集是一组独立的计算机,这些计算机相互协作以提高群集角色(之前称为应用程序和服务)的可用性和可伸缩性。 多台群集服务器(称为节点)通过物理电缆和软件连接。 如果一个或多个群集节点出现故障,其他节点就会开始提供服务(该过程称为故障转移)。 此外,群集角色会得到主动监视以验证它们是否正常工作。 如果不工作,则会重新启动这些角色或将其移动到其他节

启用功能

在所有需要加入群集的服务器都需要执行本节内容
notion image
notion image
notion image
notion image
notion image
notion image
notion image

创建群集

创建群集在任意一台进行一次就好,后续还可以加入其他服务器
  1. 开始 –> Windows Administrative Tools –> Failover Cluster Manager
    1. notion image
  1. 下一步
    1. notion image
  1. 填写需要加入群集的服务器Host name;如果一次加入多台用逗号分隔,加入群集的服务器用户密码要一致否则会报错没有管理员权限
    1. notion image
  1. 运行测试,第一次创建群集会出现这个对话框,如果选的no,则直接跳到步骤9
    1. notion image
    notion image
      notion image
        notion image
          notion image
          1. 填写群集名称
            1. notion image
            notion image
              notion image
              1. 检查是否创建成功
                1. notion image

              设置群集IP

              1. 点击创建好的群集,设置属性
                1. notion image
              1. 填写预先规划的群集IP: 10.34.50.13
                1. notion image
                  notion image
              1. 手工将群集上线,上线后,ping 群集IP,ping通了即可
                1. notion image

              连接已存在的群集

              1. 开始 –> Windows Administrative Tools –> Failover Cluster Manager
                1. notion image
              1. 填写群集名称(群集IP对应的名称,如果本机是群集中的节点则不必填写)
                1. notion image

              手工迁移群集IP

              切换主节点
              1. 开始 –> Windows Administrative Tools –> Failover Cluster Manager > 已存在群集 > more action > move core cluster resource > select node
                1. notion image
              1. 选择需要迁移的ip
                1. notion image

              域群集:企业标准配置

              步骤类同无域故障转移群集,不同处是:不要求加入群集的服务器存在密码一致的系统用户,要求加入群集的服务器事先已经加入了域,且需要用域账户登录操作需要加入群集的服务器。

              第六步:配置AlwaysOn可用性组

              AlwaysOn是SQL Server的杀手级功能,它能让你的数据库实现真正的高可用性。想象一下:主服务器出问题了,备份服务器自动顶上,用户甚至感觉不到任何中断。这就是AlwaysOn的魅力。
              ⚠️
              重要提醒
              • 启用该功能需要重启SQL Server服务
              • 主库必须先进行完整备份
              • 备库不能存在与主库同名的数据库
              • 需要企业版才能支持备库可读
              • 各服务器之间必须创建加密连接端点

              灵活性特点

              • 一台服务器可以同时管理多个可用性组
              • 一个可用性组可以包含多个数据库
              • 一个主库可以配置多个备库(一主多备)
              • 管理操作可以在任意节点进行

              启用功能

              该步骤需要在所有相关SQL-SERVER服务器上执行。
              1. 开始 –> Microsoft SQL Server 2017 –>SQL Server 2017 Configuration Manager
                1. notion image
              1. 重启SQL Server服务
                1. notion image

              创建加密连接端点

              该步骤用来主备服务器通讯加密,需要分别在主备服务器上执行
              1. 各服务器上创建目录:C:,并设置权限Everyone完全控制
              1. 开始 –> Microsoft SQL Server Tools 17 –>Microsoft SQL Server Management Studio 17
                1. notion image
              1. 新建查询,按下面要求填写sql语句并执行
                1. notion image
              1. 创建用于通讯验证的非对称密钥对
                  • 在主库节点执行:
                  • 在备库节点执行:
              1. 交换主备节点公钥 将上面目录C:,手工复制交换,确保主库节点目录C:,备库节点目录C:
              1. 创建主备登录验证
                  • 在主库节点执行: ```sql – Create login for the other node CREATE LOGIN PBA2Login WITH PASSWORD = ‘cloud.Zijin’ GO
                    • Create user for the login CREATE USER PBA2User FOR LOGIN PBA2Login GO
                    • Import the public key portion of the certificate from the other node CREATE CERTIFICATE PBA2_pub AUTHORIZATION PBA2User FROM FILE = ‘C:2.cert’ GO
                    • Grant the CONNECT permission to the login GRANT CONNECT ON ENDPOINT::SQLAG_Endpoint TO PBA2Login GO ```
                  • 在备库节点上执行: ```sql – Create login for the other node CREATE LOGIN PBA1Login WITH PASSWORD = ‘cloud.Zijin’ GO
                    • Create user for the login CREATE USER PBA1User FOR LOGIN PBA1Login GO
                    • Import the public key portion of the certificate from the other node CREATE CERTIFICATE PBA1_pub AUTHORIZATION PBA1User FROM FILE = ‘C:1.cert’ GO
                    • Grant the CONNECT permission to the login GRANT CONNECT ON ENDPOINT::SQLAG_Endpoint TO PBA1Login GO ```

              方案一:无域无群集(手动切换)

              该功能只能手工切换SQL-SERVER主备
              打开MSSM,开始 –> Microsoft SQL Server Tools 17 –>Microsoft SQL Server Management Studio 17
              notion image

              创建可用性组

              一般在主库节点进行
                notion image
                  notion image
                  1. 填写可用组名称,选定群集类型为“NONE”
                    1. notion image
                  1. 选定需要加入当前可用性组的数据库
                    1. notion image
                  1. 填写备库节点主机名
                    1. notion image
                  1. 择同步方式:异步提交,选择副本是否可读:Yes
                    1. notion image
                    notion image
                      notion image
                        notion image

                        查看可用性组

                        • 主库状态:
                          • notion image
                        • 备库状态:
                          • notion image

                        主备切换

                        切换可在任意一个加入同一可用性组的节点进行
                          notion image
                            notion image
                            1. 选择要切换为主库的备库节点
                              1. notion image
                            1. 确认数据丢失风险,由于当前备库节点用的异步提交方式,所以会提示有数据丢失风险,如果是同步方式则无此选项
                              1. notion image
                              notion image
                                notion image
                                  notion image
                                    • 步骤 14 > 原主库手工切换为备库后的状态:未同步
                                    • 步骤 16 > 手工启用数据同步
                                    notion image
                                      notion image
                                      1. 刷新数据库状态后可以看到备库同步了
                                        1. notion image

                                      删除可用性组

                                      需要在所有相关节点执行
                                        notion image
                                        1. 确认删除可用性组,会报错但实际已删除,刷新数据库连接可看到可用性组已删除,报错原因是没用故障转移群集却操作删除故障群集相关设置
                                          1. notion image

                                        方案二:无域群集(自动切换)

                                        使用了故障转移群集后可以实现SQL-SERVER主备节点自动故障切换,采用自动故障切换时可用性模式只能选同步提交的方式,这个功能要求先创建故障转移群集,后SQL-SERVER启用AlwaysOn特性(如果已启用需要在所有相关节点上关闭AlwaysOn后重启SQL-SERVER服务之后再次启用),最后创建AlwaysOn可用性组。

                                        创建可用性组

                                        具体创建步骤参考 AlwaysOn 无域无群集可用性组,不同之处如下
                                          • 步骤 4 > 填写可用组名称:auto (自定义)
                                          • 步骤 5 > 选择群集类型为“Windows Server Failover Cluser” ,选了这个最后会在故障转移群集中自动创建一个同步骤4填写的可用组名称的角色
                                          notion image
                                          1. 步骤 15 > 勾选“Automatic Failover”,勾选这个后SQL-SERVER会自动进行主备故障切换,且可用性模式自动改为同步提交模式
                                            1. notion image

                                          群集管理器中查看可用性组角色

                                          auto这个角色是由上面小节操作SQL AlwaysOn可用性组创建向导创建的
                                          notion image

                                          方案三:域群集(企业级方案)

                                          本功能要求系统预先加入域,其他操作同上

                                          第七步:验证自动故障切换

                                          1. 初始状态 数据库testdb的 主库节点是PBA-1,备库节点是PBA-2 在主库节点看到的状态:
                                            1. notion image
                                          1. 杀死主库节点SQL-SERVER进程 打开PBA-1的资源管理器,找到SQL-SERVER进程然后杀死
                                            1. notion image
                                          1. 查看数据库及可用性组状态 打开PBA-2的SQL-SERVER Managerment Tools,查看数据库及可用性组状态。可以看到PBA-2自动切换成了主库节点
                                            1. notion image
                                          1. 故障恢复 开始 –> Microsoft SQL Server 2017 –>SQL Server 2017 Configuration Manager 打开故障转移群集管理器:开始 –> Windows Administrative Tools –> Failover Cluster Manager 打开PBA-1的SQL-SERVER Managerment Tools,查看数据库及可用性组状态 可以看到PBA-1又变成主库节点了
                                            1. notion image
                                              notion image
                                              notion image

                                          总结

                                          通过这篇教程,我们成功搭建了一套企业级SQL Server高可用性架构。从最基础的主机配置,到复杂的故障转移群集,再到AlwaysOn可用性组的配置,每一步都是为了实现一个目标:让数据库永不宕机
                                          实战成果
                                          ✅ 主从热备架构搭建完成
                                          ✅ 自动故障转移测试通过
                                          ✅ 数据同步机制运行正常
                                          ✅ 无域环境下的高可用方案验证成功

                                          真实场景应用

                                          这套方案适用于以下场景:
                                          电商平台:订单数据库不能停,每一秒的宕机都可能带来巨大损失
                                          金融系统:交易数据的可靠性和连续性至关重要
                                          ERP系统:企业核心业务系统需要7x24小时运行
                                          SaaS服务:多租户环境下的高可用保障

                                          进阶方向

                                          掌握了基础的高可用配置后,你可以继续探索:
                                          • 读写分离:利用备库分担查询压力
                                          • 多地域部署:跨数据中心的灾备方案
                                          • 监控告警:集成监控系统实时掌握集群状态
                                          • 性能优化:同步模式vs异步模式的权衡
                                          • 云端迁移:向Azure SQL Database托管实例演进

                                          最后的话

                                          高可用性不是一次性的配置工作,而是一个持续的过程。定期演练故障切换、监控系统健康状态、优化同步性能——这些都是运维团队的日常功课。
                                          记住:最好的高可用方案,是那些你永远用不到的方案。因为它已经默默地保护了你的系统,让故障在发生的瞬间就被自动处理了。
                                          希望这篇教程能帮助你构建一个稳如磐石的数据库系统!💪

                                          参考资源

                                          Loading...