{"id":15572904,"url":"https://github.com/realzzz1874/ssh2-connect-mysql","last_synced_at":"2026-04-28T21:05:19.964Z","repository":{"id":57368932,"uuid":"459989163","full_name":"Realzzz1874/ssh2-connect-mysql","owner":"Realzzz1874","description":"ssh2 connection mysql","archived":false,"fork":false,"pushed_at":"2022-02-16T14:14:57.000Z","size":8,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-18T14:08:22.657Z","etag":null,"topics":["mysql","sql","ssh2"],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/ssh2-connect-mysql","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Realzzz1874.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2022-02-16T12:12:04.000Z","updated_at":"2022-02-21T03:01:50.000Z","dependencies_parsed_at":"2022-09-15T16:11:03.598Z","dependency_job_id":null,"html_url":"https://github.com/Realzzz1874/ssh2-connect-mysql","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Realzzz1874%2Fssh2-connect-mysql","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Realzzz1874%2Fssh2-connect-mysql/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Realzzz1874%2Fssh2-connect-mysql/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Realzzz1874%2Fssh2-connect-mysql/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Realzzz1874","download_url":"https://codeload.github.com/Realzzz1874/ssh2-connect-mysql/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246149887,"owners_count":20731405,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["mysql","sql","ssh2"],"created_at":"2024-10-02T18:07:58.539Z","updated_at":"2026-04-28T21:05:19.902Z","avatar_url":"https://github.com/Realzzz1874.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# ssh2-connect-mysql\nssh2 connection mysql\n\n为了网络安全起见，数据库和后端服务一般不会直接部署在同一台机器上，且一般也不允许直接用账户+密码的方式直接连接，通常采取的方案是通过 ssh 的方式进行连接。\n\n## 步骤\n\n1. ssh 建立连接\n\n2. 与 mysql 建立连接\n\n3. 得到 mysql 操作句柄\n\n## 用法\n\n```bash\nyarn add ssh2-connect-mysql\n\nOR\n\nnpm i ssh2-connect-mysql\n```\n\n```javascript\n\nconst ssh2mysql = require('ssh2-mysql');\nconst fs = require('fs');\n\n// see config: https://github.com/mscdex/ssh2\nconst ssh_conf = {\n  host: 'xxx.xxx.xx.1',\n  port: 22,\n  username: 'realzzz',\n  privateKey: fs.readFileSync('/path/to/my/key')\n};\n// see config: https://github.com/sidorares/node-mysql2\nconst db_conf = {\n  host: 'xxx.xxx.xx.2',\n  user: 'realzzzdb',\n  password: 'qwer123..',\n  database: 'mydb'\n};\n\n// example sql statement\nconst sql_statement = 'INSERT INTO book(name, author) VALUES(?, ?)';\nconst params = ['JavaScript高级程序设计', 'Nicholas C. Zakas'];\n\nssh2mysql.connect(ssh_conf, db_conf)\n  .then(sql =\u003e {\n    sql.query(sql_statement, params, (err, res) =\u003e {\n      if (err) {\n        console.log(err);\n      }\n      // success\n      console.log(res);\n      ssh2mysql.close();\n    });\n  });\n\n```\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frealzzz1874%2Fssh2-connect-mysql","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frealzzz1874%2Fssh2-connect-mysql","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frealzzz1874%2Fssh2-connect-mysql/lists"}