简体   繁体   English

使用 NodeJS (Express) + TypeScript 从 dump.sql 文件创建数据库

[英]Create DB from a dump.sql file with NodeJS (Express) + TypeScript

I'm using Express and TypeScript to build an app.我正在使用 Express 和 TypeScript 构建应用程序。 This app is already configured to work with MySQL but my problem is that I don't know yet how to create the database based on a dump.sql file.此应用程序已配置为与 MySQL 一起使用,但我的问题是我还不知道如何基于 dump.sql 文件创建数据库。

CREATE DATABASE IF NOT EXISTS test;

USE test;

DROP TABLE IF EXISTS testData;

CREATE TABLE testData(
  name VARCHAR(20) NOT NULL,
  valid INT NULL DEFAULT NULL,
  PRIMARY KEY (name)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

INSERT INTO testDataVALUES
('Peter', NULL),
.
.
.

This is what I'm trying to find, passing the sql file as variable or something after being exported or something from it file because with the.sql extension I can't export the file.这就是我想要找到的,将 sql 文件作为变量传递,或者在导出后或从它的文件中传递一些东西,因为使用 .sql 扩展名我无法导出文件。

connection.query(dump.sql, function (err, rows, fields) {
   if (err) throw err;
   console.log('The database was successfully created..');
});

So is it possible to do it?那么有可能做到吗? I would like to know HOW because I don't see any documentation up to date related to this... otherwise I will create the DB manually in xampp and after use the db but I think this is not the way to do it.我想知道如何,因为我没有看到任何与此相关的最新文档......否则我将在 xampp 中手动创建数据库,并在使用数据库之后,但我认为这不是这样做的方法。

Thanks!!!谢谢!!!

If this is a dump file produced by mysqldump then you should just execute a shell command to run the mysql client and source that dump file as its input.如果这是mysqldump生成的转储文件,那么您应该只 执行 shell 命令来运行 mysql 客户端并将该转储文件作为其输入。

The reason is that dump files may contain mysql client builtin commands that are not recognized by the MySQL Server.原因是转储文件可能包含MySQL 服务器无法识别的 mysql 客户端内置命令 They are preprocessed by the mysql client.它们由 mysql 客户端进行预处理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM