Mysql删除表名中有特殊字符的表
欢迎进入Linux社区论坛,与200万技术人员互动交流 >>进入 由于公司业务和应用的调整,之前在Mysql中的很多表都不需要了,故需要对数据库进行整理。 刚开始,我在想:不就删除一些表吗?很好解决,写个简单的脚本就可以了。我先看了数据库中有80000多个表,很
欢迎进入Linux社区论坛,与200万技术人员互动交流 >>进入
由于公司业务和应用的调整,之前在Mysql中的很多表都不需要了,故需要对数据库进行整理。
刚开始,我在想:不就删除一些表吗?很好解决,写个简单的脚本就可以了。我先看了数据库中有80000多个表,很多表都是以IP命名的,而这些表就是要清理的对象。
于是我使用下面一条命令,先将所有表名导出到一个文件中:
mysql -uroot -p123456 -A -e "use cdn;show tables;" >allDBName.txt
然后,执行:cat allDBName.txt|grep "^[0-9]\{1,3\}\.[0-9]\{1,3\}\.">ipDB.txt 将以IP开始的表名导入到ipDB.txt中。再使用如下脚本,进行删除。
#! /bin/bash
cat ipDB.txt|while read DBname
do
mysql -uroot -p123456 -A -e "use cdn;DROP TABLE IF EXISTS $DBname;" 2》drop.err
if [ $? -ne 0 ] ;then
echo "Drop $DBname failed"
fi
done
exit 0
我在后台执行了以上脚本,也没看执行是否正确,个人感觉应该没问题了,于是就做其它事情去了。可是,一段时间后,我在information_schema数据库下,执行下列语句,查看数据库大小时,发现与删除表之前的数据库大小不减,反而增加了(实际环境中,正在跑着业务)。
select concat(round(sum(DATA_LENGTH/1024/1024),2),'MB') as datasize from TABLES where table_schema='cdn';
再看刚才脚本的目录下,多了一个drop.err文件,发现里面全是同一个错误:
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''192.168.1.30'' at line 1
我很疑惑,为什么会报错呢?于是我在终端中进入到数据库,执行:
DROP TABLE IF EXISTS '192.168.1.30';
可是还是报相同的错误,我很是疑惑。WHY?根本就没道理啊,没有语法错误啊!后来仔细一想,感觉不对:表名里面的点一概失算特殊字符,可能是这个问题。于是我查阅了一些资料,遇到这种问题的解决方法是:把表名放到``里面,注意`是跟字母在一起的数字1的左边的字符,试了一下,果然成功了。
于是,我将脚本中的 $DBname变成`$DBname`,但是中兴的时候,居然又报错了:command not found,仔细一想应该是双引号的问题,于是将引号改成单引号,胆码如下:
#! /bin/bash
cat ipDB.txt|while read DBname
do
mysql -uroot -p123456 -A -e 'use cdn;DROP TABLE IF EXISTS `$DBname`;' 2》drop.err
if [ $? -ne 0 ] ;then
echo "Drop $DBname failed"
fi
done
exit 0
然后执行,没有再报错,问题应该解决了吧!可我一查看标的数量,居然还是没有变化,天理何在啊!没报错了,但怎么就没将表删除了。再仔细想想,原来是shell脚本中特殊字符的处理,改成以下就好了:
#! /bin/bash
cat ipDB.txt|while read DBname
do
mysql -uroot -p123456 -A -e "use cdn;DROP TABLE IF EXISTS \`$DBname\`;" 2》drop.err
if [ $? -ne 0 ] ;then
echo "Drop $DBname failed"
fi
done
exit 0
此时,再次执行,没报错,再看看表的数量,果然变少了。居然被几个符号搞了一天!唉!吸取点教训吧!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Big data structure processing skills: Chunking: Break down the data set and process it in chunks to reduce memory consumption. Generator: Generate data items one by one without loading the entire data set, suitable for unlimited data sets. Streaming: Read files or query results line by line, suitable for large files or remote data. External storage: For very large data sets, store the data in a database or NoSQL.

Backing up and restoring a MySQL database in PHP can be achieved by following these steps: Back up the database: Use the mysqldump command to dump the database into a SQL file. Restore database: Use the mysql command to restore the database from SQL files.

MySQL query performance can be optimized by building indexes that reduce lookup time from linear complexity to logarithmic complexity. Use PreparedStatements to prevent SQL injection and improve query performance. Limit query results and reduce the amount of data processed by the server. Optimize join queries, including using appropriate join types, creating indexes, and considering using subqueries. Analyze queries to identify bottlenecks; use caching to reduce database load; optimize PHP code to minimize overhead.

How to insert data into MySQL table? Connect to the database: Use mysqli to establish a connection to the database. Prepare the SQL query: Write an INSERT statement to specify the columns and values to be inserted. Execute query: Use the query() method to execute the insertion query. If successful, a confirmation message will be output.

To use MySQL stored procedures in PHP: Use PDO or the MySQLi extension to connect to a MySQL database. Prepare the statement to call the stored procedure. Execute the stored procedure. Process the result set (if the stored procedure returns results). Close the database connection.

Creating a MySQL table using PHP requires the following steps: Connect to the database. Create the database if it does not exist. Select a database. Create table. Execute the query. Close the connection.

One of the major changes introduced in MySQL 8.4 (the latest LTS release as of 2024) is that the "MySQL Native Password" plugin is no longer enabled by default. Further, MySQL 9.0 removes this plugin completely. This change affects PHP and other app

Oracle database and MySQL are both databases based on the relational model, but Oracle is superior in terms of compatibility, scalability, data types and security; while MySQL focuses on speed and flexibility and is more suitable for small to medium-sized data sets. . ① Oracle provides a wide range of data types, ② provides advanced security features, ③ is suitable for enterprise-level applications; ① MySQL supports NoSQL data types, ② has fewer security measures, and ③ is suitable for small to medium-sized applications.
