带有 Sequelize 的 MySQL:ER_BAD_DB_ERROR:未知数据库 [英] 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)
})
});
我已经安装了 MySQL.当我进入 Settings
> MySQL
并显示 MySQL Server instance is running
I have installed MySQL. When I go into Settings
> MySQL
and it says MySQL Server instance is running
当我运行 node testDB.js
时,出现以下错误:
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)
我从未创建过任何架构/表.我确实有 MySQL Workbench,我可以用它创建架构和Todo"表;但是,我的印象是 Sequelize
可以即时执行此操作吗?我做错了什么?
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?
推荐答案
设置中有一小行内容是:
There is a tiny little line in the setup that says:
相应数据库的凭据
Sync 将为您创建表,但不会创建数据库.
Sync will create the tables for you, but it's not going to create the database.
这篇关于带有 Sequelize 的 MySQL:ER_BAD_DB_ERROR:未知数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!