Вы находитесь на странице: 1из 4

High Availability and Load Balancing md5

with pgpool-II
1. pgpool-II
1)
2)
3)
4)

5)
(data sharding)
2.


pgpool.freebsd
*/
pgsql_slave# /usr/local/etc/rc.d/postgresql
reload
/*
pgpool.freebsd:*/
pgpool# psql -h pgsql.freebsd -U alp
Password for user alp:
psql (9.0.1, server 9.0.0)
Type "help" for help.
alp=> \q
pgpool# psql -h pgsql_slave.freebsd -U alp
Password for user alp:
psql (9.0.1)
Type "help" for help.
alp=> \q

4.2.3 pgpool-II
/* pgpool-II:
main.c:322
strncpy(dirnamebuf,
3. pgpool-II
dirname(conf_file), sizeof(dirnamebuf));
*/
/* pgpool-II */
pgpool# adduser
Username: pgpool
Full name: pgpool-II user
Uid (Leave empty for default):
Login group [pgpool]:
Login group is pgpool. Invite pgpool into other
groups? []:
Login class [default]:
Shell (sh csh tcsh nologin) [sh]: tcsh
Home directory [/home/pgpool]:
Home directory permissions (Leave empty for
4. pgpool-II
default):
4.1
Use password-based authentication? [yes]:
pgpool# cd /usr/ports/databases/pgpool-II
Use an empty password? (yes/no) [no]:
pgpool# make install
Use a random password? (yes/no) [no]: yes
4.2 pgpool-II Hot Lock out the account after creation? [no]:
Standby
Username : pgpool
4.2.1
Password : <blank>
192.168.1.4 pgsql.freebsd
Full Name : pgpool-II user
192.168.1.6 pgsql_slave.freebsd
Uid
: 1001
192.168.1.8 pgpool.freebsd
Class
:
4.2.2 PostgreSQL
Groups : pgpool
pgsql_slave# vi /pgdata/pg_hba.conf
Home
: /home/pgpool
/*
Home Mode :
host all
alp
192.168.1.8/24
Shell : /bin/tcsh

Locked : no
OK? (yes/no): yes
OK? (yes/no): yes
adduser: INFO: Successfully added (pgpool) to
the user database.
adduser: INFO: Password for (pgpool) is:
uaO4jFKmde

#
backend_hostname0 = 'pgsql.freebsd'
backend_port0 = 5432
backend_weight0 = 1
backend_hostname1 = 'pgsql_slave.freebsd'
backend_port1 = 5432
backend_weight1 = 1
*/
pgpool# echo host all
alp
192.168.1.1/24
md5 >>
/usr/local/etc/pool_hba.conf

/*
pcp.conf
pgpool-II */
pgpool# pg_md5 password
5f4dcc3b5aa765d61d8327deb882cf99
/*
pgpool# cp /usr/local/etc/pcp.conf.sample
*/
/usr/local/etc/pcp.conf
template1=# select passwd from pg_shadow
pgpool# echo
where usename = 'alp';
"admin:5f4dcc3b5aa765d61d8327deb882cf99"
passwd
>> /usr/local/etc/pcp.conf
------------------------------------pgpool# cp /usr/local/etc/pgpool.conf.sample- md5d805127a9bca86a6ba29b1c5e5315fe3
master-slave /usr/local/etc/pgpool.conf
(1 row)
pgpool# vi /usr/local/etc/pgpool.conf
/* :
listen_addresses = '*'
pgpool# echo
port = 5432
alp:md5d805127a9bca86a6ba29b1c5e5315fe3
load_balance_mode = true
> /usr/local/etc/pool_passwd
master_slave_mode = true
# stream slony
pgpool# chown pgpool /usr/local/etc/pool*
master_slave_sub_mode = 'stream'
enable_pool_hba = true
pgpool# vi /etc/rc.conf
pool_passwd = 'pool_passwd'
/*
pgpool_user='pgpool'
# master slave
pgpool_enable='YES'
, */

pgpool# mkdir /var/run/pgpool


delay_threshold = 16777216
pgpool# chown pgpool:pgpool /var/run/pgpool
#
,
/*
log_standby_delay = 'if_over_threshold'
pgpool pgsql*/
#
/* */

template1=# create user nobody;


health_check_timeout = 20
/* pg_hba.conf
health_check_user = 'nobody'
host all
nobody
192.168.1.8/32
# slave
trust
failover_command = '/usr/bin/ssh -i
host all
alp
192.168.1.8/32
/usr/local/etc/pgpool_rsa_key
md5
pgsql@pgsql_slave.freebsd -c \'/usr/bin/touch
*/
/pgdata/switch_to_master \''
pgpool# /usr/local/etc/rc.d/pgpool start
#
4.2.4
failback_command = ''
freebsd> psql -U alp -h pgpool.freebsd
# Password for user alp:
fail_over_on_backend_error = true
psql (8.4.4, server 9.0.0)

WARNING: psql version 8.4, server version 9.0. CREATE TABLE


Some psql features might not work.
alp=> insert into t99 values(1),(2),(3);
Type "help" for help.
INSERT 0 3
alp=>
alp=> delete from employees where
//
employee_id=152;
alp=# select * from t99;
DELETE 1
i
/* */
--alp=> delete from employees where
1
employee_id=153;
2
server closed the connection unexpectedly
3
This probably means the server terminated (3 rows)
abnormally
before or while processing the request.
4.4 Data sharding pgpool-II
The connection to the server was lost. Attempting
4.4.1
reset: Succeeded.
192.168.1.4 pgsql.freebsd
alp=> delete from employees where
192.168.1.6 pgsql_slave.freebsd
employee_id=153;
192.168.1.8 pgpool.freebsd
DELETE 1
4.4.2
alp=>
/* postgresql90-client,
postgresql90-contrib, postgresql90-server,
4.3 pgpool-II
pgpool-II
4.3.1
9000
192.168.1.4 pgsql.freebsd
*/
192.168.1.6 pgsql_slave.freebsd
192.168.1.8 pgpool.freebsd
template1=# CREATE USER pgpool;
4.3.2 pgpool
CREATE ROLE
/usr/local/etc/pgpool.conf:
template1=# CREATE DATABASE pgpool;
listen_addresses = '*'
CREATE DATABASE
port = 5432
template1=# alter DATABASE pgpool OWNER
replication_mode = true
TO pgpool;
load_balance_mode = true
ALTER DATABASE
/*
template1=# ALTER USER pgpool
ROLLBACK */
ENCRYPTED PASSWORD 'qwerty';
replication_stop_on_mismatch = false
backend_hostname1 = 'pgsql.freebsd'
// pgpool dblink
backend_port1 = 5432
pgpool=# \i
backend_weight1 = 1
/usr/local/share/postgresql/contrib/dblink.sql
backend_hostname2 = 'pgsql_slave.freebsd'
backend_port2 = 5432
// pgpool
backend_weight2 = 1
pgpool# psql -U pgpool -d pgpool -p 9000 -h
192.168.1.8
.
pgpool=> \i /usr/local/share/pgpool4.3.3
II/system_db.sql
freebsd> psql -U alp -h pgpool.freebsd
4.4.2 pgpool
Password for user alp:
/* pgpool.conf: */
psql (8.4.4, server 9.0.0)
port = 5432
WARNING: psql version 8.4, server version 9.0.
Some psql features might not work.
replication_mode = true
Type "help" for help.
load_balance_mode = false
parallel_mode = true
alp=> create table t99 (i int);

//
system_db_hostname = '192.168.1.8'
system_db_port = 9000
system_db_dbname = 'pgpool'
system_db_schema = 'pgpool_catalog'
system_db_user = 'pgpool'
system_db_password = 'qwerty'
//
# backend_hostname, backend_port,
backend_weight
# here are examples
backend_hostname0 = 'pgsql.freebsd'
backend_port0 = 5432
backend_weight0 = 1
backend_hostname1 = 'pgsql_slave.freebsd'
backend_port1 = 5432
backend_weight1 = 1
4.4.3 sharding'
// pgsql, pgsql_slave:
alp=> create table warehouses( name
varchar(255), city varchar(255), address
varchar(255)) ;

freebsd> psql -h 192.168.1.8 -U alp


Password for user alp:
psql (9.0.0)
Type "help" for help.
alp=> insert into warehouses values('little one',
'London','Elm Street, 19');
alp=> insert into warehouses values('big one',
'London','King Street, 21');
alp=> insert into warehouses values('tiny',
'Dublin','Some Street, 12');
alp=> select * from warehouses ;
name | city | address
------------+--------+----------------tiny
| Dublin | Some Street, 12
little one | London | Elm Street, 19
big one | London | King Street, 21
(3 rows)
//pgsql:
alp=# SELECT * from warehouses ;
name | city | address
------+--------+----------------tiny | Dublin | Some Street, 12
(1 row)

//
INSERT INTO pgpool_catalog.dist_def VALUES
(
//pgsql-slave:
'alp', --
alp=# SELECT * from warehouses ;
'public', --
name | city | address
'warehouses', --
------------+--------+----------------'city', --
little one | London | Elm Street, 19
-- :(
big one | London | King Street, 21
-- ):
(2 rows)
ARRAY['name', 'city', 'address'],
ARRAY['varchar(255)', 'varchar(255)',
'varchar(255)'],
--
'pgpool_catalog.dist_def_warehouses'
);
CREATE OR REPLACE FUNCTION
pgpool_catalog.dist_def_warehouses(anyelement)
RETURNS integer AS $$
SELECT CASE WHEN $1 ='London' THEN
1
ELSE 0
END;
$$ LANGUAGE sql;
4.4.4