Node.js MySQL Create Database
🚀 Node.js MySQL – Create Database
To create a database from Node.js, you must first connect to MySQL without specifying a database, then run a SQL command.
We’ll use mysql2 (recommended).
✅ 1. Install mysql2 Package
✅ 2. Create a MySQL Connection (No DB Selected)
✅ 3. Create a Database Using Node.js
create_database.js
✅ 4. Run the File
📝 Output Example
⚡ Optional: Check if Database Exists Before Creating
⚠️ Important Notes
-
Use root or a user with database creation permissions.
-
Do not specify
database:in connection config when creating a database. -
Use
IF NOT EXISTSto avoid errors if DB already exists.
