国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

【postgresql 基礎(chǔ)入門】數(shù)據(jù)庫服務(wù)的管理

這篇具有很好參考價值的文章主要介紹了【postgresql 基礎(chǔ)入門】數(shù)據(jù)庫服務(wù)的管理。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。

數(shù)據(jù)庫服務(wù)管理

?專欄內(nèi)容

  • postgresql內(nèi)核源碼分析
  • 手寫數(shù)據(jù)庫toadb
  • 并發(fā)編程

?開源貢獻

  • toadb開源庫

個人主頁:我的主頁
管理社區(qū):開源數(shù)據(jù)庫
座右銘:天行健,君子以自強不息;地勢坤,君子以厚德載物.

系列文章

  • 初始化集群
  • 數(shù)據(jù)庫服務(wù)管理

一、前言

postgresql 數(shù)據(jù)庫是一款通用的關(guān)系型數(shù)據(jù),在開源數(shù)據(jù)庫中能與商業(yè)數(shù)據(jù)媲美,在業(yè)界也越來越流行。

因為是開源數(shù)據(jù)庫,不僅公開源碼,還有很多使用案例,好用的插件,所以它的慢慢變成了數(shù)據(jù)庫的先驅(qū)和標(biāo)準(zhǔn),通過postgresql可以很好從使用到原理,徹底搞懂;

如果是學(xué)習(xí)編程,也可以學(xué)到豐富的編程知識,數(shù)據(jù)結(jié)構(gòu),編程技巧,它里面還有很多精妙的架構(gòu)設(shè)計,分層思想,可以靈活定制的思想。

本專欄主要介紹postgresql 入門使用,數(shù)據(jù)庫維護管理,通過這些使用來了解數(shù)據(jù)庫原理,慢慢了解postgresql是什么樣的數(shù)據(jù)庫,能做那些事情,以及如何做好服務(wù),最關(guān)鍵的是這些知識都是面試的必備項。

二、概述

本文主要介紹postgresql 數(shù)據(jù)庫服務(wù)的啟動,查看,停止,以及配置修改后的重新加載。

經(jīng)常接到開發(fā)人員反饋,SQL執(zhí)行失敗了,或者JDBC連不上數(shù)據(jù)庫了,不知道什么原因,有一部分原因其實很簡單,數(shù)據(jù)庫服務(wù)沒有啟動。使用數(shù)據(jù)庫,首先是啟動數(shù)據(jù)庫服務(wù),或者查看數(shù)據(jù)庫服務(wù)是否正常,保證我們后續(xù)的使用沒有問題。

三、原理

數(shù)據(jù)庫其實也是一個應(yīng)用軟件,不過它機制比較龐大而已,它一般采用C/S架構(gòu)。

【postgresql 基礎(chǔ)入門】數(shù)據(jù)庫服務(wù)的管理,postgresql,# postgresql使用,數(shù)據(jù)庫,postgresql,linux,后端,大數(shù)據(jù),數(shù)據(jù)庫開發(fā),sql

我們使用的圖形化客戶端或者命令行客戶端,都需要網(wǎng)絡(luò)連接到數(shù)據(jù)庫的服務(wù)端,執(zhí)行的命令也是由客戶端發(fā)送給服務(wù)端執(zhí)行,再由服務(wù)端發(fā)送到客戶端。

postgresql數(shù)據(jù)庫服務(wù)端,采用多進程模式,有主進程進行網(wǎng)絡(luò)監(jiān)聽服務(wù),每連接一個客戶端,都會fork產(chǎn)生一個服務(wù)進程;這樣每個客戶端都有獨立的服務(wù)進程處理;

四、數(shù)據(jù)庫服務(wù)管理命令

數(shù)據(jù)庫服務(wù)的管理命令是 pg_ctrl ,先看一下幫助
先進入到安裝目錄下 /opt/postgres/bin/, 這個路徑按自己實際安裝路徑執(zhí)行;

[senllang@hatch bin]$ cd /opt/postgres/bin/
[senllang@hatch bin]$ ./pg_ctl --help
pg_ctl is a utility to initialize, start, stop, or control a PostgreSQL server.

Usage:
  pg_ctl init[db]   [-D DATADIR] [-s] [-o OPTIONS]
  pg_ctl start      [-D DATADIR] [-l FILENAME] [-W] [-t SECS] [-s]
                    [-o OPTIONS] [-p PATH] [-c]
  pg_ctl stop       [-D DATADIR] [-m SHUTDOWN-MODE] [-W] [-t SECS] [-s]
  pg_ctl restart    [-D DATADIR] [-m SHUTDOWN-MODE] [-W] [-t SECS] [-s]
                    [-o OPTIONS] [-c]
  pg_ctl reload     [-D DATADIR] [-s]
  pg_ctl status     [-D DATADIR]
  pg_ctl promote    [-D DATADIR] [-W] [-t SECS] [-s]
  pg_ctl logrotate  [-D DATADIR] [-s]
  pg_ctl kill       SIGNALNAME PID

Common options:
  -D, --pgdata=DATADIR   location of the database storage area
  -s, --silent           only print errors, no informational messages
  -t, --timeout=SECS     seconds to wait when using -w option
  -V, --version          output version information, then exit
  -w, --wait             wait until operation completes (default)
  -W, --no-wait          do not wait until operation completes
  -?, --help             show this help, then exit
If the -D option is omitted, the environment variable PGDATA is used.

Options for start or restart:
  -c, --core-files       allow postgres to produce core files
  -l, --log=FILENAME     write (or append) server log to FILENAME
  -o, --options=OPTIONS  command line options to pass to postgres
                         (PostgreSQL server executable) or initdb
  -p PATH-TO-POSTGRES    normally not necessary

Options for stop or restart:
  -m, --mode=MODE        MODE can be "smart", "fast", or "immediate"

Shutdown modes are:
  smart       quit after all clients have disconnected
  fast        quit directly, with proper shutdown (default)
  immediate   quit without complete shutdown; will lead to recovery on restart

Allowed signal names for kill:
  ABRT HUP INT KILL QUIT TERM USR1 USR2

Report bugs to <pgsql-bugs@lists.postgresql.org>.
PostgreSQL home page: <https://www.postgresql.org/>

可以看到,主要有幾個管理選項

  • init 初始化服務(wù),也就是初始化數(shù)據(jù)庫集群,這在前面已經(jīng)分享過,這里只是用pg_ctrl來調(diào)用而已;
  • start 啟動數(shù)據(jù)庫服務(wù)
  • stop 停止數(shù)據(jù)庫服務(wù)
  • status 查看服務(wù)狀態(tài)
  • restart 重啟服務(wù)
  • reload 重新加載

五、初始化數(shù)據(jù)庫服務(wù)

前面已經(jīng)介紹過詳細的初始化數(shù)據(jù)庫集群,這是另一種命令執(zhí)行方式,最終還是調(diào)用initdb來完成 。

[senllang@hatch bin]$ ./pg_ctl init -D testdemo1
The files belonging to this database system will be owned by user "senllang".
This user must also own the server process.

Using default ICU locale "en_US".
Using language tag "en-US" for ICU locale "en_US".
The database cluster will be initialized with this locale configuration:
  provider:    icu
  ICU locale:  en-US
  LC_COLLATE:  en_US.UTF-8
  LC_CTYPE:    en_US.UTF-8
  LC_MESSAGES: en_US.UTF-8
  LC_MONETARY: en_US.UTF-8
  LC_NUMERIC:  en_US.UTF-8
  LC_TIME:     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.

creating directory testdemo1 ... 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
initdb: hint: 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:

    /opt/postgres/bin/pg_ctl -D testdemo1 -l logfile start

在當(dāng)前目錄下,初始化testdemo1數(shù)據(jù)庫集群目錄

六、啟動數(shù)據(jù)庫服務(wù)

啟動數(shù)據(jù)庫服務(wù)有兩種方式,一種是在直接執(zhí)行數(shù)據(jù)庫主程序,當(dāng)前終端退出后,數(shù)據(jù)庫服務(wù)也就停止了;另一種是將數(shù)據(jù)庫服務(wù)以后臺服務(wù)形式啟動,這樣它就不依賴于當(dāng)前終端了;

  • 后臺服務(wù)方式啟動服務(wù)
    這個命令其實在初始化結(jié)束時,已經(jīng)提示了
[senllang@hatch bin]$ ./pg_ctl -D testdemo1 -l logfile start
waiting for server to start.... done
server started

其中參數(shù)需要指定

  • -D 數(shù)據(jù)庫集群目錄,也就之前初始化的
  • -l 服務(wù)啟動日志輸出的文件

七、查看數(shù)據(jù)庫服務(wù)

在使用數(shù)據(jù)庫之前,或者連接數(shù)據(jù)庫失敗時,我們首先需要查看數(shù)據(jù)庫服務(wù)是否正常。
數(shù)據(jù)庫服務(wù)狀態(tài)的查看方式常用的有幾種:

  • 通過服務(wù)管理命令查看
  • 通過數(shù)據(jù)庫運行日志查看數(shù)據(jù)庫服務(wù)運行情況
  • 通過系統(tǒng)命令查看后進程
  • 通過系統(tǒng)網(wǎng)絡(luò)命令查看網(wǎng)絡(luò)監(jiān)聽和連接狀態(tài)

查看服務(wù)狀態(tài)

通過pg_ctl服務(wù)管理命令的 status參數(shù)查看,需要指定 -D 集群目錄(也可以是PGDATA環(huán)境變量代替)

[senllang@hatch bin]$ ./pg_ctl -D testdemo1 status
pg_ctl: server is running (PID: 136824)
/opt/postgres/bin/postgres "-D" "testdemo1"

查看運行日志

運行日志默認是沒有輸出到文件的,在啟動之前需要在配置文件中將參數(shù)logging_collector打開,這樣我們才可以在文件中進行查看,這個參數(shù)變化,需要重啟服務(wù)

vi testdemo1/postgresql.conf
logging_collector = on 

./pg_ctl -D testdemo1 -l logfile restart 

[senllang@hatch bin]$ cd $PGDATA/log
[senllang@hatch log]$ ll
total 4
-rw-------. 1 senllang develops 616 Sep  3 08:40 postgresql-2023-09-03_084025.log

這樣就可以看到log目錄,同時已經(jīng)生成了一個日志文件,文件名默認按照時間來命名,打開可以看到已經(jīng)啟動成功,等待連接的到來;

[senllang@hatch log]$ vi postgresql-2023-09-03_084025.log
2023-09-03 08:40:25.326 CST [148029] LOG:  starting PostgreSQL 16beta1 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 8.5.0 20210514 (Red Hat 8.5.0-19), 64-bit
2023-09-03 08:40:25.327 CST [148029] LOG:  listening on IPv6 address "::1", port 5433
2023-09-03 08:40:25.327 CST [148029] LOG:  listening on IPv4 address "127.0.0.1", port 5433
2023-09-03 08:40:25.333 CST [148029] LOG:  listening on Unix socket "/tmp/.s.PGSQL.5433"
2023-09-03 08:40:25.339 CST [148033] LOG:  database system was shut down at 2023-09-03 08:39:17 CST
2023-09-03 08:40:25.343 CST [148029] LOG:  database system is ready to accept connections
~
~

查看后臺進程

當(dāng)然也可以通過linux 系統(tǒng)命令查看后臺服務(wù)進程是否啟動,這需要熟悉后臺進程

[senllang@hatch bin]$ ps -ef|grep postgres|grep -v grep
senllang  148029       1  0 08:40 ?        00:00:00 /opt/postgres/bin/postgres -D testdemo1
senllang  148030  148029  0 08:40 ?        00:00:00 postgres: logger
senllang  148031  148029  0 08:40 ?        00:00:00 postgres: checkpointer
senllang  148032  148029  0 08:40 ?        00:00:00 postgres: background writer
senllang  148034  148029  0 08:40 ?        00:00:00 postgres: walwriter
senllang  148035  148029  0 08:40 ?        00:00:00 postgres: autovacuum launcher
senllang  148036  148029  0 08:40 ?        00:00:00 postgres: logical replication launcher

可以看到,父進程號1對應(yīng)的就是數(shù)據(jù)庫服務(wù)主進程,pid為148029, 其它都是它的六個子進程

查看網(wǎng)絡(luò)服務(wù)

默認配置下,監(jiān)聽IP為 localhost,端口為 5432,協(xié)議采用tcp

[senllang@hatch bin]$ netstat -an |grep tcp|grep 5432
tcp        0      0 127.0.0.1:5432            0.0.0.0:*               LISTEN
tcp6       0      0 ::1:5432                  :::*                    LISTEN

查看網(wǎng)絡(luò),可以看到正處于listen狀態(tài),如果支持IPv6,可以看到tcp6協(xié)議;

八、重新加載配置

在postgresql 的配置中,大致分為需要重啟的配置和不需要重啟服務(wù)的配置;對于不需要重啟服務(wù)的配置,就可以通過重新加載配置參數(shù),讓配置生效;

[senllang@hatch bin]$ ./pg_ctl -D testdemo1 reload
server signaled

可以看到,打印signaled,其它就是給服務(wù)主進程發(fā)送了一個信號,這就是我的專欄 并發(fā)控制中,分享的多任務(wù)間通信的一種。

九、停止數(shù)據(jù)庫服務(wù)

當(dāng)數(shù)據(jù)庫服務(wù)需要停機維護時,我們先要停止數(shù)據(jù)庫服務(wù),使用stop參數(shù)即可;

[senllang@hatch bin]$ ./pg_ctl -D testdemo1 stop
waiting for server to shut down.... done
server stopped

在stop 時,可以用-m 指定采用什么方式停止,可以有三種選項

  • smart 當(dāng)有客戶端連接存在時,會等所有客戶端連接斷開時,才進行停止;
  • fast 快速停止,當(dāng)有客戶端連接時,會讓它們主動斷開,并進行服務(wù)停止;這也是默認的方式;
  • immediate 這個就是立即停止,可能還有一些任務(wù)沒有做完,下次啟動時會觸發(fā)恢復(fù);

十、重啟數(shù)據(jù)庫服務(wù)

當(dāng)服務(wù)啟動或沒有啟動時,都可以采用重啟的方式再次啟動;

[senllang@hatch bin]$ ./pg_ctl -D testdemo1 restart
waiting for server to shut down.... done
server stopped
waiting for server to start....2023-09-03 09:39:15.106 CST [149106] LOG:  redirecting log output to logging collector process
2023-09-03 09:39:15.106 CST [149106] HINT:  Future log output will appear in directory "log".
 done
server started

可以看到,它其實就是先stop,然后再start服務(wù);

十一、總結(jié)

對于數(shù)據(jù)庫服務(wù),一般都是在后臺運行,看到不摸不著,很多時間我們都沒有注意,但當(dāng)我們遇到問題時,又一時不知所措,本文的分享希望能帶給大家一些幫助,有興趣的同學(xué)可以一起來探討,在評論區(qū)留下你的觀點。

結(jié)尾

非常感謝大家的支持,在瀏覽的同時別忘了留下您寶貴的評論,如果覺得值得鼓勵,請點贊,收藏,我會更加努力!

作者郵箱:study@senllang.onaliyun.com
如有錯誤或者疏漏歡迎指出,互相學(xué)習(xí)。

注:未經(jīng)同意,不得轉(zhuǎn)載!文章來源地址http://www.zghlxwxcb.cn/news/detail-693820.html

到了這里,關(guān)于【postgresql 基礎(chǔ)入門】數(shù)據(jù)庫服務(wù)的管理的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實不符,請點擊違法舉報進行投訴反饋,一經(jīng)查實,立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費用

相關(guān)文章

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包