How to do automatic failover for PostgreSQL with application automatic switch?
-
19-02-2021 - |
سؤال
There are two ways to try.
Method 1
Use JDBC connect the PostgreSQL cluster (primary, standby 2 servers only):
jdbc:postgresql://192.168.0.1:5432,192.168.0.2:5432/jiradb
But failed:
The configuration of your dbconfig.xml file is incorrect (user, password, or database URL etc.)
How to connect multiple servers from jdbc?
Method 2
Use pgbouncer
and repmgr
Three servers:
- pgbouncer
- PostgreSQL Primary (192.168.0.1) Installed repmgr, running repmgrd
- PostgreSQL Standby (192.168.0.2) Installed repmgr, running repmgrd
Set config in pgbouncer server(pgbouncer.ini):
[databases]
postgres = host=192.168.0.1 port=5432 dbname=postgres
postgres = host=192.168.0.2 port=5432 dbname=postgres
[pgbouncer]
listen_port = 6432
listen_addr = *
auth_type = md5
auth_file = userlist.txt
logfile = pgbouncer.log
pidfile = pgbouncer.pid
admin_users = postgres
unix_socket_dir = /tmp
When primary down, repmgr can switch to standby server as primary. But pgbouncer didn't automatic connect the second db server. Why? The config setting for databases
does not work. Is it the wrong usage? Or pgbouncer doesn't has this automatic switch feature?
المحلول
Compare the JDBC documentation:
To support simple connection fail-over it is possible to define multiple endpoints (host and port pairs) in the connection url separated by commas. The driver will try to once connect to each of them in order until the connection succeeds. If none succeed, a normal connection exception is thrown.
The syntax for the connection url is:
jdbc:postgresql://host1:port1,host2:port2/database
So your URL is fine. If JIRA complains about it, that would be a JIRA bug.