问题描述与追踪
首先是mysql连不上:
[root@VM-90-225-centos ~]# mysql -uroot -p
Enter password:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (111)
然后查看mysql的状态:
[root@VM-90-225-centos ~]# service mysqld statusERROR! MySQL is not running, but PID file exists
然后根据这篇文章:
mysql无法启动ERROR! MySQL is running but PID file could not be found
我在根目录下查看了一下磁盘文件:
[root@VM-90-225-centos ~]# df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 7.7G 0 7.7G 0% /dev
tmpfs 7.7G 0 7.7G 0% /dev/shm
tmpfs 7.7G 258M 7.5G 4% /run
tmpfs 7.7G 0 7.7G 0% /sys/fs/cgroup
/dev/vda1 99G 99G 0 100% /
tmpfs 1.6G 0 1.6G 0% /run/user/0
/dev/vdb1 197G 61M 187G 1% /data
发现/dev/vda1
这个文件已经满了
我们回到根目录,查看一下到底是哪个文件磁盘占用最高:
[root@VM-90-225-centos ~]# cd /
[root@VM-90-225-centos /]# du -sh *
0 bin
264M boot
24K data
0 dev
40M etc
17M home
0 lib
0 lib64
16K lost+found
4.0K media
4.0K mnt
32M opt
du: cannot access ‘proc/5888/task/5888/fd/3’: No such file or directory
du: cannot access ‘proc/5888/task/5888/fdinfo/3’: No such file or directory
du: cannot access ‘proc/5888/fd/3’: No such file or directory
du: cannot access ‘proc/5888/fdinfo/3’: No such file or directory
0 proc
1.7G root
258M run
0 sbin
4.0K srv
0 sys
304K tmp
96G usr
975M var
发现是usr文件夹最占用空间,依次递归查询最大文件夹:
[root@VM-90-225-centos /usr]# du -sh *
358M 1.2-compat
636M bin
4.0K etc
4.0K games
42M include
1.1G lib
559M lib64
210M libexec
92G local
12K man
51M sbin
612M share
198M src
0 tmp
[root@VM-90-225-centos /usr]# cd ./local/
[root@VM-90-225-centos /usr/local]# du -sh *
175M agenttools
14M bin
4.0K etc
4.0K ft_local
4.0K games
399M go
1.1M hio
3.3M include
468M lib
4.0K lib64
4.0K libexec
4.0K lost+found
89G mysql
1.2G mysql-8.0.27-linux-glibc2.12-x86_64.tar.xz
216M python
25M qcloud
541M sa
4.0K sbin
108K share
4.0K src
4.0K tencent
4.3M tsso_openssh
5.6M TsysAgent
[root@VM-90-225-centos /usr/local]# cd ./mysql/
[root@VM-90-225-centos /usr/local/mysql]# du -sh *
1.3G bin
87G data
24K docs
8.0K etc
404K include
463M lib
272K LICENSE
840K man
4.0K README
9.5M share
24K support-files
4.0K tmp
[root@VM-90-225-centos /usr/local/mysql]# cd ./data/
[root@VM-90-225-centos /usr/local/mysql/data]# du -sh *
4.0K auto.cnf
756K binlog.000009
1.1G binlog.000010
1.1G binlog.000011
1.1G binlog.000012
1.1G binlog.000013
1.1G binlog.000014
1.1G binlog.000015
1.1G binlog.000016
1.1G binlog.000017
1.1G binlog.000018
1.1G binlog.000019
1.1G binlog.000020
1.1G binlog.000021
1.1G binlog.000022
1.1G binlog.000023
1.1G binlog.000024
1.1G binlog.000025
1.1G binlog.000026
1.1G binlog.000027
1.1G binlog.000028
1.1G binlog.000029
1.1G binlog.000030
1.1G binlog.000031
1.1G binlog.000032
1.1G binlog.000033
1.1G binlog.000034
1.1G binlog.000035
1.1G binlog.000036
1.1G binlog.000037
1.1G binlog.000038
1.1G binlog.000039
1.1G binlog.000040
1.1G binlog.000041
1.1G binlog.000042
1.1G binlog.000043
1.1G binlog.000044
1.1G binlog.000045
1.1G binlog.000046
1.1G binlog.000047
1.1G binlog.000048
1.1G binlog.000049
1.1G binlog.000050
1.1G binlog.000051
1.1G binlog.000052
1.1G binlog.000053
1.1G binlog.000054
1.1G binlog.000055
1.1G binlog.000056
1.1G binlog.000057
1.1G binlog.000058
1.1G binlog.000059
1.1G binlog.000060
1.1G binlog.000061
1.1G binlog.000062
1.1G binlog.000063
1.1G binlog.000064
1.1G binlog.000065
1.1G binlog.000066
1.1G binlog.000067
1.1G binlog.000068
1.1G binlog.000069
1.1G binlog.000070
1.1G binlog.000071
1.1G binlog.000072
1.1G binlog.000073
1.1G binlog.000074
1.1G binlog.000075
1.1G binlog.000076
1.1G binlog.000077
1.1G binlog.000078
1.1G binlog.000079
1.1G binlog.000080
1.1G binlog.000081
1.1G binlog.000082
1.1G binlog.000083
1.1G binlog.000084
1.1G binlog.000085
1.1G binlog.000086
1.1G binlog.000087
1.1G binlog.000088
1.1G binlog.000089
1.1G binlog.000090
324M binlog.000091
4.0K binlog.000092
4.0K binlog.000093
4.0K binlog.000094
1.1G binlog.000095
1.1G binlog.000096
1.1G binlog.000097
713M binlog.000098
4.0K binlog.index
4.0K ca-key.pem
4.0K ca.pem
4.0K client-cert.pem
4.0K client-key.pem
4.0K history
192K #ib_16384_0.dblwr
8.2M #ib_16384_1.dblwr
8.0K ib_buffer_pool
12M ibdata1
48M ib_logfile0
48M ib_logfile1
12M ibtmp1
804K #innodb_temp
328K ip_to_room
303M machine_history
36K mysql
105M mysql.ibd
324M new_machine_history
21M new_room_history
463M newtest
1.6M performance_schema
4.0K private_key.pem
4.0K public_key.pem
15M room_history
410M room_province
13M room_to_ip
4.0K server-cert.pem
4.0K server-key.pem
116K sys
1.1M test
48M undo_001
48M undo_002
4.0K VM-90-225-centos.pid
发现是binlog占用了很多空间
解决方案
接下来就好办了,百度:mysql的binlog太多占用了空间
cd /etc/init.d
mysqld --user=root
# 强制进去
然后查看当前日志保存天数,默认是0,也就是logs不过期.
mysql> show variables like '%expire_logs_days%';
+------------------+-------+
| Variable_name | Value |
+------------------+-------+
| expire_logs_days | 0 |
+------------------+-------+
1 row in set (0.00 sec)
编辑/etc/my.cnf文件,在[mysqld]节点中增加如下两行
max_binlog_size = 500M
expire_logs_days = 2
service mysqld restart重新启动mysql。设置便会生效。
mysql> show variables like '%expire_logs_days%';
+------------------+-------+
| Variable_name | Value |
+------------------+-------+
| expire_logs_days | 2 |
+------------------+-------+
1 row in set (0.00 sec)
mysql> PURGE MASTER LOGS BEFORE DATE_SUB( NOW( ), INTERVAL 1 DAY);
Query OK, 0 rows affected (0.02 sec)
然后再看data文件夹:
[root@VM-90-225-centos /usr/local/mysql/data]# du -sh *
4.0K auto.cnf
1.1G binlog.000095
1.1G binlog.000096
1.1G binlog.000097
92M binlog.000098
4.0K binlog.000099
4.0K binlog.000100
4.0K binlog.000101
4.0K binlog.000102
4.0K binlog.000103
4.0K binlog.index
4.0K ca-key.pem
4.0K ca.pem
4.0K client-cert.pem
4.0K client-key.pem
4.0K history
192K #ib_16384_0.dblwr
8.2M #ib_16384_1.dblwr
12K ib_buffer_pool
12M ibdata1
48M ib_logfile0
48M ib_logfile1
12M ibtmp1
804K #innodb_temp
328K ip_to_room
303M machine_history
36K mysql
105M mysql.ibd
324M new_machine_history
21M new_room_history
463M newtest
1.6M performance_schema
4.0K private_key.pem
4.0K public_key.pem
15M room_history
410M room_province
13M room_to_ip
4.0K server-cert.pem
4.0K server-key.pem
116K sys
1.1M test
48M undo_001
48M undo_002
4.0K VM-90-225-centos.pid
旧binlog也删除了