it-source

MySQL 또는 MariaDB에 메모리 내 데이터베이스가 있습니까?

criticalcode 2023. 4. 21. 21:00
반응형

MySQL 또는 MariaDB에 메모리 내 데이터베이스가 있습니까?

유닛/통합 테스트를 하고 있습니다.SQLite는 지원하지 않습니다.RIGHT JOIN그리고.FULL OUTER JOINMySQL(또는 MariaDB)을 메모리에 완전히 저장하는 방법은 없습니까?MySQL에는 MEMORY 테이블엔진이 탑재되어 있지만 테스트에 부정합이 발생할 수 있습니다.

SQLite의 :memory: 대신 MySQL과 동일한 기능이 필요합니다.제 문제는 성능입니다.SQLite 데이터베이스의 메모리 내에서는 테스트 프로세스가 빨라지지만 일부 쿼리는 SQLite와 호환되지 않습니다.또한 프로덕션 데이터베이스가 MariaDB인 경우 SQLite에서 테스트를 수행하는 것은 좋지 않습니다.

MariaDB에는 MEMORY 스토리지 엔진이 있습니다.

다른 테이블의 데이터 읽기 전용 캐시 또는 임시 작업 영역에 가장 적합합니다.

자동 테스트 중에 데이터베이스를 빠르게 셋업하고 해체하는 데 적합합니다.

이 질문에 대한 다양한 접근법에 대한 몇 년간의 조사와 테스트 후.이 문제를 해결할 수 있는 최선의 방법은 도커를 사용하는 것이었습니다.보다 정확하게 tmpfs 볼륨을 사용합니다.이렇게 하면 데이터베이스가 디스크에 유지되지 않고 I/O 차단에 시달리지 않습니다.

관심 있는 분들을 위해 Docker는 이러한 유형의 볼륨에 대한 자체 문서를 참조해 주십시오.

tmpfs에 MariaDB/MySQL 스토리지를 복사하는 systemd 유닛을 사용하여 해킹합니다.
표준 Ubuntu/Debian 설치용으로 작성되었지만 다른 환경에도 적용할 수 있어야 합니다.더하다sudo필요한 경우.

데이터 손실 위험: 사용 사례에 적합한지 철저히 조사합니다.
(데이터베이스 사이즈도 확인합니다).

# This systemd unit puts the MariaDB/MySQL storage on tmpfs and (tries to) sync
# it back at shutdown.

# Of course, in case of power failure or any problem preventing this
# unit to stop properly and sync back the data to disk : DATA LOSS.

# To install on Debian/Ubuntu :
# - apt install rsync
# - systemctl stop mariadb
# - write this content to /etc/systemd/system/mariadbontmpfs.service
# - systemctl enable mariadbontmpfs.service
# - systemctl start mariadbontmpfs.service
# - systemctl start mariadb

# This file is free software licensed under the WTFPL (v2)

[Unit]
Description=Put /var/lib/mysql on tmpfs before MariaDB/MySQL starts
Before=mariadb.service mysql.service
ConditionPathIsDirectory=/var/lib/mysql
ConditionPathExists=/usr/bin/rsync

[Install]
WantedBy=multi-user.target

[Service]
Type=oneshot
RemainAfterExit=yes
Restart=no

# We double-check that MariaDB is not running before doing anything
ExecCondition=/bin/sh -c "systemctl --quiet is-active mariadb mysql && echo 'MariaDB or MySQL still active' && exit 255 || exit 0"

# If the ON-DISK directory doesn't exist, simply rename /var/lib/mysql to
# /var/lib/mysql.ON-DISK and recreate /var/lib/mysql
ExecStart=/bin/sh -c "if [ ! -e /var/lib/mysql.ON-DISK ] ; then mv -v /var/lib/mysql /var/lib/mysql.ON-DISK && mkdir -v /var/lib/mysql && chown -v mysql:mysql /var/lib/mysql; fi"
# Mount the tmpfs
ExecStart=/bin/mount -v -t tmpfs -o uid=mysql,gid=mysql,mode=0755 none /var/lib/mysql
# Copy the ON-DISK content to the tmpfs
ExecStart=/usr/bin/rsync -avH --delete /var/lib/mysql.ON-DISK/ /var/lib/mysql/

# At stop :
ExecStop=/bin/sh -c "echo 'Syncing tmpfs:/var/lib/mysql to disk'"
ExecStop=/usr/bin/rsync -avH --delete /var/lib/mysql/ /var/lib/mysql.ON-DISK/
ExecStop=/bin/umount -v /var/lib/mysql
ExecStop=/bin/sh -c "rmdir -v /var/lib/mysql && mv -v /var/lib/mysql.ON-DISK /var/lib/mysql"

풀 파일은 이쪽에서 입수할 수 있습니다.

언급URL : https://stackoverflow.com/questions/51523432/does-mysql-or-mariadb-have-in-memory-database

반응형