LoginSignup
4
2

More than 5 years have passed since last update.

RaspberryPi3のRedmine4.0.2のデータベースをMySQLからPostgreSQLにpgloaderで移行した

Last updated at Posted at 2019-02-27

現状の環境

  • Raspbian GNU/Linux 9 (stretch)
  • Redmine 4.0.2
  • MariaDB 10.1.37
  • apache2 + unicorn

前提

  • checkinstall 導入済み
  • PostgreSQL11.2をsystemdで導入する
    • インストールディレクトリ:/var/lib/postgresql
  • Redmineのディレクトリを %REDMINE_DIR% と記述します
  • Redmineの実行ユーザを %REDMINE_USER% と記述します
  • pgloaderは別ホストのCentOS7から実行します
    • 残念ながら、RaspberryPi でビルドしたバイナリは動きませんでした…
  • Redmineは停止済み
  • MariaDBは起動中

PostgreSQL11.2の導入

1.不足パッケージインストール(piユーザ)

sudo apt install libreadline-dev libsystemd-dev

他に足りないものがあればよしなに。

2.postgresユーザ作成(piユーザ)

sudo useradd postgres -s /bin/bash -m

3.postgresインストール用ディレクトリ作成(piユーザ)

sudo mkdir /var/lib/postgresql
sudo chown postgres:postgres /var/lib/postgresql

4.PostgreSQLをビルド(postgresユーザ)

環境変数を定義する(新規作成)

~/.bash_profile
export PGHOME=/var/lib/postgresql
export PGDATA=/var/lib/postgresql/data
PATH=$PGHOME/bin:$PATH

PostgreSQLのソースを取得してビルド

mkdir ~/src
wget https://ftp.postgresql.org/pub/source/v11.2/postgresql-11.2.tar.gz
tar zxf postgresql-11.2.tar.gz -C ~/src/
cd src/postgresql-11.2/
./configure --prefix /var/lib/postgresql --with-systemd
make
checkinstall -D --fstrans=no

/home/postgres/src/postgresql-11.2/postgresql_11.2-1_armhf.deb でパッケージが作成されました。

5.PostgreSQLインストール(piユーザ)

sudo dpkg -i /home/postgres/src/postgresql-11.2/postgresql_11.2-1_armhf.deb

6.initdb(postgresユーザ)

mkdir /var/lib/postgresql/data
initdb --encoding=UTF8

7.PostgreSQL設定変更(postgresユーザ)

以下の行を修正

/var/lib/postgresql/data/postgresql.conf
listen_addresses = '*'

以下の行を追加

/var/lib/postgresql/data/pg_hba.conf
host    all             all             0.0.0.0/0            md5

8.PostgreSQL起動(rootユーザ)

以下のファイルを新規作成

/etc/systemd/system/postgresql-11.service
[Unit]
Description=PostgreSQL 11 database server
Documentation=man:postgres(1)
After=syslog.target
After=network.target

[Service]
Type=notify

User=postgres
Group=postgres
Environment=PGDATA=/var/lib/postgresql/data/
ExecStart=/var/lib/postgresql/bin/postmaster -D ${PGDATA}
ExecReload=/bin/kill -HUP $MAINPID
KillMode=mixed
KillSignal=SIGINT
TimeoutSec=0

[Install]
WantedBy=multi-user.target

自動起動を有効&即時起動します。

systemctl enable postgresql-11
systemctl start postgresql-11

9.Redmine用ユーザ&DB作成(postgresユーザ)

psql -U postgres


CREATE ROLE %YOUR_NAME_PG% LOGIN ENCRYPTED PASSWORD '%YOUR_PASS_PG%' NOINHERIT VALID UNTIL 'infinity';
CREATE DATABASE %YOUR_DB_PG% WITH ENCODING='UTF8' OWNER=%YOUR_NAME_PG%;

'%YOUR_xx_PG%' はよしなに変更してください。

MySQL→PostgreSQLへのデータ移行

CentOS7で作業します。

rpm -ivh https://download.postgresql.org/pub/repos/yum/11/redhat/rhel-7-x86_64/pgloader-3.6.1-1.rhel7.x86_64.rpm
pgloader mysql://%YOUR_NAME_MS%:%YOUR_PASS_MS%@%YOUR_HOST_NAME%/%YOUR_DB_MS% pgsql://%YOUR_NAME_PG%:%YOUR_PASS_PG%@%YOUR_HOST_PG%/%YOUR_DB_PG%

'%YOUR_xx_MS%'、'%YOUR_xx_PG%' はよしなに変更してください。

Redmineの設定変更

1.RedmineのDB設定を変更する

変更前

%REDMINE_DIR%/config/database.yml
production:
  adapter: mysql2
  database: YOUR_DB_MS
  host: YOUR_HOST_MS
  username: YOUR_NAME_MS
  password: YOUR_PASS_MS
  encoding: utf8

変更後

%REDMINE_DIR%/config/database.yml
production:
  adapter: postgresql
  database: YOUR_DB_PG
  host: YOUR_HOST_PG
  username: YOUR_NAME_PG
  password: YOUR_PASS_PG
  encoding: utf8

2. postgresqlモジュールのgemを導入する

su - %REDMINE_USER% -s /bin/bash
cd %REDMINE_DIR%
bundle config build.pg --with-pg-config=/var/lib/postgresql/bin/pg_config
bundle install

Redmine起動

Wiki履歴等の文字化けもなく、問題なく動きました。

左:変更前、右:変更後

image.png

4
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
2