巨杉Tech | 使用 SequoiaDB + Docker + Nodejs 搭建 Web 服务器 叁歲伎倆 2021-12-01 12:10 205阅读 0赞 容器化技术的出现大大简化了应用开发人员在构建底层基础设施的工作。SequoiaDB 巨杉数据库于3.2.1版本正式推出了 Docker 容器化部署方案,本文将会基于 SequoiaDB 巨杉数据库与Nodejs的 Docker 镜像搭建一个简易的 Web 服务器。 # 1、规划部署 # 我们将会搭建一个三分区三副本的高可用 SequoiaDB 巨杉数据库。同时,我们将会创建一个 SequoiaDB 巨杉数据库的 MySQL 实例,用以提供 Nodejs 作为数据源。 <table style="width:557px;"> <thead> <tr> <td style="border-color:#dfe2e5;width:91.75pt;"> <p style="margin-left:0cm;"><strong>容器角色</strong></p> </td> <td style="border-color:#dfe2e5;width:127.6pt;"> <p style="margin-left:0cm;"><strong><span style="color:#000000;">容器名/IP:端口</span></strong></p> </td> <td style="border-color:#dfe2e5;width:63.8pt;"> <p style="margin-left:0cm;"><strong><span style="color:#000000;">分区组</span></strong></p> </td> <td style="border-color:#dfe2e5;width:134.7pt;"> <p style="margin-left:0cm;"><strong><span style="color:#000000;">镜像版本</span></strong></p> </td> </tr> </thead> <tbody> <tr> <td style="border-color:#dfe2e5;width:91.75pt;"> <p style="margin-left:0cm;"><span style="color:#000000;">数据库协调节点</span></p> </td> <td style="width:127.6pt;"> <p style="margin-left:0cm;"><span style="color:#000000;">coord_catalog/172.17.0.2:11810</span></p> </td> <td style="width:63.8pt;"> <p style="margin-left:0cm;"><span style="color:#000000;">SYSCoord</span></p> </td> <td style="width:134.7pt;"> <p style="margin-left:0cm;"><span style="color:#000000;">sequoiadb/sequoiadb:v3.2.1</span></p> </td> </tr> <tr> <td style="border-color:#dfe2e5;width:91.75pt;"> <p style="margin-left:0cm;"><span style="color:#000000;">数据库编目节点</span></p> </td> <td style="width:127.6pt;"> <p style="margin-left:0cm;"><span style="color:#000000;">coord_catalog/172.17.0.2:11800</span></p> </td> <td style="width:63.8pt;"> <p style="margin-left:0cm;"><span style="color:#000000;">SYSCatalog</span></p> </td> <td style="width:134.7pt;"> <p style="margin-left:0cm;"><span style="color:#000000;">sequoiadb/sequoiadb:v3.2.1</span></p> </td> </tr> <tr> <td style="border-color:#dfe2e5;width:91.75pt;"> <p style="margin-left:0cm;"><span style="color:#000000;">数据分区1副本1</span></p> </td> <td style="width:127.6pt;"> <p style="margin-left:0cm;"><span style="color:#000000;">sdb_data1/172.17.0.3:11820</span></p> </td> <td style="width:63.8pt;"> <p style="margin-left:0cm;"><span style="color:#000000;">group1</span></p> </td> <td style="width:134.7pt;"> <p style="margin-left:0cm;"><span style="color:#000000;">sequoiadb/sequoiadb:v3.2.1</span></p> </td> </tr> <tr> <td style="border-color:#dfe2e5;width:91.75pt;"> <p style="margin-left:0cm;"><span style="color:#000000;">数据分区1副本2</span></p> </td> <td style="width:127.6pt;"> <p style="margin-left:0cm;"><span style="color:#000000;">sdb_data2/172.17.0.4:11820</span></p> </td> <td style="width:63.8pt;"> <p style="margin-left:0cm;"><span style="color:#000000;">group1</span></p> </td> <td style="width:134.7pt;"> <p style="margin-left:0cm;"><span style="color:#000000;">sequoiadb/sequoiadb:v3.2.1</span></p> </td> </tr> <tr> <td style="border-color:#dfe2e5;width:91.75pt;"> <p style="margin-left:0cm;"><span style="color:#000000;">数据分区1副本3</span></p> </td> <td style="width:127.6pt;"> <p style="margin-left:0cm;"><span style="color:#000000;">sdb_data3/172.17.0.5:11820</span></p> </td> <td style="width:63.8pt;"> <p style="margin-left:0cm;"><span style="color:#000000;">group1</span></p> </td> <td style="width:134.7pt;"> <p style="margin-left:0cm;"><span style="color:#000000;">sequoiadb/sequoiadb:v3.2.1</span></p> </td> </tr> <tr> <td style="border-color:#dfe2e5;width:91.75pt;"> <p style="margin-left:0cm;"><span style="color:#000000;">数据分区2副本1</span></p> </td> <td style="width:127.6pt;"> <p style="margin-left:0cm;"><span style="color:#000000;">sdb_data2/172.17.0.4:11830</span></p> </td> <td style="width:63.8pt;"> <p style="margin-left:0cm;"><span style="color:#000000;">group2</span></p> </td> <td style="width:134.7pt;"> <p style="margin-left:0cm;"><span style="color:#000000;">sequoiadb/sequoiadb:v3.2.1</span></p> </td> </tr> <tr> <td style="border-color:#dfe2e5;width:91.75pt;"> <p style="margin-left:0cm;"><span style="color:#000000;">数据分区2副本2</span></p> </td> <td style="width:127.6pt;"> <p style="margin-left:0cm;"><span style="color:#000000;">sdb_data3/172.17.0.5:11830</span></p> </td> <td style="width:63.8pt;"> <p style="margin-left:0cm;"><span style="color:#000000;">group2</span></p> </td> <td style="width:134.7pt;"> <p style="margin-left:0cm;"><span style="color:#000000;">sequoiadb/sequoiadb:v3.2.1</span></p> </td> </tr> <tr> <td style="border-color:#dfe2e5;width:91.75pt;"> <p style="margin-left:0cm;"><span style="color:#000000;">数据分区2副本3</span></p> </td> <td style="width:127.6pt;"> <p style="margin-left:0cm;"><span style="color:#000000;">sdb_data1/172.17.0.3:11830</span></p> </td> <td style="width:63.8pt;"> <p style="margin-left:0cm;"><span style="color:#000000;">group2</span></p> </td> <td style="width:134.7pt;"> <p style="margin-left:0cm;"><span style="color:#000000;">sequoiadb/sequoiadb:v3.2.1</span></p> </td> </tr> <tr> <td style="border-color:#dfe2e5;width:91.75pt;"> <p style="margin-left:0cm;"><span style="color:#000000;">数据分区3副本1</span></p> </td> <td style="width:127.6pt;"> <p style="margin-left:0cm;"><span style="color:#000000;">sdb_data3/172.17.0.5:11840</span></p> </td> <td style="width:63.8pt;"> <p style="margin-left:0cm;"><span style="color:#000000;">group3</span></p> </td> <td style="width:134.7pt;"> <p style="margin-left:0cm;"><span style="color:#000000;">sequoiadb/sequoiadb:v3.2.1</span></p> </td> </tr> <tr> <td style="border-color:#dfe2e5;width:91.75pt;"> <p style="margin-left:0cm;"><span style="color:#000000;">数据分区3副本2</span></p> </td> <td style="width:127.6pt;"> <p style="margin-left:0cm;"><span style="color:#000000;">sdb_data1/172.17.0.3:11840</span></p> </td> <td style="width:63.8pt;"> <p style="margin-left:0cm;"><span style="color:#000000;">group3</span></p> </td> <td style="width:134.7pt;"> <p style="margin-left:0cm;"><span style="color:#000000;">sequoiadb/sequoiadb:v3.2.1</span></p> </td> </tr> <tr> <td style="border-color:#dfe2e5;width:91.75pt;"> <p style="margin-left:0cm;"><span style="color:#000000;">数据分区3副本3</span></p> </td> <td style="width:127.6pt;"> <p style="margin-left:0cm;"><span style="color:#000000;">sdb_data2/172.17.0.4:11840</span></p> </td> <td style="width:63.8pt;"> <p style="margin-left:0cm;"><span style="color:#000000;">group3</span></p> </td> <td style="width:134.7pt;"> <p style="margin-left:0cm;"><span style="color:#000000;">sequoiadb/sequoiadb:v3.2.1</span></p> </td> </tr> <tr> <td style="border-color:#dfe2e5;width:91.75pt;"> <p style="margin-left:0cm;"><span style="color:#000000;">数据库 MySQL实例</span></p> </td> <td style="width:127.6pt;"> <p style="margin-left:0cm;"><span style="color:#000000;">mysql/172.17.0.6:3306</span></p> </td> <td style="width:63.8pt;"> <p style="margin-left:0cm;"><span style="color:#000000;">-</span></p> </td> <td style="width:134.7pt;"> <p style="margin-left:0cm;"><span style="color:#000000;">sequoiadb/sequoiasql-mysql:v3.2.1</span></p> </td> </tr> <tr> <td style="border-color:#dfe2e5;width:91.75pt;"> <p style="margin-left:0cm;">Nodejs Web 服务器</p> </td> <td style="width:127.6pt;"> <p style="margin-left:0cm;">nodetest/172.17.0.7:3000</p> </td> <td style="width:63.8pt;"> <p style="margin-left:0cm;">-</p> </td> <td style="width:134.7pt;"> <p style="margin-left:0cm;">node:latest</p> </td> </tr> </tbody> </table> ![watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L1NlcXVvaWFEQl9PZmZpY2lhbA_size_16_color_FFFFFF_t_70][] # 2、安装 Docker 环境 # 对于已经安装了 Docker 环境的读者可以跳过本章节。对于之前没有使用过 Docker 的读者可以通过本章节安装本地 Docker 环境。 Docker 可以运行在绝大部分主流操作系统上,包括常用的 Windows、Mac 以及 Linux 的多个版本均可支持。 对于 Mac 用户可以安装 Docker Desktop for Mac,下载地址在: <table> <tbody> <tr> <td style="width:414.5pt;"> <p style="margin-left:0cm;"><a href="https://hub.docker.com/editions/community/docker-ce-desktop-mac" rel="nofollow"><strong><span style="color:#000000;">https://hub.docker.com/editions/community/docker-ce-desktop-mac</span></strong></a></p> </td> </tr> </tbody> </table> 对于 Windows 用户可以安装 Docker Deskop for Windows,下载地址在: <table> <tbody> <tr> <td style="width:414.5pt;"> <p style="margin-left:0cm;"><a href="https://docs.docker.com/docker-for-windows/install/" rel="nofollow"><strong><span style="color:#000000;">https://docs.docker.com/docker-for-windows/install/</span></strong></a></p> </td> </tr> </tbody> </table> 对于 Linux 用户则可以直接使用 yum 或 apt-get 进行安装: <table> <tbody> <tr> <td style="width:414.5pt;"> <p style="margin-left:0cm;"><a href="https://docs.docker.com/install/linux/docker-ce/centos/" rel="nofollow"><strong><span style="color:#000000;">https://docs.docker.com/install/linux/docker-ce/centos/</span></strong></a></p> <p style="margin-left:0cm;"><span style="color:#000000;"><a href="https://docs.docker.com/install/linux/docker-ce/ubuntu/" rel="nofollow"><strong><span style="color:#000000;">https://docs.docker.com/install/linux/docker-ce/ubuntu/</span></strong></a></span></p> </td> </tr> </tbody> </table> # 3、搭建 SequoiaDB 巨杉数据库集群 # 3.1、将 SequoiaDB 引擎与 SequoiaSQL-MySQL 数据库实例下载至本地 <table> <tbody> <tr> <td style="width:414.5pt;"> <p style="margin-left:0cm;"><strong>docker pull sequoiadb/sequoiadb<span style="color:#000000;">:v3.2.1</span></strong></p> <p style="margin-left:0cm;"><strong><span style="color:#000000;">docker pull sequoiadb/sequoiasql-mysql:v3.2.1</span></strong></p> </td> </tr> </tbody> </table> 3.2、启动数据库引擎容器 <table> <tbody> <tr> <td style="width:414.5pt;"> <p style="margin-left:0cm;"><strong>docker run -it -d --name coord_catalog sequoiadb/sequoiadb:<span style="color:#000000;">v3.2.1</span></strong></p> <p style="margin-left:0cm;"><strong><span style="color:#000000;">docker run -it -d --name sdb_data1 sequoiadb/sequoiadb:v3.2.1</span></strong></p> <p style="margin-left:0cm;"><strong><span style="color:#000000;">docker run -it -d --name sdb_data2 sequoiadb/sequoiadb:v3.2.1</span></strong></p> <p style="margin-left:0cm;"><strong><span style="color:#000000;">docker run -it -d --name sdb_data3 sequoiadb/sequoiadb:v3.2.1</span></strong></p> </td> </tr> </tbody> </table> 3.3、验证各个容器的 IP 地址 <table> <tbody> <tr> <td style="width:414.5pt;"> <p style="margin-left:0cm;"><strong>docker inspect --format '{ { <span style="color:#000000;">.NetworkSettings.IPAddress}}' coord_catalog</span></strong></p> <p style="margin-left:0cm;"><strong><span style="color:#000000;">docker inspect --format '{ { .NetworkSettings.IPAddress}}' sdb_data1</span></strong></p> <p style="margin-left:0cm;"><strong><span style="color:#000000;">docker inspect --format '{ { .NetworkSettings.IPAddress}}' sdb_data2</span></strong></p> <p style="margin-left:0cm;"><strong><span style="color:#000000;">docker inspect --format '{ { .NetworkSettings.IPAddress}}' sdb_data3</span></strong></p> </td> </tr> </tbody> </table> 预期输出结果为: <table> <tbody> <tr> <td style="width:414.5pt;"> <p style="margin-left:0cm;"><strong>172.17.0.2</strong></p> <p style="margin-left:0cm;"><strong><span style="color:#000000;">172.17.0.3</span></strong></p> <p style="margin-left:0cm;"><strong><span style="color:#000000;">172.17.0.4</span></strong></p> <p style="margin-left:0cm;"><strong><span style="color:#000000;">172.17.0.5</span></strong></p> </td> </tr> </tbody> </table> 3.4、部署 SequoiaDB 巨杉数据库引擎集群 <table> <tbody> <tr> <td style="width:414.5pt;"> <p style="margin-left:0cm;"><strong>docker exec coord_catalog "/init.sh" \</strong></p> <p style="margin-left:0cm;"><strong><span style="color:#000000;"> --coord='172.17.0.2:11810' \</span></strong></p> <p style="margin-left:0cm;"><strong><span style="color:#000000;"> --catalog='172.17.0.2:11800' \</span></strong></p> <p style="margin-left:0cm;"><strong><span style="color:#000000;"> --data='group1=172.17.0.3:11820,172.17.0.4:11820,172.17.0.5:11820;group2=172.17.0.4:11830,172.17.0.5:11830,172.17.0.3:11830;group3=172.17.0.5:11840,172.17.0.3:11840,172.17.0.4:11840'</span></strong></p> </td> </tr> </tbody> </table> 预期输出结果为: <table> <tbody> <tr> <td style="width:414.5pt;"> <p style="margin-left:0cm;"><strong>Begin generating SequoiaDB conf file</strong></p> <p style="margin-left:0cm;"><strong><span style="color:#000000;">Finish generating SequoiaDB conf file</span></strong></p> <p style="margin-left:0cm;"><strong><span style="color:#000000;">Restarting sdbcm process, it will take 10 seconds</span></strong></p> <p style="margin-left:0cm;"><strong><span style="color:#000000;">Deploy...</span></strong></p> <p style="margin-left:0cm;"><strong><span style="color:#000000;">Execute command: /opt/sequoiadb/tools/deploy/../../bin/sdb -f /opt/sequoiadb/tools/deploy/quickDeploy.js -e ''</span></strong></p> <p style="margin-left:0cm;"> </p> <p style="margin-left:0cm;"><strong><span style="color:#000000;">************ Deploy SequoiaDB ************************</span></strong></p> <p style="margin-left:0cm;"><strong><span style="color:#000000;">Create catalog: 172.17.0.2:11800</span></strong></p> <p style="margin-left:0cm;"><strong><span style="color:#000000;">Create coord: 172.17.0.2:11810</span></strong></p> <p style="margin-left:0cm;"><strong><span style="color:#000000;">Create data: 172.17.0.3:11820</span></strong></p> <p style="margin-left:0cm;"><strong><span style="color:#000000;">Create data: 172.17.0.4:11820</span></strong></p> <p style="margin-left:0cm;"><strong><span style="color:#000000;">Create data: 172.17.0.5:11820</span></strong></p> <p style="margin-left:0cm;"><strong><span style="color:#000000;">Create data: 172.17.0.4:11830</span></strong></p> <p style="margin-left:0cm;"><strong><span style="color:#000000;">Create data: 172.17.0.5:11830</span></strong></p> <p style="margin-left:0cm;"><strong><span style="color:#000000;">Create data: 172.17.0.3:11830</span></strong></p> <p style="margin-left:0cm;"><strong><span style="color:#000000;">Create data: 172.17.0.5:11840</span></strong></p> <p style="margin-left:0cm;"><strong><span style="color:#000000;">Create data: 172.17.0.3:11840</span></strong></p> <p style="margin-left:0cm;"><strong><span style="color:#000000;">Create data: 172.17.0.4:11840</span></strong></p> </td> </tr> </tbody> </table> 3.5、启动 MySQL 实例容器 <table> <tbody> <tr> <td style="width:414.5pt;"> <p style="margin-left:0cm;"><strong>docker run -it -d -p 3306:3306 --name mysql sequoiadb/sequoiasql-mysql:v3.2.1</strong></p> </td> </tr> </tbody> </table> 3.6、验证 IP 地址 <table> <tbody> <tr> <td style="width:414.5pt;"> <p style="margin-left:0cm;"><strong>docker inspect --format '{ { .NetworkSettings.IPAddress}}' <span style="color:#000000;">mysql</span></strong></p> </td> </tr> </tbody> </table> 预期输出结果为: <table> <tbody> <tr> <td style="width:414.5pt;"> <p style="margin-left:0cm;"><strong>172.17.0.<span style="color:#000000;">6</span></strong></p> </td> </tr> </tbody> </table> 3.7、创建 MySQL 实例 <table> <tbody> <tr> <td style="width:414.5pt;"> <p style="margin-left:0cm;"><strong>docker exec mysql "/init.sh" --port=3306 --coord='172.17.0.2:11810'</strong></p> </td> </tr> </tbody> </table> 其中 coord 参数填写协调节点所在的 IP 地址与监听端口。 预期输出结果为: <table> <tbody> <tr> <td style="width:414.5pt;"> <p style="margin-left:0cm;"><strong>Creating SequoiaSQL instance: <span style="color:#000000;">MySQLInstance</span></strong></p> <p style="margin-left:0cm;"><strong><span style="color:#000000;">Modify configuration file and restart the instance: MySQLInstance</span></strong></p> <p style="margin-left:0cm;"><strong><span style="color:#000000;">Restarting instance: MySQLInstance</span></strong></p> <p style="margin-left:0cm;"><strong><span style="color:#000000;">Opening remote access to user root</span></strong></p> <p style="margin-left:0cm;"><strong><span style="color:#000000;">Restarting instance: MySQLInstance</span></strong></p> <p style="margin-left:0cm;"><strong><span style="color:#000000;">Instance MySQLInstance is created on port 3306, default user is root</span></strong></p> </td> </tr> </tbody> </table> # 4、登录 MySQL 并创建一个测试表 # 4.1、得到 MySQL 实例的容器 ID <table> <tbody> <tr> <td style="width:414.5pt;"> <p style="margin-left:0cm;"><strong>docker ps --filter name=mysql --format { {.ID}}</strong></p> </td> </tr> </tbody> </table> 预期结果为 MySQL 实例容器所在的 Container ID: <table> <tbody> <tr> <td style="width:414.5pt;"> <p style="margin-left:0cm;"><strong>cc17df22a908</strong></p> </td> </tr> </tbody> </table> 4.2、登录 MySQL 实例命令行 <table> <tbody> <tr> <td style="width:414.5pt;"> <p style="margin-left:0cm;"><strong>docker exec -it cc17df22a908 "/opt/sequoiasql/mysql/bin/mysql" -h 127.0.0.1 -u root</strong></p> </td> </tr> </tbody> </table> 其中 \-it 参数为 MySQL 实例的容器 ID,预期结果为: <table> <tbody> <tr> <td style="width:414.5pt;"> <p style="margin-left:0cm;"><strong>Welcome to the MySQL monitor. Commands end with ; or \g.</strong></p> <p style="margin-left:0cm;"><strong><span style="color:#000000;">Your MySQL connection id is 4</span></strong></p> <p style="margin-left:0cm;"><strong><span style="color:#000000;">Server version: 5.7.25 Source distribution</span></strong></p> <p style="margin-left:0cm;"> </p> <p style="margin-left:0cm;"><strong><span style="color:#000000;">Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.</span></strong></p> <p style="margin-left:0cm;"> </p> <p style="margin-left:0cm;"><strong><span style="color:#000000;">Oracle is a registered trademark of Oracle Corporation and/or its</span></strong></p> <p style="margin-left:0cm;"><strong><span style="color:#000000;">affiliates. Other names may be trademarks of their respective</span></strong></p> <p style="margin-left:0cm;"><strong><span style="color:#000000;">owners.</span></strong></p> <p style="margin-left:0cm;"> </p> <p style="margin-left:0cm;"><strong><span style="color:#000000;">No entry for terminal type "xterm";</span></strong></p> <p style="margin-left:0cm;"><strong><span style="color:#000000;">using dumb terminal settings.</span></strong></p> <p style="margin-left:0cm;"><strong><span style="color:#000000;">Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.</span></strong></p> <p style="margin-left:0cm;"> </p> <p style="margin-left:0cm;"><strong><span style="color:#000000;">mysql></span></strong></p> </td> </tr> </tbody> </table> 4.3、创建数据库与表,并创建一条测试数据 <table> <tbody> <tr> <td style="width:414.5pt;"> <p style="margin-left:0cm;"><strong>create database sample;</strong></p> <p style="margin-left:0cm;"><strong><span style="color:#000000;">use sample;</span></strong></p> <p style="margin-left:0cm;"><strong><span style="color:#000000;">create table t1 ( c1 varchar(100));</span></strong></p> <p style="margin-left:0cm;"><strong><span style="color:#000000;">insert into t1 values ("SequoiaDB");</span></strong></p> <p style="margin-left:0cm;"><strong><span style="color:#000000;">select * from t1;</span></strong></p> </td> </tr> </tbody> </table> 预期输出结果为: <table> <tbody> <tr> <td style="width:414.5pt;"> <p style="margin-left:0cm;"><strong>+-----------+</strong></p> <p style="margin-left:0cm;"><strong><span style="color:#000000;">| c1 |</span></strong></p> <p style="margin-left:0cm;"><strong><span style="color:#000000;">+-----------+</span></strong></p> <p style="margin-left:0cm;"><strong><span style="color:#000000;">| SequoiaDB |</span></strong></p> <p style="margin-left:0cm;"><strong><span style="color:#000000;">+-----------+</span></strong></p> <p style="margin-left:0cm;"><strong><span style="color:#000000;">1 row in set (0.02 sec)</span></strong></p> </td> </tr> </tbody> </table> # 5、创建 Nodejs 服务 # 5.1、创建 **app.js** 文件 <table> <tbody> <tr> <td style="width:414.5pt;"> <p style="margin-left:0cm;">var express = require('express'); //<span style="color:#000000;">引入</span><span style="color:#000000;">express</span><span style="color:#000000;">模块</span></p> <p style="margin-left:0cm;"><span style="color:#000000;">var mysql = require('mysql'); //</span><span style="color:#000000;">引入</span><span style="color:#000000;">mysql</span><span style="color:#000000;">模块</span></p> <p style="margin-left:0cm;"><span style="color:#000000;">var app = express(); //</span><span style="color:#000000;">创建</span><span style="color:#000000;">express</span><span style="color:#000000;">的实例</span></p> <p style="margin-left:0cm;"> </p> <p style="margin-left:0cm;"><span style="color:#000000;">var connection = mysql.createConnection({ //</span><span style="color:#000000;">创建</span><span style="color:#000000;">mysql</span><span style="color:#000000;">实例</span></p> <p style="margin-left:0cm;"><span style="color:#000000;"> host:'172.17.0.6',</span></p> <p style="margin-left:0cm;"><span style="color:#000000;"> port:'3306',</span></p> <p style="margin-left:0cm;"><span style="color:#000000;"> user:'root',</span></p> <p style="margin-left:0cm;"><span style="color:#000000;"> password:'',</span></p> <p style="margin-left:0cm;"><span style="color:#000000;"> database:'sample'</span></p> <p style="margin-left:0cm;"><span style="color:#000000;">});</span></p> <p style="margin-left:0cm;"><span style="color:#000000;">var sql = 'SELECT * FROM t1';</span></p> <p style="margin-left:0cm;"><span style="color:#000000;">connection.connect();</span></p> <p style="margin-left:0cm;"> </p> <p style="margin-left:0cm;"><span style="color:#000000;">app.get('/',function (req,res) { </span></p> <p style="margin-left:0cm;"><span style="color:#000000;"> connection.query(sql, function (err,result) { </span></p> <p style="margin-left:0cm;"><span style="color:#000000;"> if(err){ </span></p> <p style="margin-left:0cm;"><span style="color:#000000;"> console.log('[SELECT ERROR]:',err.message);</span></p> <p style="margin-left:0cm;"><span style="color:#000000;"> }</span></p> <p style="margin-left:0cm;"><span style="color:#000000;"> res.send('Hello: ' + result[0].c1 ) ;</span></p> <p style="margin-left:0cm;"><span style="color:#000000;"> });</span></p> <p style="margin-left:0cm;"><span style="color:#000000;">});</span></p> <p style="margin-left:0cm;"><span style="color:#000000;">app.listen(3000,function () { </span><span style="color:#000000;">监听</span><span style="color:#000000;">3000</span><span style="color:#000000;">端口</span></p> <p style="margin-left:0cm;"><span style="color:#000000;"> console.log('Server running at 3000 port');</span></p> <p style="margin-left:0cm;"><span style="color:#000000;">});</span></p> </td> </tr> </tbody> </table> 代码中的 host 使用 MySQL 实例的 IP 地址。 5.2、创建 **package.json** 文件 <table> <tbody> <tr> <td style="width:414.5pt;"> <p style="margin-left:0cm;"><strong>{ </strong></p> <p style="margin-left:0cm;"><strong><span style="color:#000000;"> "name": "nodetest",</span></strong></p> <p style="margin-left:0cm;"><strong><span style="color:#000000;"> "version": "1.0.0",</span></strong></p> <p style="margin-left:0cm;"><strong><span style="color:#000000;"> "description": "",</span></strong></p> <p style="margin-left:0cm;"><strong><span style="color:#000000;"> "main": "index.js",</span></strong></p> <p style="margin-left:0cm;"><strong><span style="color:#000000;"> "scripts": { </span></strong></p> <p style="margin-left:0cm;"><strong><span style="color:#000000;"> "test": "echo \"Error: no test specified\" && exit 1"</span></strong></p> <p style="margin-left:0cm;"><strong><span style="color:#000000;"> },</span></strong></p> <p style="margin-left:0cm;"><strong><span style="color:#000000;"> "author": "",</span></strong></p> <p style="margin-left:0cm;"><strong><span style="color:#000000;"> "license": "ISC",</span></strong></p> <p style="margin-left:0cm;"><strong><span style="color:#000000;"> "dependencies": { </span></strong></p> <p style="margin-left:0cm;"><strong><span style="color:#000000;"> "express": "^4.17.1",</span></strong></p> <p style="margin-left:0cm;"><strong><span style="color:#000000;"> "mysql": "^2.17.1"</span></strong></p> <p style="margin-left:0cm;"><strong><span style="color:#000000;"> }</span></strong></p> <p style="margin-left:0cm;"><strong><span style="color:#000000;">}</span></strong></p> </td> </tr> </tbody> </table> 5.3、创建 **Dockerfile** 文件 <table> <tbody> <tr> <td style="width:414.5pt;"> <p style="margin-left:0cm;"><strong>FROM node:latest</strong></p> <p style="margin-left:0cm;"><strong><span style="color:#000000;">RUN mkdir -p /usr/src/</span></strong></p> <p style="margin-left:0cm;"> </p> <p style="margin-left:0cm;"><strong><span style="color:#000000;">COPY package.json /usr/src/</span></strong></p> <p style="margin-left:0cm;"><strong><span style="color:#000000;">COPY app.js /usr/src/</span></strong></p> <p style="margin-left:0cm;"><strong><span style="color:#000000;">WORKDIR /usr/src/</span></strong></p> <p style="margin-left:0cm;"> </p> <p style="margin-left:0cm;"><strong><span style="color:#000000;">RUN npm install</span></strong></p> <p style="margin-left:0cm;"><strong><span style="color:#000000;">#</span></strong><strong><span style="color:#000000;">定义程序默认端口</span></strong></p> <p style="margin-left:0cm;"><strong><span style="color:#000000;">EXPOSE 3000</span></strong></p> <p style="margin-left:0cm;"><strong><span style="color:#000000;">#</span></strong><strong><span style="color:#000000;">运行程序命令</span></strong></p> <p style="margin-left:0cm;"><strong><span style="color:#000000;">CMD ["node","app.js"]</span></strong></p> </td> </tr> </tbody> </table> 5.4、当前目录中应仅包含三个文件 <table> <tbody> <tr> <td style="width:414.5pt;"> <p style="margin-left:0cm;"><strong>$ ls -la</strong></p> <p style="margin-left:0cm;"><strong><span style="color:#000000;">total 24</span></strong></p> <p style="margin-left:0cm;"><strong><span style="color:#000000;">drwxr-xr-x 5 sequoiadb staff 160 7 16 15:22 .</span></strong></p> <p style="margin-left:0cm;"><strong><span style="color:#000000;">drwxr-xr-x 94 sequoiadb staff 3008 7 16 10:50 ..</span></strong></p> <p style="margin-left:0cm;"><strong><span style="color:#000000;">-rw-r--r-- 1 sequoiadb staff 206 7 16 12:24 Dockerfile</span></strong></p> <p style="margin-left:0cm;"><strong><span style="color:#000000;">-rw-r--r-- 1 sequoiadb staff 766 7 16 12:27 app.js</span></strong></p> <p style="margin-left:0cm;"><strong><span style="color:#000000;">-rw-r--r-- 1 sequoiadb staff 278 7 16 12:03 package.json</span></strong></p> </td> </tr> </tbody> </table> 5.5、创建 Nodejs 服务镜像 <table> <tbody> <tr> <td style="width:414.5pt;"> <p style="margin-left:0cm;"><strong>docker build -t nodetest .</strong></p> </td> </tr> </tbody> </table> 5.6、运行 Nodejs 服务容器 <table> <tbody> <tr> <td style="width:414.5pt;"> <p style="margin-left:0cm;"><strong>docker run -d -p 3000:3000 nodetest</strong></p> </td> </tr> </tbody> </table> 5.7、打开浏览器,连接本地的 3000 端口 ![watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L1NlcXVvaWFEQl9PZmZpY2lhbA_size_16_color_FFFFFF_t_70 1][] 5.8、更改数据库中的记录,可以看到浏览器显示的内容随之发生调整 <table> <tbody> <tr> <td style="width:414.5pt;"> <p style="margin-left:0cm;"><strong>$ docker exec -it cc17df22a908 "/opt/sequoiasql/mysql/bin/mysql" -h 127.0.0.1 -u root</strong></p> <p style="margin-left:0cm;"><strong><span style="color:#000000;">Welcome to the MySQL monitor. Commands end with ; or \g.</span></strong></p> <p style="margin-left:0cm;"><strong><span style="color:#000000;">Your MySQL connection id is 7</span></strong></p> <p style="margin-left:0cm;"><strong><span style="color:#000000;">Server version: 5.7.25 Source distribution</span></strong></p> <p style="margin-left:0cm;"> </p> <p style="margin-left:0cm;"><strong><span style="color:#000000;">Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.</span></strong></p> <p style="margin-left:0cm;"> </p> <p style="margin-left:0cm;"><strong><span style="color:#000000;">Oracle is a registered trademark of Oracle Corporation and/or its</span></strong></p> <p style="margin-left:0cm;"><strong><span style="color:#000000;">affiliates. Other names may be trademarks of their respective</span></strong></p> <p style="margin-left:0cm;"><strong><span style="color:#000000;">owners.</span></strong></p> <p style="margin-left:0cm;"> </p> <p style="margin-left:0cm;"><strong><span style="color:#000000;">No entry for terminal type "xterm";</span></strong></p> <p style="margin-left:0cm;"><strong><span style="color:#000000;">using dumb terminal settings.</span></strong></p> <p style="margin-left:0cm;"><strong><span style="color:#000000;">Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.</span></strong></p> <p style="margin-left:0cm;"> </p> <p style="margin-left:0cm;"><strong><span style="color:#000000;">mysql> use sample;</span></strong></p> <p style="margin-left:0cm;"><strong><span style="color:#000000;">Reading table information for completion of table and column names</span></strong></p> <p style="margin-left:0cm;"><strong><span style="color:#000000;">You can turn off this feature to get a quicker startup with -A</span></strong></p> <p style="margin-left:0cm;"> </p> <p style="margin-left:0cm;"><strong><span style="color:#000000;">Database changed</span></strong></p> <p style="margin-left:0cm;"><strong><span style="color:#000000;">mysql> update t1 set c1="Node" where c1="SequoiaDB";</span></strong></p> <p style="margin-left:0cm;"><strong><span style="color:#000000;">Query OK, 1 row affected (0.04 sec)</span></strong></p> <p style="margin-left:0cm;"><strong><span style="color:#000000;">Rows matched: 1 Changed: 1 Warnings: 0</span></strong></p> </td> </tr> </tbody> </table> 刷新浏览器后显示: ![watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L1NlcXVvaWFEQl9PZmZpY2lhbA_size_16_color_FFFFFF_t_70 2][] # 6、小结 # 本文展示了如何通过 SequoiaDB 巨杉数据库的 Docker 容器,结合 Nodejs 快速搭建一个 Web 应用。灵活将分布式数据库结合容器使用,能够使用户快速构建开发测试环境,大幅度降低开发过程中的基础设施维护成本。 [watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L1NlcXVvaWFEQl9PZmZpY2lhbA_size_16_color_FFFFFF_t_70]: /images/20211201/76b3b7740ee74bfab90bfe9a6a8623da.png [watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L1NlcXVvaWFEQl9PZmZpY2lhbA_size_16_color_FFFFFF_t_70 1]: /images/20211201/a9e2b297443b46d9966cf60459d29618.png [watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L1NlcXVvaWFEQl9PZmZpY2lhbA_size_16_color_FFFFFF_t_70 2]: /images/20211201/5a38d67c70ec49f389d8c45192ecec00.png
还没有评论,来说两句吧...