Search code examples
node.jssequelize.jsmysql2

Unknown database 'my_db' , errno: 1049,


I am very new with backend and database I am using node ,sequelize and mysql2 to connect to connect to my_db that I have created it through MySQl workbench .

this is my code

server.js

const express = require("express");
const app = express();
const sequelize = require("./utils/db");

sequelize.sync().then((res) => {
    console.log(res);
  app.listen(5000, () => {
    console.log("runned");
  });
}).catch(err=>{
    console.log(err);
})

utils/db.js

const { Sequelize } = require("sequelize");
const sequelize = new Sequelize("my_db", "root", "3132820", {
  dialect: "mysql",
  host: "localhost",
});
module.exports = sequelize;

models/toto.js

const { DataTypes } = require("sequelize");

const sequelize = require("../utils/database");

const Todo = sequelize.define("Todo", {
    //? Model attributes
    id: {
        type: DataTypes.INTEGER,
        autoIncrement: true,
        primaryKey: true,
        allowNull: false,
    },
    text: {
        type: DataTypes.STRING,
        allowNull: false,
    },
    completed: {
        type: DataTypes.BOOLEAN,
        defaultValue: false,
        allowNull: true, //? default is true,
    },
});

module.exports = Todo;

and I get this error enter image description here

and also I have created database in mysql enter image description here how can I fix this ?


Solution

  • In the last pic, it's not a database, it's just a connection to your mysql server, you need to create a database in your mysql server.