#!/bin/bash
# Created at 2021/02/22
# Author CZH
# This Script used to set postgresql environment.
# reference : https://github.com/digoal/blog/blob/master/201608/20160803_01.md
# reference : https://github.com/digoal/blog/blob/master/201611/20161121_01.md
#
# Scripts Summary
# 1.func_check_root: run as root
# 2.func_set_hostname: set hostname or not.
# 3.func_inst_pack: install os packages.
# 4.func_disable_selinux: disable selinux
# 5.func_set_sysctl: set os kernel parameters.
# 6.func_set_dirty: set write disk ratio.
# 7.func_set_limits: set user open file and stack limits.
# 8.func_os_other: disk scheduler and disable THP.
# 9.func_set_datadir: mkdir data directory and user env.
# Exit this shell when any error occur.
set -e
# Configure Global variables.
v_curdate=`date +%Y%m%d_%H%M%S`
v_memtotal=`cat /proc/meminfo |grep MemTotal|awk '{print $2}'`
# check current user is root or not.
function func_check_root()
{
if [ ${UID} -ne 0 ]
then
echo -e "\nPlease use root run this scripts!\n"
exit 0
else
echo -e "\n>>>>>>>> This script will run as root ! <<<<<<<<\n"
fi
echo -e "\nfunc func_check_root execute Successful!\n"
}
# setting hostname
function func_set_hostname()
{
read -p "Are you want set hostname ?(y/n)" v_set_choice
v_convert_choice=$(echo ${v_set_choice} | tr [A-Z] [a-z])
if [ ${v_convert_choice} == "y" ];
then
read -p "Please input your hostname: " v_hostname
hostnamectl set-hostname ${v_hostname}
echo -e "\nThe current hostname is $(hostname).\n"
elif [ ${v_convert_choice} == "n" ];
then
echo -e "\n Choice n , will continue.\n"
echo -e "\nThe current hostname is $(hostname).\n"
else
echo -e "\n Only accept input y/n !\n"
exit 618
fi
echo -e "\nfunc func_set_hostname execute Successful!\n"
}
# install packages
function func_inst_pack()
{
rpm -q bash \
bzip2 \
zlib \
zlib-devel \
openssl \
openssl-libs \
perl \
readline \
readline-devel \
sed \
tar \
zip \
python \
make |grep -i 'not installed'|awk -F' ' '{print $2}'|xargs -r /usr/bin/yum install -y
echo -e "\nfunc func_inst_pack execute Successful!\n"
}
# Disable SElinux
function func_disable_selinux()
{
sed -i 's/enforcing/disabled/g' /etc/selinux/config
setenforce 0
echo -e "\nfunc func_disable_selinux execute Successful!\n"
}
# Configure os kernel parameter.
function func_set_sysctl()
{
# backup sysctl.conf
cp /etc/sysctl.conf /etc/sysctl.conf.bak.${v_curdate}
# 所有共享内存段相加大小限制(建议内存的80%)
v_shmall=`echo $(expr $(getconf _PHYS_PAGES) \* 8 / 10)`
# 最大单个共享内存段大小(建议为内存一半), > 9.2的版本已大幅降低共享内存的使用
v_shmmax=`echo $(expr $(getconf _PHYS_PAGES) / 2 \* $(getconf PAGE_SIZE))`
# configure sysctl.conf
cat >> /etc/sysctl.conf <<EOF
kernel.shmall = ${v_shmall}
kernel.shmmax = ${v_shmmax}
# 一共能生成多少共享内存段,每个PG数据库集群至少2个共享内存段
kernel.shmmni = 4096
vm.overcommit_memory = 2 # same as close oom.
vm.overcommit_ratio = 80 # only take effect when overcommit_memory set to 2.
vm.swappiness = 0
net.ipv4.ip_local_port_range = 10000 65535
kernel.sem = 4096 2147483647 2147483646 512000
kernel.sysrq = 1
kernel.core_uses_pid = 1
kernel.msgmnb = 65536
kernel.msgmax = 65536
kernel.msgmni = 2048
# /data/corefiles事先建好,权限777,如果是软链接,对应的目录修改为777
# 需要配合ulimit -c设置core限制,默认1024
# kernel.core_pattern= /data/corefiles/core_%e_%u_%t_%s.%p
net.ipv4.tcp_syncookies = 1
net.ipv4.conf.default.accept_source_route = 0
net.ipv4.tcp_max_syn_backlog = 4096
net.ipv4.conf.all.arp_filter = 1
net.ipv4.tcp_keepalive_intvl = 20
net.ipv4.tcp_keepalive_probes = 3
net.ipv4.tcp_keepalive_time = 60
net.ipv4.tcp_mem = 8388608 12582912 16777216
net.ipv4.tcp_fin_timeout = 5
net.ipv4.tcp_synack_retries = 2
net.core.netdev_max_backlog = 10000
# The maximum receive socket buffer size in bytes
net.core.rmem_max = 4194303
# The maximum send socket buffer size in bytes.
net.core.wmem_max = 4194303
vm.zone_reclaim_mode = 0
# 比这个值老的脏页,将被刷到磁盘。3000表示30秒
vm.dirty_expire_centisecs = 3000
# 如果系统进程刷脏页太慢,使得系统脏页超过内存 95 % 时,则用户进程如果有写磁盘的操作(如fsync, fdatasync等调用),则需要主动把系统脏页刷出
# 有效防止用户进程刷脏页,在单机多实例,并且使用CGROUP限制单实例IOPS的情况下非常有效
# vm.dirty_ratio = 95
# pdflush(或其他)后台刷脏页进程的唤醒间隔,100表示1秒
vm.dirty_writeback_centisecs = 100
# 禁用 numa, 或者在vmlinux中禁止
vm.zone_reclaim_mode = 0
EOF
# Configure min reserved memory 3% percent of the total memory.
awk 'BEGIN {OFMT = "%.0f";} /MemTotal/ {print "vm.min_free_kbytes =", $2 * .03;}' /proc/meminfo >> /etc/sysctl.conf
echo -e "\nfunc func_set_sysctl execute Successful!\n"
}
# Configure os kernel dirty flush on the basis of physical memory.
# 大于64g则设置值,否则设置比率
function func_set_dirty()
{
if [ ${v_memtotal} -gt 67108864 ];then
cat >>/etc/sysctl.conf <<EOF
vm.dirty_background_bytes = 1610612736
vm.dirty_bytes = 4294967296
EOF
else
cat >>/etc/sysctl.conf <<EOF
vm.dirty_background_ratio = 3
vm.dirty_ratio = 10
EOF
fi
echo -e "\nfunc func_set_dirty execute Successful!\n"
}
# Configure limits.conf
function func_set_limits()
{
cat >> /etc/security/limits.d/20-nproc.conf <<EOF
postgres soft nofile 524288
postgres hard nofile 524288
postgres soft nproc 131072
postgres hard nproc 131072
EOF
echo -e "\nfunc func_set_limits execute Successful!\n"
}
# Adjust Disk I/O Scheduler,disable THP,Adjust SSH,ADD USER
function func_os_other()
{
# Set Disk I/O Scheduler. Redhat 7.x and CentOS 7.x
grubby --update-kernel=ALL --args="elevator=deadline"
# Disable Transparent Hugepage On RHEL 7.x and CentOS 7.x
grubby --update-kernel=ALL --args="transparent_hugepage=never"
cat /sys/kernel/mm/*transparent_hugepage/enabled
# display grub
echo -e "\n"
grubby --info=ALL
echo -e "\n"
# Modify SSH MaxSessions.
sed -i 's/#MaxSessions.*/MaxSessions 200/g' /etc/ssh/sshd_config
sed -i 's/#MaxStartups.*/MaxStartups 200/g' /etc/ssh/sshd_config
cat /etc/ssh/sshd_config |grep MaxS
echo -e "\n"
# Add Greenplum administration group and user.
groupadd postgres
useradd postgres -g postgres
echo -e "\nfunc func_os_other execute Successful!\n"
}
# set userenv function, .bash_profile settings
function func_set_userenv()
{
cat <<EOF >> /home/postgres/.bash_profile
export PS1='[\u@\h] (\w) \$ '
export PGPORT=${v_port}
export PGDATA=${v_datadir}
export LANG=en_US.utf8
export PGHOME=/usr/local/postgres
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$PGHOME/lib:/lib64:/usr/lib64:/usr/local/lib64:/lib:/usr/lib:/usr/local/lib
export PATH=$PGHOME/bin:$PATH
export DATE=`date +"%Y%m%d%H%M"`
export MANPATH=$PGHOME/share/man:$MANPATH
export PGHOST=$PGDATA
export PGUSER=postgres
export PGDATABASE=postgres
alias rm='rm -i'
alias ll='ls -lh'
unalias vi
EOF
}
function func_set_datadir()
{
read -p "Do your want to make postgresql data directory ?(y/n)" v_mkdir_choice
v_convert_mkdir=$(echo ${v_mkdir_choice} | tr [A-Z] [a-z])
if [ ${v_convert_mkdir} == "y" ];
then
read -p 'Please input your PostgreSQL Data directory (default:(/data)): ' v_datadir
v_datadir=${v_datadir:-/data} # If user not input any directory,use the /data as directory.
read -p 'Please input your PostgreSQL Port (default:(5432)): ' v_port
v_port=${v_port:-5432}
if [ -d ${v_datadir} ] ;
then echo -e "\nThe directory was exists! will continue \n"
else
mkdir -p ${v_datadir}
chown -R postgres:postgres ${v_datadir}
chmod -R 775 ${v_datadir}
fi
func_set_userenv
echo -e "\nThe data directory is ${v_datadir}.\n"
echo -e "\nThe PostgreSQL port is ${v_port}.\n"
elif [ ${v_convert_mkdir} == "n" ];
then
func_set_userenv
else
echo -e "\n Only accept input y/n !\n"
exit 618
fi
echo -e "\nfunc func_set_datadir execute Successful!\n"
}
func_set_hostname
func_check_root
func_inst_pack
func_disable_selinux
func_set_sysctl
func_set_dirty
func_set_limits
func_os_other
func_set_datadir
echo -e "\n>>>>>>>> The operating system setup is finished, please reboot! <<<<<<<<\n"
CentOS7.5安装PostgreSQL操作系统配置脚本
最新推荐文章于 2021-09-12 11:18:07 发布
这是一个用于配置PostgreSQL运行环境的Shell脚本,包括检查root权限、设置主机名、安装依赖包、禁用SELinux、调整内核参数、设置脏页策略、限制及优化操作系统设置、创建数据目录并设置用户环境。脚本适用于系统初始化或优化PostgreSQL数据库服务。
摘要由CSDN通过智能技术生成