tempファイルの拡張・縮小

(8.0.22)
https://dev.mysql.com/doc/refman/8.0/ja/innodb-temporary-tablespace.html

グローバル一時テーブルスペース
innodb_temp_data_file_pathでサイズ指定可能(要DB再起動)

セッション一時テーブルスペース
→サイズ指定の拡張、縮小はできない

 

 

(19c)

drop tablespace temp2 including contents and datafiles;
create temporary tablespace temp2 tempfile '/u01/app/oracle/oradata/ORCL/pdb1/temp2.dbf' size 10M autoextend off;

select property_name, property_value
from database_properties
where property_name='DEFAULT_TEMP_TABLESPACE';

alter database default temporary tablespace temp2;

select file_name,bytes/1024/1024,autoextensible from dba_temp_files;


サイズ指定の拡張
alter database tempfile '/u01/app/oracle/oradata/ORCL/pdb1/temp2.dbf' resize 11M;


サイズ指定の縮小
alter database tempfile '/u01/app/oracle/oradata/ORCL/pdb1/temp2.dbf' resize 10M;

格納されているデータサイズ以下に縮小はできない

 

ファイル追加
alter tablespace temp2 add tempfile '/u01/app/oracle/oradata/ORCL/pdb1/test22.dbf' size 10M autoextend off;

ファイル削除
alter tablespace temp2 drop tempfile '/u01/app/oracle/oradata/ORCL/pdb1/test22.dbf';

 

(13)

サイズ指定の拡張、縮小はできない

 

(2019)
https://support.microsoft.com/ja-jp/topic/sql-server-%E3%81%A7-tempdb-%E3%83%87%E3%83%BC%E3%82%BF%E3%83%99%E3%83%BC%E3%82%B9%E3%82%92%E5%9C%A7%E7%B8%AE%E3%81%99%E3%82%8B%E6%96%B9%E6%B3%95-ea0a95c2-eff8-7075-9ee2-2ee42226ca1c

select database_id,name,file_id,physical_name,size * 8/1024 mbyte
from sys.master_files
;
exec sp_spaceused


サイズ指定の拡張

use tempdb
alter database tempdb modify file (name = tempdev , SIZE = 16MB);

サイズ指定の縮小

use tempdb
dbcc shrinkfile('tempdev' , 8)

※target_sizeはMB単位
格納されているデータ サイズ以下に、ファイルを圧縮することはできません。


tempファイルの追加

alter database tempdb add file
(name = tempdev2 ,filename = 'C:\test\tempdev2.ndf', SIZE = 8MB , FILEGROWTH = 64MB
)
;

ユーザ定義ファイルグループはtempdbでは許可されません

tempファイルの削除

alter database tempdb remove file tempdev2;

ファイルが空でない場合は削除できません
→シングルユーザモードで起動して削除できた

 

 

{Aurora}MySQL DB インスタンスから Aurora MySQL への移行

(1)Aurora への RDS for MySQL スナップショットの移行
(1-1)暗号化なしの場合

-- テスト用MySQL RDS作成

aws rds create-db-instance        \
--db-instance-identifier mysql01  \
--allocated-storage 20            \
--db-instance-class db.t3.micro   \
--engine mysql                    \
--master-username root            \
--master-user-password 'password' \
--no-multi-az                     \
--engine-version 5.7.34           \
--storage-type gp2                \
--no-publicly-accessible


-- スナップショット作成
aws rds create-db-snapshot           \
--db-snapshot-identifier snap01      \
--db-instance-identifier mysql01


-- インスタンススナップショットの一覧
aws rds describe-db-snapshots
aws rds describe-db-snapshots | jq -c '.DBSnapshots | [ .DBInstanceIdentifier, .DBSnapshotArn ] '

 

-- Auroraへのスナップショットの移行

aws rds restore-db-cluster-from-snapshot \
--db-cluster-identifier cluster01 \
--snapshot-identifier arn:aws:rds:ap-northeast-1:999999999999:snapshot:snap01 \
--engine aurora-mysql

aws rds create-db-instance \
--db-instance-identifier cluster01-instance01 \
--db-cluster-identifier cluster01 \
--db-instance-class db.t3.small \
--engine aurora-mysql

(1-2)暗号化ありの場合

-- テスト用MySQL RDS作成

aws rds create-db-instance        \
--db-instance-identifier mysql02  \
--allocated-storage 20            \
--db-instance-class db.t3.micro   \
--engine mysql                    \
--master-username root            \
--master-user-password 'password' \
--no-multi-az                     \
--engine-version 5.7.34           \
--storage-type gp2                \
--no-publicly-accessible          \
--storage-encrypted

-- スナップショット作成
aws rds create-db-snapshot           \
--db-snapshot-identifier snap02      \
--db-instance-identifier mysql02


-- インスタンススナップショットの一覧
aws rds describe-db-snapshots
aws rds describe-db-snapshots | jq -c '.DBSnapshots | [ .DBInstanceIdentifier, .DBSnapshotArn ] '

 

-- Auroraへのスナップショットの移行

aws rds restore-db-cluster-from-snapshot \
--db-cluster-identifier cluster02 \
--snapshot-identifier arn:aws:rds:ap-northeast-1:999999999999:snapshot:snap02 \
--engine aurora-mysql

aws rds create-db-instance \
--db-instance-identifier cluster02-instance01 \
--db-cluster-identifier cluster02 \
--db-instance-class db.t3.small \
--engine aurora-mysql

 

(2)Aurora リードレプリカを使用した、MySQL DB インスタンスから Amazon Aurora MySQL DB クラスターへのデータの移行

1 つの MySQL DB インスタンスに対して作成できる Aurora リードレプリカは、1 つだけです。

(2-1)暗号化なしの場合


-- テスト用MySQL RDS作成

aws rds create-db-instance        \
--db-instance-identifier mysql01  \
--allocated-storage 20            \
--db-instance-class db.t3.micro   \
--engine mysql                    \
--master-username root            \
--master-user-password 'password' \
--no-multi-az                     \
--engine-version 5.7.34           \
--storage-type gp2                \
--no-publicly-accessible


-- Aurora リードレプリカの作成
aws rds create-db-cluster \
--db-cluster-identifier cluster01 \
--engine aurora-mysql \
--replication-source-identifier arn:aws:rds:ap-northeast-1:999999999999:db:mysql01

aws rds create-db-instance \
--db-instance-identifier cluster01-instance01 \
--db-cluster-identifier cluster01 \
--db-instance-class db.t3.small \
--engine aurora-mysql

 

-- Aurora リードレプリカの昇格
aws rds promote-read-replica-db-cluster \
--db-cluster-identifier cluster01

aws rds describe-events

(2-2)暗号化ありの場合


-- テスト用MySQL RDS作成

aws rds create-db-instance        \
--db-instance-identifier mysql02  \
--allocated-storage 20            \
--db-instance-class db.t3.micro   \
--engine mysql                    \
--master-username root            \
--master-user-password 'password' \
--no-multi-az                     \
--engine-version 5.7.34           \
--storage-type gp2                \
--no-publicly-accessible          \
--storage-encrypted


-- Aurora リードレプリカの作成
aws rds create-db-cluster \
--db-cluster-identifier cluster02 \
--engine aurora-mysql \
--replication-source-identifier arn:aws:rds:ap-northeast-1:999999999999:db:mysql02 \
--storage-encrypted

aws rds create-db-instance \
--db-instance-identifier cluster02-instance01 \
--db-cluster-identifier cluster02 \
--db-instance-class db.t3.small \
--engine aurora-mysql


-- Aurora リードレプリカの昇格
aws rds promote-read-replica-db-cluster \
--db-cluster-identifier cluster02

aws rds describe-events

 

{Aurora}Amazon S3 バケットを使用した MySQL からのデータ移行

https://tech.drecom.co.jp/how-to-migrate-to-aws/
https://aws.amazon.com/jp/premiumsupport/knowledge-center/migrate-mysql-aurora-innobackup/

 

外部MySQL→EC2(Amazon Linux 2)に作成したMySQL 5.7
バックアップファイルの暗号化→なし
データレプリケーション時のSSL→使用しない

※ユーザやストアドプログラムは別途移行が必要

 

-- 1.MySQLインストール

 

sudo yum localinstall https://dev.mysql.com/get/mysql80-community-release-el7-1.noarch.rpm

sudo yum-config-manager --disable mysql80-community
sudo yum-config-manager --enable mysql57-community

sudo yum info mysql-community-server

sudo yum -y install mysql-community-server

sudo mysqld --version

sudo systemctl enable mysqld.service
sudo systemctl restart mysqld.service
sudo systemctl status mysqld.service

sudo cat /var/log/mysqld.log | grep password

sudo mysql_secure_installation


-- 初期設定

sudo vim /etc/my.cnf
log-bin=mysql-bin
server-id=1
innodb_flush_log_at_trx_commit=1
sync_binlog=1
character_set_server=utf8mb4

 

-- 2.テストデータ作成
テーブル、ファンクション、プロシージャ、ユーザ

mysql -u root -p

drop database test;
create database test;
use test;
drop table tab1;
create table tab1(col1 int);
insert into tab1 values(1);
select * from tab1;

show create table tab1;

drop function func1;

delimiter //
create function func1()
returns int
deterministic
begin
return 0;
end//
delimiter ;


show create function func1\G


drop procedure proc1;

delimiter //
create procedure proc1()
begin
select 1;
end//
delimiter ;

show create procedure proc1\G

drop user user1@localhost;
create user user1@localhost identified by 'password';
select user,host from mysql.user;


-- 3.Percona XtraBackup のインストール

sudo yum -y install https://repo.percona.com/yum/percona-release-latest.noarch.rpm
sudo yum -y install perl-DBD-MySQL
sudo yum -y install percona-xtrabackup-24
xtrabackup --version

 

-- 4.S3バケット作成

aws s3 mb s3://bucket123
aws s3 ls

-- 5.IAM サービスロールの作成

-- ポリシーの作成
※マニュアルの記載と異なり、"s3:GetBucketLocation"が必要


vim policy01.json

 

{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"s3:ListBucket",
"s3:GetBucketLocation"
],
"Resource": [
"arn:aws:s3:::bucket123"
]
},
{
"Effect": "Allow",
"Action": [
"s3:GetObject"
],
"Resource": [
"arn:aws:s3:::bucket123/*"
]
}
]
}

 

aws iam create-policy \
--policy-name policy01 \
--policy-document file://policy01.json

 

-- ロールの作成
vim role01.json

{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Service": "rds.amazonaws.com"
},
"Action": "sts:AssumeRole"
}
]
}

aws iam create-role \
--role-name role01 \
--assume-role-policy-document file://role01.json


-- ポリシーをロールにアタッチ
aws iam attach-role-policy --policy-arn arn:aws:iam::999999999999:policy/policy01 --role-name role01

※マニュアルの記載と異なり、クラスターパラメータグループでのaws_default_s3_roleの設定は不要

 

 

 

-- 6.Amazon Aurora MySQL DB クラスターとして復元するファイルのバックアップ

sudo mkdir -p /backup
sudo chown mysql:mysql /backup
sudo chmod 777 /backup

sudo -E xtrabackup --backup --user=root --host=localhost --password='password' --target-dir=/backup

 

 

-- 7.バックアップディレクトリおよびファイルを Amazon S3 バケットにコピー
sudo -E aws s3 cp /backup s3://bucket123 --recursive

aws s3 ls s3://bucket123


-- 8.Amazon S3 バケットからの Amazon Aurora MySQL DB クラスターの復元

aws rds restore-db-cluster-from-s3 \
--db-cluster-identifier cluster01 \
--engine aurora-mysql \
--master-username root \
--master-user-password 'password' \
--source-engine mysql \
--source-engine-version 5.7.35 \
--s3-bucket-name bucket123 \
--s3-ingestion-role-arn arn:aws:iam::999999999999:role/role01

 

aws rds create-db-instance \
--db-instance-identifier cluster01-instance01 \
--db-cluster-identifier cluster01 \
--db-instance-class db.t3.small \
--engine aurora-mysql

 


-- 9.Amazon Aurora MySQL DB クラスターと外部の MySQL データベースを同期する

aws rds describe-events


-- 外部DBで実行
host cluster01-instance01.xxxxxxxxxxxx.ap-northeast-1.rds.amazonaws.com

インスタンスエンドポイントのIPを使用する

CREATE USER 'replication'@'172.31.333.333' IDENTIFIED BY 'password';
GRANT REPLICATION CLIENT, REPLICATION SLAVE ON *.* TO 'replication'@'172.31.333.333';

select user,host from mysql.user;

外部 MySQL データベースをホストするサーバーとAurora MySQL DB クラスタ
それぞれのセキュリティグループの設定で双方向の全通信を許可する


-- Auroraで実行
CALL mysql.rds_set_external_master ( '172.31.444.444' , 3306 , 'replication' , 'password' , 'mysql-bin.000001' , 1234 , 0 );

CALL mysql.rds_start_replication;

SHOW SLAVE STATUS\G

CALL mysql.rds_stop_replication;

 


-- 10.クリーンアップ

-- ロールの一覧
aws iam list-roles | grep role01

-- ロールの削除

aws iam detach-role-policy \
--role-name role01 \
--policy-arn arn:aws:iam::999999999999:policy/policy01


aws iam delete-role --role-name role01

-- ポリシーの一覧
aws iam list-policies | grep policy01

-- ポリシーの削除
aws iam delete-policy \
--policy-arn arn:aws:iam::999999999999:policy/policy01

-- バケット一覧
aws s3 ls

-- バケット削除
aws s3 rb s3://bucket123 --force

 

データファイルの拡張・縮小

(8.0.22)
https://dev.mysql.com/doc/refman/8.0/ja/innodb-system-tablespace.html

①file-per-table テーブルスペース
(innodb_file_per_table=1の場合)
→サイズ指定の拡張、縮小はできない


②システムテーブルスペース
(innodb_file_per_table=0の場合)

→サイズ指定の拡張、縮小はできない


③一般テーブルスペース

→サイズ指定の拡張、縮小はできない

 

 

(19c)

drop tablespace test including contents and datafiles;
create tablespace test datafile '/u01/app/oracle/oradata/ORCL/pdb1/test.dbf' size 10M autoextend off;

select
d.tablespace_name,
d.mbytes "total[MB]",
NVL(f.mbytes,0) "free[MB]",
d.mbytes - NVL(f.mbytes,0) "used[MB]",
(1 - (NVL(f.mbytes,0)/d.mbytes))*100 "used_percent"
from
(SELECT tablespace_name, (SUM(bytes)/(1024*1024)) mbytes
FROM dba_data_files GROUP BY tablespace_name) d
left outer join
(SELECT tablespace_name, (SUM(bytes)/(1024*1024)) mbytes
FROM dba_free_space GROUP BY tablespace_name) f
on d.tablespace_name=f.tablespace_name
;

サイズ指定の拡張
alter database datafile '/u01/app/oracle/oradata/ORCL/pdb1/test.dbf' resize 11M;


サイズ指定の縮小
alter database datafile '/u01/app/oracle/oradata/ORCL/pdb1/test.dbf' resize 10M;

格納されているデータ サイズ以下に縮小はできない

 

ファイル追加
alter tablespace test add datafile '/u01/app/oracle/oradata/ORCL/pdb1/test2.dbf' size 10M autoextend off;

ファイル削除
alter tablespace test drop datafile '/u01/app/oracle/oradata/ORCL/pdb1/test2.dbf';

 

(13)

サイズ指定の拡張、縮小はできない

 

(2019)
https://qiita.com/gigadein/items/1b558286cfc7695a0d95

select database_id,name,file_id,physical_name,size * 8/1024 mbyte
from sys.master_files
where database_id = DB_ID()
;


alter database test remove file tbs1_01;
alter database test remove filegroup tbs1;

alter database test add filegroup tbs1;
alter database test add file
(name = tbs1_01 ,filename = 'C:\test\tbs1_01.ndf'
) to filegroup tbs1
;

サイズ指定の拡張

use test
alter database test modify file (name = tbs1_01 , SIZE = 560MB);

サイズ指定の縮小

use test
dbcc shrinkfile('tbs1_01' , 550)

※target_sizeはMB単位
格納されているデータ サイズ以下に、ファイルを圧縮することはできません。

 

 

データファイル拡張できないときの挙動

(8.0.22)
https://dev.mysql.com/doc/refman/8.0/ja/innodb-create-table-external.html


file-per-table テーブルスペースまたは一般テーブルスペースが DATA DIRECTORY = 句を使用して作成された場合、
.ibd ファイルは通常のデータディレクトリ外の指定されたパスにあります。

MySQL 8.0.21 では、DATA DIRECTORY 句を使用してデータディレクトリの外部で作成されるテーブルおよびテーブルパーティションは、
InnoDB で認識されるディレクトリに制限されます。

SELECT @@datadir,@@innodb_data_home_dir,@@innodb_directories;

 

100MBのディスクを追加しマウント
このディスクにテーブルを作成する

fdisk /dev/sdb
mkfs.xfs /dev/sdb1
mkdir /mnt/mysqltest
mount -t xfs /dev/sdb1 /mnt/mysqltest
chown mysql:mysql /mnt/mysqltest

vim /etc/my.cnf
[mysqld]
innodb_directories="/mnt/mysqltest"


drop table tab1;
create table tab1(col1 int) data directory = '/mnt/mysqltest';
insert into tab1 values(1);

insert into tab1 select * from tab1;


※ログに出力されたメッセージ
2021-07-21T23:38:33.710293Z 8 [ERROR] [MY-012144] [InnoDB] posix_fallocate(): Failed to preallocate data for file /mnt/mysqltest/test/tab1.ibd, desired size 4194304 bytes. Operating system error number 28. Check that the disk is not full or a disk quota exceeded. Make sure the file system supports this function. Refer to your operating system documentation for operating system error code information.
2021-07-21T23:38:33.736401Z 8 [Warning] [MY-012638] [InnoDB] Retry attempts for writing partial data failed.
2021-07-21T23:38:33.736447Z 8 [ERROR] [MY-012639] [InnoDB] Write to file /mnt/mysqltest/test/tab1.ibd failed at offset 94371840, 1048576 bytes should have been written, only 0 were written. Operating system error number 28. Check that your OS and file system support files of this size. Check also that the disk is not full or a disk quota exceeded.
2021-07-21T23:38:33.736466Z 8 [ERROR] [MY-012640] [InnoDB] Error number 28 means 'No space left on device'
2021-07-21T23:38:33.736479Z 8 [Warning] [MY-012145] [InnoDB] Error while writing 4194304 zeroes to /mnt/mysqltest/test/tab1.ibd starting at offset 92274688
2021-07-21T23:38:33.736645Z 8 [ERROR] [MY-013132] [Server] The table 'tab1' is full!

※クライアント側で出力されたメッセージ
ERROR 1114 (HY000): The table 'tab1' is full

 

(19c)

drop tablespace test including contents and datafiles;
create tablespace test datafile '/u01/app/oracle/oradata/ORCL/pdb1/test.dbf' size 10M autoextend off;

drop table tab1 purge;
create table tab1(col1 int) tablespace test;
insert into tab1 values(1);
commit;

insert into tab1 select * from tab1;


※ログに出力されたメッセージ
PDB1(3):ORA-1653: unable to extend table TEST.TAB1 by 128 in tablespace TEST [PDB1]

※クライアント側で出力されたメッセージ
*
行1でエラーが発生しました。:
ORA-01653: 表TEST.TAB1を128(表領域TEST)で拡張できません

 

(13)
100MBのディスクを追加し、/mnt/postgresqltestにマウント

fdisk /dev/sdb
mkfs.xfs /dev/sdb1
mkdir /mnt/postgresqltest
mount -t xfs /dev/sdb1 /mnt/postgresqltest
chown postgres:postgres /mnt/postgresqltest

drop tablespace test;
create tablespace test owner postgres location '/mnt/postgresqltest';


drop table tab1;
create table tab1(col1 int) tablespace test;
insert into tab1 values(1);

insert into tab1 select * from tab1;


※ログに出力されたメッセージ
2021-07-22 09:08:07.440 JST [2064] ERROR: could not extend file "pg_tblspc/403445/PG_13_202007201/73869/403446": No space left on device
2021-07-22 09:08:07.440 JST [2064] HINT: Check free disk space.

※クライアント側で出力されたメッセージ
ERROR: could not extend file "pg_tblspc/403445/PG_13_202007201/73869/403446": No space left on device
HINT: Check free disk space.

(2019)

use master

alter database test remove file test;
alter database test remove filegroup test;

alter database test add filegroup test;
alter database test add file
(name = test ,filename = 'C:\test\test.ndf', SIZE = 8192KB , FILEGROWTH = 0
) to filegroup test
;

use test

drop table tab1;
create table tab1(col1 int) on test;
insert into tab1 values(1);

insert into tab1 select * from tab1;

 

※ログに出力されたメッセージ
2021-07-22 09:19:13.72 spid131 エラー: 1105、重大度: 17、状態: 2。
2021-07-22 09:19:13.72 spid131 Could not allocate space for object 'dbo.tab1' in database 'test' because the 'test' filegroup is full. Create disk space by deleting unneeded files, dropping objects in the filegroup, adding additional files to the filegroup, or setting autogrowth on for existing files in the filegroup.

※クライアント側で出力されたメッセージ
メッセージ 1105、レベル 17、状態 2、行 7
データベース 'test' にオブジェクト 'dbo.tab1' の領域を割り当てられませんでした。'test' ファイル グループがいっぱいです。不要なファイルの削除、ファイル グループ内のオブジェクトの削除、ファイル グループへの新しいファイルの追加、またはファイル グループの既存のファイルの自動拡張の設定のいずれかを行ってディスク領域を作成してください。