Skip to content

Commit

Permalink
feat: add javascript mysql2 sample (#65)
Browse files Browse the repository at this point in the history
  • Loading branch information
jiu33 authored May 31, 2024
1 parent 7433ff6 commit 65fe023
Show file tree
Hide file tree
Showing 5 changed files with 178 additions and 0 deletions.
24 changes: 24 additions & 0 deletions .github/workflows/javascript.yml
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 }}
59 changes: 59 additions & 0 deletions javascript/mysql2/README-CN.md
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
```
59 changes: 59 additions & 0 deletions javascript/mysql2/README.md
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
```
28 changes: 28 additions & 0 deletions javascript/mysql2/index.js
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('关闭数据库连接成功');
});
});
8 changes: 8 additions & 0 deletions javascript/mysql2/run.sh
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

0 comments on commit 65fe023

Please sign in to comment.