0

我有一些非常基本的 Node.js 代码来创建 MySQL 数据库。我已经安装了mysql。我的代码出现了一条损坏的错误消息,指出最后数据包乱序。应用程序端口为 3306。

var mysql = require('mysql');

var con = mysql.createConnection({
    host: "localhost",
    user: "root",
    password: "75577557"
});

con.connect(function (err) {

    if (err) throw err;
    console.log("Connected!");
    con.query("CREATE DATABASE mydb", function (err, result) {
        if (err) throw err;
        console.log("Database created");
    });
});
4

2 回答 2

0

You need to specify the database name in your con object when attempting to connect to your database. The mysql.createConnection() method requires you to specify the database name, as it will fail to connect if no database name is specified. It should look something like this:

let con = mysql.createConnection({
    host: "localhost",
    user: "root",
    password: "75577557",
    database: "database_name"
});
于 2021-04-01T22:32:10.213 回答
0

看来我使用的 mysql 包不好。我安装了一个不同的“mysql2”包,效果很好。由于包裹不佳而损失了一天。我正在尝试将网站从 MVC 复制到 nodejs。第一个任务是创建和构建数据库。到目前为止,我已经成功地创建了数据库并创建了它的所有表,最终取得了进展。

于 2021-04-03T02:27:57.260 回答