|
| 1 | +:sectnums: |
| 2 | +:sectnumlevels: 5 |
| 3 | + |
| 4 | += PgBouncer |
| 5 | + |
| 6 | +== 概述 |
| 7 | + |
| 8 | +PgBouncer 是轻量级连接池中间件,部署在应用层与数据库之间,通过复用后端连接来降低连接开销、保护数据库资源、提高应用并发性能。 |
| 9 | + |
| 10 | +PgBouncer 使用标准 PostgreSQL 通信协议,IvorySQL 完全兼容该协议。 |
| 11 | + |
| 12 | +*三种连接池模式:* |
| 13 | + |
| 14 | +[cols="1,2,2"] |
| 15 | +|=== |
| 16 | +| 模式 | 说明 | 适用场景 |
| 17 | + |
| 18 | +| session |
| 19 | +| 客户端连接期间独占一个后端连接 |
| 20 | +| 需要过程内 COMMIT、完整会话特性 |
| 21 | + |
| 22 | +| transaction |
| 23 | +| 每个事务结束后归还连接 |
| 24 | +| 最常用,连接复用率最高 |
| 25 | + |
| 26 | +| statement |
| 27 | +| 每条语句后归还连接 |
| 28 | +| 限制最多,不支持显式事务 |
| 29 | +|=== |
| 30 | + |
| 31 | +== 安装 |
| 32 | + |
| 33 | +[TIP] |
| 34 | +源码测试安装环境为 Ubuntu 24.04。 |
| 35 | + |
| 36 | +=== 依赖 |
| 37 | + |
| 38 | +[literal] |
| 39 | +---- |
| 40 | +# Ubuntu / Debian |
| 41 | +sudo apt install libevent-dev libssl-dev pkg-config |
| 42 | +
|
| 43 | +# RHEL / Rocky Linux |
| 44 | +sudo dnf install libevent-devel openssl-devel pkgconfig |
| 45 | +---- |
| 46 | + |
| 47 | +=== 源码安装 |
| 48 | + |
| 49 | +[literal] |
| 50 | +---- |
| 51 | +git clone https://github.com/pgbouncer/pgbouncer.git |
| 52 | +cd pgbouncer |
| 53 | +
|
| 54 | +./autogen.sh |
| 55 | +
|
| 56 | +./configure \ |
| 57 | + --prefix=/usr/ivory-5 \ |
| 58 | + --with-openssl \ |
| 59 | + --with-pam |
| 60 | +
|
| 61 | +make -j4 |
| 62 | +make install |
| 63 | +cp pgbouncer /usr/ivory-5/bin/ |
| 64 | +---- |
| 65 | + |
| 66 | +=== 验证安装 |
| 67 | + |
| 68 | +[literal] |
| 69 | +---- |
| 70 | +pgbouncer --version |
| 71 | +# PgBouncer 1.25.1 |
| 72 | +# libevent 2.1.12-stable |
| 73 | +# tls: OpenSSL 3.0.2 15 Mar 2022 |
| 74 | +---- |
| 75 | + |
| 76 | +== 配置 |
| 77 | + |
| 78 | +=== 连接 IvorySQL PG 模式(端口 5432) |
| 79 | + |
| 80 | +创建 `/etc/pgbouncer/pgbouncer.ini`: |
| 81 | + |
| 82 | +[literal] |
| 83 | +---- |
| 84 | +[databases] |
| 85 | +postgres = host=127.0.0.1 port=5432 dbname=postgres |
| 86 | +
|
| 87 | +[pgbouncer] |
| 88 | +listen_addr = 127.0.0.1 |
| 89 | +listen_port = 6432 |
| 90 | +auth_type = trust |
| 91 | +auth_file = /etc/pgbouncer/userlist.txt |
| 92 | +pool_mode = transaction |
| 93 | +max_client_conn = 200 |
| 94 | +default_pool_size = 20 |
| 95 | +logfile = /var/log/pgbouncer/pgbouncer.log |
| 96 | +pidfile = /var/run/pgbouncer/pgbouncer.pid |
| 97 | +---- |
| 98 | + |
| 99 | +=== 连接 IvorySQL Oracle 兼容模式(端口 1521) |
| 100 | + |
| 101 | +[literal] |
| 102 | +---- |
| 103 | +[databases] |
| 104 | +postgres = host=127.0.0.1 port=1521 dbname=postgres |
| 105 | +
|
| 106 | +[pgbouncer] |
| 107 | +listen_addr = 127.0.0.1 |
| 108 | +listen_port = 2521 |
| 109 | +auth_type = trust |
| 110 | +auth_file = /etc/pgbouncer/userlist.txt |
| 111 | +
|
| 112 | +# Oracle 兼容模式建议使用 session 模式 |
| 113 | +pool_mode = session |
| 114 | +
|
| 115 | +max_client_conn = 200 |
| 116 | +default_pool_size = 20 |
| 117 | +logfile = /var/log/pgbouncer/pgbouncer_oracle.log |
| 118 | +pidfile = /var/run/pgbouncer/pgbouncer_oracle.pid |
| 119 | +---- |
| 120 | + |
| 121 | +=== 用户认证文件 |
| 122 | + |
| 123 | +[literal] |
| 124 | +---- |
| 125 | +# /etc/pgbouncer/userlist.txt |
| 126 | +# 格式:"用户名" "密码"(trust 模式密码留空) |
| 127 | +"postgres" "" |
| 128 | +"app_user" "app_password" |
| 129 | +---- |
| 130 | + |
| 131 | +=== 启动与停止 |
| 132 | + |
| 133 | +[literal] |
| 134 | +---- |
| 135 | +# 前台启动(调试) |
| 136 | +pgbouncer /etc/pgbouncer/pgbouncer.ini |
| 137 | +
|
| 138 | +# 后台启动 |
| 139 | +pgbouncer -d /etc/pgbouncer/pgbouncer.ini |
| 140 | +
|
| 141 | +# 重载配置(不中断连接) |
| 142 | +kill -HUP $(cat /var/run/pgbouncer/pgbouncer.pid) |
| 143 | +
|
| 144 | +# 停止 |
| 145 | +kill -INT $(cat /var/run/pgbouncer/pgbouncer.pid) |
| 146 | +---- |
| 147 | + |
| 148 | +== 使用 |
| 149 | + |
| 150 | +=== 客户端连接 |
| 151 | + |
| 152 | +通过 PgBouncer 连接与直连 IvorySQL 语法完全一致,只需修改端口: |
| 153 | + |
| 154 | +[literal] |
| 155 | +---- |
| 156 | +# PG 模式(经由 PgBouncer) |
| 157 | +psql -U postgres -p 6432 -d postgres |
| 158 | +
|
| 159 | +# Oracle 兼容模式(经由 PgBouncer) |
| 160 | +psql -U postgres -p 2521 -d postgres |
| 161 | +---- |
| 162 | + |
| 163 | +=== 管理控制台 |
| 164 | + |
| 165 | +PgBouncer 提供内置管理数据库: |
| 166 | + |
| 167 | +[literal] |
| 168 | +---- |
| 169 | +psql -U postgres -p 6432 -d pgbouncer |
| 170 | +---- |
| 171 | + |
| 172 | +[literal] |
| 173 | +---- |
| 174 | +-- 查看连接池状态 |
| 175 | +SHOW POOLS; |
| 176 | +
|
| 177 | +-- 查看统计信息 |
| 178 | +SHOW STATS; |
| 179 | +
|
| 180 | +-- 查看客户端连接 |
| 181 | +SHOW CLIENTS; |
| 182 | +
|
| 183 | +-- 查看后端连接 |
| 184 | +SHOW SERVERS; |
| 185 | +
|
| 186 | +-- 重载配置 |
| 187 | +RELOAD; |
| 188 | +---- |
| 189 | + |
| 190 | +=== Oracle 兼容模式 |
| 191 | + |
| 192 | +[literal] |
| 193 | +---- |
| 194 | +-- 确认 Oracle 模式已激活 |
| 195 | +SHOW ivorysql.compatible_mode; |
| 196 | +-- ivorysql.compatible_mode |
| 197 | +-- -------------------------- |
| 198 | +-- oracle |
| 199 | +
|
| 200 | +-- Oracle 数据类型与函数 |
| 201 | +CREATE TABLE bouncer_test ( |
| 202 | + id NUMBER(10) PRIMARY KEY, |
| 203 | + name VARCHAR2(100), |
| 204 | + hired DATE DEFAULT SYSDATE |
| 205 | +); |
| 206 | +
|
| 207 | +INSERT INTO bouncer_test VALUES (1, 'Alice', SYSDATE); |
| 208 | +
|
| 209 | +SELECT id, |
| 210 | + NVL(name, 'N/A') AS name, |
| 211 | + DECODE(id, 1, 'CEO', 'Staff') AS title, |
| 212 | + TO_CHAR(hired, 'YYYY-MM-DD') AS hire_date |
| 213 | +FROM bouncer_test; |
| 214 | +
|
| 215 | +-- Oracle 序列 |
| 216 | +CREATE SEQUENCE ora_seq START WITH 100 INCREMENT BY 10; |
| 217 | +SELECT ora_seq.NEXTVAL FROM DUAL; -- 100 |
| 218 | +SELECT ora_seq.NEXTVAL FROM DUAL; -- 110 |
| 219 | +SELECT ora_seq.CURRVAL FROM DUAL; -- 110 |
| 220 | +---- |
0 commit comments