pg_bouncer在使用中的坑勿踩

目录

简介

环境信息

问题配置

问题配置

启动pgbouncer

链接逻辑图

测试存在问题

pgadmin4

Idea

JAVA调用

​编辑

dbeaver

建议:


简介

前面文章说过关于pg_bouncer的安装讲解,这里讲一下在使用中的坑,在进行配置的时候需要注意。

环境信息

问题配置

|--------------|------------|----------|----------|---------------------------|
| ip | 伪库名 | 实库名 | 用户 | 配置 |
| 主:10.0.0.103 | readywrite | postgres | postgres | postgresql-15.3+pgbouncer |
| 从:10.0.0.102 | readyonly | postgres | postgres | postgresql-15.3 |

当伪库名和实库名不一致的时候会在pgadmin4工具等对数据库读写,会出现not fund database的问题。目前dbeaver使用比较多,如果使用的是dbeaver并不会出现问题。可能对于问题发现不够及时。

问题配置

在pgouncer的配置如下:

[databases]
readyonly =  host=10.0.0.102 port=25432 dbname=postgres
readywrite =  host=10.0.0.103 port=5432 dbname=postgres
[pgbouncer]
listen_addr=*
auth_type=md5
auth_file=/home/postgres/pgbouncer/share/doc/pgbouncer/userlist.txt
logfile=/home/postgres/pgbouncer/pgbouncer1.log
pidfile=/home/postgres/pgbouncer/pgbouncer1.pid
unix_socket_dir = /tmp
max_client_conn=1000
default_pool_size=50
ignore_startup_parameters = extra_float_digits

在[databases]项目下配置格式为

<伪库名><数据库地址><端口><实际库名>·······<用户名:使用默认用户postgres>

增加

[pgbouncer]

ignore_startup_parameters = extra_float_digits

避免出现:[08P01] FATAL: unsupported startup parameter: extra_float_digits. 的情况

根据以上配置

启动pgbouncer

使用命令ps -ef |grep pgbouncer 可以看到启动命令进程

链接逻辑图

当请求端口发送链接请求来的时候并不会直接连到数据库中去而是链接到pgbuncer中去,有pgbouncer再发送链接请求到对应的数据库中

测试存在问题

pgadmin4

使用pgadmin4 可以链接但是却无法使用

导致报错

connection to server at "10.0.0.103", port 6432 failed: FATAL: no such database: postgres

Idea

测试链接成功,但是仍然会提示错误信息,但是仍然是可读可写的

JAVA调用

使用Java代码依然是可读可写

package com.database;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class pgtext {
    public static void main(String[] args) {
        String url = "jdbc:postgresql://10.0.0.103:6432/readywrite?serverTimezone=Asia/Shanghai&useTimezone=true";
        try {
            // 建立数据库连接
            Connection connection = DriverManager.getConnection(url, "postgres", "postgres");

            // 创建表 t1
            String createTableSql = "CREATE TABLE t1 (id SERIAL PRIMARY KEY, data VARCHAR(255))";
            PreparedStatement createTableStatement = connection.prepareStatement(createTableSql);
            createTableStatement.executeUpdate();
            createTableStatement.close();

            // 插入数据
            String insertDataSql = "INSERT INTO t1 (data) VALUES (?)";
            PreparedStatement insertDataStatement = connection.prepareStatement(insertDataSql);
            insertDataStatement.setString(1, "Hello, World!");
            insertDataStatement.executeUpdate();
            insertDataStatement.close();

            // 查询并打印数据
            String selectDataSql = "SELECT data FROM t1";
            PreparedStatement selectDataStatement = connection.prepareStatement(selectDataSql);
            ResultSet resultSet = selectDataStatement.executeQuery();
            while (resultSet.next()) {
                System.out.println(resultSet.getString("data"));
            }
            resultSet.close();
            selectDataStatement.close();

            // 关闭连接
            connection.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

dbeaver

使用dbeaver链接均可实现读写功能,

总结:本文只是测试java的调用,pgadmin4的链接,idea和dbeaver的链接。

在idea中使用会提示[08P01] FATAL: no such database: postgres.

在pgadmin4中,测试可以通过,但是却无法使用的情况

java调用中读写均正常。

建议:

在使用pgbouncer实现读写分离管理的情况可以配置不同的用户名 使用对读写库和只读的区别链接,尽可能保持<伪库名>和<实际库名>保持一致。

相关推荐
月光水岸New2 小时前
Ubuntu 中建的mysql数据库使用Navicat for MySQL连接不上
数据库·mysql·ubuntu
狄加山6752 小时前
数据库基础1
数据库
我爱松子鱼2 小时前
mysql之规则优化器RBO
数据库·mysql
chengooooooo2 小时前
苍穹外卖day8 地址上传 用户下单 订单支付
java·服务器·数据库
Rverdoser3 小时前
【SQL】多表查询案例
数据库·sql
Galeoto3 小时前
how to export a table in sqlite, and import into another
数据库·sqlite
人间打气筒(Ada)4 小时前
MySQL主从架构
服务器·数据库·mysql
leegong231114 小时前
学习PostgreSQL专家认证
数据库·学习·postgresql
喝醉酒的小白4 小时前
PostgreSQL:更新字段慢
数据库·postgresql
敲敲敲-敲代码4 小时前
【SQL实验】触发器
数据库·笔记·sql