Files
2026-10-11 19:06:33 +08:00

112 lines
4.9 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# 11.3 数据库迁移
:::tip CLI 迁移工具
除了 SQL 脚本导入,项目还提供了 `php artisan db:migrate` 命令行工具,支持跨数据库自动迁移(MySQL → PostgreSQL 等),自动迁移索引、外键、字段注释、表注释、默认值和排序规则(collation),数据迁移基于主键稳定排序并支持事务回滚,详见 [7.6 数据库迁移工具](/zh/tools/db-migrate.html)。
:::
:::info 表结构以 SQL 脚本为准
本项目 `database/migrations/` 为空,表结构不通过 Laravel 的 `php artisan migrate` 管理,而是由 `document/` 下的 SQL 脚本导入。
:::
## SQL 脚本目录
```text
document/
├── mysql/rxthinkcmf.laravel13.naivevue.sql # MySQL 版本(约 216MB)
├── postgresql/rxthinkcmf.laravel13.naivevue.sql # PostgreSQL 版本
├── sqlserver/rxthinkcmf.laravel13.naivevue.sql # SQL Server 版本
├── oracle/rxthinkcmf.laravel13.naivevue.sql # Oracle 版本
└── sqlite/rxthinkcmf.laravel13.naivevue.sql # SQLite 版本
```
各数据库脚本已针对对应的 SQL 语法做了适配,可直接导入。表前缀统一为 `laravel_`(与 `.env` 中 `DB_PREFIX` 一致)。
## 各数据库导入命令
### MySQL
```bash
# -----------------------------------------------------------------------------
# MySQL 导入(mysql 命令行工具)
# -----------------------------------------------------------------------------
# -u root -proot → 用户名和密码
# rxthinkcmf... → 目标数据库名
# < file.sql → 将 SQL 文件作为输入执行
mysql -u root -proot rxthinkcmf.laravel13.naivevue < document/mysql/rxthinkcmf.laravel13.naivevue.sql
```
### PostgreSQL
```bash
# -----------------------------------------------------------------------------
# PostgreSQL 导入(psql 命令行工具)
# -----------------------------------------------------------------------------
# -U postgres → 连接用户
# -d ... → 目标数据库
# -f file.sql → 执行 SQL 文件
psql -U postgres -d rxthinkcmf.laravel13.naivevue -f document/postgresql/rxthinkcmf.laravel13.naivevue.sql
```
### SQL Server
```bash
# -----------------------------------------------------------------------------
# SQL Server 导入(sqlcmd 命令行工具)
# -----------------------------------------------------------------------------
# -S localhost → 服务器地址
# -U sa → 用户名(sa 为管理员)
# -P root → 密码
# -d ... → 目标数据库
# -i file.sql → 执行 SQL 文件
sqlcmd -S localhost -U sa -P root -d rxthinkcmf.laravel13.naivevue -i document/sqlserver/rxthinkcmf.laravel13.naivevue.sql
```
### Oracle
```bash
# -----------------------------------------------------------------------------
# Oracle 导入(sqlplus 命令行工具)
# -----------------------------------------------------------------------------
# root/root@localhost:1521/XE → 用户/密码@主机:端口/服务名
# @file.sql → 执行 SQL 文件
sqlplus root/root@localhost:1521/XE @document/oracle/rxthinkcmf.laravel13.naivevue.sql
```
### SQLite
```bash
# -----------------------------------------------------------------------------
# SQLite 导入(sqlite3 命令行工具)
# -----------------------------------------------------------------------------
# 数据库文件路径 → SQLite 数据库存储在单个文件中
# < file.sql → 将 SQL 文件作为输入执行
sqlite3 database/database.sqlite < document/sqlite/rxthinkcmf.laravel13.naivevue.sql
```
## 跨数据库迁移(CLI)
在已导入源数据库后,可用 `php artisan db:migrate` 在不同数据库类型间迁移:
```bash
# -----------------------------------------------------------------------------
# 跨数据库迁移(MySQL → PostgreSQL 示例)
# -----------------------------------------------------------------------------
# --src-* / --dst-* → 源库与目标库连接参数(端口省略时按驱动取默认值)
# --drop-target-first → 迁移前先删除目标库中的同名表
# --batch-size → 批量插入行数(默认 1000)
# --tables → 只迁移指定表(逗号分隔),不传则全部
# 目标库不存在时自动创建;迁到 PostgreSQL 需先在 php.ini 启用 extension=pdo_pgsql
php artisan db:migrate \
--src-type=mysql --src-host=127.0.0.1 --src-port=3306 \
--src-database=rxthinkcmf.laravel13.naivevue --src-username=root --src-password=root \
--dst-type=pgsql --dst-host=127.0.0.1 --dst-port=5432 \
--dst-database=rxthinkcmf.laravel13.naivevue --dst-username=postgres --dst-password=123456
```
## 注意事项
- 每种数据库的主脚本已做语法适配,无需手动转换
- 导入前请先创建数据库并配置好用户权限
- 脚本体积较大(约 216MB),导入耗时较长,建议在内网或本机执行;可先用 `--tables` 只迁移部分表
- 详见 [2.5 数据库初始化](/zh/quickstart/database-init.html)