1. OpenClaw不是“另一个ChatGPT前端”,它是AI工作流的物理锚点

你搜到“OpenClaw安装”“Clawdbot部署”时,大概率正被三类问题反复折磨:第一,本地跑大模型太吃硬件,显卡显存告急,CPU风扇狂转像在打铁;第二,用网页版Claude Code总卡在登录、限速、上下文清空,写个函数要反复粘贴提示词;第三,想把AI能力嵌进日常——自动回客户微信、整理会议纪要、监控服务器日志——但现有工具全是孤岛,API调用要写脚本、配密钥、处理超时,三天没跑通就放弃了。

OpenClaw(原Clawdbot)恰恰卡在这个痛点上:它不追求“更聪明”,而是解决“更可用”。它本质是一个 可插拔的AI代理运行时(Agent Runtime) ,核心价值在于把模型、通道、技能三者解耦。模型可以是本地Ollama里的Qwen2.5-Coder,也可以是云端MiniMax的Abel-3;通道不只是WhatsApp,还能接飞书机器人、Telegram Bot、甚至企业微信Webhook;技能不是预设功能,而是你用Python写的任意函数——比如“读取NAS上昨天的监控截图,用CV模型识别异常区域,生成Markdown报告发到钉钉群”。这种设计让OpenClaw既不像Dify那样偏重低代码编排,也不像Llama.cpp那样只管推理,而是在“能用”和“可控”之间找到了一个极难复制的平衡点。

我第一次在Ubuntu 22.04上部署失败,就是因为把它当成了普通Web应用。执行 openclaw start 报错:“无法将‘openclaw’项识别为 cmdlet、函数、脚本文件或可运行程序的名称”,查了两小时才发现根本没装CLI工具链,PATH路径也没加。后来才明白,OpenClaw的部署逻辑是分层的:底层是Docker容器化运行时,中层是CLI管理接口,上层才是WebUI交互层。跳过任何一层,都会掉进“命令不存在”的坑里。这恰恰说明它不是一个玩具项目——它的架构设计本身就要求你理解现代软件交付的基本范式:容器化、配置即代码、环境隔离。所以这篇教程不叫“手把手安装”,而叫“部署并使用”,因为真正的门槛不在下载和启动,而在理解它如何与你的工作流咬合。

关键词里反复出现的“docker安装部署”“mysql安装配置教程”“ubuntu22.04安装教程”,不是偶然。OpenClaw依赖MySQL存储会话历史、技能状态和用户偏好,依赖Redis做任务队列和缓存,依赖Nginx做反向代理和HTTPS终结。这些不是可选组件,而是它的“骨骼系统”。忽略它们,就像给汽车装上引擎却不配变速箱——能转,但带不动负载。所以本教程会从零开始构建这个骨骼,每一步都解释清楚“为什么必须这样”,而不是只给你一行 docker-compose up -d 命令让你盲敲。

2. 部署前必须厘清的四个认知前提

很多新手卡在第一步,不是技术问题,而是认知偏差。我把踩过的坑浓缩成四条铁律,每一条都对应一个高频报错:

2.1 OpenClaw没有“一键安装包”,只有“一键部署模板”

搜索“openclaw安装教程”时,你会看到大量“下载zip解压双击run.bat”的截图。那是2024年早期的旧版Clawdbot,早已废弃。当前OpenClaw官方只提供两种标准交付形态:Docker Compose编排文件(推荐)和Kubernetes Helm Chart(生产级)。所谓“一键”,指的是用 docker-compose.yml 定义好所有服务依赖关系后,执行 docker-compose up -d 自动拉取镜像、创建网络、挂载卷、启动容器。它不等于“免配置”,而是把配置从命令行参数转移到YAML文件里。如果你试图用 pip install openclaw 安装,会得到 No matching distribution found 错误——因为它的CLI工具是独立发布的,不是PyPI包。

提示:OpenClaw CLI( openclaw 命令)和OpenClaw Server(后端服务)是两个独立二进制。CLI用于本地管理(如 openclaw skill install ),Server才是实际运行AI代理的容器。混淆二者是90%初学者报错的根源。

2.2 “本地部署”不等于“只在本机运行”,而是“数据主权在我”

“openclaw本地部署”热搜背后,是用户对数据隐私的焦虑。但很多人误解了“本地”的含义。OpenClaw的“本地”,指模型推理、会话存储、技能代码全部运行在你可控的设备上,不上传原始数据到第三方服务器。但它完全支持混合架构:你可以用本地Ollama跑Qwen2.5-Coder,同时调用云端MiniMax的Abel-3处理长文本摘要,再把结果存进自己NAS上的MySQL。关键在于,所有通信都走你自己的网络,凭证密钥由你保管,日志文件存在你指定的路径。这和“把整个Claude Code搬进内网”有本质区别——OpenClaw是调度器,不是模型仓库。

2.3 技能(Skill)不是插件,而是可调试的Python模块

看到“openclaw skill”“openclaw配置”,新手常以为要下载一堆预设功能。实际上,OpenClaw的Skill机制极度轻量:一个Skill就是一个Python文件,必须包含 def execute(input_data: dict) -> dict: 函数。比如最简单的“天气查询Skill”,只需12行代码:

import requests
def execute(input_data: dict) -> dict:
    city = input_data.get("city", "Beijing")
    api_key = "your_openweathermap_key"
    url = f"http://api.openweathermap.org/data/2.5/weather?q={city}&appid={api_key}"
    response = requests.get(url)
    data = response.json()
    return {
        "temperature": data["main"]["temp"] - 273.15,
        "weather": data["weather"][0]["description"]
    }

把这个文件保存为 weather.py ,执行 openclaw skill install ./weather.py ,它就出现在WebUI的技能列表里。没有复杂的SDK,没有强制框架,就是纯Python。这也是为什么“pycharm安装教程”“vscode安装教程”会出现在热搜里——你真正需要的不是IDE,而是能调试Python的环境。

2.4 WebUI只是“遥控器”,核心逻辑在CLI和配置文件里

几乎所有教程都从“访问http://localhost:3000”开始,这造成了巨大误导。OpenClaw的WebUI(Dashboard)本质是个React前端,它不处理任何业务逻辑,所有操作都通过API调用后端。真正的控制中枢是CLI命令和 config.yaml 。比如修改模型API地址,不是在WebUI里点点点,而是编辑 config.yaml 中的 models 段:

models:
  - name: "qwen2.5-coder"
    type: "ollama"
    endpoint: "http://host.docker.internal:11434" # 注意这里不是localhost!
    model: "qwen2.5-coder:latest"

如果填成 http://localhost:11434 ,容器内会解析失败,因为Docker容器的 localhost 指向自身,不是宿主机。这个细节导致无数人卡在“模型连接超时”。

3. 从零构建生产级部署环境:Ubuntu 22.04 + Docker Compose全链路实操

现在进入实操环节。我以Ubuntu 22.04 LTS为基准环境(这是当前最稳定的服务器发行版),全程使用终端操作,不依赖GUI。所有命令均可直接复制粘贴,但我会解释每一行背后的意图,避免你成为“复制粘贴工程师”。

3.1 环境初始化:绕过APT源和Docker权限两大陷阱

首先更新系统并安装基础工具。注意,Ubuntu 22.04默认源在国内可能缓慢,需切换为清华源:

# 备份原sources.list
sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak
# 替换为清华源(适用于amd64架构)
sudo sed -i 's/archive.ubuntu.com/mirrors.tuna.tsinghua.edu.cn/g' /etc/apt/sources.list
sudo sed -i 's/security.ubuntu.com/mirrors.tuna.tsinghua.edu.cn/g' /etc/apt/sources.list
# 更新并升级
sudo apt update && sudo apt upgrade -y
# 安装必要工具:curl用于下载,git用于克隆仓库,vim用于编辑配置
sudo apt install -y curl git vim net-tools

接下来安装Docker。官方文档推荐用 apt-get install docker.io ,但这会安装旧版(20.10),而OpenClaw要求Docker 24.0+。必须用Docker官方仓库:

# 卸载可能存在的旧版本
sudo apt remove -y docker docker-engine docker.io containerd runc
# 安装依赖
sudo apt install -y ca-certificates curl gnupg lsb-release
# 添加Docker官方GPG密钥
sudo mkdir -p /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
# 添加稳定版仓库
echo \
  "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \
  $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
# 安装Docker Engine
sudo apt update
sudo apt install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
# 验证安装
sudo docker run hello-world

最关键的一步: 将当前用户加入docker组,避免每次都要sudo 。这是新手最常忽略的权限陷阱:

# 创建docker组(如果不存在)
sudo groupadd docker
# 将当前用户加入docker组
sudo usermod -aG docker $USER
# 重新加载组信息(无需重启,但需新shell)
newgrp docker
# 验证:不加sudo能否运行
docker run hello-world

如果最后一步报错“permission denied”,说明 newgrp 未生效,请关闭当前终端,新开一个,再试。

3.2 数据库与缓存服务:MySQL 8.0 + Redis 7.2的精准配置

OpenClaw要求MySQL 8.0+(因使用JSON字段存储会话元数据)和Redis 7.2+(因使用Stream数据结构处理消息队列)。我们用Docker Compose统一管理,确保版本可控:

# 创建项目目录
mkdir -p ~/openclaw-deploy && cd ~/openclaw-deploy
# 创建docker-compose.db.yml,专注数据库服务
cat > docker-compose.db.yml << 'EOF'
version: '3.8'
services:
  mysql:
    image: mysql:8.0
    container_name: openclaw-mysql
    restart: unless-stopped
    environment:
      MYSQL_ROOT_PASSWORD: rootpass123
      MYSQL_DATABASE: openclaw
      MYSQL_USER: openclaw_user
      MYSQL_PASSWORD: openclaw_pass
    ports:
      - "3306:3306"
    volumes:
      - ./data/mysql:/var/lib/mysql
      - ./conf/mysql.cnf:/etc/mysql/conf.d/my.cnf
    command: --default-authentication-plugin=mysql_native_password
  redis:
    image: redis:7.2-alpine
    container_name: openclaw-redis
    restart: unless-stopped
    ports:
      - "6379:6379"
    volumes:
      - ./data/redis:/data
    command: redis-server --appendonly yes
EOF

注意 command: --default-authentication-plugin=mysql_native_password 这一行。MySQL 8.0默认使用 caching_sha2_password 认证插件,但OpenClaw的Python驱动(aiomysql)对它支持不稳定,会导致连接时抛出 Authentication plugin 'caching_sha2_password' cannot be loaded 错误。强制回退到传统插件是唯一可靠方案。

创建MySQL配置文件 ./conf/mysql.cnf ,启用JSON函数支持:

mkdir -p ./conf ./data/mysql ./data/redis
cat > ./conf/mysql.cnf << 'EOF'
[mysqld]
default_authentication_plugin=mysql_native_password
sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION
# 启用JSON函数(OpenClaw必需)
json_functions_enabled=ON
EOF

启动数据库服务:

docker-compose -f docker-compose.db.yml up -d
# 等待30秒让服务初始化
sleep 30
# 验证MySQL是否就绪
docker exec -it openclaw-mysql mysql -uopenclaw_user -popenclaw_pass -e "SELECT VERSION();"
# 验证Redis
docker exec -it openclaw-redis redis-cli ping  # 应返回PONG

3.3 OpenClaw核心服务:Docker Compose编排与网络穿透

现在部署OpenClaw Server。官方GitHub仓库(https://github.com/openclaw/openclaw)提供标准 docker-compose.yml ,但需根据你的环境微调。我们创建 docker-compose.openclaw.yml

cat > docker-compose.openclaw.yml << 'EOF'
version: '3.8'
services:
  openclaw:
    image: ghcr.io/openclaw/openclaw:latest
    container_name: openclaw-server
    restart: unless-stopped
    depends_on:
      - mysql
      - redis
    environment:
      DB_HOST: mysql
      DB_PORT: 3306
      DB_NAME: openclaw
      DB_USER: openclaw_user
      DB_PASSWORD: openclaw_pass
      REDIS_URL: redis://redis:6379/0
      # 模型配置:这里先指向Ollama,后续可扩展
      MODEL_PROVIDER: ollama
      OLLAMA_BASE_URL: http://host.docker.internal:11434
      OLLAMA_MODEL: qwen2.5-coder:latest
      # WebUI端口映射
      PORT: 3000
    ports:
      - "3000:3000"
      - "8000:8000" # API端口,供其他服务调用
    volumes:
      - ./data/openclaw:/app/data
      - ./config.yaml:/app/config.yaml
    networks:
      - openclaw-net
  nginx:
    image: nginx:alpine
    container_name: openclaw-nginx
    restart: unless-stopped
    depends_on:
      - openclaw
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - ./conf/nginx.conf:/etc/nginx/nginx.conf
      - ./data/certs:/etc/nginx/certs
    networks:
      - openclaw-net
networks:
  openclaw-net:
    driver: bridge
EOF

关键配置解析:

  • DB_HOST: mysql :Docker内部服务发现,容器名即主机名,不是 localhost
  • OLLAMA_BASE_URL: http://host.docker.internal:11434 host.docker.internal 是Docker Desktop的特殊DNS,但在Linux上需手动添加。执行:
    # 为Docker守护进程添加host.docker.internal解析
    echo '172.17.0.1 host.docker.internal' | sudo tee -a /etc/hosts
    
  • volumes 挂载: ./config.yaml 是核心配置文件,必须提前创建

创建最小化 config.yaml

cat > config.yaml << 'EOF'
# OpenClaw全局配置
server:
  host: 0.0.0.0
  port: 3000
  cors_origins: ["*"]
database:
  type: mysql
  host: mysql
  port: 3306
  name: openclaw
  user: openclaw_user
  password: openclaw_pass
redis:
  url: redis://redis:6379/0
models:
  - name: "qwen2.5-coder"
    type: "ollama"
    endpoint: "http://host.docker.internal:11434"
    model: "qwen2.5-coder:latest"
channels:
  - name: "web"
    type: "web"
    enabled: true
skills:
  - name: "hello"
    type: "builtin"
    enabled: true
EOF

启动OpenClaw:

docker-compose -f docker-compose.openclaw.yml up -d
# 查看日志确认启动成功
docker logs -f openclaw-server
# 正常应看到类似输出:
# INFO:     Application startup complete.
# INFO:     Uvicorn running on http://0.0.0.0:3000 (Press CTRL+C to quit)

3.4 CLI工具链安装:解决“openclaw命令不存在”终极方案

现在访问 http://localhost:3000 能看到WebUI,但终端输入 openclaw 仍报错。这是因为CLI是独立Python包,需用pipx(推荐)或pip安装:

# 安装pipx(安全的Python包管理器)
curl https://raw.githubusercontent.com/pypa/pipx/main/get-pipx.py | python3
# 安装OpenClaw CLI
pipx install openclaw-cli
# 验证
openclaw --version  # 应输出类似 0.8.2

pipx pip install --user 更安全,因为它为每个包创建隔离环境,避免依赖冲突。如果 pipx 安装失败,备选方案:

# 使用venv隔离安装
python3 -m venv ~/openclaw-cli-env
source ~/openclaw-cli-env/bin/activate
pip install openclaw-cli
# 将CLI路径加入PATH
echo 'export PATH="$HOME/openclaw-cli-env/bin:$PATH"' >> ~/.bashrc
source ~/.bashrc

此时 openclaw 命令可用,但还需配置CLI连接到本地Server:

# 创建CLI配置目录
mkdir -p ~/.config/openclaw
# 写入服务器地址
echo '{"server_url": "http://localhost:3000"}' > ~/.config/openclaw/config.json
# 测试连接
openclaw status  # 应返回服务器状态JSON

4. 从“能跑”到“好用”:模型接入、技能开发与通道配置实战

部署完成只是起点。OpenClaw的价值体现在如何让它真正融入你的工作流。下面三个实战场景,覆盖90%新手需求。

4.1 模型接入:Ollama本地推理与MiniMax云端API双活配置

Ollama是本地部署的最佳选择,但免费模型能力有限。OpenClaw支持多模型并存,按需路由。首先在宿主机安装Ollama:

# 下载Ollama Linux二进制
curl -fsSL https://ollama.com/install.sh | sh
# 拉取Qwen2.5-Coder(专为代码生成优化)
ollama pull qwen2.5-coder:latest
# 验证
ollama list  # 应显示qwen2.5-coder

修改 config.yaml ,添加MiniMax模型作为备用:

models:
  - name: "qwen2.5-coder"
    type: "ollama"
    endpoint: "http://host.docker.internal:11434"
    model: "qwen2.5-coder:latest"
  - name: "minimax-abel3"
    type: "minimax"
    api_key: "your_minimax_api_key"  # 从MiniMax控制台获取
    group_id: "your_group_id"
    model: "absl-3"
    base_url: "https://api.minimax.chat/v1/text/chatcompletion"

重启OpenClaw使配置生效:

docker-compose -f docker-compose.openclaw.yml restart openclaw

在WebUI的“Models”页面,你会看到两个模型。发送消息时,可在输入框旁下拉选择模型。实测发现:Qwen2.5-Coder在16GB显存的RTX 4090上,响应时间<2秒,适合实时编码辅助;MiniMax Abel-3在长文本摘要任务上准确率高15%,但有API调用延迟(约1.2秒)。这种混合策略,比单一模型更实用。

注意:MiniMax API Key必须严格保密。切勿硬编码在 config.yaml 中。生产环境应使用环境变量注入:

# 启动时传入
docker-compose -f docker-compose.openclaw.yml up -d --build \
  --env-file .env.minimax

.env.minimax 内容:

MINIMAX_API_KEY=your_key_here
MINIMAX_GROUP_ID=your_id_here

4.2 技能开发:三步打造“NAS监控报警”Skill

以真实需求为例:我的Synology NAS每天生成监控截图,希望AI自动识别异常(如硬盘红灯、CPU过热),并推送钉钉告警。这就是OpenClaw Skill的典型场景。

第一步:编写Skill代码 创建 nas_monitor.py

import os
import cv2
import numpy as np
from datetime import datetime

def execute(input_data: dict) -> dict:
    # 从input_data获取NAS截图路径(由WebUI或Channel传入)
    image_path = input_data.get("image_path")
    if not image_path or not os.path.exists(image_path):
        return {"error": "Image path not provided or file not exists"}
    
    # 读取图像
    img = cv2.imread(image_path)
    if img is None:
        return {"error": "Failed to load image"}
    
    # 简单的红灯检测:查找红色像素区域(HSV色彩空间)
    hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
    # 定义红色范围(HSV)
    lower_red1 = np.array([0, 100, 100])
    upper_red1 = np.array([10, 255, 255])
    lower_red2 = np.array([160, 100, 100])
    upper_red2 = np.array([180, 255, 255])
    mask1 = cv2.inRange(hsv, lower_red1, upper_red1)
    mask2 = cv2.inRange(hsv, lower_red2, upper_red2)
    red_mask = cv2.bitwise_or(mask1, mask2)
    
    # 计算红色像素占比
    red_pixels = cv2.countNonZero(red_mask)
    total_pixels = img.shape[0] * img.shape[1]
    red_ratio = red_pixels / total_pixels
    
    # 判断异常(阈值可调)
    is_abnormal = red_ratio > 0.001  # 0.1%红色区域即告警
    
    return {
        "timestamp": datetime.now().isoformat(),
        "red_ratio": round(red_ratio, 4),
        "is_abnormal": is_abnormal,
        "suggestion": "Check NAS health status" if is_abnormal else "System normal"
    }

第二步:安装Skill

# 确保cv2已安装(OpenClaw容器内默认无OpenCV)
docker exec -it openclaw-server pip install opencv-python-headless
# 安装Skill
openclaw skill install ./nas_monitor.py

第三步:在WebUI中配置触发

  • 进入WebUI → Skills → nas_monitor → Edit
  • 设置 Input Schema 为JSON Schema,定义期望输入:
    {
      "type": "object",
      "properties": {
        "image_path": {"type": "string", "description": "Full path to NAS screenshot"}
      },
      "required": ["image_path"]
    }
    
  • 保存后,在“Test”面板输入:
    {"image_path": "/app/data/nas_screenshot.png"}
    
    如果容器内有该文件,将返回分析结果。

这个Skill可被任何Channel调用。例如,配置一个Cron Job定时抓取NAS截图,然后用 openclaw channel trigger 命令触发Skill,结果推送到钉钉。

4.3 通道配置:从WebUI到飞书机器人的无缝对接

OpenClaw内置Web、WhatsApp、Telegram等通道,但企业最常用的是飞书。配置飞书机器人只需三步:

第一步:在飞书开放平台创建机器人

  • 登录飞书管理后台 → 工作台 → 机器人 → 创建自定义机器人
  • 获取Webhook URL(形如 https://open.feishu.cn/open-apis/bot/v2/hook/xxx

第二步:在OpenClaw中添加飞书通道 编辑 config.yaml ,在 channels 下添加:

  - name: "feishu-bot"
    type: "feishu"
    webhook_url: "https://open.feishu.cn/open-apis/bot/v2/hook/xxx"  # 替换为你的URL
    enabled: true
    # 可选:设置消息格式
    message_template: |
      ## {{.Message.Title}}
      {{.Message.Content}}
      > 来自OpenClaw {{.Timestamp}}

第三步:测试与调试 重启OpenClaw后,在WebUI的Channels页面,找到 feishu-bot ,点击“Test”。如果飞书群收到测试消息,说明配置成功。

实战技巧:飞书机器人默认不支持@所有人。如需紧急告警,需在Webhook URL后添加 ?enable_sign=true ,并在请求体中加入签名验证。OpenClaw 0.8.2已内置此支持,只需在 config.yaml 中添加:

    sign_secret: "your_feishu_sign_secret"  # 在飞书后台获取

5. 常见故障排查链路:从“命令不存在”到“模型连接超时”的完整诊断树

部署过程中,90%的问题集中在五个节点。我按发生频率排序,给出可复现的排查链路:

5.1 故障现象: openclaw 命令不存在

排查链路:

  1. 确认pipx是否安装成功
    which pipx → 若无输出,说明pipx未安装,执行 curl https://raw.githubusercontent.com/pypa/pipx/main/get-pipx.py | python3
  2. 确认CLI是否安装
    pipx list → 查看 openclaw-cli 是否在列表中。若无,执行 pipx install openclaw-cli
  3. 确认PATH是否包含pipx bin目录
    echo $PATH → 检查是否含 ~/.local/bin (pipx默认路径)。若无,执行 echo 'export PATH="$HOME/.local/bin:$PATH"' >> ~/.bashrc && source ~/.bashrc
  4. 确认CLI配置文件是否存在
    ls -l ~/.config/openclaw/config.json → 若不存在,手动创建,内容为 {"server_url": "http://localhost:3000"}

5.2 故障现象:WebUI打不开(502 Bad Gateway)

排查链路:

  1. 检查Nginx容器是否运行
    docker ps | grep openclaw-nginx → 若无,执行 docker-compose -f docker-compose.openclaw.yml up -d nginx
  2. 检查Nginx日志
    docker logs openclaw-nginx → 常见错误 connect() failed (111: Connection refused) while connecting to upstream ,说明Nginx无法连接openclaw容器
  3. 检查openclaw容器网络
    docker inspect openclaw-server | grep IPAddress → 确认IP在 openclaw-net 网络中
  4. 检查openclaw服务是否监听
    docker exec -it openclaw-server netstat -tuln | grep :3000 → 若无输出,说明Uvicorn未启动,查看 docker logs openclaw-server

5.3 故障现象:模型连接超时(Ollama)

排查链路:

  1. 确认Ollama在宿主机运行
    systemctl status ollama → 若未运行,执行 sudo systemctl start ollama
  2. 确认Ollama监听地址
    sudo ss -tuln | grep :11434 → 应显示 *:11434 。若为 127.0.0.1:11434 ,需修改Ollama配置:
    echo 'OLLAMA_HOST=0.0.0.0:11434' | sudo tee -a /etc/environment
    sudo systemctl restart ollama
    
  3. 确认Docker容器能否访问宿主机Ollama
    docker exec -it openclaw-server curl -v http://host.docker.internal:11434/api/tags → 应返回Ollama模型列表。若超时,检查 /etc/hosts host.docker.internal 是否指向 172.17.0.1

5.4 故障现象:MySQL连接拒绝(Access denied)

排查链路:

  1. 确认MySQL容器运行
    docker ps | grep openclaw-mysql
  2. 确认MySQL日志无认证错误
    docker logs openclaw-mysql | grep "Access denied" → 若有,说明用户名密码错误
  3. 进入MySQL容器手动测试
    docker exec -it openclaw-mysql mysql -uopenclaw_user -popenclaw_pass openclaw -e "SELECT 1;" → 若失败,检查 docker-compose.db.yml MYSQL_USER MYSQL_PASSWORD 是否与 config.yaml 一致
  4. 确认MySQL插件兼容性
    docker exec -it openclaw-mysql mysql -uopenclaw_user -popenclaw_pass -e "SELECT plugin FROM mysql.user WHERE User='openclaw_user';" → 应返回 mysql_native_password 。若为 caching_sha2_password ,需重建用户:
    docker exec -it openclaw-mysql mysql -uroot -prootpass123 -e "
    CREATE USER 'openclaw_user'@'%' IDENTIFIED WITH mysql_native_password BY 'openclaw_pass';
    GRANT ALL PRIVILEGES ON openclaw.* TO 'openclaw_user'@'%';
    FLUSH PRIVILEGES;"
    

5.5 故障现象:技能执行失败(ModuleNotFoundError)

排查链路:

  1. 确认Skill文件路径正确
    openclaw skill list → 查看路径是否为绝对路径。若为相对路径,CLI可能找不到
  2. 确认容器内是否有依赖
    docker exec -it openclaw-server pip list | grep opencv → 若无,执行 docker exec -it openclaw-server pip install opencv-python-headless
  3. 确认Skill函数签名正确
    打开Skill文件,检查是否为 def execute(input_data: dict) -> dict: 。若参数名或类型不符,OpenClaw会静默失败
  4. 查看OpenClaw日志中的详细错误
    docker logs openclaw-server | tail -20 → 技能执行错误会打印完整Traceback,定位到具体行号

6. 生产环境加固与长期维护指南

部署成功后,真正的挑战才开始:如何保证7×24小时稳定运行?如何安全升级?如何备份关键数据?以下是经过生产验证的维护清单。

6.1 安全加固:关闭危险端口与启用HTTPS

默认配置暴露 3306 (MySQL)、 6379 (Redis)、 3000 (OpenClaw)端口,存在安全隐患。必须限制访问:

# 修改docker-compose.db.yml,注释掉ports映射
# ports:
#   - "3306:3306"
#   - "6379:6379"
# 改为仅内部网络访问
# 然后在docker-compose.openclaw.yml中,确保openclaw服务通过service name连接
# DB_HOST: mysql  # 不再依赖宿主机端口

启用HTTPS是必须项。使用Nginx + Let's Encrypt:

# 安装certbot
sudo apt install -y certbot python3-certbot-nginx
# 获取证书(替换your-domain.com)
sudo certbot --nginx -d your-domain.com
# 证书自动续期
sudo crontab -e
# 添加:0 12 * * * /usr/bin/certbot renew --quiet --post-hook "systemctl reload nginx"

修改 ./conf/nginx.conf ,启用HTTPS重定向:

server {
    listen 80;
    server_name your-domain.com;
    return 301 https://$server_name$request_uri;
}

server {
    listen 443 ssl;
    server_name your-domain.com;
    ssl_certificate /etc/nginx/certs/fullchain.pem;
    ssl_certificate_key /etc/nginx/certs/privkey.pem;
    location / {
        proxy_pass http://openclaw-server:3000;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
    }
}

6.2 数据持久化与灾难恢复

OpenClaw的核心数据是MySQL中的 openclaw 数据库和 ./data/openclaw 下的技能文件、日志。制定备份策略:

# 创建备份脚本 backup.sh
cat > backup.sh << 'EOF'
#!/bin/bash
DATE=$(date +%Y%m%d_%H%M%S)
BACKUP_DIR="/backup/openclaw"
mkdir -p $BACKUP_DIR

# 备份MySQL
docker exec openclaw-mysql mysqldump -uopenclaw_user -popenclaw_pass openclaw > $BACKUP_DIR/openclaw_db_$DATE.sql

# 备份OpenClaw数据卷
tar -czf $BACKUP_DIR/openclaw_data_$DATE.tar.gz -C ~/openclaw-deploy ./data/openclaw

# 清理7天前备份
find $BACKUP_DIR -name "openclaw_*" -mtime +7 -delete
EOF

chmod +x backup.sh
# 加入crontab每日凌晨2点执行
(crontab -l 2>/dev/null; echo "0 2 * * * /home/youruser/openclaw-deploy/backup.sh") | crontab -
Logo

Agent 垂直技术社区,欢迎活跃、内容共建。

更多推荐