-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathdotenv_nodejs_mysql.js
More file actions
32 lines (31 loc) · 994 Bytes
/
dotenv_nodejs_mysql.js
File metadata and controls
32 lines (31 loc) · 994 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
const mysql = require('mysql'); //npm install mysql
const dotenv = require('dotenv');//npm install dotenv
let result = dotenv.config();
console.log("result", result);
console.log("env variables", process.env);
//initialize mysql connection
const MYSQL_IP = "localhost"; //process.env.MYSQL_IP;
const MYSQL_LOGIN = "root"; //process.env.MYSQL_LOGIN;
const MYSQL_PASSWORD = "root"; //process.env.MYSQL_PASSWORD;
const DB_NAME = "sakila"; //process.env.DB_NAME;
console.log("Reading env variables:",process.env.MYSQL_IP);
let con = mysql.createConnection({
host: MYSQL_IP,
user: MYSQL_LOGIN,
password: MYSQL_PASSWORD,
database: DB_NAME
});
con.connect(function(err) {
if (err) throw err;
console.log("Connection with mysql established");
});
sql="SELECT first_name, last_name FROM sakila.actor limit 5";
con.query(sql, function (err, result) {
console.log("result", result);
});
/*.env content
MYSQL_IP="localhost"
MYSQL_LOGIN="root"
MYSQL_PASSWORD="root"
DB_NAME="sakila"
*/