您好,登錄后才能下訂單哦!
容器化技術的出現大大簡化了應用開發人員在構建底層基礎設施的工作。SequoiaDB 巨杉數據庫于3.2.1版本正式推出了 Docker 容器化部署方案,本文將會基于 SequoiaDB 巨杉數據庫與Nodejs的 Docker 鏡像搭建一個簡易的 Web 服務器。
我們將會搭建一個三分區三副本的高可用 SequoiaDB 巨杉數據庫。同時,我們將會創建一個 SequoiaDB 巨杉數據庫的 MySQL 實例,用以提供 Nodejs 作為數據源。
容器角色 |
容器名/IP:端口 |
分區組 |
鏡像版本 |
數據庫協調節點 |
coord_catalog/172.17.0.2:11810 |
SYSCoord |
sequoiadb/sequoiadb:v3.2.1 |
數據庫編目節點 |
coord_catalog/172.17.0.2:11800 |
SYSCatalog |
sequoiadb/sequoiadb:v3.2.1 |
數據分區1副本1 |
sdb_data1/172.17.0.3:11820 |
group1 |
sequoiadb/sequoiadb:v3.2.1 |
數據分區1副本2 |
sdb_data2/172.17.0.4:11820 |
group1 |
sequoiadb/sequoiadb:v3.2.1 |
數據分區1副本3 |
sdb_data3/172.17.0.5:11820 |
group1 |
sequoiadb/sequoiadb:v3.2.1 |
數據分區2副本1 |
sdb_data2/172.17.0.4:11830 |
group2 |
sequoiadb/sequoiadb:v3.2.1 |
數據分區2副本2 |
sdb_data3/172.17.0.5:11830 |
group2 |
sequoiadb/sequoiadb:v3.2.1 |
數據分區2副本3 |
sdb_data1/172.17.0.3:11830 |
group2 |
sequoiadb/sequoiadb:v3.2.1 |
數據分區3副本1 |
sdb_data3/172.17.0.5:11840 |
group3 |
sequoiadb/sequoiadb:v3.2.1 |
數據分區3副本2 |
sdb_data1/172.17.0.3:11840 |
group3 |
sequoiadb/sequoiadb:v3.2.1 |
數據分區3副本3 |
sdb_data2/172.17.0.4:11840 |
group3 |
sequoiadb/sequoiadb:v3.2.1 |
數據庫 MySQL實例 |
mysql/172.17.0.6:3306 |
- |
sequoiadb/sequoiasql-mysql:v3.2.1 |
Nodejs Web 服務器 |
nodetest/172.17.0.7:3000 |
- |
node:latest |
對于已經安裝了 Docker 環境的讀者可以跳過本章節。對于之前沒有使用過 Docker 的讀者可以通過本章節安裝本地 Docker 環境。
Docker 可以運行在絕大部分主流操作系統上,包括常用的 Windows、Mac 以及 Linux 的多個版本均可支持。
對于 Mac 用戶可以安裝 Docker Desktop for Mac,下載地址在:
https://hub.docker.com/editions/community/docker-ce-desktop-mac |
對于 Windows 用戶可以安裝 Docker Deskop for Windows,下載地址在:
https://docs.docker.com/docker-for-windows/install/ |
對于 Linux 用戶則可以直接使用 yum 或 apt-get 進行安裝:
https://docs.docker.com/install/linux/docker-ce/centos/ https://docs.docker.com/install/linux/docker-ce/ubuntu/ |
3.1、將 SequoiaDB 引擎與 SequoiaSQL-MySQL 數據庫實例下載至本地
docker pull sequoiadb/sequoiadb :v3.2.1 docker pull sequoiadb/sequoiasql-mysql:v3.2.1 |
3.2、啟動數據庫引擎容器
docker run -it -d --name coord_catalog sequoiadb/sequoiadb: v3.2.1 docker run -it -d --name sdb_data1 sequoiadb/sequoiadb:v3.2.1 docker run -it -d --name sdb_data2 sequoiadb/sequoiadb:v3.2.1 docker run -it -d --name sdb_data3 sequoiadb/sequoiadb:v3.2.1 |
3.3、驗證各個容器的 IP 地址
docker inspect --format '{{ .NetworkSettings.IPAddress}}' coord_catalog docker inspect --format '{{ .NetworkSettings.IPAddress}}' sdb_data1 docker inspect --format '{{ .NetworkSettings.IPAddress}}' sdb_data2 docker inspect --format '{{ .NetworkSettings.IPAddress}}' sdb_data3 |
預期輸出結果為 :
172.17.0.2 172.17.0.3 172.17.0.4 172.17.0.5 |
3.4、部署 SequoiaDB 巨杉數據庫引擎集群
docker exec coord_catalog "/init.sh" \ --coord='172.17.0.2:11810' \ --catalog='172.17.0.2:11800' \ --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' |
預期輸出結果為:
Begin generating SequoiaDB conf file Finish generating SequoiaDB conf file Restarting sdbcm process, it will take 10 seconds Deploy... Execute command: /opt/sequoiadb/tools/deploy/../../bin/sdb -f /opt/sequoiadb/tools/deploy/quickDeploy.js -e ''
************ Deploy SequoiaDB ************************ Create catalog: 172.17.0.2:11800 Create coord: 172.17.0.2:11810 Create data: 172.17.0.3:11820 Create data: 172.17.0.4:11820 Create data: 172.17.0.5:11820 Create data: 172.17.0.4:11830 Create data: 172.17.0.5:11830 Create data: 172.17.0.3:11830 Create data: 172.17.0.5:11840 Create data: 172.17.0.3:11840 Create data: 172.17.0.4:11840 |
3.5、啟動 MySQL 實例容器
docker run -it -d -p 3306:3306 --name mysql sequoiadb/sequoiasql-mysql:v3.2.1 |
3.6、驗證 IP 地址
docker inspect --format '{{ .NetworkSettings.IPAddress}}' mysql |
預期輸出結果為:
172.17.0. 6 |
3.7、創建 MySQL 實例
docker exec mysql "/init.sh" --port=3306 --coord='172.17.0.2:11810' |
其中 coord 參數填寫協調節點所在的 IP 地址與監聽端口。
預期輸出結果為:
Creating SequoiaSQL instance: MySQLInstance Modify configuration file and restart the instance: MySQLInstance Restarting instance: MySQLInstance Opening remote access to user root Restarting instance: MySQLInstance Instance MySQLInstance is created on port 3306, default user is root |
4.1、得到 MySQL 實例的容器 ID
docker ps --filter name=mysql --format {{.ID}} |
預期結果為 MySQL 實例容器所在的 Container ID:
cc17df22a908 |
4.2、登錄 MySQL 實例命令行
docker exec -it cc17df22a908 "/opt/sequoiasql/mysql/bin/mysql" -h 127.0.0.1 -u root |
其中 -it 參數為 MySQL 實例的容器 ID,預期結果為:
Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 4 Server version: 5.7.25 Source distribution
Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners.
No entry for terminal type "xterm"; using dumb terminal settings. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> |
4.3、創建數據庫與表,并創建一條測試數據
create database sample; use sample; create table t1 ( c1 varchar(100)); insert into t1 values ("SequoiaDB"); select * from t1; |
預期輸出結果為:
+-----------+ | c1 | +-----------+ | SequoiaDB | +-----------+ 1 row in set (0.02 sec) |
5.1、創建 app.js 文件
var express = require('express'); // 引入 express 模塊 var mysql = require('mysql'); // 引入 mysql 模塊 var app = express(); // 創建 express 的實例
var connection = mysql.createConnection({ // 創建 mysql 實例 host:'172.17.0.6', port:'3306', user:'root', password:'', database:'sample' }); var sql = 'SELECT * FROM t1'; connection.connect();
app.get('/',function (req,res) { connection.query(sql, function (err,result) { if(err){ console.log('[SELECT ERROR]:',err.message); } res.send('Hello: ' + result[0].c1 ) ; }); }); app.listen(3000,function () { //// 監聽 3000 端口 console.log('Server running at 3000 port'); }); |
代碼中的 host 使用 MySQL 實例的 IP 地址。
5.2、創建 package.json 文件
{ "name": "nodetest", "version": "1.0.0", "description": "", "main": "index.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "author": "", "license": "ISC", "dependencies": { "express": "^4.17.1", "mysql": "^2.17.1" } } |
5.3、創建 Dockerfile 文件
FROM node:latest RUN mkdir -p /usr/src/
COPY package.json /usr/src/ COPY app.js /usr/src/ WORKDIR /usr/src/
RUN npm install # 定義程序默認端口 EXPOSE 3000 # 運行程序命令 CMD ["node","app.js"] |
5.4、當前目錄中應僅包含三個文件
$ ls -la total 24 drwxr-xr-x 5 sequoiadb staff 160 7 16 15:22 . drwxr-xr-x 94 sequoiadb staff 3008 7 16 10:50 .. -rw-r--r-- 1 sequoiadb staff 206 7 16 12:24 Dockerfile -rw-r--r-- 1 sequoiadb staff 766 7 16 12:27 app.js -rw-r--r-- 1 sequoiadb staff 278 7 16 12:03 package.json |
5.5、創建 Nodejs 服務鏡像
docker build -t nodetest . |
5.6、運行 Nodejs 服務容器
docker run -d -p 3000:3000 nodetest |
5.7、打開瀏覽器,連接本地的 3000 端口
5.8、更改數據庫中的記錄,可以看到瀏覽器顯示的內容隨之發生調整
$ docker exec -it cc17df22a908 "/opt/sequoiasql/mysql/bin/mysql" -h 127.0.0.1 -u root Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 7 Server version: 5.7.25 Source distribution
Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners.
No entry for terminal type "xterm"; using dumb terminal settings. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> use sample; Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A
Database changed mysql> update t1 set c1="Node" where c1="SequoiaDB"; Query OK, 1 row affected (0.04 sec) Rows matched: 1 Changed: 1 Warnings: 0 |
刷新瀏覽器后顯示:
本文展示了如何通過 SequoiaDB 巨杉數據庫的 Docker 容器,結合 Nodejs 快速搭建一個 Web 應用。靈活將分布式數據庫結合容器使用,能夠使用戶快速構建開發測試環境,大幅度降低開發過程中的基礎設施維護成本。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。