Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL with Sequelize: ER_BAD_DB_ERROR: Unknown database

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?

like image 914
user1107173 Avatar asked Sep 20 '16 00:09

user1107173


1 Answers

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.

like image 184
Nix Avatar answered Nov 14 '22 23:11

Nix