I'm sure many netizens, like Lao Zuo, will find that the website is using MYSQL When the database is used, it will be found that some tables will be loaded with a lot of fragmented files. In fact, these files are recorded slowly through the cache mode. As time goes by, useless fragmented articles will be added to enrich the database tables. In fact, there is not much useful data in the database, but there may be only a few fragments.
When encountering such problems, we usually first find out whether the corresponding program may have a way to optimize the data table. If not, we have to solve the problem ourselves. Of course, before solving the problem, we need to back up the database to avoid some problems. Here Lao Zuo is also looking for some solutions to MYSQL fragments. Here is a tool record of shell script processing fragments, which is recorded here first.
#!/bin/sh
mysql_user=root
mysql_pass=123123
time_log=/opt/time
databases=/opt/databases
/usr/bin/mysql -u$mysql_user -p$mysql_pass -e "show databases" | grep -v "Database" > /opt/databases
sed -i "s/information_schema//" $databases
sed -i "s/mysql//" $databases
sed -i "s/test//" $databases
databases1=$(cat /opt/databases)
for i in $databases1
do
echo "database $i starting"
tables=$(/usr/bin/mysql $i -u$mysql_user -p$mysql_pass -e "show tables" | grep -v "Tables" > /opt/$i)
tablelist=$(cat /opt/$i)
echo "optimize database $i starting" >> $time_log
echo "$i start at $(date +[%Y/%m/%d/%H:%M:%S])" >> $time_log
for list in $tablelist
do
echo $list
/usr/bin/mysql $i -u$mysql_user -p$mysql_pass -e "optimize table $list"
done
echo "$i end at $(date +[%Y/%m/%d/%H:%M:%S])" >> $time_log
echo >> $time_log
done
We can see that the account and password of the database can be set in front of the script, and then executed accordingly. Here I do not test, but record it, and then have time to test whether it is feasible. Article reference from: https://blog.51cto.com/11910656/1839091 If we want to test ourselves, we must first back up the database.