In MySQL, databases are typically created using SQL queries.
In a Node.js application, we can use the "mysql" package to execute SQL queries to create a database.
Install the package using npm, if haven't installed it.
npm install mysql
First, create a MySQL connection to your MySQL server.
const mysql = require('mysql'); const connection = mysql.createConnection({ host: 'localhost', user: 'root', password: 'password', database: 'your_database_name' }); connection.connect((err) => { if (err) { console.error('Error connecting to MySQL:', err); return; } console.log('Connected to MySQL'); }); // Don't forget to close the connection when done connection.end();
This step is similar to what was described in the previous topic "create connection" for connecting to a MySQL database.
Note: Replace the connection options (host, user, password, database) with the appropriate values for your MySQL database server.
// Create the database connection.query('CREATE DATABASE your_database_name', (err, result) => { if (err) { console.error('Error creating database:', err); return; } console.log('Database created successfully'); });
Once the connection is established, we can execute an SQL query to create the database.
Use the "query()" method of the connection object to execute the SQL query.
This will execute the SQL query to create a database named "your_database_name". Handle errors and results appropriately within the callback function.
Finally, close the MySQL connection when you're done executing queries.
// Close the MySQL connection connection.end((err) => { if (err) { console.error('Error closing MySQL connection:', err); return; } console.log('MySQL connection closed'); });
Call the "end()" method of the connection object to close the connection. Optionally, you can provide a callback function to handle any errors that occur during the closing process.
Create the file name `db.js` and paste the below code.
const mysql = require("mysql"); function connectDB() { // Create a connection to the MySQL database const connection = mysql.createConnection({ host: "localhost", // MySQL server hostname user: "root", // MySQL username password: "password", // MySQL password database: "your_database_name", // MySQL database name }); // Connect to the MySQL server connection.connect((err) => { if (err) { console.error("Error connecting to MySQL database:", err); return; } console.log("Connected to MySQL database"); }); // Create the database connection.query('CREATE DATABASE your_database_name', (err, result) => { if (err) { console.error('Error creating database:', err); return; } console.log('Database created successfully'); }); // Close the MySQL connection when done connection.end(); } module.exports = connectDB;
Create the file name `server.js` and paste the below code.
let http = require("http"); const dbConnection = require("./db"); http .createServer((req, res) => { res.writeHead(200, { "Content-Type": "text/html" }); res.end("Server Listening..."); }) .listen(3002, () => { console.log(`server listening on port 3002!`); dbConnection(); });