sqlx:Rust 工具 · 部署与使用全指南
一句话定位:
transact-rs/sqlx是 transact-rs 开源的Rust 工具项目,GitHub 上已有 17743 Stars,主语言 Rust,Apache-2.0 开源许可。仓库地址:https://github.com/transact-rs/sqlx
一、项目介绍
sqlx 是 transact-rs 开源的项目,目前在 GitHub 上收获 17743 颗 Star,主要使用 Rust 开发,采用 Apache-2.0 开源许可。官方给出的简介是:🧰 The Rust SQL Toolkit. An async, pure Rust SQL crate featuring compile-time checked queries without a DSL. Supports PostgreSQL, MySQL, and SQLite.
它面向的是Rust 工具场景,核心是数据存储、查询优化、备份恢复与权限管理这些工程环节,适合自建或替换现有数据库服务。
本文按「是什么 → 官方信息 → 核心特性 → 环境要求 → 本地部署 → 用法示例 → 常见问题 → 相关链接 → 实操清单」的顺序整理,目标是看完就能在自己的机器上跑起来。
二、官方信息
| 项目 | 信息 |
|---|---|
| 仓库 | transact-rs/sqlx |
| 官方主页 | https://github.com/transact-rs/sqlx |
| Star 数 | 17743 |
| 主要语言 | Rust |
| 开源许可 | Apache-2.0 |
| 创建时间 | 2019-12-28 |
| 最近更新 | 2026-11-25 |
| 主题标签 | async, await, mariadb, mysql, postgres, postgresql, rust, sql |
三、项目结构与核心特性
核心特性
- async(async)
- await(await)
- mariadb(mariadb)
- mysql(mysql)
- postgres(postgres)
- postgresql(postgresql)
- Rust 工具(rust)
- 数据库(sql)
- `runtime-async-std`: Use the `async-std` runtime without enabling a TLS backend.
- `runtime-tokio`: Use the `tokio` runtime without enabling a TLS backend.
- Actix-web is fully compatible with Tokio and so a separate runtime feature is no longer needed.
- `tls-native-tls`: Use the `native-tls` TLS backend (OpenSSL on *nix, SChannel on Windows, Secure Transport on macOS).
- `tls-rustls`: Use the `rustls` TLS backend (cross-platform backend, only supports TLS 1.2 and 1.3).
- `tls-rustls-aws-lc-rs`: Use the `rustls` TLS backend with `aws-lc-rs` and bundled WebPKI CA certificates.
README 章节结构
- Install
- Cargo.toml
- PICK ONE OF THE FOLLOWING:
- tokio (no TLS)
- tokio + native-tls
- tokio + rustls with ring and WebPKI CA certificates
- tokio + rustls with aws-lc-rs and WebPKI CA certificates
- async-std (no TLS)
- async-std + native-tls
- async-std + rustls with ring and WebPKI CA certificates
- SQLx is not an ORM!
- Usage
四、环境与版本要求
官方 README 没有单独列出环境章节,按项目类型给出通用建议:
- 操作系统:Linux(Ubuntu 22.04+ / Debian 12+)优先,macOS 12+ 亦可
- 内存:2 GB 起步,AI 类项目建议 8 GB 以上
- 磁盘:预留 10 GB 以上空间存放依赖与模型
- 运行时:Rust 1.75+ 与 cargo
- 网络:能访问 GitHub 与项目依赖源(国内可配置镜像加速)
五、本地部署步骤
1. 克隆仓库
git clone https://github.com/transact-rs/sqlx.git
cd sqlx2. 安装依赖
README 未给出明确安装命令,按语言生态执行对应命令(以实际文档为准)。
3. 启动服务
# Cargo.toml
[dependencies]
# PICK ONE OF THE FOLLOWING:
# tokio (no TLS)
sqlx = { version = "0.9", features = [ "runtime-tokio" ] }
# tokio + native-tls
sqlx = { version = "0.9", features = [ "runtime-tokio", "tls-native-tls" ] }
# tokio + rustls with ring and WebPKI CA certificates
sqlx = { version = "0.9", features = [ "runtime-tokio", "tls-rustls-ring-webpki" ] }
# tokio + rustls with ring and platform's native CA certificates
sqlx = { version = "0.9", features = [ "runtime-tokio", "tls-rustls-ring-native-roots" ] }
# tokio + rustls with aws-lc-rs and WebPKI CA certificates
sqlx = { version = "0.9", features = [ "runtime-tokio", "tls-rustls-aws-lc-rs" ] }
# tokio + rustls with aws-lc-rs and platform's native CA certificates
sqlx = { version = "0.9", features = [ "runtime-tokio", "tls-rustls-aws-lc-rs-native-roots" ] }4. 验证
启动后查看终端输出的监听地址(常见为 http://127.0.0.1:3000 或 :8080),浏览器打开对应端口,或用 curl -I 检查返回状态码。
六、API 用法与示例
该项目主要面向命令行或图形界面使用,官方 README 未单独提供 HTTP API 文档。需要二次开发时,建议直接阅读源码中的入口文件与 docs/ 目录,或在仓库 Issues 中检索相关讨论。
七、常见问题 FAQ
sqlx 是做什么的?
一句话:🧰 The Rust SQL Toolkit. An async, pure Rust SQL crate featuring compile-time checked queries without a DSL. Supports PostgreSQL, MySQL, and SQLite.
部署需要什么环境?
优先 Linux,2 GB 内存起步;具体运行时版本见上文「四、环境与版本要求」,以 README 最新说明为准。
是否免费?可以商用吗?
项目采用 Apache-2.0 许可,能否商用取决于该许可条款,建议上线前确认许可证原文与依赖树的许可兼容性。
怎么升级到新版本?
在项目目录执行 git pull,重新安装依赖并按 README 的升级说明重启服务;有数据库的项目先备份。
启动失败怎么排查?
先看启动日志的首个报错行,再对照 README 的 Requirements 检查版本;仍无法解决可在仓库 Issues 搜索关键词。
八、相关链接
- GitHub 仓库:https://github.com/transact-rs/sqlx
- Releases 版本页:https://github.com/transact-rs/sqlx/releases
- Issues 问题反馈:https://github.com/transact-rs/sqlx/issues
- 官方主页:https://github.com/transact-rs/sqlx
- 组织主页:https://github.com/transact-rs
九、安装部署(实操步骤)
- 确认服务器满足上文的环境要求,安装 git 与对应运行时。
- 执行
git clone https://github.com/transact-rs/sqlx.git拉取代码。 - 进入项目目录,按 README 安装依赖(Docker 项目直接
docker compose up -d)。 - 复制并修改示例配置文件(如
cp .env.example .env),填入自己的密钥与端口。 - 启动服务,观察日志确认无报错。
- 浏览器访问监听地址,完成初始化配置。
- 如需公网访问,用 Nginx 反向代理并配置 HTTPS 证书。
- 设置开机自启(systemd 或 Docker restart 策略),并配置日志轮转与备份。