-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add javascript mysql2 sample (#65)
- Loading branch information
Showing
5 changed files
with
178 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
name: JavaScript CI | ||
|
||
on: | ||
push: | ||
paths: | ||
- '.github/workflows/javascript.yml' | ||
- 'javascript/**' | ||
pull_request: | ||
paths: | ||
- '.github/workflows/javascript.yml' | ||
- 'javascript/**' | ||
|
||
jobs: | ||
ci: | ||
strategy: | ||
matrix: | ||
module: | ||
- name: 'mysql2' | ||
with_oceanbase_container: true | ||
uses: ./.github/workflows/basic-workflow.yml | ||
with: | ||
language: 'javascript' | ||
module: ${{ matrix.module.name }} | ||
with_oceanbase_container: ${{ matrix.module.with_oceanbase_container }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
# javascript连接OceanBase 指南 | ||
|
||
[English](README.md) | 简体中文 | ||
|
||
本文介绍如何通过javascript连接 OceanBase 数据库。 | ||
|
||
## 准备工作 | ||
|
||
需要创建一个项目,确认nodejs,npm,mysql2已经安装安装。 | ||
|
||
命令 | ||
|
||
``` | ||
mkdir example | ||
cd example | ||
npm init -y | ||
npm install mysql2 | ||
``` | ||
|
||
创建 [index.js](index.js) 文件 | ||
|
||
``` | ||
const mysql = require('mysql2'); | ||
const connection = mysql.createConnection({ | ||
host: '127.0.0.1', // OceanBase服务器地址 | ||
port: 2881, // OceanBase端口 | ||
user: 'root', // 数据库用户名 | ||
password: '', // 数据库密码 | ||
database: 'test' // 数据库名称 | ||
}); | ||
// 连接到数据库 | ||
connection.connect(error => { | ||
if (error) { | ||
return console.error('连接到OceanBase数据库失败: ' + error.message); | ||
} | ||
console.log('成功连接到OceanBase数据库'); | ||
// 这里可以执行其他数据库操作 | ||
// 关闭连接 | ||
connection.end(err => { | ||
if (err) { | ||
return console.error('关闭数据库连接失败: ' + err.message); | ||
} | ||
console.log('关闭数据库连接成功'); | ||
}); | ||
}); | ||
``` | ||
|
||
修改代码中的连接信息,之后你就可以直接使用命令行运行示例代码。 | ||
|
||
```bash | ||
sh run.sh | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
# JavaScript Connection OceanBase Guide | ||
|
||
English | [简体中文](README-CN.md) | ||
|
||
This article introduces how to connect to the OceanBase database through JavaScript. | ||
|
||
## prepare | ||
|
||
We need to create a project to confirm nodejs, NPM, mysql2 has been installed and installed. | ||
|
||
command | ||
|
||
``` | ||
mkdir example | ||
cd example | ||
npm init -y | ||
npm install mysql2 | ||
``` | ||
|
||
create [index.js](index.js) file | ||
|
||
``` | ||
const mysql = require('mysql2'); | ||
const connection = mysql.createConnection({ | ||
host: '127.0.0.1', // OceanBase address | ||
port: 2881, // OceanBase port | ||
user: 'root', // username | ||
password: '', // passwd | ||
database: 'test' // database | ||
}); | ||
// Connection OceanBase server | ||
connection.connect(error => { | ||
if (error) { | ||
return console.error('Connection OceanBase faild: ' + error.message); | ||
} | ||
console.log('Connection OceanBase Successd'); | ||
// Other Database Operations | ||
// Close Connection | ||
connection.end(err => { | ||
if (err) { | ||
return console.error('Close Connection Faild: ' + err.message); | ||
} | ||
console.log('Close Connection Successd'); | ||
}); | ||
}); | ||
``` | ||
|
||
Modify the connection information in the code, and then you can directly run the example code using the command line. | ||
|
||
```bash | ||
sh run.sh | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
const mysql = require('mysql2'); | ||
|
||
const connection = mysql.createConnection({ | ||
host: '127.0.0.1', // OceanBase服务器地址 | ||
port: 2881, // OceanBase端口 | ||
user: 'root', // 数据库用户名 | ||
password: '', // 数据库密码 | ||
database: 'test' // 数据库名称 | ||
}); | ||
|
||
// 连接到数据库 | ||
connection.connect(error => { | ||
if (error) { | ||
return console.error('连接到OceanBase数据库失败: ' + error.message); | ||
} | ||
|
||
console.log('成功连接到OceanBase数据库'); | ||
|
||
// 这里可以执行其他数据库操作 | ||
|
||
// 关闭连接 | ||
connection.end(err => { | ||
if (err) { | ||
return console.error('关闭数据库连接失败: ' + err.message); | ||
} | ||
console.log('关闭数据库连接成功'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
mkdir example | ||
cd example | ||
npm init -y | ||
|
||
npm install mysql2 | ||
|
||
cp ../index.js ./ | ||
node index.js |