안녕하세요 이번글에서는 mariadb에서 spider엔진을 이용하여 데이터노드에 데이터를 분산하여 입력하는 테스트를 해보겠습니다.
기존의 MySQL의 샤딩은 DB단에서 구현하지 않고, DB 앞단에서 구현을 합니다.
Spider 엔진이란
Spider 스토리지 엔진은 샤딩 기능이 내장 된 스토리지 엔진입니다.
MariaDB에서는 Spider 엔진을 탑재하면서, DB단에서 샤딩을 구현할 수 있게 되었습니다.
샤딩 (Sharding)
Horizontal Sharding (Range based Sharding): 일반적인 샤딩은 수평 파티셔닝 방식을 뜻합니다. 수평 파티셔닝(Horizontal Sharding) 또는 샤딩은 스키마 복제 후 샤드키를 기준으로 데이터를 나누는것을 말합니다.
Vertical partitioning: 수직 파티셔닝(Vertical partitioning)은 스키마를 나누고 데이터가 따라 옮겨가는것을 말합니다. 엔티티의 컬럼을 분할하는 방식입니다. 도메인에 따라 쉽게 분리 할수 있으며, 도메인의 영향을 많이 받기 때문에 어플리케이션 단에서 CRUD 구현을 합니다.
테스트 환경
Hostname
|
IP
|
version
|
spider
|
192.168.100.114
|
mariadb 10.2
|
node1
|
192.168.100.52
|
mariadb 10.2
|
node2
|
192.168.100.53
|
mariadb 10.2
|
사전 구성
사전 maraidb 설치
▶ 데이터 노드 구성(node1, node2)
DB 및 테이블 생성
MariaDB [(none)]> CREATE TABLE backend.sbtest1 (
-> id int(10) unsigned NOT NULL AUTO_INCREMENT,
-> k int(10) unsigned NOT NULL DEFAULT '0',
-> c char(120) NOT NULL DEFAULT '',
-> pad char(60) NOT NULL DEFAULT '',
-> PRIMARY KEY (id),
-> KEY k (k)
-> ) ENGINE=InnoDB;
Query OK, 0 rows affected (0.01 sec)
DB user 생성 및 권한 부여
MariaDB [(none)]> create user 'shard'@'%' identified by 'shard';
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> grant all on *.* to shard@'%';
Query OK, 0 rows affected (0.00 sec)
Spider node 구성
Spider 엔진 설치(spider node)
[root@jh-spider ~]# mysql -uroot -proot < /mysql/share/install_spider.sql
MariaDB [(none)]> select engine, support, transactions, xa from information_schema.engines;
+--------------------+---------+--------------+------+
| engine | support | transactions | xa |
+--------------------+---------+--------------+------+
| SPIDER | YES | YES | YES |
| CSV | YES | NO | NO |
| MRG_MyISAM | YES | NO | NO |
| MyISAM | YES | NO | NO |
| SEQUENCE | YES | YES | NO |
| PERFORMANCE_SCHEMA | YES | NO | NO |
| MEMORY | YES | NO | NO |
| Aria | YES | NO | NO |
| InnoDB | DEFAULT | YES | YES |
+--------------------+---------+--------------+------+
9 rows in set (0.00 sec)
▶ 데이터 노드 정보 등록 (spider node)
MariaDB [(none)]> CREATE SERVER node1
-> FOREIGN DATA WRAPPER mysql
-> OPTIONS(
-> HOST '192.168.100.52',
-> DATABASE 'backend',
-> USER 'shard',
-> PASSWORD 'shard',
-> PORT 3306
-> );
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> CREATE SERVER node2
-> FOREIGN DATA WRAPPER mysql
-> OPTIONS(
-> HOST '192.168.100.53',
-> DATABASE 'backend',
-> USER 'shard',
-> PASSWORD 'shard',
-> PORT 3306
-> );
Query OK, 0 rows affected (0.00 sec)
▶ spider database 및 table 생성
MariaDB [(none)]> CREATE DATABASE IF NOT EXISTS backend;
Query OK, 1 row affected (0.00 sec)
MariaDB [(none)]> CREATE TABLE backend.sbtest1
-> (
-> id int(10) unsigned NOT NULL AUTO_INCREMENT,
-> k int(10) unsigned NOT NULL DEFAULT '0',
-> c char(120) NOT NULL DEFAULT '',
-> pad char(60) NOT NULL DEFAULT '',
-> PRIMARY KEY (id),
-> KEY k (k)
-> ) ENGINE=spider COMMENT='wrapper "mysql", table "sbtest1"'
-> PARTITION BY KEY (id)
-> (
-> PARTITION pt1 COMMENT = 'srv "node1"',
-> PARTITION pt2 COMMENT = 'srv "node2"'
-> ) ;
Query OK, 0 rows affected (0.00 sec)
▶ 계정 생성 및 권한 부여
MariaDB [(none)]> create user 'shard'@'%' identified by 'shard';
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> grant all on *.* to shard@'%';
Query OK, 0 rows affected (0.00 sec)
Data insert 테스트
▶ Sysbench 설치
[root@jh-spider ~]# curl -s https://packagecloud.io/install/repositories/akopytov/sysbench/script.rpm.sh | sudo bash
Detected operating system as centos/7.
Checking for curl...
Detected curl...
Downloading repository file: https://packagecloud.io/install/repositories/akopytov/sysbench/config_file.repo?os=centos&dist=7&source=script
done.
Installing pygpgme to verify GPG signatures...
Loaded plugins: fastestmirror, langpacks
Determining fastest mirrors
* base: mirror.navercorp.com
* extras: mirror.navercorp.com
* updates: ftp.jaist.ac.jp
akopytov_sysbench-source/signature | 833 B 00:00:00
Retrieving key from https://packagecloud.io/akopytov/sysbench/gpgkey
Importing GPG key 0x04DCFD39:
Userid : "https://packagecloud.io/akopytov/sysbench-prerelease (https://packagecloud.io/docs#gpg_signing) <support@packagecloud.io>"
Fingerprint: 9789 8d69 f99e e5ca c462 a0f8 cf10 4890 04dc fd39
From : https://packagecloud.io/akopytov/sysbench/gpgkey
akopytov_sysbench-source/signature | 1.0 kB 00:00:00 !!!
base | 3.6 kB 00:00:00
extras | 2.9 kB 00:00:00
updates | 2.9 kB 00:00:00
(1/4): base/7/x86_64/group_gz | 153 kB 00:00:00
(2/4): extras/7/x86_64/primary_db | 246 kB 00:00:00
(3/4): base/7/x86_64/primary_db | 6.1 MB 00:00:00
(4/4): updates/7/x86_64/primary_db | 14 MB 00:00:01
akopytov_sysbench-source/primary | 2.0 kB 00:00:01
akopytov_sysbench-source 15/15
Package pygpgme-0.3-9.el7.x86_64 already installed and latest version
Nothing to do
Installing yum-utils...
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
* base: mirror.navercorp.com
* extras: mirror.navercorp.com
* updates: ftp.jaist.ac.jp
Package yum-utils-1.1.31-54.el7_8.noarch already installed and latest version
Nothing to do
Generating yum cache for akopytov_sysbench...
Importing GPG key 0x04DCFD39:
Userid : "https://packagecloud.io/akopytov/sysbench-prerelease (https://packagecloud.io/docs#gpg_signing) <support@packagecloud.io>"
Fingerprint: 9789 8d69 f99e e5ca c462 a0f8 cf10 4890 04dc fd39
From : https://packagecloud.io/akopytov/sysbench/gpgkey
Generating yum cache for akopytov_sysbench-source...
The repository is setup! You can now install packages.
[root@jh-spider ~]# yum -y install sysbench
...(중략)
=============================================================================================
Package Arch Version Repository Size
=============================================================================================
Installing:
sysbench x86_64 1.0.20-1.el7 akopytov_sysbench 430 k
Installing for dependencies:
postgresql-libs x86_64 9.2.24-7.el7_9 updates 235 k
Transaction Summary
=============================================================================================
Install 1 Package (+1 Dependent package)
Total download size: 665 k
Installed size: 1.8 M
Downloading packages:
(1/2): postgresql-libs-9.2.24-7.el7_9.x86_64.rpm | 235 kB 00:00:00
(2/2): sysbench-1.0.20-1.el7.x86_64.rpm | 430 kB 00:00:00
---------------------------------------------------------------------------------------------
Total 663 kB/s | 665 kB 00:00:01
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : postgresql-libs-9.2.24-7.el7_9.x86_64 1/2
Installing : sysbench-1.0.20-1.el7.x86_64 2/2
Verifying : postgresql-libs-9.2.24-7.el7_9.x86_64 1/2
Verifying : sysbench-1.0.20-1.el7.x86_64 2/2
Installed:
sysbench.x86_64 0:1.0.20-1.el7
Dependency Installed:
postgresql-libs.x86_64 0:9.2.24-7.el7_9
Complete!
▶ Data insert (use sysbench)
만건 데이터 입력
[root@jh-spider ~]# sysbench /usr/share/sysbench/oltp_read_only.lua --db-driver=mysql --threads=16 --mysql-socket=/tmp/mysql.sock --mysql-db=test --mysql-user=shard --mysql-password=shard --mysql-port=3306 --table-size=10000 prepare
sysbench 1.0.20 (using bundled LuaJIT 2.1.0-beta2)
Initializing worker threads...
Creating table 'sbtest1'...
Inserting 10000 records into 'sbtest1'
Creating a secondary index on 'sbtest1'...
▶ Data 조회
MariaDB [(none)]> select count(*) from test.sbtest1;
+----------+
| count(*) |
+----------+
| 10000 |
+----------+
1 row in set (0.00 sec)
▶ 만건의 데이터를 backend DB로 복사
MariaDB [(none)]> insert into backend.sbtest1 select * from test.sbtest1;
ERROR 1429 (HY000): Unable to connect to foreign data source: node1
※ 위의 에러 발생시 아래의 명령 실행MariaDB [(none)]> use backendMariaDB [backend]> flush tables;Query OK, 0 rows affected (0.016 sec)
MariaDB [backend]> insert into backend.sbtest1 select * from test.sbtest1;Query OK, 10000 rows affected (0.255 sec)Records: 10000 Duplicates: 0 Warnings: 0
▶ 입력 후 data count 조회
MariaDB [backend]> select count(*) from backend.sbtest1;
+----------+
| count(*) |
+----------+
| 10000 |
+----------+
1 row in set (0.010 sec)
MariaDB [(none)]> select count(*) from backend.sbtest1;
+----------+
| count(*) |
+----------+
| 2960 |
+----------+
1 row in set (0.001 sec)
MariaDB [(none)]> select count(*) from backend.sbtest1;
+----------+
| count(*) |
+----------+
| 7040 |
+----------+
1 row in set (0.002 sec)
참고
'DataBase > MySQL & MariaDB' 카테고리의 다른 글
[MySQL - InnoDB cluster] part 1 Mysql 설치 & shell설치 (0) | 2022.05.14 |
---|---|
[Mysql - Performance parameter] (0) | 2022.03.24 |
[MariaDB - MHA Chain replication 구성을 통한 데이터 이관] (0) | 2022.03.10 |
[MySQL - MSR(Multi Source Replication)구성] (2) | 2022.03.10 |
[Mysql - Haproxy 구성] (0) | 2022.03.10 |