How to use the mysql2.createConnection function in mysql2

To help you get started, we’ve selected a few mysql2 examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github knex / knex / scripts / stress-test / mysql2-random-hanging-every-now-and-then.js View on Github external
async function mysql2Query(sql) {
  // recreate connection on fatal error
  if (mysql2Con._fatalError) {
    console.log('========== Reconnecting mysql2');
    mysql2Con = mysql2.createConnection({
      host: 'localhost',
      user: 'root',
      password: 'mysqlrootpassword',
      port: 23306,
      connectTimeout: 500,
      debug: true,
    });
    mysql2Con.on('error', (err) => {
      console.log('- STATS Mysql2 connection died:', err);
    });
  }
  console.log('================ MYSQL2 Running query ======');
  const res = await stdMysqlQuery(mysql2Con, sql);
  console.log('====================== done ================');
  return res;
}
github sidorares / node-mysql2 / examples / ssl / select-over-ssl.js View on Github external
'use strict';

const fs = require('fs');
const mysql = require('mysql2');

const conn = mysql.createConnection({
  user: 'root',
  password: '',
  database: 'test',
  host: '127.0.0.1',
  port: '3306',
  ssl: {
    // key: fs.readFileSync('./certs/client-key.pem'),
    // cert: fs.readFileSync('./certs/client-cert.pem')
    ca: fs.readFileSync('./certs/ca-cert.pem')
  }
});

conn.query('select 1+1 as test', function(err, res) {
  console.log(res);
  conn.query('select repeat("a", 100) as test', function(err, res) {
    console.log(res);
github lisong / code-push-server / test / api / init / database.js View on Github external
before(function() {
      connection = mysql.createConnection({
        host: config.db.host,
        user: config.db.username,
        password: config.db.password,
        database: config.db.database,
        multipleStatements: true
      });
      connection.connect();
    });
github Sannis / node-mysql-benchmarks / src / mysql2.js View on Github external
function initBenchmark(results, callback, cfg) {
    var start_hrtime;
    
    start_hrtime = process.hrtime();

    conn = mysql.createConnection({
      host:     cfg.host,
      port:     cfg.port,
      user:     cfg.user,
      password: cfg.password,
      database: cfg.database,
      typeCast: benchmark.typeCast
    });
    conn.query("DROP TABLE IF EXISTS " + cfg.test_table)
      .on('error', function(err) {
        console.error(err);
        process.exit();
      });
    conn.query(cfg.create_table_query)
      .on('error', function(err) {
        console.error(err);
        process.exit();
github iarna / sql-lexer / examples / apply-mysql.js View on Github external
var dsn = DSN.parse(process.argv[2]);
if (!dsn) {
    console.error('Could not parse your DSN');
    process.exit(1);
}

var filename = process.argv[3];

var start = process.argv[4] ? process.argv[4] : 0;

var dialect;
var connection;
switch (dsn.protocol) {
case 'mysql':
    dialect = require('../mysql.js');
    connection = require('mysql2').createConnection(dsn);
    break;
default:
    console.error('Unsupported SQL engine: '+dsn.protocol);
    process.exit(1);
}

var output = new dialect.Lex();
output.pipe(new dialect.Colorize)
      .pipe(process.stdout);

var todo = Promisable.fulfill();

var chunknum = 0;
fs.createReadStream(filename)
  .pipe(new dialect.Lex())
  .pipe(new dialect.BufferTokensByCommand())
github sidorares / node-mysql2 / examples / pass-sha.js View on Github external
'use strict';

const mysql = require('mysql2').createConnection({
  host: 'localhost',
  user: 'root',
  database: 'test',
  passwordSha1: Buffer.from('8bb6118f8fd6935ad0876a3be34a717d32708ffd', 'hex')
});

mysql.execute(
  'select ?+1 as qqq, ? as rrr, ? as yyy',
  [1, null, 3],
  (err, rows, fields) => {
    console.log(err, rows, fields);
  }
);
github wangzuo / tinyrecord / src / Base.js View on Github external
static mysql2Connection(config) {
    const { default: Mysql2Adapter } = require('./adapters/Mysql2Adapter');
    const mysql = require('mysql2');
    const connection = mysql.createConnection(config);
    return new Mysql2Adapter(connection, this.logger, null, config);
  }
github cube-js / cube.js / packages / cubejs-mongobi-driver / driver / MongoBIDriver.js View on Github external
create: async () => {
        const conn = mysql.createConnection(this.config);
        const connect = promisify(conn.connect.bind(conn));

        if (conn.on) {
          conn.on('error', () => {
            conn.destroy();
          });
        }
        conn.execute = promisify(conn.query.bind(conn));

        await connect();
        return conn;
      },
      destroy: (connection) => promisify(connection.end.bind(connection))(),
github skale-me / skale / bin / mysql2-client.js View on Github external
#!/usr/local/bin/node --harmony

'use strict';

var mysql = require('mysql2');
var connection = mysql.createConnection({
	host: '127.0.0.1',
	port: 3307,
	user: 'cedric',
	database: 'test',
	debug: true
});

connection.query('SHOW DATABASES', function(err, rows) {
	connection.end();
});