MySQL with Sequelize: ER_BAD_DB_ERROR: Unknown database

ぃ、小莉子 提交于 2021-01-27 05:43:10

问题


I am following a tutorial and below is the code:

var Sequelize = require('sequelize')
var sequelize = new Sequelize('basic-mysql-database.mysql', 'root', 'password', {
    'dialect': 'mysql',
    'host': "localhost",
    "port": "3306"
});

var Todo = sequelize.define('todo', {
    description: {
        type: Sequelize.STRING
    },
    completed: {
        type: Sequelize.BOOLEAN

    }
})

sequelize.sync().then(function(){
    console.log('Everything is synced')

    Todo.create({
        description: 'Walking my dog',
        completed: false
    }).then(function (todo){
        console.log('Finished!')
        console.log(todo)
    })
});

I have installed MySQL. When I go into Settings > MySQL and it says MySQL Server instance is running

When I run node testDB.js I get the following error:

Unhandled rejection SequelizeConnectionError: ER_BAD_DB_ERROR: Unknown database 'basic-mysql-database.mysql'
    at Handshake._callback (/Users/Kausi/Documents/Development/todo-api/node_modules/sequelize/lib/dialects/mysql/connection-manager.js:63:20)
    at Handshake.Sequence.end (/Users/Kausi/Documents/Development/todo-api/node_modules/mysql/lib/protocol/sequences/Sequence.js:85:24)
    at Handshake.ErrorPacket (/Users/Kausi/Documents/Development/todo-api/node_modules/mysql/lib/protocol/sequences/Handshake.js:105:8)
    at Protocol._parsePacket (/Users/Kausi/Documents/Development/todo-api/node_modules/mysql/lib/protocol/Protocol.js:280:23)
    at Parser.write (/Users/Kausi/Documents/Development/todo-api/node_modules/mysql/lib/protocol/Parser.js:74:12)
    at Protocol.write (/Users/Kausi/Documents/Development/todo-api/node_modules/mysql/lib/protocol/Protocol.js:39:16)
    at Socket.<anonymous> (/Users/Kausi/Documents/Development/todo-api/node_modules/mysql/lib/Connection.js:109:28)
    at emitOne (events.js:77:13)
    at Socket.emit (events.js:169:7)
    at readableAddChunk (_stream_readable.js:153:18)
    at Socket.Readable.push (_stream_readable.js:111:10)
    at TCP.onread (net.js:536:20)

I have never created any schema/table. I do have MySQL Workbench that I can create the schema and 'Todo' table with; however, I was under the impression that Sequelize does this on the fly? What am I doing wrong?


回答1:


There is a tiny little line in the setup that says:

The credentials for the respective database

Sync will create the tables for you, but it's not going to create the database.



来源:https://stackoverflow.com/questions/39584169/mysql-with-sequelize-er-bad-db-error-unknown-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!