pgbouncer+pg(fdw)+pg(datanode)分表方案
pgbouncer+pg(fdw)+pg(datanode)分表方案
(环境RHEL6.5,PG9.4.5,pgbouncer1.5.4,libevent2.0.22)
方案架构图如下:
pgbouncer+pg(fdw)+pg(datanode)分表方案架构图
(pgbouncer,PG(FDW)所在的机器为192.168.0.106,PG1所在的机器为192.168.0.108,PG2所在的机器为192.168.0.109)
在192.168.0.106,192.168.0.108,192.168.0.109三台机器上安装并配置PG9.4.5
1 从http://www.postgresql.org/ftp/source/v9.4.5/下载PG9.4.5到Linux的/opt/soft_bak/目录
1.1安装PG所需要的依赖软件包
yum -y install readline-devel zlib zlib-devel openssl openssl-devel gcc make flex bison
1.2解压并安装PG9.4.5
tar zxvf postgresql-9.4.5.tar.gz
cd postgresql-9.4.5
./configure
gmake world
gmake install-world
1.3 更改pgsql文件夹的所属主
chown –R postgres /usr/local/pgsql/
su - postgres
1.4 在/usr/local/pgsql/目录下创建data目录
mkdir data
1.5 初始化PG数据库集群
cd /usr/local/pgsql/bin
./initdb –D ../data
1.6 修改PG的配置文件
修改postgresql.conf
listen_addresses = '*'
port = 5432
在pg_hba.conf文件中添加如下语句
host all all 192.168.0.0/24 trust
1.7启动PG数据库
cd /usr/local/pgsql/bin
./pg_ctl –D ../data start
2在192.168.0.106上安装及配置libevent
2.1从http://libevent.org/下载稳定版本的libevent(libevent-2.0.22-stable.tar.gz)到Linux的/opt/soft_bak/目录下
2.2 解压并安装libevent
tar zxvf libevent-2.0.22-stable.tar.gz
cd /opt/soft_bak/libevent-2.0.22-stable
./configure –prefix=/home/postgres/libevent
make
make install
2.3 在/etc/profile文件中加入如下命令并保存
export LD_LIBRARY_PATH=/home/postgres/libevent/lib:$LD_LIBRARY_PATH
2.4使更改的profile生效
source /etc/profile
3 在192.168.0.106上安装并配置pgbouncer
3.1 从http://pgfoundry.org/projects/pgbouncer下载 pgbouncer(pgbouncer-1.5.4.tar.gz) 到Linux的/opt/soft_bak/目录下
3.2 解压并安装pgbouncer
tar zxvf pgbouncer-1.5.4.tar.gz
cd /opt/soft_bak/pgbouncer-1.5.4
./configure --prefix= /home/postgres/pgbouncer --with- libevent= /home/postgres/libevent
make
make install
3.3配置pgbouncer
在/home/postgres/pgbouncer/目录下创建pgbouncer目录config(postgres用户)
mkdir config
3.4复制pgbouncer配置文件及用户列表文件到/home/postgres/pgbouncer/config
cp /home/postgres/pgbouncer/share/doc/pgbouncer/pgbouncer.ini userlist.txt /home/postgres/pgbouncer/config
3.5配置pgbouncer.ini文件
[databases]
bouncerdb = host=127.0.0.1 port=5432 dbname=bouncerdb user=postgres password=postgres
[pgbouncer]
logfile = /home/postgres/pgbouncer/pgbouncer.log
pidfile = /home/postgres/pgbouncer/pgbouncer.pid
listen_addr = *
listen_port = 1999
auth_type = trust
auth_file = /home/postgres/pgbouncer/config/userlist.txt
admin_users = postgres
stats_users = postgres
pool_mode = transaction
server_reset_query = DISCARD ALL
max_client_conn = 100
default_pool_size = 20
3.6配置用户密码文件userlist.txt
"postgres" "postgres"
4 使用pgbouncer
在pgbouncer pg(fdw)创建bouncerdb,用于连接
create database bouncerdb;
在pgbouncer.ini中配置连接 数据库为bouncerdb
4.1启动pgbouncer
[postgres@rhel65 bin]$ ./pgbouncer -d /home/postgres/pgbouncer/config/pgbouncer.ini
2015-11-18 15:35:13.609 2014 LOG File descriptor limit: 1024 (H:4096), max_client_conn: 100, max fds possible: 130
2015-11-18 15:35:13.610 2014 LOG Stale pidfile, removing
[root@rhel65 config]# ps -ef|grep pgbouncer
postgres 2016 1 0 15:35 ? 00:00:00 ./pgbouncer -d /home/postgres/pgbouncer/config/pgbouncer.ini
root 2020 1221 0 15:37 pts/1 00:00:00 grep pgbouncer
[root@rhel65 config]#
4.2使用pgbouncer连接
[postgres@rhel65 bin]$ ./psql -h 127.0.0.1 -p 1999 pgbouncer
psql (9.4.5, server 1.5.4/bouncer)
Type "help" for help.
pgbouncer=# show help;
NOTICE: Console usage
DETAIL:
SHOW HELP|CONFIG|DATABASES|POOLS|CLIENTS|SERVERS|VERSION
SHOW STATS|FDS|SOCKETS|ACTIVE_SOCKETS|LISTS|MEM
SHOW DNS_HOSTS|DNS_ZONES
SET key = arg
RELOAD
PAUSE [<db>]
RESUME [<db>]
KILL <db>
SUSPEND
SHUTDOWN
SHOW
[postgres@rhel65 bin]$ ./psql -h 127.0.0.1 -p 1999 -U postgres -d bouncerdb
psql (9.4.5)
Type "help" for help.
bouncerdb=#
4.3在数据库192.168.0.108的PG数据库上创建数据库db108并创建测试表schema_db108.user_info108
postgres=# create database db108;
CREATE DATABASE
postgres=# \c db108
You are now connected to database "db108" as user "postgres".
db108=# create schema schema_db108;
CREATE SCHEMA
db108=# create table schema_db108.user_info108(userid int,name text,crt_time timestamp without time zone);
CREATE TABLE
4.4在数据库192.168.0.109的PG数据库上创建数据库db109并创建测试表schema_db109.user_info109
-bash-4.2$ ./psql
psql (9.4.5)
Type "help" for help.
postgres=# create database db109;
CREATE DATABASE
postgres=# \c db109
You are now connected to database "db109" as user "postgres".
db109=# create schema schema_db109;
CREATE SCHEMA
db109=# create table schema_db109.user_info109(userid int,name text,crt_time timestamp without time zone);
CREATE TABLE
db109=#
4.5在192.168.0.106的数据库bouncerdb上创建postgres_fdw
[postgres@rhel65 bin]$ ./psql -h 127.0.0.1 -p 1999 -U postgres -d bouncerdb
psql (9.4.5)
Type "help" for help.
bouncerdb=# create extension postgres_fdw;
CREATE EXTENSION
4.6在192.168.0.106上为192.168.0.108的db108数据库创建外部服务器s108
bouncerdb=# CREATE SERVER s108 FOREIGN DATA WRAPPER postgres_fdw;
CREATE SERVER
bouncerdb=# select * from pg_foreign_server;
srvname | srvowner | srvfdw | srvtype | srvversion | srvacl | srvoptions
---------+----------+--------+---------+------------+--------+------------
s108 | 10 | 16388 | | | |
(1 row)
bouncerdb=# alter server s108 options ( add hostaddr '192.168.0.108', add port '5432', add dbname 'db108');
ALTER SERVER
4.7为用户授权
bouncerdb=# grant usage on foreign server s108 to postgres;
GRANT
bouncerdb=# select * from pg_foreign_server ;
srvname | srvowner | srvfdw | srvtype | srvversion | srvacl | srvoptions
---------+----------+--------+---------+------------+-----------------------+-------------------------------------------------
s108 | 10 | 16388 | | | {postgres=U/postgres} | {hostaddr=192.168.0.108,port=5432,dbname=db108}
(1 row)
4.8为外部服务器s108创建用户映射
bouncerdb=# create user mapping for postgres server s108 options(user 'postgres',password 'postgres');
CREATE USER MAPPING
4.9为108的数据库db108中的表schema_db108.user_info108(userid int,name text,crt_time timestamp without time zone)创建外部表
bouncerdb=# create foreign table ft_user_info108(userid int,name text,crt_time timestamp without time zone)
server s108 options(schema_name 'schema_db108',table_name 'user_info108');
CREATE FOREIGN TABLE
4.10通过外部表向表中插入数据;
bouncerdb=# insert into ft_user_info108 (userid,name,crt_time)
bouncerdb-# select generate_series(1,100000),'abcdef',clock_timestamp();
INSERT 0 100000
bouncerdb=# select * from ft_user_info108 limit 5;
userid | name | crt_time
--------+--------+----------------------------
1 | abcdef | 2015-11-18 17:08:52.489583
2 | abcdef | 2015-11-18 17:08:52.491158
3 | abcdef | 2015-11-18 17:08:52.491548
4 | abcdef | 2015-11-18 17:08:52.491899
5 | abcdef | 2015-11-18 17:08:52.492339
(5 rows)
4.11在108的db108数据库中查看数据:
db108=# select * from schema_db108.user_info108 limit 5 ;
userid | name | crt_time
--------+--------+----------------------------
1 | abcdef | 2015-11-18 17:08:52.489583
2 | abcdef | 2015-11-18 17:08:52.491158
3 | abcdef | 2015-11-18 17:08:52.491548
4 | abcdef | 2015-11-18 17:08:52.491899
5 | abcdef | 2015-11-18 17:08:52.492339
(5 rows)
5.1在192.168.0.106上为192.168.0.109的数据库db109创建外部服务器s109
bouncerdb=# CREATE SERVER s109 FOREIGN DATA WRAPPER postgres_fdw;
CREATE SERVER
bouncerdb=# select * from pg_foreign_server;
srvname | srvowner | srvfdw | srvtype | srvversion | srvacl | srvoptions
---------+----------+--------+---------+------------+-----------------------+-------------------------------------------------
s108 | 10 | 16388 | | | {postgres=U/postgres} | {hostaddr=192.168.0.108,port=5432,dbname=db108}
s109 | 10 | 16388 | | | |
(2 rows)
bouncerdb=# alter server s109 options ( add hostaddr '192.168.0.109', add port '5432', add dbname 'db109');
ALTER SERVER
5.2为用户授权
bouncerdb=# grant usage on foreign server s109 to postgres;
GRANT
bouncerdb=# select * from pg_foreign_server ;
srvname | srvowner | srvfdw | srvtype | srvversion | srvacl | srvoptions
---------+----------+--------+---------+------------+-----------------------+-------------------------------------------------
s108 | 10 | 16388 | | | {postgres=U/postgres} | {hostaddr=192.168.0.108,port=5432,dbname=db108}
s109 | 10 | 16388 | | | {postgres=U/postgres} | {hostaddr=192.168.0.109,port=5432,dbname=db109}
(2 rows)
5.3创建用户映射
bouncerdb=# create user mapping for postgres server s109 options(user 'postgres',password 'postgres');
CREATE USER MAPPING
5.4为109的数据库db109中的表schema_db109.user_info109(userid int,name text,crt_time timestamp without time zone)创建外部表
bouncerdb=# create foreign table ft_user_info109(userid int,name text,crt_time timestamp without time zone)
server s109 options(schema_name 'schema_db109',table_name 'user_info109');
CREATE FOREIGN TABLE
5.5通过外部表向表中插入数据;
bouncerdb=# insert into ft_user_info109 (userid,name,crt_time)
select generate_series(1,100000),'abcdef',clock_timestamp();
INSERT 0 100000
bouncerdb=# select * from ft_user_info109 limit 5;
userid | name | crt_time
--------+--------+----------------------------
1 | abcdef | 2015-11-18 17:18:06.431162
2 | abcdef | 2015-11-18 17:18:06.43275
3 | abcdef | 2015-11-18 17:18:06.43334
4 | abcdef | 2015-11-18 17:18:06.433895
5 | abcdef | 2015-11-18 17:18:06.434497
(5 rows)
5.6在192.168.0.109的db109中查看数据:
db109=# select * from schema_db109.user_info109 limit 5;
userid | name | crt_time
--------+--------+----------------------------
1 | abcdef | 2015-11-18 17:18:06.431162
2 | abcdef | 2015-11-18 17:18:06.43275
3 | abcdef | 2015-11-18 17:18:06.43334
4 | abcdef | 2015-11-18 17:18:06.433895
5 | abcdef | 2015-11-18 17:18:06.434497
(5 rows)
pgbouncer+pg(fdw)+pg(datanode)分表方案的更多相关文章
- 重磅来袭,使用CRL实现大数据分库分表方案
关于分库分表方案详细介绍 http://blog.csdn.net/bluishglc/article/details/7696085 这里就不作详细描述了 分库分表方案基本脱离不了这个结构,受制于实 ...
- Mysql分库分表方案
Mysql分库分表方案 1.为什么要分表: 当一张表的数据达到几千万时,你查询一次所花的时间会变多,如果有联合查询的话,我想有可能会死在那儿了.分表的目的就在于此,减小数据库的负担,缩短查询时间. m ...
- 【分库、分表】MySQL分库分表方案
一.Mysql分库分表方案 1.为什么要分表: 当一张表的数据达到几千万时,你查询一次所花的时间会变多,如果有联合查询的话,我想有可能会死在那儿了.分表的目的就在于此,减小数据库的负担,缩短查询时间. ...
- Mysql 之分库分表方案
Mysql分库分表方案 为什么要分表 当一张表的数据达到几千万时,你查询一次所花的时间会变多,如果有联合查询的话,我想有可能会死在那儿了.分表的目的就在于此,减小数据库的负担,缩短查询时间. mysq ...
- MySQL 分库分表方案,总结的非常好!
前言 公司最近在搞服务分离,数据切分方面的东西,因为单张包裹表的数据量实在是太大,并且还在以每天60W的量增长. 之前了解过数据库的分库分表,读过几篇博文,但就只知道个模糊概念, 而且现在回想起来什么 ...
- 基于Mysql数据库亿级数据下的分库分表方案
移动互联网时代,海量的用户数据每天都在产生,基于用户使用数据的用户行为分析等这样的分析,都需要依靠数据都统计和分析,当数据量小时,问题没有暴露出来,数据库方面的优化显得不太重要,一旦数据量越来越大时, ...
- MySQL主从(MySQL proxy Lua读写分离设置,一主多从同步配置,分库分表方案)
Mysql Proxy Lua读写分离设置 一.读写分离说明 读写分离(Read/Write Splitting),基本的原理是让主数据库处理事务性增.改.删操作(INSERT.UPDATE.DELE ...
- Mysql 分库分表方案
0 引言 当一张表的数据达到几千万时,你查询一次所花的时间会变多,如果有联合查询的话,我想有可能会死在那儿了.分表的目的就在于此,减小数据库的负担,缩短查询时间. mysql中有一种机制是表锁定和行锁 ...
- mysql 数据库 分表后 怎么进行分页查询?Mysql分库分表方案?
Mysql分库分表方案 1.为什么要分表: 当一张表的数据达到几千万时,你查询一次所花的时间会变多,如果有联合查询的话,我想有可能会死在那儿了.分表的目的就在于此,减小数据库的负担,缩短查询时间. m ...
随机推荐
- mysql线程缓存和表缓存
一.线程缓存1.thread_cache_size定义了线程缓冲中的数量.每个缓存中的线程通常消耗256kb内存2.Threads_cached,可以看到已经建立的线程二.表缓存(table_cach ...
- A2DP协议笔记
1.概述 A2DP(Advanced Audio Distribution Profile)是蓝牙的音频传输协议,典型应用为蓝牙耳机.A2DP协议的音频数据在ACL Link上传输,这与SCO ...
- HttpContext为null new HttpContextWrapper(System.Web.HttpContext.Current)
HttpContext = (context == null ? new HttpContextWrapper(System.Web.HttpContext.Current) : context);
- C++省略参数(va_list va_start va_arg va_end)的简单应用
原文参考自:http://www.cnblogs.com/hanyonglu/archive/2011/05/07/2039916.html #include <iostream> #in ...
- Windows Runtime - 面向对象化的C++(并非意味着托管)
Windows 8的开发平台总体上分为两部分:一是全新的WinRT,界面搭配Metro style,二是传统的Win32..NET(SL).IE三大平台,界面为传统窗体风格.其中全新的WinRT被微软 ...
- [LeetCode]题解(python):112 Path Sum
题目来源 https://leetcode.com/problems/path-sum/ Given a binary tree and a sum, determine if the tree ha ...
- Dictionary、SortedDictionary、Hashtable 、SortedList
HashTable数据结构存在问题:空间利用率偏低.受填充因子影响大.扩容时所有的数据需要重新进行散列计算.虽然Hash具有O(1)的数据 检索效率,但它空间开销却通常很大,是以空间换取时间.所以Ha ...
- C#连接数据库的四种方法(转)
C#连接数据库的四种方法 在进行以下连接数据库之前,请先在本地安装好Oracle Client,同时本次测试System.Data的版本为:2.0.0.0. 在安装Oracle Client上请注意, ...
- Selenium2学习-028-WebUI自动化实战实例-026-获取页面元素值或者元素属性值
在自动化脚本编写过程中,经常需要获取页面元素的文本进行判断,以便对于不同的文本进行不同的处理.比如:很多的购物网站,加入购物车的按钮是有多个状态的(加入购物车.到货通知.暂不销售等),那么在实际的操作 ...
- 批量处理_cmd_matlab
cd \ cd D:\Projects_Face_Detection\Datasets\afw d: dir /b/s/p/w *jpg > Path_Images.txt 1.ground_t ...