linux运用软链接解决目录空间不足
由于磁盘空间不足,而且只有一块磁盘,在前期分区的问题上,把/根目录分得太小,且没有新增加的磁盘,磁盘分区为非LVM,
现在把/根目录下占用比较大的文件夹 软连接到/home目录
[root@localhost ~]# df -h Filesystem Size Used Avail Use% Mounted on /dev/sda3 14G 7.6G 5.4G 59% / devtmpfs 977M 0 977M 0% /dev tmpfs 993M 0 993M 0% /dev/shm tmpfs 993M 18M 975M 2% /run tmpfs 993M 0 993M 0% /sys/fs/cgroup /dev/sda2 37G 56M 35G 1% /home /dev/sda1 922M 131M 729M 16% /boot /dev/sda5 190M 8.0K 190M 1% /boot/efi tmpfs 199M 48K 199M 1% /run/user/1000 tmpfs 199M 0 199M 0% /run/user/0 [root@localhost mysql]# lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT sda 8:0 0 60G 0 disk ├─sda1 8:1 0 953M 0 part /boot ├─sda2 8:2 0 37.3G 0 part /home ├─sda3 8:3 0 14G 0 part / ├─sda4 8:4 0 1K 0 part ├─sda5 8:5 0 190M 0 part /boot/efi └─sda6 8:6 0 7.7G 0 part └─centos-swap 253:0 0 7.7G 0 lvm [SWAP] sr0 11:0 1 1024M 0 rom
测试,在/下安装mysql数据库
[root@localhost ~]# ps -ef|grep mysql mysql 67516 1 0 Jul06 ? 00:00:00 /bin/sh /usr/bin/mysqld_safe mysql 67706 67516 2 Jul06 ? 00:28:20 /usr/sbin/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib64/mysql/plugin --log-error=/var/log/mysqld.log --pid-file=/var/run/mysqld/mysqld.pid --socket=/var/lib/mysql/mysql.sock root 79745 79644 0 09:38 pts/1 00:00:00 grep --color=auto mysql
默认情况下,数据文件及日志都是放在/var/lib/mysql
建立软连接
[root@localhost home]# mkdir /home/mysqldata/ ##新数据文件目录 [root@localhost home]# mv /var/lib/mysql /home/mysqldata/ [root@localhost home]# ln -s /home/mysqldata/mysql /var/lib/mysql [root@localhost home]# chown -R mysql:mysql /home/mysqldata/ [root@localhost home]# chown -R mysql:mysql /var/lib/mysql/
查看各目录大小占用情况
[root@localhost mysql]# du -sh /* --exclude="proc" 0 /bin 128M /boot 0 /dev 40M /etc 286M /home 0 /lib 0 /lib64 16K /lost+found 4.0K /media 4.0K /mnt 2.9G /opt 100K /root du: cannot access ‘/run/user/1000/gvfs’: Permission denied 18M /run 0 /sbin 4.0K /srv 0 /sys 3.6M /tmp 3.4G /usr 1.1G /var
创建存储过程,批量插入数据,看磁盘占用变化
create table t1(id int(11), named varchar(100), emaild varchar(100), create_time timestamp); select curdate(), curtime(), now(), localtimestamp(); select concat('a','b'); call f_batch_t1(100000); select count(*) from t1; --- DROP PROCEDURE IF EXISTS f_batch_t1; DELIMITER $ CREATE PROCEDURE f_batch_t1( p1 int) BEGIN DECLARE i INT DEFAULT 1; DECLARE j INT DEFAULT 1; WHILE i<=p1 DO INSERT INTO t1(id,named,emaild,create_time) VALUES (i,concat(i,'b'),concat(j,'b'),localtimestamp()); SET i = i+1; SET j = i+1; IF j=1000 THEN COMMIT; SET j:=1; END IF; END WHILE;
COMMIT; END $ --
插入1kw数据
call f_batch_t1(10000000);
[root@localhost mysql]# du -sh /home/mysqldata/mysql/ 815M /home/mysqldata/mysql/ [root@localhost mysql]# du -sh /* --exclude="proc" 0 /bin 128M /boot 0 /dev 40M /etc 822M /home ###home实际存储的数据增加 0 /lib 0 /lib64 16K /lost+found 4.0K /media 4.0K /mnt 2.9G /opt 100K /root du: cannot access ‘/run/user/1000/gvfs’: Permission denied 18M /run 0 /sbin 4.0K /srv 0 /sys 3.6M /tmp 3.4G /usr 1.1G /var ###此软连接的目录没有变化
[root@localhost mysql]# rm -rf /home/mysqldata/mysql/ ####会删除原来的文件 ln -s 原始文件或文件夹 目标文件或文件夹 #建立软链接 mv /var/lib/mysql /home/ ln -s /home/mysql /var/lib/mysql #删除软连接 #当删除链接文件时,如果不小心会把原始文件删掉 rm -rf /var/lib/mysql #只是少了一个“ / ” 删除的 就是链接文件, 也就是在文件的最后不要加上"/"