Using Sequelize with Node.js and MySQL: Comprehensive Installation Guide
What Will You Learn in This Guide?
In this guide, you will learn how to manage the MySQL database in Node.js projects with Sequelize ORM.
Connection setup, model definition, CRUD operations, table relationships and production suggestions are discussed.
Technical Summary
- Subject: Node.js + Sequelize + MySQL
- Purpose: Secure and sustainable data management without writing SQL
- Solved Problem: Complex queries and fragile database structures
- Scope: Model, CRUD, relationships,
.env, performance
Preliminary Preparations
- A development environment with Node.js installed
- A working MySQL server
- Basic JavaScript and SQL knowledge
1. Project Setup and Installation of Packages
mkdir genixnode-orm-projesi
cd genixnode-orm-projesi
npm init -y
npm install sequelize mysql2 dotenv
- This step creates the project structure and installs the necessary packages.
2. Configuring Database Connection Securely
- Connection information should be kept in the .env file.
DB_ISMI=genixnode_kutuphane
DB_KULLANICI=genixnode_admin
DB_SIFRE=GucluSifre123!
DB_HOST=localhost
DB_DIALECT=mysql
- Starting the Sequelize Connection
require('dotenv').config();
const { Sequelize } = require("sequelize");
const sequelize = new Sequelize(
process.env.DB_ISMI,
process.env.DB_KULLANICI,
process.env.DB_SIFRE,
{
host: process.env.DB_HOST,
dialect: process.env.DB_DIALECT
}
);
sequelize.authenticate()
.then(() => console.log("Veritabanı bağlantısı başarılı"))
.catch(err => console.error("Bağlantı hatası:", err));
- This code tests MySQL connection.
3. Creating a Model (Table)
- In Sequelize, each table is represented by a model.
const { DataTypes } = require("sequelize");
const Kitap = sequelize.define("kitaplar", {
baslik: {
type: DataTypes.STRING,
allowNull: false
},
yazar: {
type: DataTypes.STRING,
allowNull: false
},
yayin_tarihi: {
type: DataTypes.DATEONLY
},
konu_id: {
type: DataTypes.INTEGER
}
});
sequelize.sync().then(() => {
console.log("Kitaplar tablosu hazır");
});
sync() yalnızca geliştirme ortamında kullanılmalıdır.
4. Basic CRUD Operations
- Adding a Record
Kitap.create({
baslik: "Simyacı",
yazar: "Paulo Coelho",
yayin_tarihi: "1988-01-01",
konu_id: 1
});
Kayıt Okuma
js
Kitap.findAll();
js
Kitap.findOne({ where: { id: 1 } });
Kayıt Silme
js
Kitap.destroy({ where: { id: 2 } });
5. Inter-Table Relationships (Associations)
- One-to-Many
const Ogrenci = sequelize.define("ogrenciler", { isim: DataTypes.STRING });
const Sinif = sequelize.define("siniflar", { seviye: DataTypes.INTEGER });
Sinif.hasMany(Ogrenci);
Ogrenci.belongsTo(Sinif);
Sinif.findAll({ include: Ogrenci });
Many-to-Many (Çoka-Çok)
const Ders = sequelize.define("dersler", { ad: DataTypes.STRING });
const OgrenciDers = sequelize.define("OgrenciDers", {});
Ders.belongsToMany(Ogrenci, { through: OgrenciDers });
Ogrenci.belongsToMany(Ders, { through: OgrenciDers });
6. Safety and Performance Tips
-
Never write passwords in code
-
Do not use sequelize.sync() in production environment
-
Choose a migration system
-
Add indexes to frequently queried fields
-
Configure connection pool settings
Frequently Asked Questions
1. Does Sequelize reduce performance? No. In most scenarios the difference is negligible.
2. Why is migration important? It prevents data loss in live systems.
3. What databases are supported? MySQL, PostgreSQL, MariaDB, SQLite and MSSQL.
4. Can I write Raw SQL? Yes, it is possible with sequelize.query().
Result
Sequelize is a powerful ORM solution for Node.js and MySQL projects. When configured correctly, it is secure, scalable and easy to maintain. You can safely use this architecture on the GenixNode infrastructure.

