如何免費(fèi)制作一個(gè)自己的網(wǎng)站外匯交易平臺
文章目錄
- 說明
- 1. 準(zhǔn)備工作
- 1.1 源碼包下載
- 1.2 解壓安裝目錄
- 1.3 安裝依賴包
- 1.4 添加用戶
- 1.5 創(chuàng)建數(shù)據(jù)目錄
- 2. 編譯安裝
- 2.1 源碼編譯
- 2.2 配置環(huán)境變量
- 2.3 初始化數(shù)據(jù)庫
- 2.4 啟動(dòng)數(shù)據(jù)庫
- 2.5 連接數(shù)據(jù)庫
- 3. 參數(shù)調(diào)整
- 3.1 配置 pg_hba
- 3.2 監(jiān)聽相關(guān)
- 2.4 日志文件
- 2.5 內(nèi)存參數(shù)
說明
本篇文章介紹 PostgreSQL 單機(jī)源碼編譯部署的詳細(xì)步驟。
1. 準(zhǔn)備工作
1.1 源碼包下載
進(jìn)入 PostgreSQL 官網(wǎng)下載頁面 選擇 Source 欄目:
接著就進(jìn)入源碼版本目錄,選擇需要安裝的版本下載即可。
1.2 解壓安裝目錄
源碼包下載完成后,上傳到服務(wù)器,進(jìn)行解壓縮:
tar -xf postgresql-14.8.tar.gz
1.3 安裝依賴包
yum install gcc gcc-c++ readline-devel readline readline-dev zlib-devel
1.4 添加用戶
groupadd postgres
useradd -g postgres postgres
1.5 創(chuàng)建數(shù)據(jù)目錄
為 PostgreSQL 創(chuàng)建存儲(chǔ)數(shù)據(jù)的目錄:
mkdir -p /data/pgsql/{data,logs}
chown -R postgres:postgres /data/pgsql/
2. 編譯安裝
2.1 源碼編譯
cd 到源碼目錄下:
cd /opt/postgresql-14.8
執(zhí)行 configure:
./configure --prefix=/usr/local/pgsql
參數(shù)名 | 含義 |
---|---|
prefix | 軟件目錄也就是安裝目錄 |
with-perl | 編譯時(shí)添加該參數(shù)才能夠使用 perl 語法的 PL/Perl 過程語言寫自定義函數(shù),需要提前安裝好相關(guān)的 perl 開發(fā)包:libperl-dev |
with-python | 編譯時(shí)添加該參數(shù)才能夠使用 python 語法的 PL/Perl 過程語言寫自定義函數(shù),需要提前安裝好相關(guān)的 python 開發(fā)包:python-dev |
with-blocksize & with-wal-blocksize | 默認(rèn)情況下 PG 數(shù)據(jù)庫的數(shù)據(jù)頁大小為 8KB,若數(shù)據(jù)庫用來做數(shù)倉業(yè)務(wù),可在編譯時(shí)將數(shù)據(jù)頁進(jìn)行調(diào)整,以提高磁盤 IO |
編譯安裝:
make && make install
編譯完成后,會(huì)在 prefix 參數(shù)指定的目錄下生成 PostgreSQL 程序文件。
2.2 配置環(huán)境變量
根據(jù)自己實(shí)際環(huán)境,修改安裝目錄和數(shù)據(jù)目錄:
vi /etc/profile
export PGHOME=/usr/local/pgsql
export PGDATA=/data/pgsql/data
export PATH=$PGHOME/bin:$PATH
export LD_LIBRARY_PATH=/usr/local/pgsql/lib
source /etc/profile
2.3 初始化數(shù)據(jù)庫
切換到 postgres 用戶:
su postgres
執(zhí)行數(shù)據(jù)庫初始化 -D 選項(xiàng)后面是數(shù)據(jù)目錄:
initdb -D /data/pgsql/data/
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 "zh_CN.UTF-8".
The default database encoding has accordingly been set to "UTF8".
initdb: could not find suitable text search configuration for locale "zh_CN.UTF-8"
The default text search configuration will be set to "simple".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 ... okinitdb: 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.4 啟動(dòng)數(shù)據(jù)庫
進(jìn)入 logs 目錄下,創(chuàng)建啟動(dòng)日志文件,將啟動(dòng)時(shí)的日志輸出到該文件:
touch /data/pgsql/logs/start.log
啟動(dòng) PostgreSQL:
pg_ctl -D /data/pgsql/data/ -l /data/pgsql/logs/start.log start
關(guān)閉數(shù)據(jù)庫可以使用下方命令:
# 關(guān)閉數(shù)據(jù)庫
pg_ctl -D /data/pgsql/data/ -l /data/pgsql/logs/start.log stop
# 重啟數(shù)據(jù)庫
pg_ctl -D /data/pgsql/data/ -l /data/pgsql/logs/start.log restart
2.5 連接數(shù)據(jù)庫
啟動(dòng)成功使用 psql 即可進(jìn)入數(shù)據(jù)庫:
>>>>$ psql
psql (14.8)
Type "help" for help.postgres=# select version();version
---------------------------------------------------------------------------------------------------------PostgreSQL 14.8 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-44), 64-bit
3. 參數(shù)調(diào)整
3.1 配置 pg_hba
PostgreSQL 數(shù)據(jù)目錄中,會(huì)自動(dòng)生成 pg_hba.conf
文件,該文件是一個(gè)黑名單訪問控制文件,可以控制允許哪些 IP 地址的機(jī)器訪問數(shù)據(jù)庫。默認(rèn),不允許遠(yuǎn)程訪問數(shù)據(jù),所以安裝完成后需要配置下。
# TYPE DATABASE USER ADDRESS METHOD# "local" is for Unix domain socket connections only
# Type 表示訪問方式,local 表示本地套接字訪問,DATABASE、USER 分別表示數(shù)據(jù)庫和用戶
# 參數(shù) all 表示所有的數(shù)據(jù)庫或用戶,ADDRESS 表示一個(gè)地址或者網(wǎng)段,METHOD 表示驗(yàn)證的方式
# 默認(rèn)的 trust 表示完全信任,password 表示發(fā)送明文密碼,不建議使用,建議使用 md5 模式
local all all trust
# IPv4 local connections:
host all all 127.0.0.1/32 trust
# IPv6 local connections:
host all all ::1/128 trust
# Allow replication connections from localhost, by a user with the
# replication privilege.
local replication all trust
host replication all 127.0.0.1/32 trust
host replication all ::1/128 trust
可以在 pg_hba 文件中加入下面一行,表示允許任何用戶遠(yuǎn)程連接數(shù)據(jù)庫,連接時(shí)需要提供密碼:
host all all 0/0 md5
詳細(xì)可參考文檔:pg_hba 文件說明
3.2 監(jiān)聽相關(guān)
在數(shù)據(jù)目錄中的 postgresql.cnf
中,可以找到如下內(nèi)容:
listen_addresses = 'localhoset' # what IP address(es) to listen on;# comma-separated list of addresses;# defaults to 'localhost'; use '*' for all# (change requires restart)
其中,參數(shù) listen_addresses
表示監(jiān)聽的 IP 地址,默認(rèn)是在 localhost/127.0.0.1
處監(jiān)聽,這樣會(huì)導(dǎo)致遠(yuǎn)程主機(jī)無法訪問數(shù)據(jù)庫,如果需要遠(yuǎn)程訪問,需要將其設(shè)置為實(shí)際網(wǎng)絡(luò)地址,設(shè)置為 *
表示監(jiān)聽所有地址,該參數(shù)修改重啟生效。
PS:配置完 3.1 和 3.2 兩個(gè)步驟,PostgreSQL 就可以支持遠(yuǎn)程連接。
下表是其它常見監(jiān)聽相關(guān)的參數(shù),按需設(shè)置:
參數(shù) | 含義 |
---|---|
port | 服務(wù)器監(jiān)聽TCP端口,默認(rèn) 5432 |
max_connections | Server 端允許最大連接數(shù),默認(rèn) 100 |
superuser_reserved_connections | Server 端為超級賬號保留的連接數(shù),默認(rèn)3 |
unix_socket_directory | Server 監(jiān)聽客戶端 Unix 嵌套字目錄,默認(rèn) /tmp |
2.4 日志文件
下面是 PostgreSQL 日志相關(guān)的參數(shù),一般都是需要配置:
參數(shù) | 含義 |
---|---|
logging_collector | 是否打開日志 |
log_rotation_age | 超過多少天生產(chǎn)一個(gè)新的日志文件 |
log_rotation_size | 超過多少大小生成一個(gè)新的日志文件 |
log_directory | 日志目錄,可以是絕對路徑或相對 PGDATA 的相對路徑 |
log_destination | 日志記錄類型,默認(rèn)是 stderr,只記錄錯(cuò)誤輸出 |
log_filename | 日志文件名,默認(rèn)是 postgresql-%Y-%m-%d_%H%M%S.log |
log_truncate_on_rotation | 當(dāng)日志名已存在時(shí),是否覆蓋原文件 |
以下是幾個(gè)常用的配置模版,每天生成一個(gè)新的日志文件:
logging_collector = on
log_directory = '/data/pgsql/logs'
log_filename = 'postgresql-%Y-%m-%d_%H%M%S.log'
log_truncate_on_rotation = off
log_rotation_age = 1d
log_rotation_size = 0
每當(dāng)一個(gè)日志寫滿時(shí)(如 100MB)切換一個(gè)日志:
logging_collector = on
log_directory = '/data/pgsql/logs'
log_filename = 'postgresql-%Y-%m-%d_%H%M%S.log'
log_truncate_on_rotation = off
log_rotation_age = 0
log_rotation_size = 100MB
只保留最近 7 天的日志,進(jìn)行循環(huán)覆蓋:
logging_collector = on
log_directory = '/data/pgsql/logs'
log_filename = 'error_log.log'
log_truncate_on_rotation = on
log_rotation_age = 7d
log_rotation_size = 0
2.5 內(nèi)存參數(shù)
熟悉 MySQL 的同學(xué)都知道它有一個(gè)參數(shù) innodb_buffer_pool 限制 innodb 引擎緩沖池的大小,buffer pool 越大可以緩存的頁就越多,可以減少很多磁盤 IO 消耗,提升數(shù)據(jù)庫的性能。shared_buffer 在 PostgreSQL 中與 MySQL 的 buffer pool 是異曲同工。
參數(shù) | 含義 |
---|---|
shared_buffer | 共享內(nèi)存緩存區(qū)大小,默認(rèn) 128MB |
temp_buffers | 每個(gè)會(huì)話使用的臨時(shí)緩存區(qū)大小,默認(rèn) 8MB |
work_mem | 內(nèi)存臨時(shí)表排序操作或者 hash join 需要使用到的內(nèi)存緩存大小,默認(rèn) 4MB |
maintenance_work_mem | 對于維護(hù)性操作(vacuum、create index)最大使用內(nèi)存,默認(rèn) 64M,最小 1M |
max_stack_depth | Server 端執(zhí)行堆棧最大安全深度,默認(rèn) 2M,若發(fā)現(xiàn)無法執(zhí)行復(fù)雜函數(shù)時(shí)可適當(dāng)調(diào)整該參數(shù) |