/*
To download and install the "mysql" module,
open the Command Terminal and execute the following: npm install mysql
*/
var mysql = require('mysql');
var con = mysql.createConnection({
host: "localhost",
user: "root",
password: "12345"
});
con.connect(function(err) {
if (err) throw err;
console.log("Connected!");
});
var mysql = require('mysql');
var con = mysql.createConnection({
host: "localhost",
user: "root",
password: "12345"
});
con.connect(function(err) {
if (err) throw err;
console.log("Connected!");
});
const mysql = require("mysql");
const mysqlDb = mysql.createConnection({
host: process.env.mysql_HOST || "localhost",
user: process.env.mysql_USER || "root",
password: process.env.mysql_PASSWORD || "",
database: process.env.mysql_DATABSE || "my_db"
});
mysqlDb.connect(err => {
if(err) throw err;
});
module.exports = mysqlDb;
var mysql = require('mysql');
var connection = mysql.createConnection({
host : 'example.org',
user : 'bob',
password : 'secret'
});
connection.connect(function(err) {
if (err) {
console.error('error connecting: ' + err.stack);
return;
}
console.log('connected as id ' + connection.threadId);
});
let connection = mysql.createConnection({
host: 'localhost',
user: 'root',
password: '',
database: 'todoapp'
});
Code language: JavaScript (javascript)
connection.connect(function(err) {
if (err) {
return console.error('error: ' + err.message);
}
console.log('Connected to the MySQL server.');
});
Code language: JavaScript (javascript)
// you can't connect to mysql in client side js
// but you can use it in nodejs which is server-side
// for nodejs you can use package named mysql2
npm i mysql2
// read Doc here
// https://www.npmjs.com/package/mysql2