网站首页 > 技术教程 正文
Overview
随着应用程序的膨胀,我们往往需要扩展程序的基础架构。这篇文章是使用 NGINX 作为 MySQL 的 TCP 负载均衡器的实践。
使用 TCP 负载均衡器的好处:
- 对数据库进行负载均衡,避免数据库过载。
- 应用程序可以使用同样的 IP 或者主机名访问集群中的所有数据库
- 如果一个节点崩溃,可以绕过该节点并保持应用持续运行
- 横向扩展比纵向扩展更简单
Let's Do It
我将使用 Docker Compose 在 Linux 系统中部署多个 MySQL 节点和一个 NGINX 节点。Docker Compose 启动文件docker-compose.yml如下:
version: '3.8'
networks:
nginx_mariadb:
services:
load_balancer:
image: nginx:stable-alpine
container_name: nginx-load-balancer
ports:
- "3306:3306"
- "33060:33060"
volumes:
- ./nginx/nginx.conf:/etc/nginx/nginx.conf
depends_on:
- mariadb-master
- mariadb-slave
networks:
- nginx_mariadb
mariadb-master:
image: 'bitnami/mariadb:latest'
hostname: "master.janwee.ubuntu"
container_name: mariadb_master
volumes:
- ./mariadb:/var/lib/mysql
restart: unless-stopped
tty: true
environment:
MARIADB_REPLICATION_MODE: master
MARIADB_DATABASE: nerddb
MARIADB_USER: janwee
MARIADB_PASSWORD: janwee
MARIADB_ROOT_PASSWORD: janwee
MARIADB_REPLICATION_USER: rplusr
MARIADB_REPLICATION_PASSWORD: janwee
networks:
- nginx_mariadb
mariadb-slave:
image: 'bitnami/mariadb:latest'
hostname: "slave.janwee.ubuntu"
container_name: mariadb_slave
restart: unless-stopped
tty: true
environment:
MARIADB_REPLICATION_MODE: slave
MARIADB_REPLICATION_USER: rplusr
MARIADB_REPLICATION_PASSWORD: janwee
MARIADB_MASTER_HOST: "master.janwee.ubuntu"
MARIADB_MASTER_PORT_NUMBER: 3306
MARIADB_MASTER_ROOT_PASSWORD: janwee
networks:
- nginx_mariadb
depends_on:
- mariadb-master
这里使用 bitnami 的镜像是因为更容易部署 MySQL 主从复制架构。主节点的主机名是 master.janwee.ubuntu,从节点的主机名是 slave.janwee.ubuntu。将三个节点都放在同一个网络下,在 NGINX 负载均衡器中暴露 3306 和 33060 端口。
NGINX 节点中的 volumes 选项将配置文件 nginx/nginx.conf 挂载到容器内部:
worker_processes 1;
# Configuration of connection processing
events {
worker_connections 1024;
}
# Configuration specific to TCP/UDP and affecting all virtual servers
stream {
log_format log_stream '$remote_addr - [$time_local] $protocol $status $bytes_sent $bytes_received $session_time "$upstream_addr"';
access_log /var/log/nginx/mysql.log log_stream;
upstream mariadb_read {
server master.janwee.ubuntu:3306;
server slave.janwee.ubuntu:3306;
}
# Configuration of a TCP virtual server
server {
listen 3306;
# Specify the several addresse
proxy_pass mariadb_read;
proxy_connect_timeout 1s;
error_log /var/log/nginx/mysql_error.log;
}
upstream mariadb_write {
server master.janwee.ubuntu:3306;
}
server {
listen 33060;
proxy_pass mariadb_write;
proxy_connect_timeout 1s;
error_log /var/log/nginx/mysql_error.log;
}
}
这里设置了两个输入流:mariadb_read 和 mariadb_write。read 同时指向主节点和从节点,write 指向主节点。
然后将 nginx 主机名 janwee.ubuntu 加入本地 DNS 配置文件 /etc/hosts中,在 Linux 终端中使用如下命令:
echo "127.0.0.1 db.janwee.ubuntu" >> /etc/hosts
在docker-compose.yml所在目录下运行如下命令启动容器组:
docker compose up -d
测试连接到 3306 端口并使用 SELECT @@hostname;查询当前主机名:
$ mysql -h janwee.ubuntu -P 3306 -u root -pjanwee -e "select @@hostname";
+---------------------+
| @@hostname |
+---------------------+
| slave.janwee.ubuntu |
+---------------------+
$ mysql -h janwee.ubuntu -P 3306 -u root -pjanwee -e "select @@hostname";
+----------------------+
| @@hostname |
+----------------------+
| master.janwee.ubuntu |
+----------------------+
测试连接到 33060 端口并使用 SELECT @@hostname; 查询当前主机名:
$ mysql -h janwee.ubuntu -P 33060 -u root -pjanwee -e "select @@hostname";
+---------------------+
| @@hostname |
+---------------------+
| master.janwee.ubuntu |
+---------------------+
$ mysql -h janwee.ubuntu -P 33060 -u root -pjanwee -e "select @@hostname";
+----------------------+
| @@hostname |
+----------------------+
| master.janwee.ubuntu |
+----------------------+
测试结果表明,当连接 3306 端口时 NGINX 会在主节点和从节点之间变更,而连接33060时只会连接到主节点。
原文链接;https://xie.infoq.cn/article/7a6044de8f0e058e5e0fa4295
猜你喜欢
- 2024-10-12 MySQL 复制与负载均衡(mysql负载均衡原理)
- 2024-10-12 「干货」总结最新的官方稳定版nginx\mongo\mysql快速安装部署
- 2024-09-25 利用linux 负载均衡nginx stream模块反向代理mysql数据库
- 2024-09-25 快速搭建NGINX-HTTPD-PHP-MySQL环境
- 2024-09-25 云计算--Docker典型命令Docker run部署nginx\mysql\redis
- 2024-09-25 MySQL与Redis数据库连接池介绍(图示+源码+代码演示)
- 2024-09-25 tornado + django + nginx + mysql搭建网站源码分享
- 2024-09-25 Centos 6.X Nginx Mysql PHP(即LNMP) 安装与部署(非源码编译)
- 2024-09-25 从零开始学网站开发——2.搭建Linux+Nginx+MySQL+PHP环境
- 2024-09-25 跟着腾讯T4学架构:微服务+MySQL+Nginx+Redis+容器化+虚拟机
你 发表评论:
欢迎- 08-06linux 和 windows文件格式互相转换
- 08-06谷歌 ChromeOS 已支持 7z、iso、tar 文件格式
- 08-06Linux下比较文件内容的6种方法
- 08-06文件格式及功能汇总
- 08-0610个Linux文件内容查看命令的实用示例
- 08-06Linux-如何区分不同文件类型
- 08-06Zabbix技术分享——监控windows进程资源使用情况
- 08-06Linux系统卡顿?学会ps命令这三招,轻松定位问题进程
- 最近发表
- 标签列表
-
- 下划线是什么 (87)
- 精美网站 (58)
- qq登录界面 (90)
- nginx 命令 (82)
- nginx .http (73)
- nginx lua (70)
- nginx 重定向 (68)
- Nginx超时 (65)
- nginx 监控 (57)
- odbc (59)
- rar密码破解工具 (62)
- annotation (71)
- 红黑树 (57)
- 智力题 (62)
- php空间申请 (61)
- 按键精灵 注册码 (69)
- 软件测试报告 (59)
- ntcreatefile (64)
- 闪动文字 (56)
- guid (66)
- abap (63)
- mpeg 2 (65)
- column (63)
- dreamweaver教程 (57)
- excel行列转换 (56)
本文暂时没有评论,来添加一个吧(●'◡'●)