Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/hjzheng/ssh2mysql


https://github.com/hjzheng/ssh2mysql

Last synced: 27 days ago
JSON representation

Awesome Lists containing this project

README

        

# ssh2mysql

```js
npm install @jxz/ssh2mysql
```

```ts
// 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
```