今天安装数据库,要求使用lvm划分磁盘,方便以后添加容量。
磁盘总大小为500G,用来安装MySQL数据库。准备划分400G存储数据文件,100G存储bin日志
操作系统:CentOS Linux release 7.3.1611 (Core)
基本原理如下图:
http://linux.vbird.org/linux_basic/0420quota.php#lvm
详细原理可参考鳥哥的 Linux 私房菜
[root@plat-ecloud01-oscar-prod-db-mysql01 ~]# fdisk -l
Disk /dev/vda: 21.5 GB, 21474836480 bytes, 41943040 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x000b9a2e
Device Boot Start End Blocks Id System
/dev/vda1 * 2048 41929649 20963801 83 Linux
Disk /dev/vdb: 536.9 GB, 536870912000 bytes, 1048576000 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
将整个/dev/vdb创建为Physical Volume,即物理卷
物理卷在逻辑卷管理中处于最底层,它可以是实际物理硬盘上的分区,也可以是整个物理硬盘,也可以是raid设备。
[root@plat-ecloud01-oscar-prod-db-mysql01 ~]# pvcreate /dev/vdb
Physical volume "/dev/vdb" successfully created.
查看pv状态
[root@plat-ecloud01-oscar-prod-db-mysql01 ~]# pvs
PV VG Fmt Attr PSize PFree
/dev/vdb lvm2 --- 500.00g 500.00g
用新建的物理卷创建Volumne Group,即卷组
卷组建立在物理卷之上,一个卷组中至少要包括一个物理卷,在卷组建立之后可动态添加物理卷到卷组中。一个逻辑卷管理系统工程中可以只有一个卷组,也可以拥有多个卷组。
[root@plat-ecloud01-oscar-prod-db-mysql01 ~]# vgcreate vg_database /dev/vdb
Volume group "vg_database" successfully created
查看vg
[root@plat-ecloud01-oscar-prod-db-mysql01 ~]# vgs
VG #PV #LV #SN Attr VSize VFree
vg_database 1 0 0 wz--n- 500.00g 500.00g
[root@plat-ecloud01-oscar-prod-db-mysql01 ~]# vgdisplay
--- Volume group ---
VG Name vg_database
System ID
Format lvm2
Metadata Areas 1
Metadata Sequence No 1
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 0
Open LV 0
Max PV 0
Cur PV 1
Act PV 1
VG Size 500.00 GiB
PE Size 4.00 MiB
Total PE 127999
Alloc PE / Size 0 / 0
Free PE / Size 127999 / 500.00 GiB
VG UUID jsOXYn-hcW2-6mI4-FD2a-YLHR-IRud-nQgimq
创建一个Logical Volume,即逻辑卷
逻辑卷建立在卷组之上,卷组中的未分配空间可以用于建立新的逻辑卷,逻辑卷建立后可以动态地扩展和缩小空间。系统中的多个逻辑卷可以属于同一个卷组,也可以属于不同的多个卷组。
分配400G空间
[root@plat-ecloud01-oscar-prod-db-mysql01 ~]# lvcreate -n lv_database -L 400G vg_database
Logical volume "lv_database" created.
查看lv状态
[root@plat-ecloud01-oscar-prod-db-mysql01 ~]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
lv_database vg_database -wi-a----- 400.00g
[root@plat-ecloud01-oscar-prod-db-mysql01 ~]# vgdisplay
--- Volume group ---
VG Name vg_database
System ID
Format lvm2
Metadata Areas 1
Metadata Sequence No 2
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 1
Open LV 0
Max PV 0
Cur PV 1
Act PV 1
VG Size 500.00 GiB
PE Size 4.00 MiB
Total PE 127999
Alloc PE / Size 102400 / 400.00 GiB
Free PE / Size 25599 / 100.00 GiB
VG UUID jsOXYn-hcW2-6mI4-FD2a-YLHR-IRud-nQgimq
将剩下的磁盘资源分配各log,注意留1M的free PE
[root@plat-ecloud01-oscar-prod-db-mysql01 ~]# lvcreate -n lv_log -l 25598 vg_database
Logical volume "lv_log" created.
[root@plat-ecloud01-oscar-prod-db-mysql01 ~]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
lv_database vg_database -wi-a----- 400.00g
lv_log vg_database -wi-a----- 99.99g
[root@plat-ecloud01-oscar-prod-db-mysql01 ~]# vgdisplay
--- Volume group ---
VG Name vg_database
System ID
Format lvm2
Metadata Areas 1
Metadata Sequence No 3
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 2
Open LV 0
Max PV 0
Cur PV 1
Act PV 1
VG Size 500.00 GiB
PE Size 4.00 MiB
Total PE 127999
Alloc PE / Size 127998 / 499.99 GiB
Free PE / Size 1 / 4.00 MiB
VG UUID jsOXYn-hcW2-6mI4-FD2a-YLHR-IRud-nQgimq
[root@plat-ecloud01-oscar-prod-db-mysql01 ~]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
lv_database vg_database -wi-a----- 400.00g
lv_log vg_database -wi-a----- 99.99g
看内核版本
[root@plat-ecloud01-oscar-prod-db-mysql01 ~]# uname -r
3.10.0-514.26.2.el7.x86_64
初始化
[root@plat-ecloud01-oscar-prod-db-mysql01 ~]# mkfs.xfs /dev/mapper/vg_database-lv_database
meta-data=/dev/mapper/vg_database-lv_database isize=512 agcount=4, agsize=26214400 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0, sparse=0
data = bsize=4096 blocks=104857600, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=1
log =internal log bsize=4096 blocks=51200, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
修改fstab脚本
[root@plat-ecloud01-oscar-prod-db-mysql01 ~]# vi /etc/fstab
/dev/vg_database/lv_database /app/data xfs defaults 0 0
创建路径
[root@plat-ecloud01-oscar-prod-db-mysql01 ~]# mkdir /app/data -p
挂载磁盘
[root@plat-ecloud01-oscar-prod-db-mysql01 ~]# mount -a
确认状态
[root@plat-ecloud01-oscar-prod-db-mysql01 ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/vda1 20G 1.7G 18G 9% /
devtmpfs 1.9G 0 1.9G 0% /dev
tmpfs 1.9G 0 1.9G 0% /dev/shm
tmpfs 1.9G 328K 1.9G 1% /run
tmpfs 1.9G 0 1.9G 0% /sys/fs/cgroup
tmpfs 380M 0 380M 0% /run/user/0
/dev/mapper/vg_database-lv_database 400G 33M 400G 1% /app/data
lvextend -L +1G /dev/vg0/lv1
说明:在lv1原有的基础上增加了1G.
resize2fs /dev/vg0/lv1
创建pv: pvcreate /dev/sdh
扩容vg: vgextend vg0 /dev/sdh
扩容至指定大小:lvextend -L 55G vg0
resize:resize2fs vg0