Chinaunix首页 | 论坛 | 博客
  • 博客访问: 3063865
  • 博文数量: 206
  • 博客积分: 3409
  • 博客等级: 中校
  • 技术积分: 4066
  • 用 户 组: 普通用户
  • 注册时间: 2010-12-24 10:21
个人简介

● ITPUB名人堂嘉宾 ● ChinaUnix社区博客专家 ● ChinaUnix社区Oracle板块版主 ● 优酷网认证音乐牛人:EricGuitar ● SDOUG 核心成员 ●E-mail:gaoqiangdba@163.com

文章分类

全部博文(206)

文章存档

2021年(11)

2020年(7)

2019年(7)

2016年(5)

2015年(36)

2014年(23)

2013年(15)

2012年(23)

2011年(61)

2010年(18)

分类: Mysql/postgreSQL

2016-03-16 16:34:34

 










在一些场景下,需要在一台服务器上运行多个数据库实例,可以通过cluster_name参数设置进程的标示,这样的话在系统中查看多个运行的PG实例的时候,可以通过进程前的cluster_name区分,以便进行分析或者维护操作。

体验一下cluster_name的魅力:

修改参数之前,系统中看到的进程均没有前缀,目前系统中只运行了1个PostgreSQL数据库实例:
[root@dbserver tmp]# ps -ef|grep postgres
postgres  3335     1  0 17:59 ?        00:00:00 /usr/pgsql-9.5/bin/postmaster -D /var/lib/pgsql/9.5/data
postgres  3337  3335  0 17:59 ?        00:00:00 postgres: logger process                                
postgres  3339  3335  0 17:59 ?        00:00:00 postgres: checkpointer process                          
postgres  3340  3335  0 17:59 ?        00:00:00 postgres: writer process                                
postgres  3341  3335  0 17:59 ?        00:00:00 postgres: wal writer process                            
postgres  3342  3335  0 17:59 ?        00:00:00 postgres: autovacuum launcher process                   
postgres  3343  3335  0 17:59 ?        00:00:00 postgres: stats collector process                       
root      3487  3390  0 18:03 pts/0    00:00:00 grep postgres



修改配置文件postgresql.conf中的cluster_name参数:
改成了如下:
cluster_name = 'eric'                   # added to process titles if nonempty


然后保存postgresql.conf,重启数据库查看一下进程名称已经有了cluster name:
[root@dbserver pg_log]# service postgresql-9.5 restart
停止 postgresql-9.5 服务:                                 [确定]
启动 postgresql-9.5 服务:                                 [确定]
[root@dbserver pg_log]# ps -ef|grep postgres
postgres  5246     1  0 18:17 ?        00:00:00 /usr/pgsql-9.5/bin/postmaster -D /var/lib/pgsql/9.5/data
postgres  5248  5246  0 18:17 ?        00:00:00 postgres: eric: logger process                          
postgres  5250  5246  0 18:17 ?        00:00:00 postgres: eric: checkpointer process                    
postgres  5251  5246  0 18:17 ?        00:00:00 postgres: eric: writer process                          
postgres  5252  5246  0 18:17 ?        00:00:00 postgres: eric: wal writer process                      
postgres  5253  5246  0 18:17 ?        00:00:00 postgres: eric: autovacuum launcher process             
postgres  5254  5246  0 18:17 ?        00:00:00 postgres: eric: stats collector process                 
root      5259  4945  0 18:17 pts/0    00:00:00 grep postgres

进程的前面已经有了cluster_name前缀。

多个实例运行的时候,该功能效果比较明显,我们再创建一个实例并且设置一下cluster_name:
创建第2个实例的目录:
[root@dbserver /]# mkdir pgdata2
[root@dbserver /]# chown -R postgres.postgres /pgdata2


创建新的实例:
-bash-3.2$ cd /usr/pgsql-9.5/bin/
-bash-3.2$ ./initdb -D /pgdata2
属于此数据库系统的文件宿主为用户 "postgres".
此用户也必须为服务器进程的宿主.
数据库簇将使用本地化语言 "zh_CN.UTF-8"进行初始化.
默认的数据库编码已经相应的设置为 "UTF8".
initdb: 无法为本地化语言环境"zh_CN.UTF-8"找到合适的文本搜索配置
缺省的文本搜索配置将会被设置到"simple"

禁止为数据页生成校验和.

修复已存在目录 /pgdata2 的权限 ... 成功
正在创建子目录 ... 成功
选择默认最大联接数 (max_connections) ... 100
选择默认共享缓冲区大小 (shared_buffers) ... 128MB
选择动态共享内存实现 ......posix
创建配置文件 ... 成功
在 /pgdata2/base/1 中创建 template1 数据库 ... 成功
初始化 pg_authid ...  成功
初始化dependencies ... 成功
创建系统视图 ... 成功
正在加载系统对象描述 ...成功
创建(字符集)校对规则 ... 成功
创建字符集转换 ... 成功
正在创建字典 ... 成功
对内建对象设置权限 ... 成功
创建信息模式 ... 成功
正在装载PL/pgSQL服务器端编程语言...成功
清理数据库 template1 ... 成功
拷贝 template1 到 template0 ... 成功
拷贝 template1 到 template0 ... 成功
同步数据到磁盘...成功

警告:为本地连接启动了 "trust" 认证.
你可以通过编辑 pg_hba.conf 更改或你下次
行 initdb 时使用 -A或者--auth-local和--auth-host选项.

Success. You can now start the database server using:

    ./pg_ctl -D /pgdata2 -l logfile start

改一下新实例的端口:
-bash-3.2$ cd /pgdata2
-bash-3.2$ ls
base          pg_dynshmem    pg_multixact  pg_snapshots  pg_tblspc    postgresql.auto.conf
global        pg_hba.conf    pg_notify     pg_stat       pg_twophase  postgresql.conf
pg_clog       pg_ident.conf  pg_replslot   pg_stat_tmp   PG_VERSION
pg_commit_ts  pg_logical     pg_serial     pg_subtrans   pg_xlog

-bash-3.2$ vi postgresql.conf

改成:
port = 5433

启动新实例:
-bash-3.2$ ./pg_ctl -D /pgdata2 start
正在启动服务器进程
-bash-3.2$ < 2016-01-01 18:44:24.778 CST >日志:  日志输出重定向到日志收集进程
< 2016-01-01 18:44:24.778 CST >提示:  后续的日志输出将出现在目录 "pg_log"中.

查看一下系统中2个实例的进程标示有什么不同:
-bash-3.2$ ps -ef|grep postgres
postgres  5393     1  0 18:25 pts/0    00:00:00 /usr/pgsql-9.5/bin/postgres -D /var/lib/pgsql/9.5/data
postgres  5394  5393  0 18:25 ?        00:00:00 postgres: eric: logger process                        
postgres  5396  5393  0 18:25 ?        00:00:00 postgres: eric: checkpointer process                  
postgres  5397  5393  0 18:25 ?        00:00:00 postgres: eric: writer process                        
postgres  5398  5393  0 18:25 ?        00:00:00 postgres: eric: wal writer process                    
postgres  5399  5393  0 18:25 ?        00:00:00 postgres: eric: autovacuum launcher process           
postgres  5400  5393  0 18:25 ?        00:00:00 postgres: eric: stats collector process               
root      5601  4945  0 18:39 pts/0    00:00:00 su - postgres
postgres  5602  5601  0 18:39 pts/0    00:00:00 -bash
postgres  5722     1  0 18:44 pts/0    00:00:00 /usr/pgsql-9.5/bin/postgres -D /pgdata2
postgres  5723  5722  0 18:44 ?        00:00:00 postgres: logger process               
postgres  5725  5722  0 18:44 ?        00:00:00 postgres: checkpointer process         
postgres  5726  5722  0 18:44 ?        00:00:00 postgres: writer process               
postgres  5727  5722  0 18:44 ?        00:00:00 postgres: wal writer process           
postgres  5728  5722  0 18:44 ?        00:00:00 postgres: autovacuum launcher process   
postgres  5729  5722  0 18:44 ?        00:00:00 postgres: stats collector process      
postgres  5730  5602  0 18:44 pts/0    00:00:00 ps -ef
postgres  5731  5602  0 18:44 pts/0    00:00:00 grep postgres

新的实例没有前缀cluster_name的标识,目前还是可以比较清楚的分辨2个实例,那如果有更多的实例在同1个OS中,就需要明确的区分了。

设置新实例的cluster_name:
修改postgresql.conf:
cluster_name = 'gao'   

重启新实例:
-bash-3.2$ /usr/pgsql-9.5/bin/pg_c-D /pgdata2 restart -m fast
pg_config       pg_controldata  pg_ctl          
-bash-3.2$ /usr/pgsql-9.5/bin/pg_ctl -D /pgdata2 restart -m fast
等待服务器进程关闭 .... 完成
服务器进程已经关闭
正在启动服务器进程
-bash-3.2$ < 2016-01-01 18:46:27.848 CST >日志:  日志输出重定向到日志收集进程
< 2016-01-01 18:46:27.848 CST >提示:  后续的日志输出将出现在目录 "pg_log"中.

不同的实例已经进行了区分:
-bash-3.2$ ps -ef|grep postgres
postgres  5393     1  0 18:25 pts/0    00:00:00 /usr/pgsql-9.5/bin/postgres -D /var/lib/pgsql/9.5/data
postgres  5394  5393  0 18:25 ?        00:00:00 postgres: eric: logger process                        
postgres  5396  5393  0 18:25 ?        00:00:00 postgres: eric: checkpointer process                  
postgres  5397  5393  0 18:25 ?        00:00:00 postgres: eric: writer process                        
postgres  5398  5393  0 18:25 ?        00:00:00 postgres: eric: wal writer process                    
postgres  5399  5393  0 18:25 ?        00:00:00 postgres: eric: autovacuum launcher process           
postgres  5400  5393  0 18:25 ?        00:00:00 postgres: eric: stats collector process               
root      5601  4945  0 18:39 pts/0    00:00:00 su - postgres
postgres  5602  5601  0 18:39 pts/0    00:00:00 -bash
postgres  5789     1  0 18:46 pts/0    00:00:00 /usr/pgsql-9.5/bin/postgres -D /pgdata2
postgres  5790  5789  0 18:46 ?        00:00:00 postgres: gao: logger process          
postgres  5792  5789  0 18:46 ?        00:00:00 postgres: gao: checkpointer process    
postgres  5793  5789  0 18:46 ?        00:00:00 postgres: gao: writer process          
postgres  5794  5789  0 18:46 ?        00:00:00 postgres: gao: wal writer process      
postgres  5795  5789  0 18:46 ?        00:00:00 postgres: gao: autovacuum launcher process   
postgres  5796  5789  0 18:46 ?        00:00:00 postgres: gao: stats collector process   
postgres  5797  5602  0 18:46 pts/0    00:00:00 ps -ef
postgres  5798  5602  0 18:46 pts/0    00:00:00 grep postgres

-bash-3.2$ /usr/pgsql-9.5/bin/pg_ctl -D /pgdata2 start
正在启动服务器进程
-bash-3.2$ < 2016-01-01 20:55:00.710 CST >日志:  日志输出重定向到日志收集进程
< 2016-01-01 20:55:00.710 CST >提示:  后续的日志输出将出现在目录 "pg_log"中.


分别登录2个实例,验证一下可用性:
-bash-3.2$ psql -p 5433
psql (9.5beta2)
输入 "help" 来获取帮助信息.

postgres=# \l
                                     数据库列表
   名称    |  拥有者  | 字元编码 |  校对规则   |    Ctype    |       存取权限        
-----------+----------+----------+-------------+-------------+-----------------------
 postgres  | postgres | UTF8     | zh_CN.UTF-8 | zh_CN.UTF-8 | 
 template0 | postgres | UTF8     | zh_CN.UTF-8 | zh_CN.UTF-8 | =c/postgres          +
           |          |          |             |             | postgres=CTc/postgres
 template1 | postgres | UTF8     | zh_CN.UTF-8 | zh_CN.UTF-8 | =c/postgres          +
           |          |          |             |             | postgres=CTc/postgres
(3 行记录)

postgres=# \q
-bash-3.2$ psql -p 5432
psql (9.5beta2)
输入 "help" 来获取帮助信息.

postgres=# \l
                                     数据库列表
   名称    |  拥有者  | 字元编码 |  校对规则   |    Ctype    |       存取权限        
-----------+----------+----------+-------------+-------------+-----------------------
 music     | postgres | UTF8     | zh_CN.UTF-8 | zh_CN.UTF-8 | =Tc/postgres         +
           |          |          |             |             | postgres=CTc/postgres+
           |          |          |             |             | eric=C*T*c*/postgres
 postgres  | postgres | UTF8     | zh_CN.UTF-8 | zh_CN.UTF-8 | 
 template0 | postgres | UTF8     | zh_CN.UTF-8 | zh_CN.UTF-8 | =c/postgres          +
           |          |          |             |             | postgres=CTc/postgres
 template1 | postgres | UTF8     | zh_CN.UTF-8 | zh_CN.UTF-8 | =c/postgres          +
           |          |          |             |             | postgres=CTc/postgres
(4 行记录)


OK~~~
阅读(5006) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~