容器化技術(shù)的出現(xiàn)大大簡(jiǎn)化了應(yīng)用開發(fā)人員在構(gòu)建底層基礎(chǔ)設(shè)施的工作。SequoiaDB 巨杉數(shù)據(jù)庫于3.2.1版本正式推出了 Docker 容器化部署方案,本文將會(huì)基于 SequoiaDB 巨杉數(shù)據(jù)庫與Nodejs的 Docker 鏡像搭建一個(gè)簡(jiǎn)易的 Web 服務(wù)器。
創(chuàng)新互聯(lián)于2013年創(chuàng)立,先為應(yīng)縣等服務(wù)建站,應(yīng)縣等地企業(yè),進(jìn)行企業(yè)商務(wù)咨詢服務(wù)。為應(yīng)縣企業(yè)網(wǎng)站制作PC+手機(jī)+微官網(wǎng)三網(wǎng)同步一站式服務(wù)解決您的所有建站問題。
我們將會(huì)搭建一個(gè)三分區(qū)三副本的高可用 SequoiaDB 巨杉數(shù)據(jù)庫。同時(shí),我們將會(huì)創(chuàng)建一個(gè) SequoiaDB 巨杉數(shù)據(jù)庫的 MySQL 實(shí)例,用以提供 Nodejs 作為數(shù)據(jù)源。
容器角色 |
容器名/IP:端口 |
分區(qū)組 |
鏡像版本 |
數(shù)據(jù)庫協(xié)調(diào)節(jié)點(diǎn) |
coord_catalog/172.17.0.2:11810 |
SYSCoord |
sequoiadb/sequoiadb:v3.2.1 |
數(shù)據(jù)庫編目節(jié)點(diǎn) |
coord_catalog/172.17.0.2:11800 |
SYSCatalog |
sequoiadb/sequoiadb:v3.2.1 |
數(shù)據(jù)分區(qū)1副本1 |
sdb_data1/172.17.0.3:11820 |
group1 |
sequoiadb/sequoiadb:v3.2.1 |
數(shù)據(jù)分區(qū)1副本2 |
sdb_data2/172.17.0.4:11820 |
group1 |
sequoiadb/sequoiadb:v3.2.1 |
數(shù)據(jù)分區(qū)1副本3 |
sdb_data3/172.17.0.5:11820 |
group1 |
sequoiadb/sequoiadb:v3.2.1 |
數(shù)據(jù)分區(qū)2副本1 |
sdb_data2/172.17.0.4:11830 |
group2 |
sequoiadb/sequoiadb:v3.2.1 |
數(shù)據(jù)分區(qū)2副本2 |
sdb_data3/172.17.0.5:11830 |
group2 |
sequoiadb/sequoiadb:v3.2.1 |
數(shù)據(jù)分區(qū)2副本3 |
sdb_data1/172.17.0.3:11830 |
group2 |
sequoiadb/sequoiadb:v3.2.1 |
數(shù)據(jù)分區(qū)3副本1 |
sdb_data3/172.17.0.5:11840 |
group3 |
sequoiadb/sequoiadb:v3.2.1 |
數(shù)據(jù)分區(qū)3副本2 |
sdb_data1/172.17.0.3:11840 |
group3 |
sequoiadb/sequoiadb:v3.2.1 |
數(shù)據(jù)分區(qū)3副本3 |
sdb_data2/172.17.0.4:11840 |
group3 |
sequoiadb/sequoiadb:v3.2.1 |
數(shù)據(jù)庫 MySQL實(shí)例 |
mysql/172.17.0.6:3306 |
- |
sequoiadb/sequoiasql-mysql:v3.2.1 |
Nodejs Web 服務(wù)器 |
nodetest/172.17.0.7:3000 |
- |
node:latest |
對(duì)于已經(jīng)安裝了 Docker 環(huán)境的讀者可以跳過本章節(jié)。對(duì)于之前沒有使用過 Docker 的讀者可以通過本章節(jié)安裝本地 Docker 環(huán)境。
Docker 可以運(yùn)行在絕大部分主流操作系統(tǒng)上,包括常用的 Windows、Mac 以及 Linux 的多個(gè)版本均可支持。
對(duì)于 Mac 用戶可以安裝 Docker Desktop for Mac,下載地址在:
https://hub.docker.com/editions/community/docker-ce-desktop-mac |
對(duì)于 Windows 用戶可以安裝 Docker Deskop for Windows,下載地址在:
https://docs.docker.com/docker-for-windows/install/ |
對(duì)于 Linux 用戶則可以直接使用 yum 或 apt-get 進(jìn)行安裝:
https://docs.docker.com/install/linux/docker-ce/centos/ https://docs.docker.com/install/linux/docker-ce/ubuntu/ |
3.1、將 SequoiaDB 引擎與 SequoiaSQL-MySQL 數(shù)據(jù)庫實(shí)例下載至本地
docker pull sequoiadb/sequoiadb :v3.2.1 docker pull sequoiadb/sequoiasql-mysql:v3.2.1 |
3.2、啟動(dòng)數(shù)據(jù)庫引擎容器
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、驗(yàn)證各個(gè)容器的 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 |
預(yù)期輸出結(jié)果為 :
172.17.0.2 172.17.0.3 172.17.0.4 172.17.0.5 |
3.4、部署 SequoiaDB 巨杉數(shù)據(jù)庫引擎集群
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' |
預(yù)期輸出結(jié)果為:
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、啟動(dòng) MySQL 實(shí)例容器
docker run -it -d -p 3306:3306 --name mysql sequoiadb/sequoiasql-mysql:v3.2.1 |
3.6、驗(yàn)證 IP 地址
docker inspect --format '{{ .NetworkSettings.IPAddress}}' mysql |
預(yù)期輸出結(jié)果為:
172.17.0. 6 |
3.7、創(chuàng)建 MySQL 實(shí)例
docker exec mysql "/init.sh" --port=3306 --coord='172.17.0.2:11810' |
其中 coord 參數(shù)填寫協(xié)調(diào)節(jié)點(diǎn)所在的 IP 地址與監(jiān)聽端口。
預(yù)期輸出結(jié)果為:
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 實(shí)例的容器 ID
docker ps --filter name=mysql --format {{.ID}} |
預(yù)期結(jié)果為 MySQL 實(shí)例容器所在的 Container ID:
cc17df22a908 |
4.2、登錄 MySQL 實(shí)例命令行
docker exec -it cc17df22a908 "/opt/sequoiasql/mysql/bin/mysql" -h 127.0.0.1 -u root |
其中 -it 參數(shù)為 MySQL 實(shí)例的容器 ID,預(yù)期結(jié)果為:
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、創(chuàng)建數(shù)據(jù)庫與表,并創(chuàng)建一條測(cè)試數(shù)據(jù)
create database sample; use sample; create table t1 ( c1 varchar(100)); insert into t1 values ("SequoiaDB"); select * from t1; |
預(yù)期輸出結(jié)果為:
+-----------+ | c1 | +-----------+ | SequoiaDB | +-----------+ 1 row in set (0.02 sec) |
5.1、創(chuàng)建 app.js 文件
var express = require('express'); // 引入 express 模塊 var mysql = require('mysql'); // 引入 mysql 模塊 var app = express(); // 創(chuàng)建 express 的實(shí)例
var connection = mysql.createConnection({ // 創(chuàng)建 mysql 實(shí)例 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 () { //// 監(jiān)聽 3000 端口 console.log('Server running at 3000 port'); }); |
代碼中的 host 使用 MySQL 實(shí)例的 IP 地址。
5.2、創(chuàng)建 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、創(chuàng)建 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 # 定義程序默認(rèn)端口 EXPOSE 3000 # 運(yùn)行程序命令 CMD ["node","app.js"] |
5.4、當(dāng)前目錄中應(yīng)僅包含三個(gè)文件
$ 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、創(chuàng)建 Nodejs 服務(wù)鏡像
docker build -t nodetest . |
5.6、運(yùn)行 Nodejs 服務(wù)容器
docker run -d -p 3000:3000 nodetest |
5.7、打開瀏覽器,連接本地的 3000 端口
5.8、更改數(shù)據(jù)庫中的記錄,可以看到瀏覽器顯示的內(nèi)容隨之發(fā)生調(diào)整
$ 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 巨杉數(shù)據(jù)庫的 Docker 容器,結(jié)合 Nodejs 快速搭建一個(gè) Web 應(yīng)用。靈活將分布式數(shù)據(jù)庫結(jié)合容器使用,能夠使用戶快速構(gòu)建開發(fā)測(cè)試環(huán)境,大幅度降低開發(fā)過程中的基礎(chǔ)設(shè)施維護(hù)成本。