0.0.3 • Published 5 years ago

@jxz/ssh2mysql v0.0.3

Weekly downloads
3
License
MIT
Repository
github
Last release
5 years ago

ssh2mysql

npm install @jxz/ssh2mysql
// config.json
// you can get db configuration description from mysql2 and
// ssh configuration description from tunnel-ssh

"db": {
    "type": "mysql",
    "host": "10.0.1.57",
    "port": 3306,
    "username": "xxxx",
    "password": "passwd",
    "database": "db"
  },
  "ssh": {
    "username": "root",
    "host": "10.0.1.60",
    "port": 22,
    "dstHost": "10.0.1.57",
    "dstPort": 3306,
    "localHost": "127.0.0.1",
    "localPort": 12345,
    "privateKey": "/Users/hjzheng/.ssh/id_rsa"
  }

// execSql.ts

import * as ssh2mysql from '@jxz/ssh2mysql'
import { config } from '@ys/api'

const fs = require('fs')
const dbConf = config.db
const sshConf = config.ssh

let mysql

const execSql = async (sql: string) => {
  if (!mysql) {
    mysql = await ssh2mysql.connect(
      {
        ...sshConf,
        privateKey: fs.readFileSync(sshConf.privateKey),
      },
      { ...dbConf, user: dbConf.username },
    )
  }
  return mysql.execSql(sql)
}

export default execSql
0.0.3

5 years ago

0.0.2

5 years ago

0.0.1

5 years ago

1.0.0

5 years ago