1 基本操作
df #可用磁盘文件名
文件系统 1K-块 已用 可用 已用% 挂载点
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/mapper/vg_jydsjdb-lv_root 51606140 1687912 47296788 4% /
tmpfs 16440212 16 16440196 1% /dev/shm
/dev/sda1 495844 33463 436781 8% /boot
/dev/mapper/vg_jydsjdb-lv_home 40776268 180104 38524820 1% /home
物理主机上的 /dev/sda2 是对应着主机硬盘的分区,后面的数字表示分区号,数字前面的字母 a 表示第几块硬盘(也可能是可移动磁盘),你如果主机上有多块硬盘则可能还会出现 /dev/sdb,/dev/sdc 这些磁盘设备都会在 /dev 目录下以文件的存在形式。
df -h #查挂载情况
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/vg_jydsjdb-lv_root 50G 1.7G 46G 4% /
tmpfs 16G 16K 16G 1% /dev/shm
/dev/sda1 485M 33M 427M 8% /boot
/dev/mapper/vg_jydsjdb-lv_home 39G 176M 37G 1% /home
df / #系统根目录所在磁盘
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/mapper/vg_jydsjdb-lv_root 51606140 1687912 47296788 4% /
fdisk -l #列出全部磁盘信息
Disk /dev/sda: 107.4 GB, 107374182400 bytes
255 heads, 63 sectors/track, 13054 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000c6a68
Device Boot Start End Blocks Id System
/dev/sda1 * 1 64 512000 83 Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2 64 13055 104344576 8e Linux LVM
Disk /dev/sdb: 1099.5 GB, 1099511627776 bytes
255 heads, 63 sectors/track, 133674 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000f0aac
Device Boot Start End Blocks Id System
Disk /dev/mapper/vg_jydsjdb-lv_root: 53.7 GB, 53687091200 bytes
255 heads, 63 sectors/track, 6527 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000
Disk /dev/mapper/vg_jydsjdb-lv_swap: 10.7 GB, 10737418240 bytes
255 heads, 63 sectors/track, 1305 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000
Disk /dev/mapper/vg_jydsjdb-lv_home: 42.4 GB, 42421190656 bytes
255 heads, 63 sectors/track, 5157 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000
2 创建磁盘
对于Disk /dev/sdb: 1099.5 GB
df -T 只可以查看已经挂载的分区和文件系统类型。
fdisk -l 可以显示出所有挂载和未挂载的分区,但不显示文件系统类型。
parted -l 可以查看未挂载的文件系统类型,以及哪些分区尚未格式化。
lsblk -f 也可以查看未挂载的文件系统类型。
file -s /dev/sdb
parted -l
Model: VMware Virtual disk (scsi)
Disk /dev/sdb: 1100GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
sudo mount #查看主机已经挂载的文件系统
/dev/mapper/vg_jydsjdb-lv_root on / type ext4 (rw)
proc on /proc type proc (rw)
sysfs on /sys type sysfs (rw)
devpts on /dev/pts type devpts (rw,gid=5,mode=620)
tmpfs on /dev/shm type tmpfs (rw,rootcontext="system_u:object_r:tmpfs_t:s0")
/dev/sda1 on /boot type ext4 (rw)
/dev/mapper/vg_jydsjdb-lv_home on /home type ext4 (rw)
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)
gvfs-fuse-daemon on /root/.gvfs type fuse.gvfs-fuse-daemon (rw,nosuid,nodev)
- 使用 mkfs 命令格式化磁盘
输入 sudo mkfs 然后按下Tab键,不同的后缀其实就是表示着不同的文件系统。
sudo mkfs.ext4 /dev/sdb
/dev/sdb is entire device, not just one partition!
Proceed anyway? (y,n) y
- 使用 mount 命令挂载磁盘到目录树,umount 命令卸载已挂载磁盘
mount 命令挂载创建的虚拟磁盘镜像
mount [options] [source] [directory]
mount [-o [操作选项]] [-t 文件系统类型] [-w|--rw|--ro] [文件系统源] [挂载点]
# 也可以省略挂载类型,很多时候 mount 会自动识别
mount -o loop -t ext4 virtual.img /mnt
# 在home目录下面创建文件夹mysql,并且将设备挂载至/home/mysql
sudo mount /dev/sdb /mnt #将设备挂载至/mnt
# 查挂载情况
df -h
umount 命令卸载/mnt的已挂载磁盘
sudo umount /home/myuser/mysql/data
- 将磁盘UUID写入fstab
blkid #获得磁盘分区的UUID
/dev/sda1: UUID="634a8c28-e69a-4a3c-9e5a-c356935c508a" TYPE="ext4"
/dev/sda2: UUID="BPEeOS-bltq-WN1L-tA0f-nM4j-hkk2-i2eg2b" TYPE="LVM2_member"
/dev/mapper/vg_jydsjdb-lv_root: UUID="d64b54d0-c405-49ea-8bb4-b6ba71cc3839" TYPE="ext4"
/dev/mapper/vg_jydsjdb-lv_swap: UUID="2bcf867e-086e-4a4f-a33f-6efb966ffc84" TYPE="swap"
/dev/mapper/vg_jydsjdb-lv_home: UUID="d99b11f5-d3f4-4392-8c24-2a60d45b9681" TYPE="ext4"
/dev/sdb: UUID="7418b0fb-6bd1-4524-90c0-ed4da3d1f470" TYPE="ext4"
通过vim /etc/fstab在最后一行添加/dev/sdb的磁盘信息
#
# /etc/fstab
# Created by anaconda on Mon Apr 15 15:27:32 2019
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/vg_jydsjdb-lv_root / ext4 defaults 1 1
UUID=634a8c28-e69a-4a3c-9e5a-c356935c508a /boot ext4 defaults 1 2
/dev/mapper/vg_jydsjdb-lv_home /home ext4 defaults 1 2
/dev/mapper/vg_jydsjdb-lv_swap swap swap defaults 0 0
tmpfs /dev/shm tmpfs defaults 0 0
devpts /dev/pts devpts gid=5,mode=620 0 0
sysfs /sys sysfs defaults 0 0
proc /proc proc defaults 0 0
最后一行加数据库挂载路径
UUID=7418b0fb-6bd1-4524-90c0-ed4da3d1f470 /home/myuser/mysql/data ext4 defaults 0 0
或
/dev/sdb /home/mysql ext4 defaults 0 0
- 将mysql存数据文件夹挂载为新磁盘
方法一:直接挂载
cp -r /home/myuser/mysql-5.6.43/data /home/myuser/mysql-5.6.43/data.bk #递归备份data文件夹
sudo mount /dev/sdb /home/myuser/mysql-5.6.43/data #挂载新磁盘到data文件夹
cp -r /home/myuser/mysql-5.6.43/data.bk/* /home/myuser/mysql-5.6.43/data #递归复制data.bk文件夹内容
chown -R mysql.mysql /home/myuser/mysql-5.6.43/data #给新挂载文件夹权限
#在mysql创建测试数据库,看data文件夹和磁盘使用量变化
df -h
#umount 命令卸载已挂载磁盘
sudo umount /home/myuser/mysql/data
方法二:软连接
见文章《Linux将MySQL数据库目录挂载至新数据盘》 https://www.cnblogs.com/chenjianxiang/p/7060304.html
参考资料
文件系统操作与磁盘管理 https://www.shiyanlou.com/courses/1/labs/62/document/
linux下查看磁盘分区的文件系统格式 https://www.cnblogs.com/youbiyoufang/p/7607174.html
在linux下新增一块硬盘的操作过程 https://blog.csdn.net/chun8416/article/details/51548077
Linux将MySQL数据库目录挂载至新数据盘 https://www.cnblogs.com/chenjianxiang/p/7060304.html
磁盘的分割、格式化、检验与挂载 http://cn.linux.vbird.org/linux_basic/0230filesystem_3.php
配置启动挂载 http://cn.linux.vbird.org/linux_basic/0230filesystem_4.php