install percona-toolkit on MySQL 5.7 (CentOS7)

[root@node1 ~]# yum install -y http://www.percona.com/downloads/percona-release/redhat/0.1-6/percona-release-0.1-6.noarch.rpm ★
読み込んだプラグイン:fastestmirror, langpacks
percona-release-0.1-6.noarch.rpm | 14 kB 00:00:00
/var/tmp/yum-root-8JTwM3/percona-release-0.1-6.noarch.rpm を調べています: percona-release-0.1-6.noarch
/var/tmp/yum-root-8JTwM3/percona-release-0.1-6.noarch.rpm をインストール済みとして設定しています
依存性の解決をしています
--> トランザクションの確認を実行しています。
---> パッケージ percona-release.noarch 0:0.1-6 を インストール
--> 依存性解決を終了しました。
base/7/x86_64 | 3.6 kB 00:00:00
extras/7/x86_64 | 3.4 kB 00:00:00
extras/7/x86_64/primary_db | 205 kB 00:00:00
ol7_UEKR5/x86_64 | 2.5 kB 00:00:00
ol7_UEKR5/x86_64/updateinfo | 33 kB 00:00:00
ol7_UEKR5/x86_64/primary_db | 3.9 MB 00:00:00
pgdg10/7/x86_64 | 3.6 kB 00:00:00
pgdg10/7/x86_64/primary_db | 286 kB 00:00:00
pgdg11/7/x86_64 | 3.6 kB 00:00:00
pgdg11/7/x86_64/primary_db | 231 kB 00:00:00
pgdg94/7/x86_64 | 3.6 kB 00:00:00
pgdg94/7/x86_64/primary_db | 307 kB 00:00:00
pgdg95/7/x86_64 | 3.6 kB 00:00:00
pgdg95/7/x86_64/primary_db | 302 kB 00:00:00
pgdg96/7/x86_64 | 3.6 kB 00:00:00
pgdg96/7/x86_64/primary_db | 303 kB 00:00:00
updates/7/x86_64 | 3.4 kB 00:00:00
updates/7/x86_64/primary_db | 6.5 MB 00:00:00

依存性を解決しました

==========================================================================================================================================================================================================================================
Package アーキテクチャー バージョン リポジトリー 容量
==========================================================================================================================================================================================================================================
インストール中:
percona-release noarch 0.1-6 /percona-release-0.1-6.noarch 16 k

トランザクションの要約
==========================================================================================================================================================================================================================================
インストール 1 パッケージ

合計容量: 16 k
インストール容量: 16 k
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
警告: RPMDB は yum 以外で変更されました。
インストール中 : percona-release-0.1-6.noarch 1/1
検証中 : percona-release-0.1-6.noarch 1/1

インストール:
percona-release.noarch 0:0.1-6

完了しました!
[root@node1 ~]# yum install -y percona-toolkit ★
読み込んだプラグイン:fastestmirror, langpacks
percona-release-noarch | 2.9 kB 00:00:00
percona-release-x86_64 | 2.9 kB 00:00:00
(1/2): percona-release-noarch/7/primary_db | 21 kB 00:00:00
(2/2): percona-release-x86_64/7/x86_64/primary_db | 971 kB 00:00:01
Determining fastest mirrors
* base: ftp.riken.jp
* extras: ftp.riken.jp
* updates: ftp.riken.jp
依存性の解決をしています
--> トランザクションの確認を実行しています。
---> パッケージ percona-toolkit.x86_64 0:3.0.13-1.el7 を インストール
--> 依存性の処理をしています: perl(DBD::mysql) >= 1.0 のパッケージ: percona-toolkit-3.0.13-1.el7.x86_64
--> 依存性の処理をしています: perl(Digest::MD5) のパッケージ: percona-toolkit-3.0.13-1.el7.x86_64
--> 依存性の処理をしています: perl(IO::Socket::SSL) のパッケージ: percona-toolkit-3.0.13-1.el7.x86_64
--> トランザクションの確認を実行しています。
---> パッケージ perl-DBD-MySQL.x86_64 0:4.023-6.el7 を インストール
--> 依存性の処理をしています: libmysqlclient.so.18(libmysqlclient_18)(64bit) のパッケージ: perl-DBD-MySQL-4.023-6.el7.x86_64
--> 依存性の処理をしています: libmysqlclient.so.18()(64bit) のパッケージ: perl-DBD-MySQL-4.023-6.el7.x86_64
---> パッケージ perl-Digest-MD5.x86_64 0:2.52-3.el7 を インストール
--> 依存性の処理をしています: perl(Digest::base) >= 1.00 のパッケージ: perl-Digest-MD5-2.52-3.el7.x86_64
---> パッケージ perl-IO-Socket-SSL.noarch 0:1.94-7.el7 を インストール
--> 依存性の処理をしています: perl-Net-SSLeay >= 1.55-5 のパッケージ: perl-IO-Socket-SSL-1.94-7.el7.noarch
--> 依存性の処理をしています: perl(Net::SSLeay) >= 1.21 のパッケージ: perl-IO-Socket-SSL-1.94-7.el7.noarch
--> 依存性の処理をしています: perl(IO::Socket::IP) >= 0.20 のパッケージ: perl-IO-Socket-SSL-1.94-7.el7.noarch
--> 依存性の処理をしています: perl(Net::SSLeay) のパッケージ: perl-IO-Socket-SSL-1.94-7.el7.noarch
--> 依存性の処理をしています: perl(Net::LibIDN) のパッケージ: perl-IO-Socket-SSL-1.94-7.el7.noarch
--> 依存性の処理をしています: perl(Mozilla::CA) のパッケージ: perl-IO-Socket-SSL-1.94-7.el7.noarch
--> トランザクションの確認を実行しています。
---> パッケージ Percona-Server-shared-56.x86_64 0:5.6.44-rel86.0.el7 を インストール
---> パッケージ perl-Digest.noarch 0:1.17-245.el7 を インストール
---> パッケージ perl-IO-Socket-IP.noarch 0:0.21-5.el7 を インストール
---> パッケージ perl-Mozilla-CA.noarch 0:20130114-5.el7 を インストール
---> パッケージ perl-Net-LibIDN.x86_64 0:0.12-15.el7 を インストール
---> パッケージ perl-Net-SSLeay.x86_64 0:1.55-6.el7 を インストール
--> 依存性解決を終了しました。

依存性を解決しました

==========================================================================================================================================================================================================================================
Package アーキテクチャー バージョン リポジトリー 容量
==========================================================================================================================================================================================================================================
インストール中:
percona-toolkit x86_64 3.0.13-1.el7 percona-release-x86_64 7.4 M
依存性関連でのインストールをします:
Percona-Server-shared-56 x86_64 5.6.44-rel86.0.el7 percona-release-x86_64 619 k
perl-DBD-MySQL x86_64 4.023-6.el7 base 140 k
perl-Digest noarch 1.17-245.el7 base 23 k
perl-Digest-MD5 x86_64 2.52-3.el7 base 30 k
perl-IO-Socket-IP noarch 0.21-5.el7 base 36 k
perl-IO-Socket-SSL noarch 1.94-7.el7 base 115 k
perl-Mozilla-CA noarch 20130114-5.el7 base 11 k
perl-Net-LibIDN x86_64 0.12-15.el7 base 28 k
perl-Net-SSLeay x86_64 1.55-6.el7 base 285 k

トランザクションの要約
==========================================================================================================================================================================================================================================
インストール 1 パッケージ (+9 個の依存関係のパッケージ)

総ダウンロード容量: 8.7 M
インストール容量: 9.7 M
Downloading packages:
(1/10): perl-Digest-1.17-245.el7.noarch.rpm | 23 kB 00:00:00
(2/10): perl-DBD-MySQL-4.023-6.el7.x86_64.rpm | 140 kB 00:00:00
(3/10): perl-Digest-MD5-2.52-3.el7.x86_64.rpm | 30 kB 00:00:00
(4/10): perl-IO-Socket-IP-0.21-5.el7.noarch.rpm | 36 kB 00:00:00
(5/10): perl-Mozilla-CA-20130114-5.el7.noarch.rpm | 11 kB 00:00:00
(6/10): perl-IO-Socket-SSL-1.94-7.el7.noarch.rpm | 115 kB 00:00:00
(7/10): perl-Net-LibIDN-0.12-15.el7.x86_64.rpm | 28 kB 00:00:00
(8/10): perl-Net-SSLeay-1.55-6.el7.x86_64.rpm | 285 kB 00:00:00
warning: /var/cache/yum/x86_64/7/percona-release-x86_64/packages/Percona-Server-shared-56-5.6.44-rel86.0.el7.x86_64.rpm: Header V4 RSA/SHA256 Signature, key ID 8507efa5: NOKEY ] 290 kB/s | 1.4 MB 00:00:25 ETA
Percona-Server-shared-56-5.6.44-rel86.0.el7.x86_64.rpm の公開鍵がインストールされていません
(9/10): Percona-Server-shared-56-5.6.44-rel86.0.el7.x86_64.rpm | 619 kB 00:00:01
(10/10): percona-toolkit-3.0.13-1.el7.x86_64.rpm | 7.4 MB 00:00:02
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
合計 3.2 MB/s | 8.7 MB 00:00:02
file:///etc/pki/rpm-gpg/RPM-GPG-KEY-Percona から鍵を取得中です。


The GPG keys listed for the "Percona-Release YUM repository - x86_64" repository are already installed but they are not correct for this package. ★失敗した
Check that the correct key URLs are configured for this repository.


Failing package is: Percona-Server-shared-56-5.6.44-rel86.0.el7.x86_64 ★失敗の原因としては、このパッケージです。
GPG Keys are configured as: file:///etc/pki/rpm-gpg/RPM-GPG-KEY-Percona

[root@node1 ~]
[root@node1 ~]# yum install Percona-Server-shared-compat-57 --nogpgcheck ★失敗のパッケージだけをまずインストールしてみる
読み込んだプラグイン:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
* base: ftp.iij.ad.jp
* extras: ftp.iij.ad.jp
* updates: ftp.iij.ad.jp
依存性の解決をしています
--> トランザクションの確認を実行しています。
---> パッケージ Percona-Server-shared-compat-57.x86_64 0:5.7.26-29.1.el7 を インストール
--> 依存性解決を終了しました。

依存性を解決しました

==========================================================================================================================================================================================================================================
Package アーキテクチャー バージョン リポジトリー 容量
==========================================================================================================================================================================================================================================
インストール中:
Percona-Server-shared-compat-57 x86_64 5.7.26-29.1.el7 percona-release-x86_64 1.2 M

トランザクションの要約
==========================================================================================================================================================================================================================================
インストール 1 パッケージ

総ダウンロード容量: 1.2 M
インストール容量: 1.2 M
Is this ok [y/d/N]: y
Downloading packages:
Percona-Server-shared-compat-57-5.7.26-29.1.el7.x86_64.rpm | 1.2 MB 00:00:02
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
インストール中 : Percona-Server-shared-compat-57-5.7.26-29.1.el7.x86_64 1/1
検証中 : Percona-Server-shared-compat-57-5.7.26-29.1.el7.x86_64 1/1

インストール:
Percona-Server-shared-compat-57.x86_64 0:5.7.26-29.1.el7

完了しました! ★成功した
[root@node1 ~]# yum install -y percona-toolkit --nogpgcheck ★ツールを再度インストールしてみる
読み込んだプラグイン:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
* base: ftp.iij.ad.jp
* extras: ftp.iij.ad.jp
* updates: ftp.iij.ad.jp
依存性の解決をしています
--> トランザクションの確認を実行しています。
---> パッケージ percona-toolkit.x86_64 0:3.0.13-1.el7 を インストール
--> 依存性の処理をしています: perl(DBD::mysql) >= 1.0 のパッケージ: percona-toolkit-3.0.13-1.el7.x86_64
--> 依存性の処理をしています: perl(Digest::MD5) のパッケージ: percona-toolkit-3.0.13-1.el7.x86_64
--> 依存性の処理をしています: perl(IO::Socket::SSL) のパッケージ: percona-toolkit-3.0.13-1.el7.x86_64
--> トランザクションの確認を実行しています。
---> パッケージ perl-DBD-MySQL.x86_64 0:4.023-6.el7 を インストール
---> パッケージ perl-Digest-MD5.x86_64 0:2.52-3.el7 を インストール
--> 依存性の処理をしています: perl(Digest::base) >= 1.00 のパッケージ: perl-Digest-MD5-2.52-3.el7.x86_64
---> パッケージ perl-IO-Socket-SSL.noarch 0:1.94-7.el7 を インストール
--> 依存性の処理をしています: perl-Net-SSLeay >= 1.55-5 のパッケージ: perl-IO-Socket-SSL-1.94-7.el7.noarch
--> 依存性の処理をしています: perl(Net::SSLeay) >= 1.21 のパッケージ: perl-IO-Socket-SSL-1.94-7.el7.noarch
--> 依存性の処理をしています: perl(IO::Socket::IP) >= 0.20 のパッケージ: perl-IO-Socket-SSL-1.94-7.el7.noarch
--> 依存性の処理をしています: perl(Net::SSLeay) のパッケージ: perl-IO-Socket-SSL-1.94-7.el7.noarch
--> 依存性の処理をしています: perl(Net::LibIDN) のパッケージ: perl-IO-Socket-SSL-1.94-7.el7.noarch
--> 依存性の処理をしています: perl(Mozilla::CA) のパッケージ: perl-IO-Socket-SSL-1.94-7.el7.noarch
--> トランザクションの確認を実行しています。
---> パッケージ perl-Digest.noarch 0:1.17-245.el7 を インストール
---> パッケージ perl-IO-Socket-IP.noarch 0:0.21-5.el7 を インストール
---> パッケージ perl-Mozilla-CA.noarch 0:20130114-5.el7 を インストール
---> パッケージ perl-Net-LibIDN.x86_64 0:0.12-15.el7 を インストール
---> パッケージ perl-Net-SSLeay.x86_64 0:1.55-6.el7 を インストール
--> 依存性解決を終了しました。

依存性を解決しました

==========================================================================================================================================================================================================================================
Package アーキテクチャー バージョン リポジトリー 容量
==========================================================================================================================================================================================================================================
インストール中:
percona-toolkit x86_64 3.0.13-1.el7 percona-release-x86_64 7.4 M
依存性関連でのインストールをします:
perl-DBD-MySQL x86_64 4.023-6.el7 base 140 k
perl-Digest noarch 1.17-245.el7 base 23 k
perl-Digest-MD5 x86_64 2.52-3.el7 base 30 k
perl-IO-Socket-IP noarch 0.21-5.el7 base 36 k
perl-IO-Socket-SSL noarch 1.94-7.el7 base 115 k
perl-Mozilla-CA noarch 20130114-5.el7 base 11 k
perl-Net-LibIDN x86_64 0.12-15.el7 base 28 k
perl-Net-SSLeay x86_64 1.55-6.el7 base 285 k

トランザクションの要約
==========================================================================================================================================================================================================================================
インストール 1 パッケージ (+8 個の依存関係のパッケージ)

合計容量: 8.1 M
インストール容量: 9.1 M
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
インストール中 : perl-Mozilla-CA-20130114-5.el7.noarch 1/9
インストール中 : perl-Digest-1.17-245.el7.noarch 2/9
インストール中 : perl-Digest-MD5-2.52-3.el7.x86_64 3/9
インストール中 : perl-Net-LibIDN-0.12-15.el7.x86_64 4/9
インストール中 : perl-Net-SSLeay-1.55-6.el7.x86_64 5/9
インストール中 : perl-DBD-MySQL-4.023-6.el7.x86_64 6/9
インストール中 : perl-IO-Socket-IP-0.21-5.el7.noarch 7/9
インストール中 : perl-IO-Socket-SSL-1.94-7.el7.noarch 8/9
インストール中 : percona-toolkit-3.0.13-1.el7.x86_64 9/9
検証中 : perl-Digest-MD5-2.52-3.el7.x86_64 1/9
検証中 : perl-IO-Socket-IP-0.21-5.el7.noarch 2/9
検証中 : percona-toolkit-3.0.13-1.el7.x86_64 3/9
検証中 : perl-DBD-MySQL-4.023-6.el7.x86_64 4/9
検証中 : perl-Net-SSLeay-1.55-6.el7.x86_64 5/9
検証中 : perl-Net-LibIDN-0.12-15.el7.x86_64 6/9
検証中 : perl-Digest-1.17-245.el7.noarch 7/9
検証中 : perl-IO-Socket-SSL-1.94-7.el7.noarch 8/9
検証中 : perl-Mozilla-CA-20130114-5.el7.noarch 9/9

インストール:
percona-toolkit.x86_64 0:3.0.13-1.el7

依存性関連をインストールしました:
perl-DBD-MySQL.x86_64 0:4.023-6.el7 perl-Digest.noarch 0:1.17-245.el7 perl-Digest-MD5.x86_64 0:2.52-3.el7 perl-IO-Socket-IP.noarch 0:0.21-5.el7 perl-IO-Socket-SSL.noarch 0:1.94-7.el7 perl-Mozilla-CA.noarch 0:20130114-5.el7
perl-Net-LibIDN.x86_64 0:0.12-15.el7 perl-Net-SSLeay.x86_64 0:1.55-6.el7

完了しました! ★今度は成功した。
[root@node1 ~]#

 

About Slow query of MySQL 5.7

[root@node1 ~]# mysql --auto-rehash -u root -p'RootRoot!1' test_db ★接続
mysql: [Warning] Using a password on the command line interface can be insecure.
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.26 MySQL Community Server (GPL) ★5.7.26

Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> \! ls -l; ★OSコマンドを実行してみる
合計 16
-rw-r--r--. 1 root root 2065 7月 22 16:32 addressbook.sql
-rw-------. 1 root root 1886 1月 9 2019 anaconda-ks.cfg
-rw-r--r--. 1 root root 1917 1月 9 2019 initial-setup-ks.cfg
drwxr-xr-x. 2 root root 4096 6月 19 14:25 mysql
drwxr-xr-x. 2 root root 20 7月 17 11:17 test
drwxr-xr-x. 2 root root 6 1月 9 2019 ダウンロード
drwxr-xr-x. 2 root root 6 1月 9 2019 テンプレート
drwxr-xr-x. 2 root root 6 1月 9 2019 デスクトップ
drwxr-xr-x. 2 root root 6 1月 9 2019 ドキュメント
drwxr-xr-x. 2 root root 6 1月 9 2019 ビデオ
drwxr-xr-x. 2 root root 6 1月 9 2019 音楽
drwxr-xr-x. 2 root root 6 1月 9 2019 画像
drwxr-xr-x. 2 root root 6 1月 9 2019 公開
mysql> show variables like '%secur%'; ★secure_file_privパラメータを確認する
+--------------------------+-----------------------+
| Variable_name | Value |
+--------------------------+-----------------------+
| require_secure_transport | OFF |
| secure_auth | ON |
| secure_file_priv | /var/lib/mysql-files/ |
+--------------------------+-----------------------+
3 rows in set (0.01 sec)

mysql> show variables like '%quer%'; ★スロークエリに相関するパラメータを確認する
+----------------------------------------+-------------------------------+
| Variable_name | Value |
+----------------------------------------+-------------------------------+
| binlog_rows_query_log_events | OFF |
| ft_query_expansion_limit | 20 |
| have_query_cache | YES |
| log_queries_not_using_indexes | OFF | ★
| log_throttle_queries_not_using_indexes | 0 |
| long_query_time | 10.000000 | ★
| query_alloc_block_size | 8192 |
| query_cache_limit | 1048576 |
| query_cache_min_res_unit | 4096 |
| query_cache_size | 1048576 |
| query_cache_type | OFF |
| query_cache_wlock_invalidate | OFF |
| query_prealloc_size | 8192 |
| slow_query_log | OFF | ★
| slow_query_log_file | /var/lib/mysql/node1-slow.log | ★
+----------------------------------------+-------------------------------+
15 rows in set (0.01 sec)

mysql> set global slow_query_log=1; ★
Query OK, 0 rows affected (0.00 sec)

mysql> set global long_query_time=0.1; ★
Query OK, 0 rows affected (0.00 sec)

mysql> set global log_queries_not_using_indexes=1; ★
Query OK, 0 rows affected (0.00 sec)

mysql> set global slow_query_log_file ='/usr/local/var/mysql/slow_query.log'; ★slow_query_log_fileパラメータをセットする際にエラーになった
ERROR 1231 (42000): Variable 'slow_query_log_file' can't be set to the value of '/usr/local/var/mysql/slow_query.log'
mysql> \! ls -ld /usr/local/var/mysql/
drwxr-xr-x. 2 mysql mysql 6 7月 23 11:23 /usr/local/var/mysql/
mysql> \! touch /usr/local/var/mysql/slow_query.log ★ファイルを作成する
mysql> \! ls -l /usr/local/var/mysql/
合計 0
-rw-r--r--. 1 root root 0 7月 23 11:47 slow_query.log
mysql> \! chown mysql:mysql /usr/local/var/mysql/slow_query.log ★
mysql> \! chmod 755 /usr/local/var/mysql/slow_query.log ★十分の権限を与える
mysql> \! ls -l /usr/local/var/mysql/
合計 0
-rwxr-xr-x. 1 mysql mysql 0 7月 23 11:47 slow_query.log
mysql> set global slow_query_log_file ='/usr/local/var/mysql/slow_query.log';
ERROR 29 (HY000): File '/usr/local/var/mysql/slow_query.log' not found (Errcode: 13 - Permission denied) ★それでもエラー
mysql> show variables like '%secur%';
+--------------------------+-----------------------+
| Variable_name | Value |
+--------------------------+-----------------------+
| require_secure_transport | OFF |
| secure_auth | ON |
| secure_file_priv | /var/lib/mysql-files/ |
+--------------------------+-----------------------+
3 rows in set (0.01 sec)

mysql> \! ls -l /var/lib/mysql-files/
合計 18512
-rwxr-xr-x. 1 mysql mysql 18951360 7月 23 10:29 KEN_ALL_UTF8.CSV
-rw-r-----. 1 mysql mysql 179 7月 23 11:39 slow_query.log
mysql> \! rm -rf /var/lib/mysql-files/slow_query.log
mysql> set global slow_query_log_file ='/var/lib/mysql-files/slow_query.log'; ★secure_file_priv配下へ変更することで、設定可能になる
Query OK, 0 rows affected (0.00 sec)

mysql> exit
Bye
[root@node1 ~]# mysql --auto-rehash -u root -p'RootRoot!1' test_db ★再度ログイン(そうでもしないと、long_query_timeが変更されたかを確認することはできません。)
mysql: [Warning] Using a password on the command line interface can be insecure.
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.26 MySQL Community Server (GPL)

Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show variables like '%quer%';
+----------------------------------------+-------------------------------------+
| Variable_name | Value |
+----------------------------------------+-------------------------------------+
| binlog_rows_query_log_events | OFF |
| ft_query_expansion_limit | 20 |
| have_query_cache | YES |
| log_queries_not_using_indexes | ON | ★OK
| log_throttle_queries_not_using_indexes | 0 |
| long_query_time | 0.100000 | ★OK
| query_alloc_block_size | 8192 |
| query_cache_limit | 1048576 |
| query_cache_min_res_unit | 4096 |
| query_cache_size | 1048576 |
| query_cache_type | OFF |
| query_cache_wlock_invalidate | OFF |
| query_prealloc_size | 8192 |
| slow_query_log | OFF | ★NG
| slow_query_log_file | /var/lib/mysql-files/slow_query.log | ★OK
+----------------------------------------+-------------------------------------+
15 rows in set (0.01 sec)

mysql> set global slow_query_log=1; ★もう一度、セット
Query OK, 0 rows affected (0.00 sec)

mysql> show variables like '%quer%';
+----------------------------------------+-------------------------------------+
| Variable_name | Value |
+----------------------------------------+-------------------------------------+
| binlog_rows_query_log_events | OFF |
| ft_query_expansion_limit | 20 |
| have_query_cache | YES |
| log_queries_not_using_indexes | ON |
| log_throttle_queries_not_using_indexes | 0 |
| long_query_time | 0.100000 |
| query_alloc_block_size | 8192 |
| query_cache_limit | 1048576 |
| query_cache_min_res_unit | 4096 |
| query_cache_size | 1048576 |
| query_cache_type | OFF |
| query_cache_wlock_invalidate | OFF |
| query_prealloc_size | 8192 |
| slow_query_log | ON | ★OKになった
| slow_query_log_file | /var/lib/mysql-files/slow_query.log |
+----------------------------------------+-------------------------------------+
15 rows in set (0.00 sec)

mysql> show global status like '%slow%'; ★スロークエリの統計を確認する
+---------------------+-------+
| Variable_name | Value |
+---------------------+-------+
| Slow_launch_threads | 0 |
| Slow_queries | 0 | ★まだゼロ
+---------------------+-------+
2 rows in set (0.00 sec)

mysql> SELECT COUNT(*) FROM zipcode WHERE city='渋谷区' AND prefecture='東京都'; ★索引が存在しないテーブルzipcodeでクエリを実行する
+----------+
| COUNT(*) |
+----------+
| 73 |
+----------+
1 row in set (0.27 sec) ★

mysql> show global status like '%slow%';
+---------------------+-------+
| Variable_name | Value |
+---------------------+-------+
| Slow_launch_threads | 0 |
| Slow_queries | 1 | ★
+---------------------+-------+
2 rows in set (0.01 sec)

mysql> \! cat /var/lib/mysql-files/slow_query.log
/usr/sbin/mysqld, Version: 5.7.26 (MySQL Community Server (GPL)). started with:
Tcp port: 3306 Unix socket: /var/lib/mysql/mysql.sock
Time Id Command Argument
# Time: 2019-07-23T02:50:13.178454Z
# User@Host: root[root] @ localhost Id: 4
# Query_time: 0.268895 Lock_time: 0.000324 Rows_sent: 1 Rows_examined: 124271
use test_db;
SET timestamp=1563850213;
SELECT COUNT(*) FROM zipcode WHERE city='渋谷区' AND prefecture='東京都';
mysql> show create table addressbook \G ★もっと試してみる
*************************** 1. row ***************************
Table: addressbook ★テストテーブルでは
Create Table: CREATE TABLE `addressbook` (
`id` int(3) NOT NULL,
`name` varchar(20) DEFAULT NULL,
`address` varchar(20) DEFAULT NULL,
`telephone` varchar(13) DEFAULT NULL,
PRIMARY KEY (`id`) ★索引がある
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.00 sec)
mysql> select count(name) from addressbook; ★索引を使わないクエリであれば
+-------------+
| count(name) |
+-------------+
| 27 |
+-------------+
1 row in set (0.00 sec) ★0.1を超えていない

mysql> show global status like '%slow%';
+---------------------+-------+
| Variable_name | Value |
+---------------------+-------+
| Slow_launch_threads | 0 |
| Slow_queries | 2 | ★カウントされる
+---------------------+-------+
2 rows in set (0.01 sec)

mysql> \! cat /var/lib/mysql-files/slow_query.log
/usr/sbin/mysqld, Version: 5.7.26 (MySQL Community Server (GPL)). started with:
Tcp port: 3306 Unix socket: /var/lib/mysql/mysql.sock
Time Id Command Argument
# Time: 2019-07-23T02:50:13.178454Z
# User@Host: root[root] @ localhost Id: 4
# Query_time: 0.268895 Lock_time: 0.000324 Rows_sent: 1 Rows_examined: 124271
use test_db;
SET timestamp=1563850213;
SELECT COUNT(*) FROM zipcode WHERE city='渋谷区' AND prefecture='東京都';
# Time: 2019-07-23T02:56:52.763902Z
# User@Host: root[root] @ localhost [] Id: 4
# Query_time: 0.002047 Lock_time: 0.000297 Rows_sent: 1 Rows_examined: 27
SET timestamp=1563850612;
select count(name) from addressbook; ★
mysql> select count(id) from addressbook; ★索引を使うクエリであれば
+-----------+
| count(id) |
+-----------+
| 27 |
+-----------+
1 row in set (0.00 sec) ★0.1を超えていない

mysql> show global status like '%slow%';
+---------------------+-------+
| Variable_name | Value |
+---------------------+-------+
| Slow_launch_threads | 0 |
| Slow_queries | 2 | ★カウントされない
+---------------------+-------+
2 rows in set (0.00 sec)

 

 

pgpool-II導入してみた・その3(負荷分散・レプリケーション)

※DBサーバ(primary:node1,standby:node2)のprimaryが独立、standbyのDBサーバとwebサーバ(pgpool)が同梱であるサンプル
①pgpoolとpostgresのインストールは同じであるため、割愛する
②webサーバ(pgpool)を設定する
[root@node2 pgpool-II]# egrep -i 'backend_|connection_cache|num_init_children|max_pool|enable_pool_hba|memory_cache_enabled|listen_addresses|client_min_messages|log_min_messages|sr_check_' pgpool.conf ★
listen_addresses = '*' ★触った
...
backend_hostname0 = '192.168.56.101' ★
backend_port0 = 5432 ★
backend_weight0 = 1 ★
backend_data_directory0 = '/var/lib/pgsql/11/data' ★
backend_flag0 = 'ALLOW_TO_FAILOVER' ★
backend_hostname1 = '192.168.56.102' ★
backend_port1 = 5432 ★
backend_weight1 = 1 ★
backend_data_directory1 = '/var/lib/pgsql/11/data' ★
backend_flag1 = 'ALLOW_TO_FAILOVER' ★
enable_pool_hba = on ★
num_init_children = 30 ★
max_pool = 2 ★
client_min_messages = log # values in order of decreasing detail: ★
log_min_messages = info # values in order of decreasing detail: ★
connection_cache = on ★
sr_check_period = 10 ★
sr_check_user = 'postgres' ★
...
sr_check_password = 'postgres' ★
sr_check_database = 'postgres' ★
[root@node2 pgpool-II]# cat pool_hba.conf
# pgpool Client Authentication Configuration File
# ===============================================
#
# The format rule in this file follows the rules in the PostgreSQL
# Administrator's Guide. Refer to chapter "Client Authentication" for a
...
# "local" is for Unix domain socket connections only
local all all trust
# IPv4 local connections:
host all all 127.0.0.1/32 trust
host all all ::1/128 trust
host postgres postgres 192.168.56.0/24 md5 ★追加したエントリはこれ
[root@node2 pgpool-II]# cat pool_passwd ★パスワードファイルも作成しておく


postgres:md53175bce1d3201d16594cebf9d7eb3f9d
[root@node2 pgpool-II]# pgpool -n -d > /var/log/pgpool/pgpool.log 2>&1 & ★pgpoolを起動させる
[1] 11414
[root@node2 pgpool-II]# ps -ef | egrep -i 'pgpool|postgres'
root 3372 3240 0 14:49 pts/1 00:00:00 su - postgres
postgres 3373 3372 0 14:49 pts/1 00:00:00 -bash
postgres 6746 1 0 15:30 pts/1 00:00:00 /usr/pgsql-11/bin/postgres
postgres 6747 6746 0 15:30 ? 00:00:00 postgres: pg11: logger
postgres 6748 6746 0 15:30 ? 00:00:00 postgres: pg11: startup recovering 000000010000000000000018
postgres 6749 6746 0 15:30 ? 00:00:00 postgres: pg11: checkpointer
postgres 6750 6746 0 15:30 ? 00:00:00 postgres: pg11: background writer
postgres 6751 6746 0 15:30 ? 00:00:00 postgres: pg11: stats collector
postgres 6752 6746 0 15:30 ? 00:00:05 postgres: pg11: walreceiver streaming 0/18C81B68
root 11414 3178 0 16:26 pts/0 00:00:00 pgpool -n -d
root 11415 11414 0 16:26 pts/0 00:00:00 pgpool: wait for connection request
...
root 11444 11414 0 16:26 pts/0 00:00:00 pgpool: wait for connection request
root 11446 11414 0 16:26 pts/0 00:00:00 pgpool: PCP: wait for connection request
root 11447 11414 0 16:26 pts/0 00:00:00 pgpool: worker process
root 11448 11414 0 16:26 pts/0 00:00:00 pgpool: health check process(0)
root 11449 11414 0 16:26 pts/0 00:00:00 pgpool: health check process(1)
root 11459 3178 0 16:26 pts/0 00:00:00 grep -E --color=auto -i pgpool|postgres
③クライアント側からpgpool経由でDBサーバへ接続する

[root@node2 ~]# psql -d postgres -h 192.168.56.102 -p 9999 -U postgres ★rootユーザでも接続できるぞ!
ユーザ postgres のパスワード: ★ここでパスワードを入力する必要がありますが
psql (11.3)
"help" でヘルプを表示します。

postgres=#
postgres=# show pool_nodes; ★
node_id | hostname | port | status | lb_weight | role | select_cnt | load_balance_node | replication_delay | last_status_change
---------+----------------+------+--------+-----------+---------+------------+-------------------+-------------------+---------------------
0 | 192.168.56.101 | 5432 | up | 0.500000 | primary | 0 | true | 0 | 2019-06-12 15:41:23
1 | 192.168.56.102 | 5432 | up | 0.500000 | standby | 0 | false | 0 | 2019-06-12 15:41:23
(2 行)


[root@node2 ~]# psql -d postgres -h 192.168.56.102 -p 9999 -U postgres ★二番目の接続も立ち上げる
ユーザ postgres のパスワード:
psql (11.3)
"help" でヘルプを表示します。

postgres=#


④DBサーバで接続バックエンドプロセスを確認する
※プライマリ側
-bash-4.2$ date;ps -ef | grep postgres;date
2019年 6月 12日 水曜日 16:41:08 JST
root 3268 3213 0 14:49 pts/0 00:00:00 su - postgres
postgres 3269 3268 0 14:49 pts/0 00:00:00 -bash
postgres 6276 1 0 15:30 pts/0 00:00:01 /usr/pgsql-11/bin/postgres
postgres 6278 6276 0 15:30 ? 00:00:00 postgres: pg11: logger
postgres 6280 6276 0 15:30 ? 00:00:00 postgres: pg11: checkpointer
postgres 6281 6276 0 15:30 ? 00:00:00 postgres: pg11: background writer
postgres 6282 6276 0 15:30 ? 00:00:00 postgres: pg11: walwriter
postgres 6283 6276 0 15:30 ? 00:00:00 postgres: pg11: autovacuum launcher
postgres 6284 6276 0 15:30 ? 00:00:00 postgres: pg11: archiver
postgres 6285 6276 0 15:30 ? 00:00:00 postgres: pg11: stats collector
postgres 6286 6276 0 15:30 ? 00:00:00 postgres: pg11: logical replication launcher
postgres 6290 6276 0 15:30 ? 00:00:00 postgres: pg11: walsender postgres 192.168.56.102(55469) streaming 0/18C81B68
postgres 10521 6276 0 16:27 ? 00:00:00 postgres: pg11: postgres postgres 192.168.56.102(57067) idle ★接続プロセス1
postgres 11420 6276 0 16:39 ? 00:00:00 postgres: pg11: postgres postgres 192.168.56.102(57375) idle ★接続プロセス2
postgres 11526 3269 0 16:41 pts/0 00:00:00 ps -ef
postgres 11527 3269 0 16:41 pts/0 00:00:00 grep --color=auto postgres
2019年 6月 12日 水曜日 16:41:08 JST
※スタンバイ側
[root@node2 pgpool-II]# ps -ef | egrep -i 'pgpool|postgres'
root 3372 3240 0 14:49 pts/1 00:00:00 su - postgres
postgres 3373 3372 0 14:49 pts/1 00:00:00 -bash
postgres 6746 1 0 15:30 pts/1 00:00:00 /usr/pgsql-11/bin/postgres
postgres 6747 6746 0 15:30 ? 00:00:00 postgres: pg11: logger
postgres 6748 6746 0 15:30 ? 00:00:00 postgres: pg11: startup recovering 000000010000000000000018
postgres 6749 6746 0 15:30 ? 00:00:00 postgres: pg11: checkpointer
postgres 6750 6746 0 15:30 ? 00:00:00 postgres: pg11: background writer
postgres 6751 6746 0 15:30 ? 00:00:00 postgres: pg11: stats collector
postgres 6752 6746 0 15:30 ? 00:00:07 postgres: pg11: walreceiver streaming 0/18C81B68
root 11414 3178 0 16:26 pts/0 00:00:00 pgpool -n -d
root 11415 11414 0 16:26 pts/0 00:00:00 pgpool: wait for connection request
...
root 11442 11414 0 16:26 pts/0 00:00:00 pgpool: wait for connection request
root 11443 11414 0 16:26 pts/0 00:00:00 pgpool: postgres postgres 192.168.56.102(51261) idle ★poolプロセス1
root 11444 11414 0 16:26 pts/0 00:00:00 pgpool: postgres postgres 192.168.56.102(50953) idle ★poolプロセス2
root 11446 11414 0 16:26 pts/0 00:00:00 pgpool: PCP: wait for connection request
root 11447 11414 0 16:26 pts/0 00:00:00 pgpool: worker process
root 11448 11414 0 16:26 pts/0 00:00:00 pgpool: health check process(0)
root 11449 11414 0 16:26 pts/0 00:00:00 pgpool: health check process(1)
postgres 11464 3373 0 16:27 pts/1 00:00:00 psql -d postgres -h 192.168.56.102 -p 9999 -U postgres
postgres 11468 6746 0 16:27 ? 00:00:00 postgres: pg11: postgres postgres 192.168.56.102(58671) idle ★接続プロセス1
root 12346 7924 0 16:39 pts/2 00:00:00 psql -d postgres -h 192.168.56.102 -p 9999 -U postgres
postgres 12357 6746 0 16:39 ? 00:00:00 postgres: pg11: postgres postgres 192.168.56.102(58979) idle ★接続プロセス2
root 12573 3178 0 16:42 pts/0 00:00:00 grep -E --color=auto -i pgpool|postgres
※結論:pgpool経由でprimary-standby構成のDBへ接続する際に、デフォルトは、両方接続させることになる。
④もちろん、この場合でも、localhostの方法で利用が可能です。
[root@node2 pgpool-II]# grep 'listen_addresses' pgpool.conf ★
listen_addresses = 'localhost' ★「*⇒localhost」へ変更
pcp_listen_addresses = '*'
[root@node2 pgpool-II]# cat pool_hba.conf
# pgpool Client Authentication Configuration File
# ===============================================
#

host all all ::1/128 trust
#host postgres postgres 192.168.56.0/24 md5 ★コメントアウトしたエントリ
host postgres postgres ::1/128 md5 ★追加したエントリ
<省略>
-bash-4.2$ psql -d postgres -h localhost -p 9999 -U postgres ★「-h localhost」指定でも可能になった。
psql (11.3) ★localhostの場合、パスワードも聞かれなくなった。
"help" でヘルプを表示します。

postgres=#
<省略>
プロセス状況は一緒でであるため、割愛する

pgpool-II導入してみた・その2(コネクションプールモード)

 

※DBサーバ(postgresql)が独立、clientサーバ(postgresql、initdbを実行なし)とwebサーバ(pgpool)が同梱であるサンプル
①pgpoolとpostgresのインストールは同じであるため、割愛する
②webサーバ(pgpool)を設定する
[root@node2 ~]# cd /etc/pgpool-II/ ★
[root@node2 pgpool-II]# pwd
/etc/pgpool-II
[root@node2 pgpool-II]# ll
合計 208
-rwxrw-r--. 1 root root 858 5月 15 16:38 pcp.conf
-rwxrw-r--. 1 root root 40625 5月 15 16:38 pgpool.conf
-rw-r--r--. 1 root root 39335 5月 15 16:38 pgpool.conf.sample-logical
-rw-r--r--. 1 root root 40567 5月 15 16:38 pgpool.conf.sample-master-slave
-rw-r--r--. 1 root root 40515 5月 15 16:38 pgpool.conf.sample-replication
-rw-r--r--. 1 root root 40588 5月 15 16:38 pgpool.conf.sample-stream
-rwxrw-r--. 1 root root 3277 5月 15 16:38 pool_hba.conf
[root@node2 pgpool-II]# mkdir /var/log/pgpool ★ログを格納するためのディレクトリを作成する
[root@node2 pgpool-II]# chown postgres:postgres /var/log/pgpool/ ★適切な権限を与える
[root@node2 pgpool-II]# vi /etc/pgpool-II/pool_passwd ★パスワードファイルを作成する
[root@node2 pgpool-II]# chown postgres:postgres /etc/pgpool-II/pool_passwd ★適切な権限を与える
[root@node2 pgpool-II]# pg_md5 -m -u postgres postgres ★
[root@node2 pgpool-II]# cat /etc/pgpool-II/pool_passwd


postgres:md53175bce1d3201d16594cebf9d7eb3f9d ★
[root@node2 pgpool-II]# vi pgpool.conf ★pgpool.confを改修する
[root@node2 pgpool-II]# egrep -i 'backend_|connection_cache|num_init_children|max_pool|enable_pool_hba|memory_cache_enabled' pgpool.conf
# num_init_children * listen_backlog_multiplier.
backend_hostname0 = '192.168.56.101' ★DBサーバを指定する必要がある
backend_port0 = 5432 ★DBサーバのポート
backend_weight0 = 1
backend_data_directory0 = '/var/lib/pgsql/11/data' ★DBサーバのデータディレクト
backend_flag0 = 'ALLOW_TO_FAILOVER'
#backend_hostname1 = 'host2'
#backend_port1 = 5433
#backend_weight1 = 1
#backend_data_directory1 = '/data1'
#backend_flag1 = 'ALLOW_TO_FAILOVER'
enable_pool_hba = on ★クライアントサーバにpostgresqlクラスタが作成されていないため、enable_pool_hbaをONにする必要がある
num_init_children = 32
max_pool = 1
connection_cache = on
failover_on_backend_error = on
memory_cache_enabled = on
[root@node2 pgpool-II]# cat pool_hba.conf ★
# pgpool Client Authentication Configuration File
# ===============================================
#
# The format rule in this file follows the rules in the PostgreSQL
# Administrator's Guide. Refer to chapter "Client Authentication" for a
<省略>
# TYPE DATABASE USER CIDR-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
host all all ::1/128 trust ★このエントリを使う
③クライアント側からpgpool経由でDBサーバへ接続する

-bash-4.2$ psql -h localhost -p 9999 -d testdb -U postgres ★接続1
psql (11.3)
"help" でヘルプを表示します。

testdb=#
<省略>
[root@node2 ~]# su - postgres
最終ログイン: 2019/06/12 (水) 13:01:43 JST日時 pts/1
-bash-4.2$ psql -h localhost -p 9999 -d testdb -U postgres ★接続2
psql (11.3)
"help" でヘルプを表示します。

testdb=#
<省略>
-bash-4.2$ psql -h localhost -p 9999 -d testdb -U postgres ★接続3
psql (11.3)
"help" でヘルプを表示します。

testdb=#
<省略>
[root@node2 pgpool-II]# ps -ef | egrep -i 'pgpool|postgres' ★poolingプロセスを確認する
root 6710 6668 0 13:01 pts/1 00:00:00 su - postgres
postgres 6711 6710 0 13:01 pts/1 00:00:00 -bash
root 6949 5848 0 13:11 pts/0 00:00:00 pgpool -n -d
root 6951 6949 0 13:11 pts/0 00:00:00 pgpool: wait for connection request
root 6952 6949 0 13:11 pts/0 00:00:00 pgpool: wait for connection request
root 6953 6949 0 13:11 pts/0 00:00:00 pgpool: wait for connection request
root 6954 6949 0 13:11 pts/0 00:00:00 pgpool: wait for connection request
root 6955 6949 0 13:11 pts/0 00:00:00 pgpool: wait for connection request
root 6956 6949 0 13:11 pts/0 00:00:00 pgpool: wait for connection request
root 6957 6949 0 13:11 pts/0 00:00:00 pgpool: wait for connection request
root 6958 6949 0 13:11 pts/0 00:00:00 pgpool: wait for connection request
root 6959 6949 0 13:11 pts/0 00:00:00 pgpool: wait for connection request
root 6960 6949 0 13:11 pts/0 00:00:00 pgpool: wait for connection request
root 6961 6949 0 13:11 pts/0 00:00:00 pgpool: wait for connection request
root 6962 6949 0 13:11 pts/0 00:00:00 pgpool: wait for connection request
root 6963 6949 0 13:11 pts/0 00:00:00 pgpool: wait for connection request
root 6964 6949 0 13:11 pts/0 00:00:00 pgpool: wait for connection request
root 6965 6949 0 13:11 pts/0 00:00:00 pgpool: wait for connection request
root 6966 6949 0 13:11 pts/0 00:00:00 pgpool: wait for connection request
root 6967 6949 0 13:11 pts/0 00:00:00 pgpool: wait for connection request
root 6968 6949 0 13:11 pts/0 00:00:00 pgpool: wait for connection request
root 6969 6949 0 13:11 pts/0 00:00:00 pgpool: wait for connection request
root 6970 6949 0 13:11 pts/0 00:00:00 pgpool: wait for connection request
root 6971 6949 0 13:11 pts/0 00:00:00 pgpool: wait for connection request
root 6972 6949 0 13:11 pts/0 00:00:00 pgpool: wait for connection request
root 6973 6949 0 13:11 pts/0 00:00:00 pgpool: wait for connection request
root 6974 6949 0 13:11 pts/0 00:00:00 pgpool: wait for connection request
root 6975 6949 0 13:11 pts/0 00:00:00 pgpool: wait for connection request
root 6976 6949 0 13:11 pts/0 00:00:00 pgpool: wait for connection request
root 6977 6949 0 13:11 pts/0 00:00:00 pgpool: wait for connection request
root 6978 6949 0 13:11 pts/0 00:00:00 pgpool: wait for connection request
root 6979 6949 0 13:11 pts/0 00:00:00 pgpool: wait for connection request
root 6980 6949 0 13:11 pts/0 00:00:00 pgpool: postgres testdb ::1(28742) idle ★poolingプロセス1
root 6981 6949 0 13:11 pts/0 00:00:00 pgpool: postgres testdb ::1(28738) idle ★poolingプロセス2
root 6982 6949 0 13:11 pts/0 00:00:00 pgpool: postgres testdb ::1(28734) idle ★poolingプロセス3
root 6983 6949 0 13:11 pts/0 00:00:00 pgpool: PCP: wait for connection request
root 6984 6949 0 13:11 pts/0 00:00:00 pgpool: worker process
root 6985 6949 0 13:11 pts/0 00:00:00 pgpool: health check process(0)
postgres 6988 6711 0 13:11 pts/1 00:00:00 psql -h localhost -p 9999 -d testdb -U postgres
root 7118 7019 0 13:12 pts/2 00:00:00 su - postgres
postgres 7119 7118 0 13:12 pts/2 00:00:00 -bash
root 7181 7081 0 13:12 pts/3 00:00:00 su - postgres
postgres 7182 7181 0 13:12 pts/3 00:00:00 -bash
postgres 7252 7119 0 13:12 pts/2 00:00:00 psql -h localhost -p 9999 -d testdb -U postgres
postgres 7253 7182 0 13:12 pts/3 00:00:00 psql -h localhost -p 9999 -d testdb -U postgres
root 7255 5848 0 13:12 pts/0 00:00:00 grep -E --color=auto -i pgpool|postgres
④DBサーバで接続バックエンドプロセスを確認する
-bash-4.2$ date;ps -ef | grep postgres;date
2019年 6月 12日 水曜日 13:12:54 JST
root 6195 5889 0 12:52 pts/0 00:00:00 su - postgres
postgres 6196 6195 0 12:52 pts/0 00:00:00 -bash
postgres 6326 1 0 12:53 pts/0 00:00:00 /usr/pgsql-11/bin/postgres
postgres 6327 6326 0 12:53 ? 00:00:00 postgres: pg11: logger
postgres 6329 6326 0 12:53 ? 00:00:00 postgres: pg11: checkpointer
postgres 6330 6326 0 12:53 ? 00:00:00 postgres: pg11: background writer
postgres 6331 6326 0 12:53 ? 00:00:00 postgres: pg11: walwriter
postgres 6332 6326 0 12:53 ? 00:00:00 postgres: pg11: autovacuum launcher
postgres 6333 6326 0 12:53 ? 00:00:00 postgres: pg11: archiver
postgres 6334 6326 0 12:53 ? 00:00:00 postgres: pg11: stats collector
postgres 6335 6326 0 12:53 ? 00:00:00 postgres: pg11: logical replication launcher
postgres 7653 6326 0 13:11 ? 00:00:00 postgres: pg11: postgres testdb 192.168.56.102(50708) idle ★接続バックエンドプロセス1
postgres 7724 6326 0 13:12 ? 00:00:00 postgres: pg11: postgres testdb 192.168.56.102(50712) idle ★接続バックエンドプロセス2
postgres 7726 6326 0 13:12 ? 00:00:00 postgres: pg11: postgres testdb 192.168.56.102(50716) idle ★接続バックエンドプロセス3
postgres 7731 6196 0 13:12 pts/0 00:00:00 ps -ef
postgres 7732 6196 0 13:12 pts/0 00:00:00 grep --color=auto postgres
2019年 6月 12日 水曜日 13:12:54 JST

pgpool-II導入してみた(コネクションプールモード)

①インストール
[root@node1 ~]# yum install http://www.pgpool.net/yum/rpms/4.0/redhat/rhel-7-x86_64/pgpool-II-release-4.0-1.noarch.rpm ★①
読み込んだプラグイン:fastestmirror, langpacks
pgpool-II-release-4.0-1.noarch.rpm | 5.2 kB 00:00:00
/var/tmp/yum-root-kLrXQ2/pgpool-II-release-4.0-1.noarch.rpm を調べています: pgpool-II-release-4.0-1.noarch
/var/tmp/yum-root-kLrXQ2/pgpool-II-release-4.0-1.noarch.rpm をインストール済みとして設定しています
依存性の解決をしています
--> トランザクションの確認を実行しています。
---> パッケージ pgpool-II-release.noarch 0:4.0-1 を インストール
--> 依存性解決を終了しました。
base/7/x86_64 | 3.6 kB 00:00:00
extras/7/x86_64 | 3.4 kB 00:00:00
ol7_UEKR5/x86_64 | 2.5 kB 00:00:00
ol7_UEKR5/x86_64/updateinfo | 27 kB 00:00:00
ol7_UEKR5/x86_64/primary_db | 3.9 MB 00:00:00
pgdg10/7/x86_64 | 3.6 kB 00:00:00
pgdg10/7/x86_64/primary_db | 243 kB 00:00:00
pgdg11/7/x86_64 | 3.6 kB 00:00:00
pgdg11/7/x86_64/primary_db | 204 kB 00:00:00
pgdg94/7/x86_64 | 3.6 kB 00:00:00
pgdg94/7/x86_64/primary_db | 280 kB 00:00:00
pgdg95/7/x86_64 | 3.6 kB 00:00:00
pgdg95/7/x86_64/primary_db | 268 kB 00:00:00
pgdg96/7/x86_64 | 3.6 kB 00:00:00
pgdg96/7/x86_64/primary_db | 269 kB 00:00:00
updates/7/x86_64 | 3.4 kB 00:00:00
updates/7/x86_64/primary_db | 5.7 MB 00:00:00

依存性を解決しました

==========================================================================================================================================================================================================================================
Package アーキテクチャー バージョン リポジトリー 容量
==========================================================================================================================================================================================================================================
インストール中:
pgpool-II-release noarch 4.0-1 /pgpool-II-release-4.0-1.noarch 2.2 k

トランザクションの要約
==========================================================================================================================================================================================================================================
インストール 1 パッケージ

合計容量: 2.2 k
インストール容量: 2.2 k
Is this ok [y/d/N]: y
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
インストール中 : pgpool-II-release-4.0-1.noarch 1/1
検証中 : pgpool-II-release-4.0-1.noarch 1/1

インストール:
pgpool-II-release.noarch 0:4.0-1

完了しました!
[root@node1 ~]# yum install -y pgpool-II-pg11.x86_64 pgpool-II-pg11-debuginfo.x86_64 pgpool-II-pg11-devel.x86_64 pgpool-II-pg11-extensions.x86_64 ★②
読み込んだプラグイン:fastestmirror, langpacks
pgpool40 | 1.3 kB 00:00:00
pgpool40/7/x86_64/primary | 23 kB 00:00:00
Determining fastest mirrors
* base: ftp.riken.jp
* extras: ftp.riken.jp
* updates: ftp.riken.jp
pgpool40 183/183
依存性の解決をしています
--> トランザクションの確認を実行しています。
---> パッケージ pgpool-II-pg11.x86_64 0:4.0.5-1pgdg.rhel7 を インストール
--> 依存性の処理をしています: libmemcached.so.11()(64bit) のパッケージ: pgpool-II-pg11-4.0.5-1pgdg.rhel7.x86_64
---> パッケージ pgpool-II-pg11-debuginfo.x86_64 0:4.0.5-1pgdg.rhel7 を インストール
---> パッケージ pgpool-II-pg11-devel.x86_64 0:4.0.5-1pgdg.rhel7 を インストール
---> パッケージ pgpool-II-pg11-extensions.x86_64 0:4.0.5-1pgdg.rhel7 を インストール
--> トランザクションの確認を実行しています。
---> パッケージ libmemcached.x86_64 0:1.0.16-5.el7 を インストール
--> 依存性解決を終了しました。

依存性を解決しました

==========================================================================================================================================================================================================================================
Package アーキテクチャー バージョン リポジトリー 容量
==========================================================================================================================================================================================================================================
インストール中:
pgpool-II-pg11 x86_64 4.0.5-1pgdg.rhel7 pgpool40 1.2 M
pgpool-II-pg11-debuginfo x86_64 4.0.5-1pgdg.rhel7 pgpool40 1.9 M
pgpool-II-pg11-devel x86_64 4.0.5-1pgdg.rhel7 pgpool40 17 k
pgpool-II-pg11-extensions x86_64 4.0.5-1pgdg.rhel7 pgpool40 41 k
依存性関連でのインストールをします:
libmemcached x86_64 1.0.16-5.el7 base 237 k

トランザクションの要約
==========================================================================================================================================================================================================================================
インストール 4 パッケージ (+1 個の依存関係のパッケージ)

総ダウンロード容量: 3.4 M
インストール容量: 17 M
Downloading packages:
(1/5): libmemcached-1.0.16-5.el7.x86_64.rpm | 237 kB 00:00:00
(2/5): pgpool-II-pg11-debuginfo-4.0.5-1pgdg.rhel7.x86_64.rpm | 1.9 MB 00:00:02
(3/5): pgpool-II-pg11-devel-4.0.5-1pgdg.rhel7.x86_64.rpm | 17 kB 00:00:00
(4/5): pgpool-II-pg11-4.0.5-1pgdg.rhel7.x86_64.rpm | 1.2 MB 00:00:03
(5/5): pgpool-II-pg11-extensions-4.0.5-1pgdg.rhel7.x86_64.rpm | 41 kB 00:00:00
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
合計 1.0 MB/s | 3.4 MB 00:00:03
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
インストール中 : libmemcached-1.0.16-5.el7.x86_64 1/5
インストール中 : pgpool-II-pg11-4.0.5-1pgdg.rhel7.x86_64 2/5
インストール中 : pgpool-II-pg11-extensions-4.0.5-1pgdg.rhel7.x86_64 3/5
インストール中 : pgpool-II-pg11-devel-4.0.5-1pgdg.rhel7.x86_64 4/5
インストール中 : pgpool-II-pg11-debuginfo-4.0.5-1pgdg.rhel7.x86_64 5/5
検証中 : libmemcached-1.0.16-5.el7.x86_64 1/5
検証中 : pgpool-II-pg11-extensions-4.0.5-1pgdg.rhel7.x86_64 2/5
検証中 : pgpool-II-pg11-debuginfo-4.0.5-1pgdg.rhel7.x86_64 3/5
検証中 : pgpool-II-pg11-devel-4.0.5-1pgdg.rhel7.x86_64 4/5
検証中 : pgpool-II-pg11-4.0.5-1pgdg.rhel7.x86_64 5/5

インストール:
pgpool-II-pg11.x86_64 0:4.0.5-1pgdg.rhel7 pgpool-II-pg11-debuginfo.x86_64 0:4.0.5-1pgdg.rhel7 pgpool-II-pg11-devel.x86_64 0:4.0.5-1pgdg.rhel7 pgpool-II-pg11-extensions.x86_64 0:4.0.5-1pgdg.rhel7

依存性関連をインストールしました:
libmemcached.x86_64 0:1.0.16-5.el7

完了しました! ★
[root@node1 ~]#
[root@node1 ~]# pgpool -v ★yum完了した後、このコマンドによりインストールが成功したかを確認する
pgpool-II version 4.0.5 (torokiboshi)
[root@node1 ~]# mkdir /var/log/pgpool ★pgpoolの起動ログを格納する箇所を用意する(systemctl start pgpool.serviceコマンドで起動する場合、Linuxのmessageにログが出力されます)
[root@node1 ~]# chown postgres:postgres /var/log/pgpool/ ★適切に権限を与える
[root@node1 ~]# cd /etc/pgpool-II/ ★/etc/pgpool-IIフォルダーに遷移する
[root@node1 ~]# vi /etc/pgpool-II/pool_passwd ★pool_passwdファイルを作成する
[root@node1 pgpool-II]# ll
合計 212
-rwxrw-r--. 1 root root 858 5月 15 16:38 pcp.conf
-rwxrw-r--. 1 root root 40632 6月 11 17:13 pgpool.conf
-rw-r--r--. 1 root root 39335 5月 15 16:38 pgpool.conf.sample-logical
-rw-r--r--. 1 root root 40567 5月 15 16:38 pgpool.conf.sample-master-slave
-rw-r--r--. 1 root root 40515 5月 15 16:38 pgpool.conf.sample-replication
-rw-r--r--. 1 root root 40588 5月 15 16:38 pgpool.conf.sample-stream
-rwxrw-r--. 1 root root 3277 5月 15 16:38 pool_hba.conf
-rw-r--r--. 1 root root 2 6月 11 17:13 pool_passwd ★作成された
[root@node1 pgpool-II]# chown postgres:postgres /etc/pgpool-II/pool_passwd ★適切に権限を与える
[root@node1 pgpool-II]# pg_md5 -m -u testuser testuser
[root@node1 pgpool-II]# pg_md5 -m -u postgres postgres ★接続用のユーザにパスワードを用意する
[root@node1 pgpool-II]# cat pool_passwd


testuser:md5d70a0452418aeb8fb4030eae69ca2856
postgres:md53175bce1d3201d16594cebf9d7eb3f9d ★
[root@node1 pgpool-II]# vi pgpool.conf ★pgpool.confを最低限に編集する
[root@node1 pgpool-II]# egrep -i 'backend_|connection_cache|num_init_children|max_pool' pgpool.conf ★
# num_init_children * listen_backlog_multiplier.
backend_hostname0 = 'localhost' ★触ったもの
backend_port0 = 5432 ★
backend_weight0 = 1 ★
backend_data_directory0 = '/var/lib/pgsql/11/data' ★
backend_flag0 = 'ALLOW_TO_FAILOVER' ★
#backend_hostname1 = 'host2'
#backend_port1 = 5433
#backend_weight1 = 1
#backend_data_directory1 = '/data1'
#backend_flag1 = 'ALLOW_TO_FAILOVER'
enable_pool_hba = off ★
num_init_children = 32 ★
max_pool = 4 ★
connection_cache = on ★
failover_on_backend_error = on
root@node1 pgpool-II]# pgpool -n -d > /var/log/pgpool/pgpool.log 2>&1 & ★pgpoolを起動させる
[1] 23702
[root@node1 pgpool-II]# cat /var/log/pgpool/pgpool.log ★相関のログはこれ
2019-06-12 10:36:46: pid 23702: DEBUG: initializing pool configuration
2019-06-12 10:36:46: pid 23702: DETAIL: num_backends: 1 total_weight: 1.000000
2019-06-12 10:36:46: pid 23702: DEBUG: initializing pool configuration
2019-06-12 10:36:46: pid 23702: DETAIL: backend 0 weight: 2147483647.000000 flag: 0000
2019-06-12 10:36:46: pid 23702: DEBUG: pool_coninfo_size: num_init_children (32) * max_pool (4) * MAX_NUM_BACKENDS (128) * sizeof(ConnectionInfo) (136) = 2228224 bytes requested for shared memory
2019-06-12 10:36:46: pid 23702: DEBUG: ProcessInfo: num_init_children (32) * sizeof(ProcessInfo) (32) = 1024 bytes requested for shared memory
2019-06-12 10:36:46: pid 23702: DEBUG: Request info are: sizeof(POOL_REQUEST_INFO) 5264 bytes requested for shared memory
2019-06-12 10:36:46: pid 23702: DEBUG: Recovery management area: sizeof(int) 4 bytes requested for shared memory
2019-06-12 10:36:46: pid 23702: LOG: memory cache initialized
2019-06-12 10:36:46: pid 23702: DETAIL: memcache blocks :64
2019-06-12 10:36:46: pid 23702: DEBUG: memory cache request size : 67108864
2019-06-12 10:36:46: pid 23702: LOG: pool_discard_oid_maps: discarded memqcache oid maps
2019-06-12 10:36:46: pid 23702: LOG: Setting up socket for 0.0.0.0:9999
2019-06-12 10:36:46: pid 23702: LOG: Setting up socket for :::9999
2019-06-12 10:36:46: pid 23708: DEBUG: initializing backend status
2019-06-12 10:36:46: pid 23709: DEBUG: initializing backend status
2019-06-12 10:36:46: pid 23704: DEBUG: initializing backend status
2019-06-12 10:36:46: pid 23705: DEBUG: initializing backend status
2019-06-12 10:36:46: pid 23706: DEBUG: initializing backend status
2019-06-12 10:36:46: pid 23707: DEBUG: initializing backend status
2019-06-12 10:36:46: pid 23710: DEBUG: initializing backend status
2019-06-12 10:36:46: pid 23711: DEBUG: initializing backend status
2019-06-12 10:36:46: pid 23702: DEBUG: find_primary_node_repeatedly: not in streaming replication mode
2019-06-12 10:36:46: pid 23712: DEBUG: initializing backend status
2019-06-12 10:36:46: pid 23713: DEBUG: initializing backend status
2019-06-12 10:36:46: pid 23714: DEBUG: initializing backend status
2019-06-12 10:36:46: pid 23715: DEBUG: initializing backend status
2019-06-12 10:36:46: pid 23723: DEBUG: initializing backend status
2019-06-12 10:36:46: pid 23725: DEBUG: initializing backend status
2019-06-12 10:36:46: pid 23726: DEBUG: initializing backend status
2019-06-12 10:36:46: pid 23728: DEBUG: initializing backend status
2019-06-12 10:36:46: pid 23731: DEBUG: initializing backend status
2019-06-12 10:36:46: pid 23724: DEBUG: initializing backend status
2019-06-12 10:36:46: pid 23720: DEBUG: initializing backend status
2019-06-12 10:36:46: pid 23716: DEBUG: initializing backend status
2019-06-12 10:36:46: pid 23717: DEBUG: initializing backend status
2019-06-12 10:36:46: pid 23718: DEBUG: initializing backend status
2019-06-12 10:36:46: pid 23719: DEBUG: initializing backend status
2019-06-12 10:36:46: pid 23722: DEBUG: initializing backend status
2019-06-12 10:36:46: pid 23727: DEBUG: initializing backend status
2019-06-12 10:36:46: pid 23729: DEBUG: initializing backend status
2019-06-12 10:36:46: pid 23732: DEBUG: initializing backend status
2019-06-12 10:36:46: pid 23721: DEBUG: initializing backend status
2019-06-12 10:36:46: pid 23730: DEBUG: initializing backend status
2019-06-12 10:36:46: pid 23734: DEBUG: initializing backend status
2019-06-12 10:36:46: pid 23736: DEBUG: I am PCP child with pid:23736
2019-06-12 10:36:46: pid 23737: DEBUG: I am 23737
2019-06-12 10:36:46: pid 23737: DEBUG: initializing backend status
2019-06-12 10:36:46: pid 23702: LOG: pgpool-II successfully started. version 4.0.5 (torokiboshi)
2019-06-12 10:36:46: pid 23733: DEBUG: initializing backend status
2019-06-12 10:36:46: pid 23738: DEBUG: I am health check process pid:23738 DB node id:0
2019-06-12 10:36:46: pid 23738: DEBUG: initializing backend status
2019-06-12 10:36:46: pid 23735: DEBUG: initializing backend status

②postgres DB 側
[root@node1 ~]# su - postgres ★postgresユーザへ遷移
最終ログイン: 2019/06/03 (月) 23:21:56 JST日時 pts/2
-bash-4.2$ export PATH=/usr/pgsql-11/bin:$PATH ★環境変数を設定する
-bash-4.2$ export PGDATA=/var/lib/pgsql/11/data ★環境変数を設定する
-bash-4.2$ cd /var/lib/pgsql/11/data ★環境変数を設定する
-bash-4.2$ cat pg_hba.conf
# PostgreSQL Client Authentication Configuration File
# ===================================================
#
# Refer to the "Client Authentication" section in the PostgreSQL
# documentation for a complete description of this file. A short
# synopsis follows.
#
<省略>

# TYPE DATABASE USER ADDRESS METHOD

# "local" is for Unix domain socket connections only
local testdb testuser trust
local all postgres trust
local all all peer
# IPv4 local connections:
#host all all 127.0.0.1/32 ident
host all all ::1/128 trust ★localhost用の接続エントリはこれ
host testdb postgres 192.168.56.0/24 trust
# IPv6 local connections:
#host all all ::1/128 ident
# Allow replication connections from localhost, by a user with the
# replication privilege.
local replication all peer
#host replication all 127.0.0.1/32 ident
#host replication all ::1/128 ident
-bash-4.2$ pg_ctl start ★起動させる
サーバの起動完了を待っています....2019-06-11 17:17:52.743 JST [3925] LOG: 00000: listening on IPv4 address "0.0.0.0", port 5432
2019-06-11 17:17:52.743 JST [3925] LOCATION: StreamServerPort, pqcomm.c:593
2019-06-11 17:17:52.743 JST [3925] LOG: 00000: listening on IPv6 address "::", port 5432
2019-06-11 17:17:52.743 JST [3925] LOCATION: StreamServerPort, pqcomm.c:593
2019-06-11 17:17:52.750 JST [3925] LOG: 00000: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
2019-06-11 17:17:52.750 JST [3925] LOCATION: StreamServerPort, pqcomm.c:587
2019-06-11 17:17:52.758 JST [3925] LOG: 00000: listening on Unix socket "/tmp/.s.PGSQL.5432"
2019-06-11 17:17:52.758 JST [3925] LOCATION: StreamServerPort, pqcomm.c:587
2019-06-11 17:17:52.947 JST [3925] LOG: 00000: redirecting log output to logging collector process
2019-06-11 17:17:52.947 JST [3925] HINT: Future log output will appear in directory "log".
2019-06-11 17:17:52.947 JST [3925] LOCATION: SysLogger_Start, syslogger.c:668
完了
サーバ起動完了
-bash-4.2$ psql -h localhost -p 5432 -U postgres -d testdb ★5432ポートでも接続できる(pgpool経由しなく)
psql (11.3)
"help" でヘルプを表示します。

testdb=# \q
-bash-4.2$ psql -h localhost -p 9999 -U postgres -d testdb ★9999ポートでも接続できる(pgpool経由する)
psql (11.3)
"help" でヘルプを表示します。

testdb=# \q

③pgpool経由する場合のプロセス状況
[root@node1 pgpool-II]# ps -ef | egrep -i 'pgpool|postgres'
root 3847 3805 0 6月11 pts/1 00:00:00 su - postgres
postgres 3848 3847 0 6月11 pts/1 00:00:00 -bash
root 25077 3314 0 11:13 pts/0 00:00:00 pgpool -n -d
root 25079 25077 0 11:13 pts/0 00:00:00 pgpool: wait for connection request
root 25080 25077 0 11:13 pts/0 00:00:00 pgpool: wait for connection request
root 25081 25077 0 11:13 pts/0 00:00:00 pgpool: wait for connection request
root 25082 25077 0 11:13 pts/0 00:00:00 pgpool: wait for connection request
root 25083 25077 0 11:13 pts/0 00:00:00 pgpool: wait for connection request
root 25084 25077 0 11:13 pts/0 00:00:00 pgpool: wait for connection request
root 25085 25077 0 11:13 pts/0 00:00:00 pgpool: wait for connection request
root 25086 25077 0 11:13 pts/0 00:00:00 pgpool: wait for connection request
root 25087 25077 0 11:13 pts/0 00:00:00 pgpool: wait for connection request
root 25088 25077 0 11:13 pts/0 00:00:00 pgpool: wait for connection request
root 25089 25077 0 11:13 pts/0 00:00:00 pgpool: wait for connection request
root 25090 25077 0 11:13 pts/0 00:00:00 pgpool: wait for connection request
root 25091 25077 0 11:13 pts/0 00:00:00 pgpool: wait for connection request
root 25092 25077 0 11:13 pts/0 00:00:00 pgpool: wait for connection request
root 25093 25077 0 11:13 pts/0 00:00:00 pgpool: wait for connection request
root 25094 25077 0 11:13 pts/0 00:00:00 pgpool: wait for connection request
root 25095 25077 0 11:13 pts/0 00:00:00 pgpool: wait for connection request
root 25096 25077 0 11:13 pts/0 00:00:00 pgpool: wait for connection request
root 25097 25077 0 11:13 pts/0 00:00:00 pgpool: wait for connection request
root 25098 25077 0 11:13 pts/0 00:00:00 pgpool: wait for connection request
root 25099 25077 0 11:13 pts/0 00:00:00 pgpool: wait for connection request
root 25100 25077 0 11:13 pts/0 00:00:00 pgpool: wait for connection request
root 25101 25077 0 11:13 pts/0 00:00:00 pgpool: wait for connection request
root 25102 25077 0 11:13 pts/0 00:00:00 pgpool: wait for connection request
root 25103 25077 0 11:13 pts/0 00:00:00 pgpool: wait for connection request
root 25104 25077 0 11:13 pts/0 00:00:00 pgpool: wait for connection request
root 25105 25077 0 11:13 pts/0 00:00:00 pgpool: wait for connection request
root 25106 25077 0 11:13 pts/0 00:00:00 pgpool: wait for connection request
root 25107 25077 0 11:13 pts/0 00:00:00 pgpool: wait for connection request
root 25108 25077 0 11:13 pts/0 00:00:00 pgpool: postgres testdb ::1(45453) idle
root 25109 25077 0 11:13 pts/0 00:00:00 pgpool: postgres testdb ::1(45449) idle
root 25110 25077 0 11:13 pts/0 00:00:00 pgpool: postgres testdb ::1(45447) idle
root 25111 25077 0 11:13 pts/0 00:00:00 pgpool: PCP: wait for connection request
root 25112 25077 0 11:13 pts/0 00:00:00 pgpool: worker process
root 25113 25077 0 11:13 pts/0 00:00:00 pgpool: health check process(0)
postgres 25116 1 0 11:13 pts/1 00:00:00 /usr/pgsql-11/bin/postgres
postgres 25117 25116 0 11:13 ? 00:00:00 postgres: pg11: logger
postgres 25119 25116 0 11:13 ? 00:00:00 postgres: pg11: checkpointer
postgres 25120 25116 0 11:13 ? 00:00:00 postgres: pg11: background writer
postgres 25121 25116 0 11:13 ? 00:00:00 postgres: pg11: walwriter
postgres 25122 25116 0 11:13 ? 00:00:00 postgres: pg11: autovacuum launcher
postgres 25123 25116 0 11:13 ? 00:00:00 postgres: pg11: archiver
postgres 25124 25116 0 11:13 ? 00:00:00 postgres: pg11: stats collector
postgres 25125 25116 0 11:13 ? 00:00:00 postgres: pg11: logical replication launcher
postgres 25127 25116 0 11:13 ? 00:00:00 postgres: pg11: postgres testdb ::1(43737) idle
postgres 25258 3848 0 11:22 pts/1 00:00:00 psql -h localhost -p 9999 -U postgres -d testdb ★9999ポートでの接続①
root 25338 25295 0 11:23 pts/2 00:00:00 su - postgres
postgres 25339 25338 0 11:23 pts/2 00:00:00 -bash
postgres 25409 25339 0 11:24 pts/2 00:00:00 psql -h localhost -p 9999 -U postgres -d testdb ★9999ポートでの接続②
postgres 25410 25116 0 11:24 ? 00:00:00 postgres: pg11: postgres testdb ::1(43743) idle
root 25530 25493 0 11:25 pts/3 00:00:00 su - postgres
postgres 25531 25530 0 11:25 pts/3 00:00:00 -bash
postgres 25594 25531 0 11:25 pts/3 00:00:00 psql -h localhost -p 9999 -U postgres -d testdb ★9999ポートでの接続③
postgres 25595 25116 0 11:25 ? 00:00:00 postgres: pg11: postgres testdb ::1(43747) idle
root 25666 25629 0 11:26 pts/4 00:00:00 su - postgres
postgres 25667 25666 0 11:26 pts/4 00:00:00 -bash
postgres 25730 25667 0 11:26 pts/4 00:00:00 psql -h localhost -p 5432 -U postgres -d testdb ★5432ポートでの接続
postgres 25731 25116 0 11:26 ? 00:00:00 postgres: pg11: postgres testdb ::1(43749) idle
root 25734 3314 0 11:26 pts/0 00:00:00 grep -E --color=auto -i pgpool|postgres
[root@node1 pgpool-II]# netstat -nal|grep 9999
tcp 0 0 127.0.0.1:9999 0.0.0.0:* LISTEN
tcp6 0 0 ::1:9999 :::* LISTEN
tcp6 0 0 ::1:9999 ::1:45447 ESTABLISHED
tcp6 0 0 ::1:45449 ::1:9999 ESTABLISHED
tcp6 0 0 ::1:9999 ::1:45449 ESTABLISHED
tcp6 0 0 ::1:45447 ::1:9999 ESTABLISHED
tcp6 0 0 ::1:45453 ::1:9999 ESTABLISHED
tcp6 0 0 ::1:9999 ::1:45453 ESTABLISHED
unix 2 [ ACC ] STREAM LISTENING 118593 /tmp/.s.PGSQL.9999
[root@node1 pgpool-II]# netstat -nal|grep 5432
tcp 0 0 0.0.0.0:5432 0.0.0.0:* LISTEN
tcp6 0 0 :::5432 :::* LISTEN
tcp6 0 0 ::1:43749 ::1:5432 ESTABLISHED
tcp6 0 0 ::1:43737 ::1:5432 ESTABLISHED
tcp6 0 0 ::1:5432 ::1:43737 ESTABLISHED
tcp6 0 0 ::1:5432 ::1:43743 ESTABLISHED
tcp6 0 0 ::1:5432 ::1:43749 ESTABLISHED
tcp6 0 0 ::1:43743 ::1:5432 ESTABLISHED
tcp6 0 0 ::1:5432 ::1:43747 ESTABLISHED
tcp6 0 0 ::1:43747 ::1:5432 ESTABLISHED
unix 2 [ ACC ] STREAM LISTENING 118614 /var/run/postgresql/.s.PGSQL.5432
unix 2 [ ACC ] STREAM LISTENING 118616 /tmp/.s.PGSQL.5432
<省略>
root 25666 25629 0 11:26 pts/4 00:00:00 su - postgres
postgres 25667 25666 0 11:26 pts/4 00:00:00 -bash
postgres 25862 25667 0 11:30 pts/4 00:00:00 psql -p 5432 -U postgres -d testdb ★「-h localhost」を指定しく、直接接続する場合
postgres 25863 25116 0 11:30 ? 00:00:00 postgres: pg11: postgres testdb [local] idle ★プロセスがこれ
root 25865 3314 0 11:30 pts/0 00:00:00 grep -E --color=auto -i pgpool|postgres
<省略>
[root@node1 pgpool-II]# ps -ef | egrep -i 'pgpool|postgres'
root 3847 3805 0 6月11 pts/1 00:00:00 su - postgres
postgres 3848 3847 0 6月11 pts/1 00:00:00 -bash
root 25077 3314 0 11:13 pts/0 00:00:00 pgpool -n -d
root 25079 25077 0 11:13 pts/0 00:00:00 pgpool: wait for connection request
root 25080 25077 0 11:13 pts/0 00:00:00 pgpool: wait for connection request
root 25081 25077 0 11:13 pts/0 00:00:00 pgpool: wait for connection request
root 25082 25077 0 11:13 pts/0 00:00:00 pgpool: wait for connection request
root 25083 25077 0 11:13 pts/0 00:00:00 pgpool: wait for connection request
root 25084 25077 0 11:13 pts/0 00:00:00 pgpool: wait for connection request
root 25085 25077 0 11:13 pts/0 00:00:00 pgpool: wait for connection request
root 25086 25077 0 11:13 pts/0 00:00:00 pgpool: wait for connection request
root 25087 25077 0 11:13 pts/0 00:00:00 pgpool: wait for connection request
root 25088 25077 0 11:13 pts/0 00:00:00 pgpool: wait for connection request
root 25089 25077 0 11:13 pts/0 00:00:00 pgpool: wait for connection request
root 25090 25077 0 11:13 pts/0 00:00:00 pgpool: wait for connection request
root 25091 25077 0 11:13 pts/0 00:00:00 pgpool: wait for connection request
root 25092 25077 0 11:13 pts/0 00:00:00 pgpool: wait for connection request
root 25093 25077 0 11:13 pts/0 00:00:00 pgpool: wait for connection request
root 25094 25077 0 11:13 pts/0 00:00:00 pgpool: wait for connection request
root 25095 25077 0 11:13 pts/0 00:00:00 pgpool: wait for connection request
root 25096 25077 0 11:13 pts/0 00:00:00 pgpool: wait for connection request
root 25097 25077 0 11:13 pts/0 00:00:00 pgpool: wait for connection request
root 25098 25077 0 11:13 pts/0 00:00:00 pgpool: wait for connection request
root 25099 25077 0 11:13 pts/0 00:00:00 pgpool: wait for connection request
root 25100 25077 0 11:13 pts/0 00:00:00 pgpool: wait for connection request
root 25101 25077 0 11:13 pts/0 00:00:00 pgpool: wait for connection request
root 25102 25077 0 11:13 pts/0 00:00:00 pgpool: wait for connection request
root 25103 25077 0 11:13 pts/0 00:00:00 pgpool: wait for connection request
root 25104 25077 0 11:13 pts/0 00:00:00 pgpool: wait for connection request
root 25105 25077 0 11:13 pts/0 00:00:00 pgpool: wait for connection request
root 25106 25077 0 11:13 pts/0 00:00:00 pgpool: wait for connection request
root 25107 25077 0 11:13 pts/0 00:00:00 pgpool: wait for connection request
root 25108 25077 0 11:13 pts/0 00:00:00 pgpool: wait for connection request
root 25109 25077 0 11:13 pts/0 00:00:00 pgpool: wait for connection request
root 25110 25077 0 11:13 pts/0 00:00:00 pgpool: wait for connection request
root 25111 25077 0 11:13 pts/0 00:00:00 pgpool: PCP: wait for connection request
root 25112 25077 0 11:13 pts/0 00:00:00 pgpool: worker process
root 25113 25077 0 11:13 pts/0 00:00:00 pgpool: health check process(0)
postgres 25116 1 0 11:13 pts/1 00:00:00 /usr/pgsql-11/bin/postgres
postgres 25117 25116 0 11:13 ? 00:00:00 postgres: pg11: logger
postgres 25119 25116 0 11:13 ? 00:00:00 postgres: pg11: checkpointer
postgres 25120 25116 0 11:13 ? 00:00:00 postgres: pg11: background writer
postgres 25121 25116 0 11:13 ? 00:00:00 postgres: pg11: walwriter
postgres 25122 25116 0 11:13 ? 00:00:00 postgres: pg11: autovacuum launcher
postgres 25123 25116 0 11:13 ? 00:00:00 postgres: pg11: archiver
postgres 25124 25116 0 11:13 ? 00:00:00 postgres: pg11: stats collector
postgres 25125 25116 0 11:13 ? 00:00:00 postgres: pg11: logical replication launcher
postgres 25127 25116 0 11:13 ? 00:00:00 postgres: pg11: postgres testdb ::1(43737) idle ★
root 25338 25295 0 11:23 pts/2 00:00:00 su - postgres
postgres 25339 25338 0 11:23 pts/2 00:00:00 -bash
postgres 25410 25116 0 11:24 ? 00:00:00 postgres: pg11: postgres testdb ::1(43743) idle ★
root 25530 25493 0 11:25 pts/3 00:00:00 su - postgres
postgres 25531 25530 0 11:25 pts/3 00:00:00 -bash
postgres 25595 25116 0 11:25 ? 00:00:00 postgres: pg11: postgres testdb ::1(43747) idle ★
root 25666 25629 0 11:26 pts/4 00:00:00 su - postgres
postgres 25667 25666 0 11:26 pts/4 00:00:00 -bash
postgres 25985 25116 0 11:40 ? 00:00:00 postgres: pg11: postgres testdb ::1(43759) idle ★psqlでの接続が終了しても、postgresqlへの接続プロセスの数は減らない。
root 26022 3314 0 11:40 pts/0 00:00:00 grep -E --color=auto -i pgpool|postgres
[root@node1 pgpool-II]#

PostgreSQLのレプリケーション構成をインストールしてみた。

************************************************************
******************** ①マスタ側を設定する******************
************************************************************

[root@node2 ~]# vi /etc/hosts ★hostsファイルを改修する
[root@node2 ~]# cat /etc/hosts
# Do not remove the following line, or various programs
# that require network functionality will fail.
#127.0.0.1 localhost.localdomain localhost
#::1 localhost6.localdomain6 localhost6

192.168.56.112 node2
192.168.56.113 node3
[root@node2 ~]# /etc/init.d/iptables stop ★Firewallを止める
ファイアウォールルールを適用中: [ OK ]
チェインポリシーを ACCEPT に設定中filter [ OK ]
iptables モジュールを取り外し中 [ OK ]

-bash-3.2$ vi postgresql.conf ★postgresql.confを改修する。
 ★改修内容は次の通りです。
| # Add settings for extensions here
| listen_addresses = '*'
| checkpoint_timeout = 15min
| hot_standby = on
| logging_collector = on
| log_filename = 'postgresql-%Y-%m-%d.log'
| log_min_duration_statement = 60
| log_checkpoints = on
| log_lock_waits = on
| log_error_verbosity = verbose
| log_line_prefix = '%m [%p] '
|
| wal_level = hot_standby
| max_wal_senders = 3
-bash-3.2$ egrep -i 'listen_addresses|checkpoint_timeout|hot_standby|logging_collector|log_filename|log_min_duration_statement|log_checkpoints|log_lock_waits|log_error_verbosity|log_line_prefix|wal_level|max_wal_senders' postgresql.conf
#listen_addresses = 'localhost' # what IP address(es) to listen on;
listen_addresses = '*'
wal_level = hot_standby # minimal, archive, hot_standby, or logical
checkpoint_timeout = 15min # range 30s-1h
max_wal_senders = 3 # max number of walsender processes
hot_standby = on # "on" allows queries during recovery
#hot_standby_feedback = off # send info from standby to prevent
# requires logging_collector to be on.
logging_collector = on # Enable capturing of stderr and csvlog
# These are only used if logging_collector is on:
#log_filename = 'postgresql-%a.log' # log file name pattern,
log_filename = 'postgresql-%Y%m%d.log'
log_min_duration_statement = 60 # -1 is disabled, 0 logs all statements
log_checkpoints = on
log_error_verbosity = verbose # terse, default, or verbose messages
log_line_prefix = '%m [%p] '
log_lock_waits = on # log lock waits >= deadlock_timeout

-bash-3.2$ vi pg_hba.conf ★pg_hba.confを改修する。
-bash-3.2$ cat pg_hba.conf
# PostgreSQL Client Authentication Configuration File
# ===================================================
#
<省略>
local all postgres peer
host hoge piyo 192.168.56.0/24 trust
host replication postgres 192.168.56.0/24 trust ★追加した行
-bash-3.2$ pg_ctl start -w ★マスタ側をスタートする
サーバの起動完了を待っています....2019-05-09 13:31:54.159 JST [3490] LOG: XX000: could not create IPv6 socket: Address family not supported by protocol
2019-05-09 13:31:54.159 JST [3490] LOCATION: StreamServerPort, pqcomm.c:435
2019-05-09 13:31:54.381 JST [3490] LOG: 00000: redirecting log output to logging collector process
2019-05-09 13:31:54.381 JST [3490] HINT: Future log output will appear in directory "pg_log".
2019-05-09 13:31:54.381 JST [3490] LOCATION: SysLogger_Start, syslogger.c:622
完了
サーバ起動完了
-bash-3.2$

************************************************************
********************②スレーブ側を設定する******************
****(前提:RPMインストール、PGDATA環境変数を設定する)*******
************************************************************
[root@node3 ~]# cat /etc/hosts ★hostsファイルを改修する
# Do not remove the following line, or various programs
# that require network functionality will fail.
#127.0.0.1 localhost.localdomain localhost
#::1 localhost6.localdomain6 localhost6

#127.0.0.1 node3 localhost
#172.16.30.170 node3 localhost

192.168.56.112 node2
192.168.56.113 node3
[root@node3 ~]# /etc/init.d/iptables stop ★Firewallを止める
ファイアウォールルールを適用中: [ OK ]
チェインポリシーを ACCEPT に設定中filter [ OK ]
iptables モジュールを取り外し中 [ OK ]
[root@node3 ~]# su - postgres
-bash-3.2$ pg_basebackup -h 192.168.56.112 -D $PGDATA -X stream --progress -U postgres -R ★ベースバックアップを取得する
29756/29756 kB (100%), 1/1 tablespace
-bash-3.2$ env | grep PG
PGDATA=/var/lib/pgsql/9.5/data
-bash-3.2$ cd /var/lib/pgsql/9.5/data
-bash-3.2$ ll
合計 124
-rw------- 1 postgres postgres 4 5月 9 13:34 PG_VERSION
-rw------- 1 postgres postgres 206 5月 9 13:34 backup_label ★取得したベースバックアップ
drwx------ 6 postgres postgres 4096 5月 9 13:34 base
drwx------ 2 postgres postgres 4096 5月 9 13:34 global
drwx------ 2 postgres postgres 4096 5月 9 13:34 pg_clog
drwx------ 2 postgres postgres 4096 5月 9 13:34 pg_commit_ts
drwx------ 2 postgres postgres 4096 5月 9 13:34 pg_dynshmem
-rw------- 1 postgres postgres 4574 5月 9 13:34 pg_hba.conf
-rw------- 1 postgres postgres 1636 5月 9 13:34 pg_ident.conf
drwx------ 2 postgres postgres 4096 5月 9 13:34 pg_log
drwx------ 4 postgres postgres 4096 5月 9 13:34 pg_logical
drwx------ 4 postgres postgres 4096 5月 9 13:34 pg_multixact
drwx------ 2 postgres postgres 4096 5月 9 13:34 pg_notify
drwx------ 2 postgres postgres 4096 5月 9 13:34 pg_replslot
drwx------ 2 postgres postgres 4096 5月 9 13:34 pg_serial
drwx------ 2 postgres postgres 4096 5月 9 13:34 pg_snapshots
drwx------ 2 postgres postgres 4096 5月 9 13:34 pg_stat
drwx------ 2 postgres postgres 4096 5月 9 13:34 pg_stat_tmp
drwx------ 2 postgres postgres 4096 5月 9 13:34 pg_subtrans
drwx------ 2 postgres postgres 4096 5月 9 13:34 pg_tblspc
drwx------ 2 postgres postgres 4096 5月 9 13:34 pg_twophase
drwx------ 3 postgres postgres 4096 5月 9 13:34 pg_xlog
-rw------- 1 postgres postgres 88 5月 9 13:34 postgresql.auto.conf
-rw------- 1 postgres postgres 21319 5月 9 13:34 postgresql.conf
-rw-r--r-- 1 postgres postgres 137 5月 9 13:34 recovery.conf
-bash-3.2$ vi recovery.conf ★recovery.confを改修する
-bash-3.2$ cat recovery.conf
#standby_mode = 'on' ★コメントアウトした行
#primary_conninfo = 'user=postgres host=192.168.56.112 port=5432 sslmode=prefer sslcompression=1 krbsrvname=postgres' ★コメントアウトした行

standby_mode = 'on' ★追加した行
primary_conninfo = 'user=postgres host=192.168.56.112 port=5432' ★追加した行


-bash-3.2$ pg_ctl start -w
サーバの起動完了を待っています....2019-05-09 13:39:02.788 JST [4317] LOG: XX000: could not create IPv6 socket: Address family not supported by protocol
2019-05-09 13:39:02.788 JST [4317] LOCATION: StreamServerPort, pqcomm.c:435
2019-05-09 13:39:02.858 JST [4317] LOG: 00000: redirecting log output to logging collector process
2019-05-09 13:39:02.858 JST [4317] HINT: Future log output will appear in directory "pg_log".
2019-05-09 13:39:02.858 JST [4317] LOCATION: SysLogger_Start, syslogger.c:622
.完了
サーバ起動完了

**********************************************************
************************③動作確認する********************
**********************************************************
※マスタ側
-bash-3.2$ psql postgres -c "SELECT * FROM pg_stat_replication" -x
-[ RECORD 1 ]----+------------------------------
pid | 3670
usesysid | 10
usename | postgres
application_name | walreceiver
client_addr | 192.168.56.113
client_hostname |
client_port | 25908
backend_start | 2019-05-09 13:39:04.380463+09
backend_xmin |
state | streaming
sent_location | 0/3000060
write_location | 0/3000060
flush_location | 0/3000060
replay_location | 0/3000060
sync_priority | 0
sync_state | async ★非同期モードで同期中との意味です。

-bash-3.2$ psql postgres -c "CREATE TABLE test_table(i int)" ★テスト用の表を作成する
CREATE TABLE
-bash-3.2$ psql postgres -c "INSERT INTO test_table VALUES (1)"★一行データを投入する
INSERT 0 1
-bash-3.2$ psql postgres -c "SELECT * FROM test_table" ★結果が取得てきる
i
---
1
(1 行)
※スレーブ側へ遷移する
-bash-3.2$ psql postgres -c "SELECT * FROM test_table" ★スレーブ側も同じ結果を取得できた
i
---
1
(1 行)

************************************************************
********************④同期モードへ変更する******************
************************************************************
※マスタ側
-bash-3.2$ cat postgresql.conf | grep synchronous_standby_names
#synchronous_standby_names = '' # standby servers that provide sync rep
-bash-3.2$ vi postgresql.conf ★変更する
-bash-3.2$
-bash-3.2$ cat postgresql.conf | grep synchronous_standby_names
synchronous_standby_names = 'node3' ★追加した行。スレーブ側のホスト名を指定した
#synchronous_standby_names = '' # standby servers that provide sync rep
-bash-3.2$ pg_ctl start -w ★マスタを起動させる
サーバの起動完了を待っています....2019-05-09 14:22:19.058 JST [3715] LOG: XX000: could not create IPv6 socket: Address family not supported by protocol
2019-05-09 14:22:19.059 JST [3715] LOCATION: StreamServerPort, pqcomm.c:435
2019-05-09 14:22:19.089 JST [3715] LOG: 00000: redirecting log output to logging collector process
2019-05-09 14:22:19.089 JST [3715] HINT: Future log output will appear in directory "pg_log".
2019-05-09 14:22:19.089 JST [3715] LOCATION: SysLogger_Start, syslogger.c:622
完了
サーバ起動完了
-bash-3.2$ ps -ef | grep postgres
root 3593 3379 0 14:03 pts/1 00:00:00 su - postgres
postgres 3594 3593 0 14:03 pts/1 00:00:00 -bash
postgres 3715 1 0 14:22 pts/1 00:00:00 /usr/pgsql-9.5/bin/postgres
postgres 3722 3715 0 14:22 ? 00:00:00 postgres: logger process
postgres 3724 3715 0 14:22 ? 00:00:00 postgres: checkpointer process
postgres 3725 3715 0 14:22 ? 00:00:00 postgres: writer process
postgres 3726 3715 0 14:22 ? 00:00:00 postgres: wal writer process
postgres 3728 3715 0 14:22 ? 00:00:00 postgres: wal sender process postgres 192.168.56.113(44368) streaming 0/3015858 ★
postgres 3745 3594 0 14:30 pts/1 00:00:00 ps -ef
postgres 3746 3594 0 14:30 pts/1 00:00:00 grep postgres

※スレーブ側へ遷移する
-bash-3.2$ cat recovery.conf
#standby_mode = 'on'
#primary_conninfo = 'user=postgres host=192.168.56.112 port=5432 sslmode=prefer sslcompression=1 krbsrvname=postgres'

standby_mode = 'on'
primary_conninfo = 'user=postgres host=192.168.56.112 port=5432'

-bash-3.2$ vi recovery.conf
-bash-3.2$ cat recovery.conf
#standby_mode = 'on'
#primary_conninfo = 'user=postgres host=192.168.56.112 port=5432 sslmode=prefer sslcompression=1 krbsrvname=postgres'

standby_mode = 'on'
primary_conninfo = 'user=postgres host=192.168.56.112 port=5432 application_name=node3' ★編集した箇所
-bash-3.2$ pg_ctl start -w ★スレーブ側を起動させる
サーバの起動完了を待っています....2019-05-09 14:22:27.030 JST [3372] LOG: XX000: could not create IPv6 socket: Address family not supported by protocol
2019-05-09 14:22:27.030 JST [3372] LOCATION: StreamServerPort, pqcomm.c:435
2019-05-09 14:22:27.250 JST [3372] LOG: 00000: redirecting log output to logging collector process
2019-05-09 14:22:27.250 JST [3372] HINT: Future log output will appear in directory "pg_log".
2019-05-09 14:22:27.251 JST [3372] LOCATION: SysLogger_Start, syslogger.c:622
完了
サーバ起動完了
-bash-3.2$ ps -ef | grep postgres
root 3295 3155 0 14:05 pts/1 00:00:00 su - postgres
postgres 3296 3295 0 14:05 pts/1 00:00:00 -bash
postgres 3372 1 0 14:22 pts/1 00:00:00 /usr/pgsql-9.5/bin/postgres
postgres 3379 3372 0 14:22 ? 00:00:00 postgres: logger process
postgres 3380 3372 0 14:22 ? 00:00:00 postgres: startup process recovering 000000010000000000000003 ★
postgres 3381 3372 0 14:22 ? 00:00:00 postgres: checkpointer process
postgres 3382 3372 0 14:22 ? 00:00:00 postgres: writer process
postgres 3383 3372 0 14:22 ? 00:00:00 postgres: wal receiver process streaming 0/3015858 ★
postgres 3402 3296 0 14:31 pts/1 00:00:00 ps -ef
postgres 3403 3296 0 14:31 pts/1 00:00:00 grep postgres

※マスタ側へ戻る
-bash-3.2$ psql postgres -c "SELECT * FROM pg_stat_replication" -x ★
-[ RECORD 1 ]----+------------------------------
pid | 3728
usesysid | 10
usename | postgres
application_name | node3 ★application_nameもきちんと表示されている
client_addr | 192.168.56.113
client_hostname |
client_port | 44368
backend_start | 2019-05-09 14:22:27.036182+09
backend_xmin |
state | streaming
sent_location | 0/3015858
write_location | 0/3015858
flush_location | 0/3015858
replay_location | 0/3015820
sync_priority | 1
sync_state | sync ★同期モードになった