How to use the createConnection function from mysql

Find comprehensive JavaScript mysql.createConnection code examples handpicked from public code repositorys.

mysql.createConnection is a function in the Node.js MySQL package that creates a new MySQL database connection.

488
489
490
491
492
493
494
495
496
497
      queryable.end()
      queryable = undefined
    }
  }

  queryable = mysql.createConnection(connectionOptions)
  queryable.connect()

  cb()
})
fork icon213
star icon539
watch icon0

5
6
7
8
9
10
11
12
13
14
15
16
const port = 3000;


app.use(cors());
app.use(bodyParser.json());


const db = mysql.createConnection({
	host: 'localhost',
	user: 'root',
	password: 'password',
	database: 'myschedule',
fork icon2
star icon1
watch icon1

How does mysql.createConnection work?

mysql.createConnection() is a method provided by the Node.js MySQL module that creates a new connection to a MySQL server with the given connection options. It returns a new Connection object that can be used to perform various operations on the database, such as executing queries and managing transactions. When the connection is no longer needed, it should be closed using the connection.end() method.

2
3
4
5
6
7
8
9
10
11
12
var dbconnect;


var storage= function() {
  var _connect=function(callback) {
    if (dbconnect === undefined) {
      dbconnect = _mysql.createConnection({
            host: config.storage.env.Host,
            port: config.storage.env.Port,
            user: config.storage.env.User,
            password: config.storage.env.Password,
fork icon2
star icon1
watch icon2

0
1
2
3
4
5
6
7
8
9
10
11
12
13
const mysql = require('mysql');
const dbconfig = require('../config/dbconfig');






const mysqlConnection = mysql.createConnection(dbconfig);


mysqlConnection.connect((err) => {
    if (!err)
        console.log('Connection Established Successfully');
fork icon0
star icon1
watch icon1

Ai Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
const mysql = require("mysql");

const connection = mysql.createConnection({
  host: "localhost",
  user: "root",
  password: "password",
  database: "mydatabase",
});

connection.connect((err) => {
  if (err) {
    console.error("Error connecting to database:", err.stack);
    return;
  }

  console.log("Connected to database as ID", connection.threadId);
});

This code creates a connection to a MySQL database running on localhost, using the username root and password password. It then prints the ID of the connection if the connection was successful. Note that you should replace mydatabase with the name of the database you want to connect to.

10
11
12
13
14
15
16
17
18
19
app.use(
  cors({
    origin: "*",
  })
);
var mysqlConnection = mysql.createConnection({
  host: "localhost",
  user: "root",
  password: "rohit123",
  database: "react_app",
fork icon0
star icon1
watch icon1

7
8
9
10
11
12
13
14
15
16
17
18
const cheerio = require('cheerio');


const client = new Client({ intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages] });


//create mysql connection
/*var con = mysql.createConnection({
    host: "192.168.31.210",
    user: "boyphongsakorn",
    password: "team1556th",
    database: "discordbot"
fork icon0
star icon1
watch icon2

34
35
36
37
38
39
40
41
42
43
    database: "coba",
  };
}
var konfigDB = new Konfigurasi;
var platformOS = process.platform;
var conn = mysql.createConnection(konfigDB.dbConfig);
const tetapAktif = () => {
  console.log("Mengaktifkan perintah selalu terhubung...");
  const sql = 'SELECT CONCAT(NOW(), " : Terhubung") AS cek';
  const data = null;
fork icon1
star icon0
watch icon1

18
19
20
21
22
23
24
25
26
27
28
29
    user: "root",
    password: process.env.PASSWORD,
    database: process.env.DATABASE,
}
console.log(dbConfig)
const connection = mysql.createConnection(dbConfig);


const publicDirectory = path.join(__dirname, "./public");


app.use(express.static(publicDirectory));
fork icon0
star icon0
watch icon1

+ 4 other calls in file

31
32
33
34
35
36
37
38
39
40
  res.send({
    imageUrl: req.file.filename,
  });
});
//mysql 연결하기
const conn = mysql.createConnection({
  host: "localhost",
  user: "root",
  password: "1234",
  port: "3306",
fork icon0
star icon0
watch icon1

31
32
33
34
35
36
37
38
39
40
41
const emailRegax = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;


function mysqlConnect() {
    return new Promise(
        resolve => {
            connection = mysql.createConnection({
                host: "localhost",
                user: "root",
                password: "",
                database: "push_over",
fork icon0
star icon0
watch icon1

53
54
55
56
57
58
59
60
61
62
63
64
}


//login post request
exports.postLogin = (req, res, next) => {


    var connectDB = mysql.createConnection({
        host: "localhost",
        user: "root",
        password: "@Beatsbydre99",
        database: "cars"
fork icon0
star icon0
watch icon1

+ 23 other calls in file

0
1
2
3
4
5
6
7
8
9
10
11
const express = require('express');
const router = express.Router();
var mysql = require('mysql');




var connection = mysql.createConnection({
    host: 'localhost',
    user: 'root',
    password: 'CS348Project',
    database: 'Project'
fork icon0
star icon0
watch icon1

+ 3 other calls in file

29
30
31
32
33
34
35
36
37
38
39
40
    })
    return res.data.choices[0].text
}




const conn = sql.createConnection({
    host: 'localhost',
    user: 'root',
    password: '',
    database: 'database',
fork icon0
star icon0
watch icon1

+ 2 other calls in file

8
9
10
11
12
13
14
15
16
17
18


// 데이터베이스 계정 정보 파일 가져오기
const data = fs.readFileSync('./database.json');
const conf = JSON.parse(data);
const mysql = require("mysql");
const connection = mysql.createConnection({
    host: conf.host,
    user: conf.user,
    password: conf.password,
    port: conf.port,
fork icon0
star icon0
watch icon1

+ 3 other calls in file

-3
fork icon0
star icon0
watch icon1

+ 2 other calls in file

11
12
13
14
15
16
17
18
19
20
21
22
});


var mysql = require('mysql');
//const { send } = require("process");


var con = mysql.createConnection({
    host: "localhost",
    user: "root",
    password: "",
    database: "try"
fork icon0
star icon0
watch icon1

-1
fork icon0
star icon0
watch icon2

+ 2 other calls in file

3
4
5
6
7
8
9
10
11
12
13
14


//フォームの値を受け取るために必要な典型文
app.use(express.urlencoded({extended: false}));


// //DBへの接続情報(local)
// const connection = mysql.createConnection({
//     host: 'localhost',
//     user: 'root',
//     password: 'seiya224',
//     database: 'lunchtestDB'
fork icon0
star icon0
watch icon1

+ 5 other calls in file

51
52
53
54
55
56
57
58
59
60
// 使用cookie-parser
app.use(cookieParser())
//引入mysql
let mysql = require('mysql')
//配置mysql
let conn = mysql.createConnection({
    host: 'localhost',//主机
    user: 'root',//用户名
    password: '123456',//密码
    database: 'yuliner'//数据库
fork icon0
star icon0
watch icon1

38
39
40
41
42
43
44
45
46
47
48
49
    return next();
  },
  multipleStatements: true
};


let connection = mysql.createConnection(CONFIG);


let promisifiedQueryFunction;
let promisifiedStartTransaction;
let promisifedCommitTransaction;
fork icon0
star icon0
watch icon1

+ 3 other calls in file