LoginSignup
4
7

More than 3 years have passed since last update.

pgloaderでMySQL→Postgresへの移行を行う

Last updated at Posted at 2019-10-22

はじめに

pgloaderでMySQLで作成したZabbixのDBをPostgresにインポートする手順を記載する。

環境

  • CentOS 7.6
  • Zabbix 4.2
  • Postgres 11.5
  • MySQL 8.0

pgloaderのインストール

* gitのインストール
sudo yum install -y git


* インストールしたいディレクトリに移動
cd /usr/local/src

* インストール
git clone https://github.com/dimitri/pgloader.git
cd pgloader
chmod +x ./bootstrap-centos7.sh
sudo ./bootstrap-centos7.sh
make pgloader

下記のようなエラーが発生した場合は、10を入力し、
CONTINUEしたところ、正常にインストールされた。(インストールが正常に行われたかは不明)

...
[package pgloader.citus]..........................                                                                                                 [3537/4093]
[package pgloader.utils]                                                                                                                                      
debugger invoked on a NAME-CONFLICT in thread                                                                                                                 
#<THREAD "main thread" RUNNING {10005E85B3}>:                                                                                                                 
  EXPORT PGLOADER.CATALOG:FIELD-NAME causes name-conflicts in                                                                                                 
  #<PACKAGE "PGLOADER.PARSER"> between the following symbols:                                                                                                 
    PGLOADER.CATALOG:FIELD-NAME, PGLOADER.PARSER::FIELD-NAME                                                                                                  
See also:                                                                                                                                                     
  The ANSI Standard, Section 11.1.1.2.5                                                                                                                       

Type HELP for debugger help, or (SB-EXT:EXIT) to exit from SBCL.                                                                                              

restarts (invokable by number or by possibly-abbreviated name):                                                                                               
  0: [KEEP-OLD                     ] Keep PGLOADER.PARSER::FIELD-NAME accessible in PGLOADER.PARSER (shadowing PGLOADER.CATALOG:FIELD-NAME).                  
  1: [TAKE-NEW                     ] Make PGLOADER.CATALOG:FIELD-NAME accessible in PGLOADER.PARSER (uninterning PGLOADER.PARSER::FIELD-NAME).                
  2: [RESOLVE-CONFLICT             ] Resolve conflict.                                                                                                        
  3: [RETRY                        ] Retry                                                                                                                    
                                     compiling #<CL-SOURCE-FILE "pgloader" "src" "package">.                                                                  
  4: [ACCEPT                       ] Continue, treating                                                                                                       
                                     compiling #<CL-SOURCE-FILE "pgloader" "src" "package">                                                                   
                                     as having been successful.                                                                                               
  5:                                 Retry ASDF operation.                                                                                                    
  6: [CLEAR-CONFIGURATION-AND-RETRY] Retry ASDF operation after resetting the                                                                                 
                                     configuration.                                                                                                           
  7:                                 Retry ASDF operation.                                                                                                    
  8:                                 Retry ASDF operation after resetting the                                                                                 
                                     configuration.                                                                                                           
  9: [ABORT                        ] Give up on "pgloader"                                                                                                    
 10: [CONTINUE                     ] Ignore runtime option --eval "(ql:quickload \"pgloader\")".                                                              
 11:                                 Skip rest of --eval and --load options.                                                                                  
 12:                                 Skip to toplevel READ/EVAL/PRINT loop.                                                                                   
 13: [EXIT                         ] Exit SBCL (calling #'EXIT, killing the process).                                                                         

(NAME-CONFLICT #<PACKAGE "PGLOADER.PARSER"> EXPORT PGLOADER.CATALOG:FIELD-NAME PGLOADER.CATALOG:FIELD-NAME PGLOADER.PARSER::FIELD-NAME)                       
0]

zabbixを停止

systemctl stop zabbix-server.service
systemctl status zabbix-server.service

mysqlを起動

mysqlが起動していない場合は、起動する。

systemctl start mysqld.service
systemctl status mysqld.service

postgresを起動

postgresが起動していない場合は、起動する。

systemctl start postgresql-11.service
systemctl status postgresql-11.service

初期セットアップが完了していない場合、下記を実施。

sudo -u postgres /usr/pgsql-11/bin/initdb -D /var/lib/pgsql/data -X /var/lib/pgsql/data/pg_xlog --encoding=UTF8 --locale=ja_JP.UTF-8

postgresにzabbixのDB作成

* ユーザの作成
createuser -U postgres zabbix --pwprompt -S -R -D

* DBの作成
createdb -U postgres zabbix -O zabbix


※ -Oでオーナーをつけるのを忘れた場合は、下記コマンドで後からつける。

psql -U postgres -c"ALTER DATABASE zabbix OWNER TO zabbix"

* zabbixのテーブルを作成
zcat /usr/share/doc/zabbix-server-pgsql-*/create.sql.gz | psql -U zabbix zabbix

mysql→postgresへのマイグレーション

  • スキーマをPostgresの"public"に設定するためのスクリプトファイルを作成

$ vim /tmp/commands.load

    load database
       from mysql://root:root@localhost/zabbix
       into pgsql://postgres:postgres@localhost/zabbix
       alter schema 'zabbix' rename to 'public';
  • pgloaderでマイグレーション
* pgloaderでマイグレーション

build/bin/pgloader mysql://root:user_password@localhost/zabbix postgresql://postgres:user_password@localhost/zabbix

build/bin/pgloader /tmp/commands.load

* 結果確認
tail /tmp/pgloader/pgloader.log

* DBのエンコーディングを確認
mysql -u zabbix -pzabbix -e "SHOW CREATE DATABASE zabbix;"
psql -U postgres -l

* テーブル情報を確認
mysql -u root -ppassword -e "SHOW TABLES FROM zabbix;"
psql -U zabbix -d zabbix -c "\dt"

※ zabbixのDBのテーブル情報を確認し、OWNERがzabbixでなかった場合は、ALTERでOWNERを変更する
https://qiita.com/YusukeHigaki/items/ea81633bd1ccac078c7c

* ユーザ情報を表示
mysql -u root -ppassword -e "select host,user from mysql.user;"
psql -U zabbix -d zabbix -c "\du"

Zabbixの設定ファイルを修正

下記のようにconfファイルを変更。

$ vi /etc/zabbix/zabbix_server.conf

#DBSocket=/var/lib/mysql/mysql.sock
DBPort=5432
$ vi /etc/zabbix/web/zabbix.conf.php

# SELECT IBM_DB2, MYSQL, ORACLE, POSTGRESQL.
#$DB['TYPE']     = 'MYSQL';
$DB['TYPE']     = 'POSTGRESQL';
$DB['SERVER']   = 'localhost';
$DB['PORT']     = '5432';

Zabbixを再起動

systemctl start zabbix-server.service
systemctl status zabbix-server.service

参考

4
7
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
7