把pgboucer做成postgresql服务

把pgbouncer启动命令加入到postgresql服务配置里面。这样方便操作

vi /etc/init.d/postgresql

加入如下红色命令。路径换成你的pgbouncer安装目录

case $ in
start)
echo -n "Starting PostgreSQL: "
test -e "$PG_OOM_ADJUST_FILE" && echo "$PG_MASTER_OOM_SCORE_ADJ" > "$PG_OOM_ADJUST_FILE"
su - $PGUSER -c "$DAEMON_ENV $DAEMON -D '$PGDATA' &" >>$PGLOG >&
su - $PGUSER -c "/路径/pgbouncer/bin/pgbouncer -d /路径/pgbouncer/conf/pgbouncer.ini"
echo "ok"
;;
stop)
echo -n "Stopping PostgreSQL: "
su - $PGUSER -c "$PGCTL stop -D '$PGDATA' -s -m fast"
pkill -9 pgbouncer
echo "ok"
;;
restart)
echo -n "Restarting PostgreSQL: "
su - $PGUSER -c "$PGCTL stop -D '$PGDATA' -s -m fast -w"
test -e "$PG_OOM_ADJUST_FILE" && echo "$PG_MASTER_OOM_SCORE_ADJ" > "$PG_OOM_ADJUST_FILE"
su - $PGUSER -c "$DAEMON_ENV $DAEMON -D '$PGDATA' &" >>$PGLOG >&
su - $PGUSER -c "/路径/pgbouncer/bin/pgbouncer -d /路径/pgbouncer/conf/pgbouncer.ini"
echo "ok"
;;
reload)
echo -n "Reload PostgreSQL: "
su - $PGUSER -c "$PGCTL reload -D '$PGDATA' -s"
echo "ok"
;;
status)
su - $PGUSER -c "$PGCTL status -D '$PGDATA'"
;;

加入后。以后重启服务器。postgresql服务启动后。然后 pgbouncer也启动了。

好这里我们测试下

[root@iZ236bfrzgoZ pgbouncer]# vi /etc/init.d/postgresql
[root@iZ236bfrzgoZ pgbouncer]# ps -ef|grep pgbouncer
postgres 6625 1 0 20:35 ? 00:00:00 /alidata/local/pgbouncer/bin/pgbouncer -d /alidata/local/pgbouncer/conf/pgbouncer.ini
root 18568 29466 0 21:05 pts/1 00:00:00 grep --color=auto pgbouncer
[root@iZ236bfrzgoZ pgbouncer]# pkill -9 pgbouncer
[root@iZ236bfrzgoZ pgbouncer]# ps -ef|grep pgbouncer
root 18674 29466 0 21:05 pts/1 00:00:00 grep --color=auto pgbouncer
[root@iZ236bfrzgoZ pgbouncer]# service postgresql start
Starting PostgreSQL: 2016-11-04 21:05:17.651 18772 LOG File descriptor limit: 65535 (H:65535), max_client_conn: 1000, max fds possible: 1190
2016-11-04 21:05:17.652 18772 LOG Stale pidfile, removing
ok
[root@iZ236bfrzgoZ pgbouncer]# ps -ef|grep pgbouncer
postgres 18789 1 0 21:05 ? 00:00:00 /alidata/local/pgbouncer/bin/pgbouncer -d /alidata/local/pgbouncer/conf/pgbouncer.ini
root 18917 29466 0 21:05 pts/1 00:00:00 grep --color=auto pgbouncer
[root@iZ236bfrzgoZ pgbouncer]#

这里能看到pgbouncer被强制结束掉后,我们用servcie postgresql start  命令启动,pgbouncer也启动了,打开数据库。能正常打开

以上红色命令可以换成如下命令进行 postgresql+pgbouncer服务管理
检测配置文件
/usr/local/pgbouncer/bin/pgbouncer -v /data/pgbouncer/pgbouncer.ini
启动:
/usr/local/pgbouncer/bin/pgbouncer -d /data/pgbouncer/pgbouncer.ini
重启:
/usr/local/pgbouncer/bin/pgbouncer -R -d /data/pgbouncer/pgbouncer.ini  【慎用,吃过一次亏,它会自动把userlist.txt删掉后再启动,然后导致程序连不上数据库】
停止:
psql -p 6432 pgbouncer -h 127.0.0.1 -U postgres-c 'shutdown'  【这里会要求密码,如果图方便把密码也加上,但是不安全】
加载配置:
psql -p 6432 pgbouncer -h 127.0.0.1 -U postgres -c 'reload'  【这里会要求密码】
上一篇:IDEA启动Tomcat报错Address localhost:1099 is already in use解决办法


下一篇:js 类型转换学习