迁移 SQLite 数据库到 MySQL
为方便查看 AI 网关上游渠道具体错误,于是开启了错误日志详细记录,其次个人使用最初只是参考官方简单部署默认使用到内置到 SqLite 数据库,为了方便数据统一备份,于是准备迁移到 mysql 数据,已经用了很久了必须把数据库数据库迁移过去。
查阅相关资料 SQLite 数据库迁移到 MySQL,有说使用 Navicat 导出数据再导入的,还有直接通过数据传输直接弄过去的。
上面这 2 种方法可以是可以,如果发现两边字段类型不一致的话需要反复修正然后才能成功。
最后再往上找到了基于 python 的组件 sqlite3-to-mysql,直接安装后几行命令就直接迁移成功,没有那么问题,在此记录一下。
安装组件
pip install sqlite3-to-mysql
安装之后执行一下sqlite3mysql --help 验证
打印如下日志则成功:
Usage: sqlite3mysql [OPTIONS]
sqlite3mysql version 2.6.0 Copyright (c) 2018-2026 Klemen Tusar
Options:
-f, --sqlite-file PATH SQLite3 database file [required]
-t, --sqlite-tables TUPLE Transfer only these specific tables (space
separated table names). Implies --without-
foreign-keys which inhibits the transfer of
foreign keys. Can not be used together with
--exclude-sqlite-tables.
-e, --exclude-sqlite-tables TUPLE
Transfer all tables except these specific
tables (space separated table names).
Implies --without-foreign-keys which
inhibits the transfer of foreign keys. Can
not be used together with --sqlite-tables.
-A, --sqlite-views-as-tables Materialize SQLite views as tables in MySQL
instead of creating matching MySQL views
(legacy behavior).
-X, --without-foreign-keys Do not transfer foreign keys.
-W, --ignore-duplicate-keys Ignore duplicate keys. The default behavior
is to create new ones with a numerical
suffix, e.g. 'exising_key' ->
'existing_key_1'
-d, --mysql-database TEXT MySQL database name [required]
-u, --mysql-user TEXT MySQL user [required]
-p, --prompt-mysql-password Prompt for MySQL password
--mysql-password TEXT MySQL password
-h, --mysql-host TEXT MySQL host. Defaults to localhost.
-P, --mysql-port INTEGER MySQL port. Defaults to 3306.
-k, --mysql-socket FILE Path to MySQL unix socket file. Cannot be
used with --mysql-ssl-* options.
--mysql-ssl-ca PATH Path to SSL CA certificate file. Cannot be
used with --mysql-socket or --skip-ssl.
--mysql-ssl-cert PATH Path to SSL certificate file. Must be
provided together with --mysql-ssl-key.
Cannot be used with --mysql-socket or
--skip-ssl.
--mysql-ssl-key PATH Path to SSL key file. Must be provided
together with --mysql-ssl-cert. Cannot be
used with --mysql-socket or --skip-ssl.
-S, --skip-ssl Disable MySQL connection encryption. Cannot
be used with --mysql-ssl-* options.
-i, --mysql-insert-method [DEFAULT|IGNORE|UPDATE]
MySQL insert method. DEFAULT will throw
errors when encountering duplicate records;
UPDATE will update existing rows; IGNORE
will ignore insert errors. Defaults to
IGNORE.
-E, --mysql-truncate-tables Truncates existing tables before inserting
data.
--mysql-integer-type TEXT MySQL default integer field type. Defaults
to INT(11).
--mysql-string-type TEXT MySQL default string field type. Defaults to
VARCHAR(255).
--mysql-text-type [LONGTEXT|MEDIUMTEXT|TEXT|TINYTEXT]
MySQL default text field type. Defaults to
TEXT.
--mysql-charset TEXT MySQL database and table character set
[default: utf8mb4]
--mysql-collation TEXT MySQL database and table collation
-T, --use-fulltext Use FULLTEXT indexes on TEXT columns. Will
throw an error if your MySQL version does
not support InnoDB FULLTEXT indexes!
--with-rowid Transfer rowid columns.
-c, --chunk INTEGER Chunk reading/writing SQL records
-K, --mysql-skip-create-tables Skip creating tables in MySQL.
-J, --mysql-skip-transfer-data Skip transferring data to MySQL.
-l, --log-file PATH Log file
-q, --quiet Quiet. Display only errors.
--debug Debug mode. Will throw exceptions.
--version Show the version and exit.
--help Show this message and exit.
正式迁移命令
sqlite3mysql \
--sqlite-file ./ai-proxy.db \
--mysql-user root \
--mysql-password "yourmysqlpasswd" \
--mysql-database 目标mysql数据库名 \
--mysql-host 目标mysql数据库host \
--mysql-port 目标mysql数据库端口执行完命令之后直接到 mysql 数据库中,启动项目验证即可。
商业转载请联系作者获得授权,非商业转载请注明本文出处及文章链接