問題描述與追蹤
首先是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也刪除了