首頁 > 軟體

Linux將MySQL資料庫目錄掛載至新資料盤

2020-06-16 17:12:06

對於Linux系統來說,掛載磁碟的方法其實都大同小異,所以本文以CentOS系統為例,介紹下Linux系統磁碟掛載方法,前面大部分內容源於天翼云的論壇。

1.檢視磁碟情況
使用命令fdisk -l # 列出全部磁碟資訊

[root@ linuxidc.com ~]# fdisk -l

Disk
/dev/xvda: 42.9 GB, 42949672960 bytes
255 heads, 63 sectors/track, 5221 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:
0x0008a9a5

Device Boot          Start        End      Blocks  Id  System
/dev/xvda1              1        523    4194304  82  Linux swap / Solaris
Partition
1 does not end on cylinder boundary.
/dev/xvda2  *        523        5222    37747712  83  Linux

Disk
/dev/xvde: 322.1 GB, 322122547200 bytes
255 heads, 63 sectors/track, 39162 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.建立分割區
 fdisk /dev/xvde # 對xvde硬碟進行分割區
  n  # 建立一個新分割區
  p  # 建立一個主分割區(e為擴充套件分割區)
  1  # 1表示第一個主分割區
  回車  # 從第1柱面開始分割區
  回車  # 表示整個xvde全寫入第1分割區(若需要將sdb分為多個分割區,此時輸入+分割區大小)
  P  # 檢視下已建立好的分割區
  w  # 儲存並退出

[root@ linuxidc.com ~]# fdisk /dev/xvde
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a
new DOS disklabel with disk identifier 0x590ca8b1.
Changes will remain
in memory only, until you decide to write them.
After that, of course, the previous content won
't be recoverable.

Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)

WARNING: DOS-compatible mode is deprecated. It
's strongly recommended to
       
switch off the mode (command 'c') and change display units to
        sectors (command
'u').

Command (m
for help): n
Command action
  e  extended
  p  primary partition (
1-4)
p
Partition number (
1-4): 1
First cylinder (
1-39162, default 1):
Using
default value 1
Last cylinder,
+cylinders or +size{K,M,G} (1-39162, default 39162):
Using
default value 39162

Command (m
for help): p

Disk
/dev/xvde: 322.1 GB, 322122547200 bytes
255 heads, 63 sectors/track, 39162 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:
0x590ca8b1

    Device Boot      Start        End      Blocks  Id  System
/dev/xvde1              1      39162  314568733+  83  Linux

Command (m
for help): w
The partition table has been altered
!

Calling ioctl() to re
-read partition table.
Syncing disks.


3.建立分割區檔案系統並格式化磁碟
# mkfs.ext4 /dev/xvde1 # 將xvde1以ext4格式進行格式化

[root@ linuxidc.com ~]# mkfs.ext4 xvde1
mke2fs 1.41.12 (17-May-2010)
Could not stat xvde1 --- No such file or directory

The device apparently does not exist; did you specify it correctly?
[root@ linuxidc.com ~]# mkfs.ext4 /dev/xvde1
mke2fs 1.41.12 (17-May-2010)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
19660800 inodes, 78642183 blocks
3932109 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=4294967296
2400 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks: 
        32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208, 
        4096000, 7962624, 11239424, 20480000, 23887872, 71663616

Writing inode tables: done                            
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done

This filesystem will be automatically checked every 25 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.


4.在home目錄下面建立資料夾MySQL,並且將裝置掛載至/home/mysql。

[root@ linuxidc.com home]# mount /dev/xvde1 /home/mysql
[root@ linuxidc.com home]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/xvda2             36G  2.4G   32G   7% /
tmpfs                 3.9G     0  3.9G   0% /dev/shm
/dev/xvde1            296G  191M  281G   1% /home/mysql


假如原來資料夾中有檔案掛載之後檔案會消失並且出現lost+found資料夾,這時不用擔心,可以用umount命令進行取消掛載。
原因是這樣的。
這個牽扯到linux的VFS(虛擬檔案系統)機制。登入以後,你看到的各個目錄,檔案都是核心在載入時候構造在記憶體中的VFS目錄樹,而不是直接看到硬碟上的實際目錄樹。
按照我對  http://www.linuxidc.com/Linux/2017-06/1450.htm  這篇文章所描述的粗淺理解,當你掛載某個裝置到一個VFS掛載點上時(比如/home),系統就把VFS中的這個掛載點/home指向你最後所掛載的那個裝置上。那麼你現在存取該掛載點時,就會看到你最後掛載在此處的裝置。而之前所掛載的裝置依然在那裡,只不過掛載點/home已經不再指向之前的裝置。你可以把原來的裝置解除安裝以後掛載到一個新的掛載點上來存取。

5.將磁碟UUID寫入fstab

對於Linux系統來說,磁碟xvda和xvde的分割區是根據系統啟動時識別的先後順序來排列,這樣就會出現一個當主機重新啟動後,出現跳盤的情況,為了避免此情況的發生,就需要我們將磁碟的UUID資訊寫入fstab檔案,將分割區與磁碟系結。
1)通過blkid獲得磁碟分割區的UUID
[root@ linuxidc.com ~]# blkid
/dev/xvda1: UUID="5546dd0c-ef40-451b-b266-df8ef3a49967" TYPE="swap" 
/dev/xvda2: UUID="77fc0962-b3cf-400a-8903-0632e077fa09" TYPE="ext3" 
/dev/xvde1: UUID="1d17945b-c369-42de-85a8-47217d24d3e5" TYPE="ext4"

2)通過vim /etc/fstab在最後一行新增xvde1的磁碟資訊

#
# /etc/fstab
# Created by anaconda on Thu Jan 12 02:47:21 2017
#
# 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
#
UUID=77fc0962-b3cf-400a-8903-0632e077fa09 /                       ext3    defaults        1 1
UUID=5546dd0c-ef40-451b-b266-df8ef3a49967 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=d17945b-c369-42de-85a8-47217d24d3e5 /home/mysql ext4 defaults      0 0


6.將/var/lib/mysql/下的內容轉移至/home/mysql/,並且在/var/lib/下建立mysql的連結,實際目錄其實為/home/mysql

[root@ linuxidc.com /]# cd home
[root@ linuxidc.com home]# mkdir mysql
[root@ linuxidc.com home]# mv /var/lib/mysql/* /home/mysql
[root@ linuxidc.com home]# ln /home/mysql /var/lib/mysql


7.驗證一下,通過df命令檢視磁碟的使用情況。然後進入mysql命令,建立資料庫test1...,執行之後會在/home/mysql/資料夾下出現資料庫test1,另外使用df命令檢視磁碟使用情況。會發現/dev/xvde1使用量在增長。

[root@ linuxidc.com mysql]# df
Filesystem           1K-blocks      Used Available Use% Mounted on
/dev/xvda2            37155392   2417608  32850400   7% /
tmpfs                  4018012         0   4018012   0% /dev/shm
/dev/xvde1           309633052    217092 293687524   1% /home/mysql

本文永久更新連結地址http://www.linuxidc.com/Linux/2017-06/145066.htm


IT145.com E-mail:sddin#qq.com