目录
简介
前面文章说过关于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实现读写分离管理的情况可以配置不同的用户名 使用对读写库和只读的区别链接,尽可能保持<伪库名>和<实际库名>保持一致。