Marlock Homes Diary

備忘録。忘れないように書きます。

PostgreSQL 9.4.1 インストール

PostgreSQLインストールメモ(バージョン 9.4.1)

最新バージョンの9.4.1をインストールする。

環境 バージョン
CentOS 7.0

# cat /etc/redhat-release
CentOS Linux release 7.0.1406 (Core)


1.環境準備
1.1.make

# gmake -version
GNU Make 3.82


1.2.gcc

# gcc -v
gcc バージョン 4.8.2 20140120 (Red Hat 4.8.2-16) (GCC)


1.3. Readline

# yum install readline
パッケージ readline-6.2-9.el7.x86_64 はインストール済みか最新バージョンです
何もしません
# yum install readline-devel
パッケージ readline-devel-6.2-9.el7.x86_64 はインストール済みか最新バージョンで す
何もしません

1.4. zlib

# yum install zlib
パッケージ zlib-1.2.7-13.el7.x86_64 はインストール済みか最新バージョンです
何もしません
# yum install zlib-devel
パッケージ zlib-devel-1.2.7-13.el7.x86_64 はインストール済みか最新バージョンです
何もしません

2.PostgreSQLインストール

2.1.postgresユーザ作成

# useradd postgres

2.2.ソースの入手と解凍

# cd /usr/local/src/
# wget https://ftp.postgresql.org/pub/source/v9.4.1/postgresql-9.4.1.tar.gz
# tar xvzf postgresql-9.4.1.tar.gz
# chown -R postgres:postgres postgresql-9.4.1

2.3.コンパイル

# su - postgres
$ $ cd /usr/local/src/postgresql-9.4.1/
$ ./configure
$ gmake world ※
(省略)
PostgreSQL, contrib, and documentation successfully made. Ready to install.

※ドキュメント(HTMLやman)や追加モジュール(contrib)を含め、構築可能なもの全てを構築したい場合、上記の様にworldを付与して実行

2.4.リグレーションテスト

$ gmake check
(省略)
=======================
 All 145 tests passed.
=======================
(省略)

2.5.インストール

$ su
# gmake install-world
(省略)
PostgreSQL, contrib, and documentation installation complete.


2.6.インストールディレクトリの所有者変更

# cd /usr/local/
# chown -R postgres:postgres pgsql/

2.7.postgresユーザの環境変数設定

# su - postgres
$ vi .bash_profile 
(下記を設定)

# PostgreSQL PATH
export POSTGRES_HOME=/usr/local/pgsql
export PGDATA=/home/postgres/data
export PATH=$PATH:$POSTGRES_HOME/bin
export PGLIB=/usr/local/pgsql/lib
export MANPATH="$MANPATH":$POSTGRES_HOME/man

2.7.postgresユーザの環境変数設定読み込み

$ source .bash_profile

2.8.データベースクラスタの作成

$ initdb -E UTF8 --no-locale


2.9.ログディレクトリの作成

$ mkdir -p /home/postgres/data/pg_log

2.10.ログ出力設定
# ログのみ出力(その他の設定は省略)

$ vi /home/postgres/data/postgresql.conf
#logging_collector = off                # Enable capturing of stderr and csvlog
logging_collector = on

2.10.PostgreSQL起動

$ pg_ctl start
server starting
-bash-4.2$ LOG:  redirecting log output to logging collector process
HINT:  Future log output will appear in directory "pg_log".

これでPostgreSQL9.4の新機能を試すことができる。

以上

WordPressでPostgreSQLを使用する!!

こんにちは!!

ブログの管理に、WordPressを使ってみたいなと!!と思っていましたが、
デフォルトのデータベースがMySQLでした。。。
MySQLより、PostgreSQLの方が慣れているので、
PostgreSQL for WordPress (PG4WP)を使用して、
データベースをPostgreSQLに変更してみました!!

# 以下の手順はPostgreSQLをインストールしていることを前提にしています。

前提条件

1.OSのバージョン

# cat /etc/redhat-release
CentOS release 6.5 (Final)


2.WordPressのダウンロード

# cd /var/www/html/
# wget http://ja.wordpress.org/wordpress-3.9.1-ja.zip

3.WordPressの展開

# unzip wordpress-3.9.1-ja.zip

4.WordPress設定ファイルの修正

# cd wordpress
# cp -p wp-config-sample.php wp-config.php
# vi wp-config.php
(変更後)
// **PostgreSQLの設定
define('DB_NAME', 'wordpress');
define('DB_USER', 'postgres');
define('DB_PASSWORD', '');
define('DB_HOST', 'localhost');
define('DB_CHARSET', 'utf8');
define('DB_COLLATE', '');

//https://api.wordpress.org/secret-key/1.1/salt/ WordPress.org の秘密鍵サービスで自動生成した値を記載
define('AUTH_KEY',         'xxxxx');
(以降、省略)

5.PostgreSQL for WordPress (PG4WP)の配置

# cd /var/www/html/wordpress/wp-content/plugins/
# wget http://downloads.wordpress.org/plugin/postgresql-for-wordpress.1.3.0.zip
# unzip postgresql-for-wordpress.1.3.0.zip
# mv postgresql-for-wordpress/pg4wp ./
# rm postgresql-for-wordpress.1.3.0.zip

6.PostgreSQL for WordPress (PG4WP)の設定

# cd pg4wp/
# vi db.php
(編集後)
define( 'PG4WP_INSECURE', true);
wb-content直下に移動させる
# mv db.php ../../

7.WordPress用のデータベース作成

# su - postgres
$ createdb wordpress

8.WordPressインストール
ブラウザから/wordpress/wp-admin/install.phpにアクセスしてインストール。

補足
a.Word-Pressインストール時のエラー対処

1.以下のメッセージがブラウザに出力された

Your PHP installation appears to be missing the PostgreSQL extension which is required by WordPress with PG4WP.

以下を実行

# yum install php-pgsql

2.ブラウザに何も表示されない。
apacheerror_logを確認

tail -f /var/log/httpd/error_log
PHP Fatal error:  Call to undefined function wpsql_errno() in /var/www/html/wordpress/wp-content/plugins/pg4wp/core.php(32) : eval()'d code on line 1531

以下を修正

# vi /var/www/html/wordpress/wp-content/plugins/pg4wp/driver_pgsql.php
(以下を追加)
        function wpsql_errno( $connection) {
                $result = pg_get_result($connection);
                $result_status = pg_result_status($result);
        return pg_result_error_field($result_status, PGSQL_DIAG_SQLSTATE);
        }
||<        

参考URL
https://vitoriodelage.wordpress.com/2014/06/06/add-missing-wpsql_errno-in-pg4wp-plugin/

b.インストール完了時のメッセージ

(原因調査中)
>||
WordPress database error: [ERROR: syntax error at or near "," LINE 1: DELETE a, b FROM wp_options a, wp_options b WHERE ^]
DELETE a, b FROM wp_options a, wp_options b WHERE a.option_name LIKE '\_transient\_%' AND a.option_name NOT LIKE '\_transient\_timeout\_%' AND b.option_name = CONCAT( '_transient_timeout_', SUBSTRING( a.option_name, 12 ) ) AND b.option_value < 1403947144

WordPress database error: [ERROR: syntax error at or near "," LINE 1: DELETE a, b FROM wp_options a, wp_options b WHERE ^]
DELETE a, b FROM wp_options a, wp_options b WHERE a.option_name LIKE '\_site\_transient\_%' AND a.option_name NOT LIKE '\_site\_transient\_timeout\_%' AND b.option_name = CONCAT( '_site_transient_timeout_', SUBSTRING( a.option_name, 17 ) ) AND b.option_value < 1403947144

成功しました !

WordPress がインストールされました。もっと何か作業をしたかったですか ? がっかりさせてゴメンナサイ。これだけです 

MySQL 5.6.17インストール手順

MySQLをインストールしたことがなかったので、
試しにインストールしてみる事にしました。

ソースコードコンパイルし、インストールすることにします。

MySQL 5.6.17インストール手順の備忘録

OS:CentOS release 6.5 (Final)

0.前提条件

# yum list installed cmake
Installed Packages
cmake.x86_64                          2.6.4-5.el6                          @base

1.ソースコードの取得

a.下記のURLアクセス
http://dev.mysql.com/downloads/mysql/
b.Select Platform:「Source Code」を設定
c.「Generic Linux (Architecture Independent), Compressed TAR Archive」横の
  「DownLoad」を押下
※ただし、ダウンロードにはユーザ登録が必要


2.取得したソースコードをアップロード
インストールを行うサーバに以下の資材を格納する。

・ソースコード:mysql-5.6.17.tar.gz
・ディレクトリ:/usr/local/src
# ls -l /usr/local/src/mysql-5.6.17.tar.gz
-rw-r--r-- 1 root root 32862539  5月 23 00:36 2014 /usr/local/src/mysql-5.6.17.tar.gz

3.ソースコードの解凍とインストール

# cd /usr/local/src/
# tar zxvf mysql-5.6.17.tar.gz
# cd mysql-5.6.17
# cmake -DMYSQL_DATADIR=/var/lib/mysql
※データディレクトリのみ指定する。
(cmake実行結果①)
-- Could NOT find Curses  (missing:  CURSES_LIBRARY CURSES_INCLUDE_PATH)
CMake Error at cmake/readline.cmake:85 (MESSAGE):
  Curses library not found.  Please install appropriate package,

上記メッセージが出力されたため、以下を実施。

# yum install ncurses-devel
# yum list installed ncurses-devel
Installed Packages
ncurses-devel.x86_64                  5.7-3.20090208.el6                   @base
# rm CMakeCache.txt
rm: remove 通常ファイル `CMakeCache.txt'? Y
(cmake実行結果②)
-- Configuring done
-- Generating done
-- Build files have been written to: /usr/local/src/mysql-5.6.17
[100%] Built target mysqltest_embedded
Scanning dependencies of target my_safe_process
[100%] Building CXX object mysql-test/lib/My/SafeProcess/CMakeFiles/my_safe_process.dir/safe_process.cc.o
Linking CXX executable my_safe_process
# make install
-- Installing: /usr/local/mysql/man/man1/mysql_zap.1
-- Installing: /usr/local/mysql/man/man1/myisam_ftdump.1
-- Installing: /usr/local/mysql/man/man8/mysqld.8
-- Installing: /usr/local/mysql/support-files/solaris/postinstall-solaris

4.ユーザ作成

# groupadd mysql
# useradd -g mysql mysql

5.データベースの初期化

# cd /usr/local/mysql
# scripts/mysql_install_db --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --user=mysql

6.ディレクトリの権限変更

# chown -R mysql:mysql /usr/local/mysql

7.起動スクリプトの配置

# cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld

8.設定ファイルの修正

# cp /usr/local/mysql/support-files/my-default.cnf
/etc/my.cnf
cp: `/etc/my.cnf' を上書きしてもよろしいですか(yes/no)? yes
# vi /etc/my.cnf
(以下を追加)
basedir = /usr/local/mysql
datadir = /usr/local/mysql/data
port = 3306
socket = /usr/local/mysql/mysql.sock

9.MySQLの起動

# /etc/init.d/mysqld start
Starting MySQL SUCCESS!

10.MySQLへの接続

# /usr/local/mysql/bin/mysql -h 127.0.0.1 -u mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 6
Server version: 5.6.17 Source distribution

Copyright (c) 2000, 2014, 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>

PostgreSQLのマスタ判断

PostgreSQLのストリーミングレプリケーションで、どちらのサーバがマスタとして動作しているのか、判断する方法です。
リカバリ中かを判定する関数を実行し、確認します。スレーブでは、マスタのWALを受け取り、リカバリを行うため、常にリカバリ中となります。


1.マスタとして動作していた場合 ※

$ psql -h localhost  -c  "SELECT pg_is_in_recovery();"
 pg_is_in_recovery
-------------------
 f
(1 行)


2.スレーブとして動作していた場合

$ psql -h localhost  -c  "SELECT pg_is_in_recovery();"
 pg_is_in_recovery
-------------------
 t
(1 行)


※ ただし、スタンドアローンで動作している場合も、fとなるので注意が必要。
  マスタとして動作しているのか、スタンドアローンで動作しているかは、
  以下のコマンドで確認する。
  スタンドアローンの場合、データが表示されない。

$ psql -h localhost  -c  "SELECT * FROM pg_stat_replication"

PostgreSQLでインデックスが使用されないSQL

以下にURLにインデックスが使用されないSQLが発見されたとのブログがあったので、忘れないようにメモ。

http://www.databasesoup.com/2014/05/new-finding-unused-indexes-query.html

-
可能な限り、PostgreSQLのクエリの見直しを行っている。
http://www.databasesoup.com/2014/04/new-new-index-bloat-query.html

上記の他にも、インデックスが使用されないクエリがある。以下のURLが参考になるでしょう。
https://gist.github.com/jberkus/6b1bcaf7724dfc2a54f3

とのこと
-