1. 程式人生 > >PostgreSQL+REDIS_FDW詳細記錄踩坑過程(一)

PostgreSQL+REDIS_FDW詳細記錄踩坑過程(一)

案例環境:

虛擬機器:CenterOS 7.0.1406(注意:centerOS自帶postgres9.2的版本安裝包)

PG版本:postgresql-10.5-1-linux-x64-binaries.tar.gz

redis_fwd版本:redis_fdw-REL_10_STABLE.zip

redis版本:redis-4.0.11.tar.gz

Hiredis (這裡最好直接拷貝redis解壓目錄deps下的Hiredis)

PG下載地址:https://www.enterprisedb.com/download-postgresql-binaries

redis_fwd下載地址:

https://github.com/pg-redis-fdw/redis_fdw/branches

redis下載地址:https://redis.io/download

虛擬機器下載地址:http://archive.kernel.org/centos-vault/7.0.1406/isos/x86_64/

安裝虛擬機器以及redis的過程 這裡省略。

安裝PG

       CenterOS是自帶PG的安裝版本的 使用 yum list postgres*可以檢視安裝了包 和可用的包

[[email protected] /]# yum list postgres*
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
 * base: mirrors.cn99.com
 * extras: mirrors.163.com
 * updates: mirrors.163.com
Available Packages
postgresql.i686                                                                                             9.2.24-1.el7_5                                                                                updates
postgresql.x86_64                                                                                           9.2.24-1.el7_5                                                                                updates
postgresql-contrib.x86_64                                                                                   9.2.24-1.el7_5                                                                                updates
postgresql-devel.i686                                                                                       9.2.24-1.el7_5                                                                                updates
postgresql-devel.x86_64                                                                                     9.2.24-1.el7_5                                                                                updates
postgresql-docs.x86_64                                                                                      9.2.24-1.el7_5                                                                                updates
postgresql-jdbc.noarch                                                                                      9.2.1002-6.el7_5                                                                              updates
postgresql-jdbc-javadoc.noarch                                                                              9.2.1002-6.el7_5                                                                              updates
postgresql-libs.i686                                                                                        9.2.24-1.el7_5                                                                                updates
postgresql-libs.x86_64                                                                                      9.2.24-1.el7_5                                                                                updates
postgresql-odbc.x86_64                                                                                      09.03.0100-2.el7                                                                              base   
postgresql-plperl.x86_64                                                                                    9.2.24-1.el7_5                                                                                updates
postgresql-plpython.x86_64                                                                                  9.2.24-1.el7_5                                                                                updates
postgresql-pltcl.x86_64                                                                                     9.2.24-1.el7_5                                                                                updates
postgresql-server.x86_64                                                                                    9.2.24-1.el7_5                                                                                updates
postgresql-static.i686                                                                                      9.2.24-1.el7_5                                                                                updates
postgresql-static.x86_64                                                                                    9.2.24-1.el7_5                                                                                updates
postgresql-test.x86_64                                                                                      9.2.24-1.el7_5                                                                                updates
postgresql-upgrade.x86_64                                                                                   9.2.24-1.el7_5                                                                                updates
[
[email protected]
/]# ^C

      現在開始解壓:

     1.postgresql-10.5-1-linux-x64-binaries.tar.gz (解壓檔案,解壓檔案目錄為pgsql/,目前9.2及以上的都是為免安裝版本的。)

lz圖片都是用pg10來安裝的  (後面會改掉)

[[email protected] admin]# ll
total 144920
drwxr-xr-x. 2 admin admin         6 Sep 19 22:21 Desktop
drwxr-xr-x. 2 admin admin         6 Sep 19 22:21 Documents
drwxr-xr-x. 2 admin admin         6 Sep 19 22:21 Downloads
drwxr-xr-x. 4 root  root       4096 Sep 26 19:52 hiredis
drwxr-xr-x. 2 admin admin         6 Sep 19 22:21 Music
drwxrwxr-x. 9   501   501        97 Aug  8 22:00 pgsql
drwxr-xr-x. 2 admin admin         6 Sep 19 22:21 Pictures
-rw-r--r--. 1 root  root  148357162 Sep 26 19:51 postgresql-10.5-1-linux-x64-binaries.tar.gz
drwxr-xr-x. 2 admin admin         6 Sep 19 22:21 Public
-rw-r--r--. 1 root  root      32028 Sep 26 19:51 redis_fdw-REL_10_STABLE.zip
drwxr-xr-x. 2 admin admin         6 Sep 19 22:21 Templates
drwxr-xr-x. 2 admin admin         6 Sep 19 22:21 Videos
[
[email protected]
admin]#

    2.使用root使用者 su root 

[[email protected] /]# su root

   3.建立新使用者 useradd postgres  設定使用者密碼:passwd postgres

[[email protected] /]# useradd postgres 
[[email protected] /]# passwd postgres
Changing password for user postgres.
New password: 
BAD PASSWORD: The password fails the dictionary check - it is based on a dictionary word
Retype new password: 
passwd: all authentication tokens updated successfully.
[[email protected] /]# 

  4.在根目錄下建立 mkdir /monchickey    繼續建立 mkdir /monchickey/pgsql_data/(pgsql_data 用於初始化目錄)

[[email protected] /]# mkdir /monchickey
[[email protected] /]# mkdir /monchickey/pgsql_data

   4.1 需要把解壓出來的 pgsql移動到monchickey  目錄下 :mv pgsql/  /monchickey   

[[email protected] /]# mv /home/admin/pgsql/ /monchickey/

   5.給postgres使用者賦予對monchickey  的操作許可權:

[[email protected] /]# chown postgres /monchickey/pgsql_data/
[[email protected] /]# chown postgres /monchickey/

  這些都是用root使用者進行操作的。

   6.切換到postgres使用者  su - postgres

[[email protected] /]# su - postgres

    7.執行:export PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin(執行這句話是因為新建立的使用者無論執行什麼系統命令都會報 command not found),所以一定要執行。

[[email protected] ~]$ export PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin

  8.修改postgres使用者的環境變數:vi ~/.bash_profile

                            PGDATA=/monchickey/pgsql_data   初始化目錄   

                            PATH=/monchickey/pgsql 安裝目錄(記得遮蔽原有的PATH屬性) 儲存 esc  + :wq

fi

# User specific environment and startup programs

#PATH=$PATH:$HOME/.local/bin:$HOME/bin
PGDATA=/monchickey/pgsql_data
PATH=/monchickey/pgsql
export PATH
~
~
~
~
~
~
~
~
~
~
~

           9.初始化資料:/monchickey/pgsql/bin/initdb -D /monchickey/pgsql_data/

//在沒有初始化之前這個資料夾下是空的                                                                                                                                                      
[[email protected] ~]$ cd /monchickey/pgsql_data/
[[email protected] pgsql_data]$ ll
total 0
//運行了初始化的程式碼之後就生成了檔案(那麼他是在哪裡指向的呢?)是在我們剛剛為postgres使用者配置環境的時候指向了pgdata
[[email protected] pgsql_data]$ /monchickey/pgsql/bin/initdb -D /monchickey/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 "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 /monchickey/pgsql_data ... ok
creating subdirectories ... ok
selecting default max_connections ... 100
selecting default shared_buffers ... 128MB
selecting dynamic shared memory implementation ... posix
creating configuration files ... ok
running bootstrap script ... ok
performing post-bootstrap initialization ... ok
syncing data to disk ... ok

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:

    /monchickey/pgsql/bin/pg_ctl -D /monchickey/pgsql_data/ -l logfile start

[[email protected] pgsql_data]$ ll
total 48
drwx------. 5 postgres postgres    38 Sep 26 20:06 base
drwx------. 2 postgres postgres  4096 Sep 26 20:06 global
drwx------. 2 postgres postgres     6 Sep 26 20:06 pg_commit_ts
drwx------. 2 postgres postgres     6 Sep 26 20:06 pg_dynshmem
-rw-------. 1 postgres postgres  4513 Sep 26 20:06 pg_hba.conf
-rw-------. 1 postgres postgres  1636 Sep 26 20:06 pg_ident.conf
drwx------. 4 postgres postgres    65 Sep 26 20:06 pg_logical
drwx------. 4 postgres postgres    34 Sep 26 20:06 pg_multixact
drwx------. 2 postgres postgres    17 Sep 26 20:06 pg_notify
drwx------. 2 postgres postgres     6 Sep 26 20:06 pg_replslot
drwx------. 2 postgres postgres     6 Sep 26 20:06 pg_serial
drwx------. 2 postgres postgres     6 Sep 26 20:06 pg_snapshots
drwx------. 2 postgres postgres     6 Sep 26 20:06 pg_stat
drwx------. 2 postgres postgres     6 Sep 26 20:06 pg_stat_tmp
drwx------. 2 postgres postgres    17 Sep 26 20:06 pg_subtrans
drwx------. 2 postgres postgres     6 Sep 26 20:06 pg_tblspc
drwx------. 2 postgres postgres     6 Sep 26 20:06 pg_twophase
-rw-------. 1 postgres postgres     3 Sep 26 20:06 PG_VERSION
drwx------. 3 postgres postgres    58 Sep 26 20:06 pg_wal
drwx------. 2 postgres postgres    17 Sep 26 20:06 pg_xact
-rw-------. 1 postgres postgres    88 Sep 26 20:06 postgresql.auto.conf
-rw-------. 1 postgres postgres 22778 Sep 26 20:06 postgresql.conf
[[email protected] pgsql_data]$

          10:啟動資料庫:/monchickey/pgsql/bin/pg_ctl -D /monchickey/pgsql_data/ -l logfile start

[[email protected] pgsql_data]$ /monchickey/pgsql/bin/pg_ctl -D /monchickey/pgsql_data/ -l logfile start
waiting for server to start.... done
server started
[[email protected] pgsql_data]$ 

           11:檢視資料庫是否啟動成功:ps -ef | grep postgres  或者檢視埠號:lsof -i:5432  或者 netstat -an |grep 5432

[[email protected] pgsql_data]$ ps -ef | grep postgres 
root      12338  12153  0 20:02 pts/1    00:00:00 su - postgres
postgres  12339  12338  0 20:02 pts/1    00:00:00 -bash
root      12408  12384  0 20:03 pts/1    00:00:00 su - postgres
postgres  12409  12408  0 20:03 pts/1    00:00:00 -bash
postgres  12503      1  0 20:08 pts/1    00:00:00 /monchickey/pgsql/bin/postgres -D /monchickey/pgsql_data
postgres  12505  12503  0 20:08 ?        00:00:00 postgres: checkpointer process   
postgres  12506  12503  0 20:08 ?        00:00:00 postgres: writer process   
postgres  12507  12503  0 20:08 ?        00:00:00 postgres: wal writer process   
postgres  12508  12503  0 20:08 ?        00:00:00 postgres: autovacuum launcher process   
postgres  12509  12503  0 20:08 ?        00:00:00 postgres: stats collector process   
postgres  12510  12503  0 20:08 ?        00:00:00 postgres: bgworker: logical replication launcher   
postgres  12519  12409  0 20:09 pts/1    00:00:00 ps -ef
postgres  12520  12409  0 20:09 pts/1    00:00:00 grep --color=auto postgres
[[email protected] pgsql_data]$ lsof -i:5432
COMMAND    PID     USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
postgres 12503 postgres    3u  IPv6  49884      0t0  TCP localhost:postgres (LISTEN)
postgres 12503 postgres    4u  IPv4  49885      0t0  TCP localhost:postgres (LISTEN)

          12:關閉資料庫:/monchickey/pgsql/bin/pg_ctl -D /monchickey/pgsql_data/ stop

[[email protected] pgsql_data]$ /monchickey/pgsql/bin/pg_ctl -D /monchickey/pgsql_data/ stop
waiting for server to shut down.... done
server stopped
[[email protected] pgsql_data]$ 

           13:資料庫安裝成功,但是你使用遠端連線PG資料庫是  連線不成功
                  解決方式:去初始化目錄 pgsql_data

                  修改:vi  pg_hba.conf  在最後新增一行:host    all             all             192.168.0.0/16          trust 並且儲存

                

[[email protected] pgsql_data]$ vi pg_hba.conf 

# TYPE  DATABASE        USER            ADDRESS                 METHOD

# "local" is for Unix domain socket connections only
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
host    all             all             192.168.0.0/16          trust

 

                 修改 vi postgresql.conf      修改   #listen_addresses = 'localhost'  為 listen_addresses = '*' 在這裡加入星號允許訪問  並且要去掉註釋哦!!

[[email protected] pgsql_data]$ vi postgresql.conf 
#listen_addresses = 'localhost'         # what IP address(es) to listen on;
                                        # comma-separated list of addresses;
                                        # defaults to 'localhost'; use '*' for all
                                        # (change requires restart)
#port = 5432                            # (change requires restart)
max_connections = 100                   # (change requires restart)
#superuser_reserved_connections = 3     # (change requires restart)
#unix_socket_directories = '/tmp'       # comma-separated list of directories
                                        # (change requires restart)
#unix_socket_group = ''                 # (change requires restart)
#unix_socket_permissions = 0777         # begin with 0 to use octal notation
                                        # (change requires restart)
#bonjour = off                          # advertise server via Bonjour
                                        # (change requires restart)
#bonjour_name = ''                      # defaults to the computer name
                                        # (change requires restart)

 修改為


# - Connection Settings -

listen_addresses = '*'          # what IP address(es) to listen on;
                                        # comma-separated list of addresses;
                                        # defaults to 'localhost'; use '*' for all
                                        # (change requires restart)
#port = 5432                            # (change requires restart)
max_connections = 100                   # (change requires restart)
#superuser_reserved_connections = 3     # (change requires restart)
#unix_socket_directories = '/tmp'       # comma-separated list of directories
                                        # (change requires restart)
#unix_socket_group = ''                 # (change requires restart)
#unix_socket_permissions = 0777         # begin with 0 to use octal notation
                                        # (change requires restart)
#bonjour = off                          # advertise server via Bonjour
                                        # (change requires restart)
#bonjour_name = ''                      # defaults to the computer name
                                        # (change requires restart)

           14.重啟postgres服務 /monchickey/pgsql/bin/pg_ctl -D /monchickey/pgsql_data/ -l logfile start

[[email protected] pgsql_data]$  /monchickey/pgsql/bin/pg_ctl -D /monchickey/pgsql_data/ -l logfile start
waiting for server to start.... done
server started

           15.防火牆開啟埠 這裡要切換為root使用者哦 sudo firewall-cmd --permanent --zone=public --add-port=5432/tcp
               sudo firewall-cmd --reload

[[email protected] pgsql_data]$ su 
Password: 
[[email protected] pgsql_data]# sudo firewall-cmd --permanent --zone=public --add-port=5432/tcp
success
[[email protected] pgsql_data]#   sudo firewall-cmd --reload
success

參考:https://www.cnblogs.com/freeweb/p/8006639.html