0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

ECCUBE2 のデータベースを PostgreSQL から MariaDB に切り替える

Last updated at Posted at 2023-04-01

MariaDB の用意

ECCUBE2 を MariaDB で使うようにインストール

MariaDB のテーブル数は、133 になります。(プラグインにより変わります。)

> show tables;
133 rows in set

PostgreSQL から持ってくるテーブルを削除

*_seq という名前のテーブル以外になります。

対象になるテーブルを抽出する SQL

show_tables.sql
select table_name from information_schema.tables
        where table_schema='test_db' and table_name not like '%_seq';
exit

削除コマンド

USER=scott
PASSWORD=tiger123
DBASE=test_db
mysql -u$USER -p$PASSWORD $DBASE < show_tables.sql > tmp01
awk '!/table_name/{print "drop table",$1";"}' tmp01 > drop_tables.sql
echo 'exit' >> drop_tables.sql
#
mysql -u$USER -p$PASSWORD $DBASE < drop_tables.sql > tmp02

テーブルの数は 33 になります。

> show tables;
33 rows in set

PostgreSQL からデータを export

go_export.sh
FILE_SQL="mar3101.sql"
#
export PGPASSWORD=tiger123
USER=scott
DBASE=test_db
pg_dump -h 'localhost' -U $USER \
	--inserts \
	--no-owner \
	--no-privileges \
	--disable-dollar-quoting \
	--column-inserts  $DBASE \
	 > $FILE_SQL
#

出力

mar3101.sql

SQL を MariaDB で読めるように変換

入力

mar3101.sql

go_henkan.sh
NAME="mar3101"
FILE_MOD=$NAME"_mod.sql"
./psql_to_maria.sh $NAME
psql_to_maria.sh
./postgresql_mysql.py < $1".sql" > $1"_mod.sql"
postgresql_mysql.py
#! /usr/bin/python
# -*- coding: utf-8 -*-
#
#	postgresql_mysql.py
#
#						Mar/12/2023
# --------------------------------------------------------------------
import sys
# --------------------------------------------------------------------
# sys.stderr.write("*** 開始 ***\n")

flag_sharp = False
lines = sys.stdin.readlines()
for line in lines:
	target=line[:-1]
	target = target.replace(" timestamp"," datetime")
	target = target.replace("smallint DEFAULT (0)::smallint","smallint DEFAULT (0)")
	target = target.replace("ALTER TABLE ONLY","ALTER TABLE")
	target = target.replace("without time zone DEFAULT now()","")
	target = target.replace("without time zone","")
#
	target = target.replace("CREATE OR REPLACE PROCEDURAL LANGUAGE plpgsql;","")
#
	target = target.replace("CREATE TABLE public.","CREATE TABLE ")
	target = target.replace("INSERT INTO public.","INSERT INTO ")
#
	if '::regclass' in target:
		target = "# " + target
	if (target[:3] == "SET"):
		target = "# " + target
	if 'CREATE INDEX' in target:
		target = "# " + target
	if 'ALTER SEQUENCE' in target:
		target = "# " + target
	if 'SELECT pg_catalog.set' in target:
		target = "# " + target
	if 'PROCEDURAL LANGUAGE plpgsql' in target:
		target = "# " + target
	if 'ALTER TABLE public.' in target:
		target = "# " + target
		if not ';' in target:
			flag_sharp = True
#
#
	if 'CREATE SEQUENCE' in target:
		flag_sharp = True
	elif 'CREATE EXTENSION' in target:
		flag_sharp = True
	elif 'COMMENT ON' in target:
		flag_sharp = True
#
#
	if 'INSERT INTO dtb_session' in target:
		flag_sharp = True
#
#
	if (flag_sharp):
		target = "# " + target
	print(target)
	llx = len(target)
#	sys.stderr.write("llx = %d\n" % llx)
#	if ';' in target:
	if (0 < llx):
		if target[llx-1] == ';':
			flag_sharp = False
# sys.stderr.write("*** 終了 ***\n")
# --------------------------------------------------------------------

出力

mar3101_mod.sql

MariaDB へインポート

入力

mar3101_mod.sql

go_import.sh
USER=scott
PASSWORD=tiger123
DBASE=test_db
LOG=/tmp/tmp0036.txt
FILE_IN="mar3101_mod.sql"
#
#
date > $LOG
mysql -u$USER -p$PASSWORD $DBASE < $FILE_IN >> $LOG
date >> $LOG
#

次の処理は、SSD でないと3時間以上かかるので、バックグラウンドで処理します。

./go_import.sh &

*_seq の値を調べる

*_seq というテーブルの一覧

MariaDB で調べる SQL

> show tables like '%_seq';
seq.txt
dtb_api_account_api_account_id_seq
dtb_api_config_api_config_id_seq
dtb_best_products_best_id_seq
dtb_bloc_bloc_id_seq
dtb_category_category_id_seq
dtb_class_class_id_seq
dtb_classcategory_classcategory_id_seq
dtb_csv_no_seq
dtb_csv_sql_sql_id_seq
dtb_customer_customer_id_seq
dtb_deliv_deliv_id_seq
dtb_holiday_holiday_id_seq
dtb_kiyaku_kiyaku_id_seq
dtb_mail_history_send_id_seq
dtb_mailmaga_template_template_id_seq
dtb_maker_maker_id_seq
dtb_member_member_id_seq
dtb_module_update_logs_log_id_seq
dtb_news_news_id_seq
dtb_order_detail_order_detail_id_seq
dtb_order_order_id_seq
dtb_other_deliv_other_deliv_id_seq
dtb_pagelayout_page_id_seq
dtb_payment_payment_id_seq
dtb_plugin_hookpoint_plugin_hookpoint_id_seq
dtb_plugin_plugin_id_seq
dtb_products_class_product_class_id_seq
dtb_products_product_id_seq
dtb_review_review_id_seq
dtb_send_history_send_id_seq
dtb_tax_rule_tax_rule_id_seq
plg_productoptions_dtb_option_option_id_seq
plg_productoptions_dtb_optioncategory_optioncategory_id_seq
go_gen.sh
awk -f aw02 seq.txt > get_seq.sql
./go_pg < get_seq.sql > tmp01
awk '/_seq/{print $1,$3}' tmp01 > tmp02
awk '{print$1,$2+1}' tmp02 > seq_value.txt
aw02
{print "select sequence_name,last_value from "$1";"}
go_pg
export PGPASSWORD=tiger123
USER=scott
DBASE=test_db
psql -U $USER $DBASE

出力

seq_value.txt

MariaDB に seq の値をセット

入力

seq_value.txt

go_seq_gen.sh
awk -f aw_alter seq_value.txt  > alter_value.sql
./gom < alter_value.sql
gom
USER=scott
PASSWORD=tiger123
DBASE=test_db
mysql -u$USER -p$PASSWORD $DBASE

config.php の修正

DB_TYPE を、pgsql から mysql に変更
DB_USER,DB_PASSWORD,DB_NAME も変更した場合は変更

data/config/config.php
省略
define('DB_TYPE', 'mysql');
define('DB_USER', 'scott');
define('DB_PASSWORD', 'tiger123');
define('DB_SERVER', '127.0.0.1');
define('DB_NAME', 'test_db');
省略

確認したバージョン

EC-CUBEバージョン 2.13.5
PostgreSQL 9.2.24
10.3.38-MariaDB

0
0
1

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?