简体   繁体   English

NodeJS - 连接到本地 Mysql 数据库

[英]NodeJS - Connecting to a local Mysql Database

I am trying to create a mysql database connection for my node app and ran sequelize.authenticate().then(function(errors) { console.log(errors) });我正在尝试为我的节点应用程序创建一个 mysql 数据库连接并运行sequelize.authenticate().then(function(errors) { console.log(errors) }); to test if the connection worked.测试连接是否有效。 The response that is logged to my console is Executing (default): SELECT 1+1 AS result undefined The undefined portion makes me think that the connection either didn't work or that there isn't any database found.记录到我的控制台的响应是Executing (default): SELECT 1+1 AS result undefined undefined部分让我认为连接要么不起作用,要么没有找到任何数据库。 I can't seem to figure that out.我似乎无法弄清楚。 I thought by creating a database through Sequel Pro and connecting to localhost via the Socket, I can use the same credentials for connecting with my Node app.我想通过 Sequel Pro 创建一个数据库并通过 Socket 连接到localhost ,我可以使用相同的凭据来连接我的 Node 应用程序。 Do I need to create a file within my app for the database and not use the Sequel Pro database?我是否需要在我的应用程序中为数据库创建一个文件而不使用 Sequel Pro 数据库?

Controller file (where the connection is created):控制器文件(创建连接的位置):

var Sequelize = require('sequelize');
var sequelize = new Sequelize('synotate', 'root', '', {
    host:'localhost',
    port:'3306',
    dialect: 'mysql'
});

sequelize.authenticate().then(function(errors) { console.log(errors) });

var db = {}

db.Annotation = sequelize.import(__dirname + "/ann-model");

db.sequelize = sequelize;
db.Sequelize = Sequelize;

module.exports = db;

ann-model.js (where my table is being defined): ann-model.js(定义我的表的地方):

module.exports = function(sequelize, DataTypes) {

var Ann = sequelize.define('annotations', {
    ann_id: {
        type: DataTypes.INTEGER,
        primaryKey: true
    },
    ann_date: DataTypes.DATE,

}, {
    freezeTableName: true
});
    return Ann;
}

本地主机的数据库

Try this one, Executing (default): SELECT 1+1 AS result means that everything okay试试这个, Executing (default): SELECT 1+1 AS result意味着一切正常

 sequelize
        .authenticate()
        .then(function(err) {
            if (!!err) {
                console.log('Unable to connect to the database:', err)
            } else {
                console.log('Connection has been established successfully.')
            }
        });

But i didn't know where you get undefined但我不知道你在哪里得到undefined

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

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