CentOS7.5安装PostgreSQL操作系统配置脚本

#!/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"

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值