기사 디렉토리
1. 기본 환경 구성
1.1 방화벽 끄기
systemctl stop firewalld.service #停止防火墙
systemctl disable firewalld.service #禁止开机启动
1.2 selinux 닫기
setenforce 0
sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
cat /etc/sysconfig/selinux
/usr/sbin/sestatus -v
1.3 운영 체제 제한
스왑 파티션 설정
echo "vm.swappiness = 1" >>/etc/sysctl.conf
sysctl -p
#通过sysctl -a 查看
PS :swappiness值在0-100之间,0尽力使用物理内存,100尽力使用swap分区。
자원 할당
cat /etc/security/limits.conf
...
* soft nproc 65536
* hard nproc 65536
* soft nofile 65536
* hard nofile 65536
...
투명한 거대한 페이지 닫기
echo never >> /sys/kernel/mm/transparent_hugepage/enabled
echo never >> /sys/kernel/mm/transparent_hugepage/defrag
noatime 열기
每个文件上都有以下上个时间:
ctime: 改变时间
mtime: 修改时间
atime: 访问时间
通常Postgresql 并不使用这三个时间;
首先禁止的是:atime
mtime 和 ctime 有时还有些作用
设置 noatime 如下:vim /etc/fstab
/dev/vda1 / xfs noatime,errors=remount-ro 0 1
미리 읽기 조정
Linux 下块设备通常都默认打开了预读,可以使用下面的命令查看预读的大小:
blockdev --getra /dev/sdf
注意,上面的命令中值的单位为扇区,即 512bytes. 在下面的示例中:
sudo blockdev --getra /dev/sda
返回值为256,表示是256个扇区,即为128KB
设置预读的命令如下:
blockdev --setra 4096 /dev/sdf
上面的设置并不会永久生效,机器重启后就会失效,如果想要永久生效,应该
把命令放到自动脚本中
如果想让全表扫描更快一些,可以把预读调整大一些,如像上例那样把预读设置为2MB
IO 스케줄러 조정
linux 下通常有一下三种I/O调度器:
1.cfq: completely fair queuing,完全公平队列,尝试为所有的请求分配公平的I/O带宽,注意时带宽,而不是响应时间
2.deadline: 平衡所有的请求,避免某个请求被饿死,让响应时间最优化
3.noop: 除了基本的块合并及排序工作,其他基本里上什么都不做。
echo deadline > /sys/block/vda/queue/scheduler
1.4 누마 끄기
sed -i 's/GRUB_CMDLINE_LINUX.*/GRUB_CMDLINE_LINUX="crashkernel=auto rd.lvm.lv=centos\/root rd.lvm.lv=centos\/swap rhgb quiet numa=off"/g' /etc/default/grub
grub2-mkconfig -o /etc/grub2.cfg
cat /etc/grub2.cfg
reboot
cat /proc/cmdline
dmesg | grep -i numa
2. 소프트웨어 설치
2.1 컴파일 및 설치
[root@zijie ~]# tar xf postgresql-12.4.tar.gz -C /usr/local/
[root@zijie ~]# mv /usr/local/postgresql-12.4/ /usr/local/postgresql
[root@zijie ~]# cd /usr/local/postgresql
[root@zijie ~]# ./configure --prefix=/usr/local/pgsql --with-perl --with-tcl --with-python --with-openssl --with-pam --without-ldap --with-libxml --with-libxslt --enable-thread-safety --with-wal-blocksize=16 --with-blocksize=16 --enable-dtrace --enable-debug
[root@zijie ~]# make && make install
오류 처리
问题1:
checking for dtrace... no
configure: error: dtrace not found
解决方法: yum install -y systemtap-sdt-devel.x86_64
问题2:
checking for flags to link embedded Perl... Can't locate ExtUtils/Embed.pm in @INC (@INC contains: /usr/local/lib64/perl5 /usr/local/share/perl5 /usr/lib64/perl5/vendor_perl /usr/share/perl5/vendor_perl /usr/lib64/perl5 /usr/share/perl5 .).
BEGIN failed--compilation aborted.
no
configure: error: could not determine flags for linking embedded Perl.
This probably means that ExtUtils::Embed or ExtUtils::MakeMaker is not
installed.
解决方法:
yum install perl-ExtUtils-Embed -y
问题3:
configure: error: could not determine flags for linking embedded Perl.
This probably means that ExtUtils::Embed or ExtUtils::MakeMaker is not
installed.
解决方法:
yum install perl-ExtUtils-Embed
问题4:
configure: error: readline library not found
If you have readline already installed, see config.log for details on the
failure. It is possible the compiler isn't looking in the proper directory.
Use --without-readline to disable readline support.
解决方法:
yum install readline readline-devel
问题5:
checking for inflate in -lz... no
configure: error: zlib library not found
If you have zlib already installed, see config.log for details on the
failure. It is possible the compiler isn't looking in the proper directory.
Use --without-zlib to disable zlib support.
解决方法:
yum install zlib zlib-devel
问题6:
checking for CRYPTO_new_ex_data in -lcrypto... no
configure: error: library 'crypto' is required for OpenSSL
解决方法:
yum install openssl openssl-devel
问题7:
checking for pam_start in -lpam... no
configure: error: library 'pam' is required for PAM
解决方法:
yum install pam pam-devel
问题8:
checking for xmlSaveToBuffer in -lxml2... no
configure: error: library 'xml2' (version >= 2.6.23) is required for XML support
解决方法:
yum install libxml2 libxml2-devel
问题9:
checking for xsltCleanupGlobals in -lxslt... no
configure: error: library 'xslt' is required for XSLT support
解决方法:
yum install libxslt libxslt-devel
问题10:
configure: error: Tcl shell not found
解决方法:
yum install tcl tcl-devel
问题11:
checking for ldap.h... no
configure: error: header file is required for LDAP
解决方法:
yum install openldap openldap-devel
问题12:
checking for Python.h... no
configure: error: header file <Python.h> is required for Python
解决方法:
yum install python python-devel
2.2 사용자, 디렉터리 및 권한 구성
[root@zijie postgresql]# groupadd postgres
[root@zijie postgresql]# useradd postgres -g postgres
[root@zijie postgresql]# passwd postgres
[root@zijie postgresql]# mkdir -p /data/pgsql/data
[root@zijie postgresql]# mkdir -p /data/pgsql/log
[root@zijie postgresql]# chown -R postgres:postgres /data/pgsql/
[root@zijie postgresql]# chown -R postgres:postgres /usr/local/pgsql/
[root@zijie postgresql]# chown -R postgres:postgres /usr/local/postgresql/
2.3 환경 변수 구성
#修改环境变量
export LD_LIBRARY_PATH=/usr/local/pgsql/lib:$LD_LIBRARY_PATH
export PGDATA=/data/pgsql/data
export PGHOST=/tmp
export PGHOME=/usr/local/pgsql
export PATH=$PATH:$PGHOME/bin
#使其生效
source /etc/profile
2.4 데이터베이스 초기화
[root@zijie postgresql]# su - postgres
Last login: Sun Aug 30 22:11:36 CST 2020 on pts/2
[postgres@zijie ~]$ initdb
The files belonging to this database system will be owned by user "postgres".
This user must also own the server process.
The database cluster will be initialized with locale "en_US.UTF-8".
The default database encoding has accordingly been set to "UTF8".
The default text search configuration will be set to "english".
Data page checksums are disabled.
fixing permissions on existing directory /data/pgsql/data ... ok
creating subdirectories ... ok
selecting dynamic shared memory implementation ... posix
selecting default max_connections ... 100
selecting default shared_buffers ... 128MB
selecting default time zone ... Asia/Shanghai
creating configuration files ... ok
running bootstrap script ... ok
performing post-bootstrap initialization ... ok
syncing data to disk ... ok
initdb: warning: enabling "trust" authentication for local connections
You can change this by editing pg_hba.conf or using the option -A, or
--auth-local and --auth-host, the next time you run initdb.
Success. You can now start the database server using:
pg_ctl -D /data/pgsql/data -l logfile start
2.5 데이터 디렉토리보기
[postgres@zijie data]$ ll
total 120K
drwx------ 5 postgres postgres 4.0K Sep 3 23:41 base
drwx------ 2 postgres postgres 4.0K Sep 3 23:41 global
drwx------ 2 postgres postgres 4.0K Sep 3 23:41 pg_commit_ts
drwx------ 2 postgres postgres 4.0K Sep 3 23:41 pg_dynshmem
-rw------- 1 postgres postgres 4.7K Sep 3 23:51 pg_hba.conf
-rw------- 1 postgres postgres 1.6K Sep 3 23:41 pg_ident.conf
drwx------ 4 postgres postgres 4.0K Sep 3 23:53 pg_logical
drwx------ 4 postgres postgres 4.0K Sep 3 23:41 pg_multixact
drwx------ 2 postgres postgres 4.0K Sep 3 23:41 pg_notify
drwx------ 2 postgres postgres 4.0K Sep 3 23:41 pg_replslot
drwx------ 2 postgres postgres 4.0K Sep 3 23:41 pg_serial
drwx------ 2 postgres postgres 4.0K Sep 3 23:41 pg_snapshots
drwx------ 2 postgres postgres 4.0K Sep 3 23:53 pg_stat
drwx------ 2 postgres postgres 4.0K Sep 3 23:53 pg_stat_tmp
drwx------ 2 postgres postgres 4.0K Sep 3 23:41 pg_subtrans
drwx------ 2 postgres postgres 4.0K Sep 3 23:41 pg_tblspc
drwx------ 2 postgres postgres 4.0K Sep 3 23:41 pg_twophase
-rw------- 1 postgres postgres 3 Sep 3 23:41 PG_VERSION
drwx------ 3 postgres postgres 4.0K Sep 3 23:41 pg_wal
drwx------ 2 postgres postgres 4.0K Sep 3 23:41 pg_xact
-rw------- 1 postgres postgres 88 Sep 3 23:41 postgresql.auto.conf
-rw------- 1 postgres postgres 27K Sep 3 23:50 postgresql.conf
-rw------- 1 postgres postgres 54 Sep 3 23:41 postmaster.opts
postgresql的磁盘布局:
data/base
存放各数据库实例对应文件夹,命名方式是数据库的OID,select oid,datname from pg_database;可以查询每个数据库的OID,对应的可以在base目录下找到这个数据库的文件夹。
数据表使用表名对应的relfilenode作为文件名存放在数据库文件夹下,select relfilenode,relname from pg_class;
可以查询表的relfilenode,然后在相应的数据库文件夹下可以看到这个表的文件,该文件用于存储表的数据,最大1G,超出自动扩展,扩展名为relfilenode.1,relfilenode.2 ...
为了提高I/O性能,pg总是以8K大小的块执行IO。因此数据文件总是以8K的步长增长。(物理复制时,双方需要使用相同的块大小)
global -全局数据
包括全局系统表
pg_clog -提交日志
提交日志是一个工作数据库实例的一个重要组成部分。它存储系统上进行的事物的状态。
一个事物有四种状态(TRANSACTION_STATUS_IN_PROGRESS,TRANSACTION_STATUS_COMMITTED,TRANSACTION_STATUS_ABORTED,TRANSACTION_STATUS_SUB_COMMITTED),
如果一个事物的提交日志的状态是不可用,postgresql将不知道是否应该被看到。
pg_hba.conf -基于主机的网络配置
常用认证方式
trust
ident
md5
pg_ident.conf -身份认证
pg_notify-监听、通知数据
系统存储关于监听、通知的信息(异步后端接口)
pg_serial-关于提交序列化事物的信息
序列化事物的信息存储在这里。我们必须在磁盘上存储序列化事物提交信息,以确保长时间运行的事物不会膨胀内存,内部采用一个简单的SLRU结构来跟踪这些事物
pg_snapshot-输出快照
pg_stat_tmp-临时统计数据
该信息被大多数pg_stat_*系统视图所需要
pg_subtrans -子事物数据
pg_xlog postgresql的事物日志(WAL)
WAL(Write Ahead Log)和XLOG是一个事物的两个名字。默认事物日志文件大小16M。
xlog不可能无限增大,需要删除无用的xlog,原则:如果xlog文件中的所有更改也被放到数据文件中,xlog才可以被截断(删除)。需要检查点的配置
postgresql.conf -pg配置文件
세, postgresql 구성 조정
3.1 pg_hba.conf 파일 수정
#将IPV4的连接修改为
# IPv4 local connections:
host all all 0.0.0.0/0 md5
3.2 postgresql.conf 구성 파일 수정
#listen_addresses = 'localhosts'调整为listen_addresses = '*' # what IP address(es) to listen on;
3.3 기본 매개 변수 조정
log_filename = 'postgresql-%a.log'
log_truncate_on_rotation = on
log_rotation_age = 1d
log_rotation_size = 0
shared_buffer=8192M
work_mem=64M
3.4 데이터베이스 시작
[postgres@zijie ~]$ pg_ctl -D /data/pgsql/data -l logfile start
waiting for server to start.... done
server started
3.5 데이터베이스에 연결
[postgres@zijie data]$ psql
psql (12.4)
Type "help" for help.
postgres=# \l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+----------+----------+-------------+-------------+-----------------------
postgres | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
template0 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
(3 rows)