ELEVATE YOUR BUSINESS WITH

Limitless customization options & Elementor compatibility let anyone create a beautiful website with Valiance.

Mysql Create Database in NodeJs

SELECT * FROM `itio_tutorial_master` WHERE `tutorial_menu`='22' AND `tutorial_submenu`='1387' AND `tutorial_status`=1 LIMIT 1

Mysql Create Database in NodeJs

Creating a MySQL database in Node.js is super easy using the mysql or mysql2 packages.

Let me show you how to do it step by step 👇


✅ Step 1: Install MySQL Package

Use either mysql or mysql2. We'll go with mysql here:

bash

npm install mysql

(If you want async/await support, use mysql2 instead.)


🛠️ Step 2: Create the Database

js

const connection = mysql.createConnection({ host: 'localhost', user: 'root', password: '' // 🔒 Replace with your actual password});connection.connect((err) => { if (err) throw err; console.log('Connected to MySQL!'); // Create database connection.query('CREATE DATABASE myDatabase', (err, result) => { if (err) throw err; console.log('Database created!'); connection.end(); // Close the connection });});


🧠 Notes:

  • If the DB already exists, you'll get an error. You can use:

    sql

    CREATE DATABASE IF NOT EXISTS myDatabase

  • Replace root and password with your actual MySQL credentials.

  • If you use XAMPP, your host is usually localhost and password is empty by default.


🔄 Want to use mysql2 with async/await?

bash

npm install mysql2

js

const mysql = require('mysql2/promise');async function createDB() { const connection = await mysql.createConnection({ host: 'localhost', user: 'root', password: '' }); await connection.query('CREATE DATABASE IF NOT EXISTS myDatabase'); console.log('Database created!'); await connection.end();}createDB();


Let me know if you want to:

  • Create tables after this

  • Connect to the database

  • Insert or query data

Disclaimer for AI-Generated Content:
The content provided in these tutorials is generated using artificial intelligence and is intended for educational purposes only.
html
docker
php
kubernetes
golang
mysql