我试图弄清楚如何在数据库中定位一个url的所有事件。我想搜索所有表和所有字段。但我不知道从哪里开始,甚至不知道是否有可能。
当前回答
我不记得我在哪里遇到这个脚本,但我一直在使用它与XCloner移动我的WP多站点。
<?php
// Setup the associative array for replacing the old string with new string
$replace_array = array( 'FIND' => 'REPLACE', 'FIND' => 'REPLACE');
$mysql_link = mysql_connect( 'localhost', 'USERNAME', 'PASSWORD' );
if( ! $mysql_link) {
die( 'Could not connect: ' . mysql_error() );
}
$mysql_db = mysql_select_db( 'DATABASE', $mysql_link );
if(! $mysql_db ) {
die( 'Can\'t select database: ' . mysql_error() );
}
// Traverse all tables
$tables_query = 'SHOW TABLES';
$tables_result = mysql_query( $tables_query );
while( $tables_rows = mysql_fetch_row( $tables_result ) ) {
foreach( $tables_rows as $table ) {
// Traverse all columns
$columns_query = 'SHOW COLUMNS FROM ' . $table;
$columns_result = mysql_query( $columns_query );
while( $columns_row = mysql_fetch_assoc( $columns_result ) ) {
$column = $columns_row['Field'];
$type = $columns_row['Type'];
// Process only text-based columns
if( strpos( $type, 'char' ) !== false || strpos( $type, 'text' ) !== false ) {
// Process all replacements for the specific column
foreach( $replace_array as $old_string => $new_string ) {
$replace_query = 'UPDATE ' . $table .
' SET ' . $column . ' = REPLACE(' . $column .
', \'' . $old_string . '\', \'' . $new_string . '\')';
mysql_query( $replace_query );
}
}
}
}
}
mysql_free_result( $columns_result );
mysql_free_result( $tables_result );
mysql_close( $mysql_link );
echo 'Done!';
?>
其他回答
SQLyog是基于GUI的解决方案,可以跨所有列、表和数据库进行数据搜索。用户可以自定义搜索,将其限制为字段、表和数据库。
在它的数据搜索功能中,用户可以像使用谷歌一样搜索字符串。
这不是一个优雅的解决方案,但可以通过嵌套循环结构来实现
// select tables from database and store in an array
// loop through the array
foreach table in database
{
// select columns in the table and store in an array
// loop through the array
foreach column in table
{
// select * from table where column = url
}
}
您可以通过在构建列数组时检查哪些列包含字符串,以及将每个表的所有列合并在一个用逗号分隔的WHERE子句中来加快此过程。
这里有两个简单的代码。首先执行mysqldump:
mysqldump -uUSERNAME -p DATABASE_NAME > database-dump.sql
然后grep sqldump文件:
grep -i "Search string" database-dump.sql
也可以找到/替换并重新导入到数据库中。
使用MySQL Workbench,您可以从"Database" -> " search Table Data"菜单选项中搜索字符串。
指定LIKE %URL_TO_SEARCH%,并在左侧选择要搜索的所有表。您可以使用“Cntrl + A”选择左侧的整个树,然后取消选择您不关心的对象。
在unix机器中,如果数据库不是太大:
mysqldump -u <username> -p <password> <database_name> --extended=FALSE | grep <String to search> | less -S
推荐文章
- 如何通过查询在MySQL中获得数据库结构?
- MySQL删除表中的所有行,并将ID重置为零
- 在准备语句中使用“like”通配符
- MySQL中的表名是否区分大小写?
- 库未加载:libmysqlclient.16。在OS X 10.6上使用mysql2 gem运行'rails server'时出现dylib错误
- 如何知道MySQL表最近一次更新?
- 在MySQL中的一个查询中更新多个具有不同值的行
- 如果表存在则删除表并创建它,如果不存在则创建它
- MySQL OR与IN性能
- 将值从同一表中的一列复制到另一列
- 删除id与其他表不匹配的sql行
- MySQL CPU使用率高
- INT和VARCHAR主键之间有真正的性能差异吗?
- 拒绝访问;您需要(至少一个)SUPER特权来执行此操作
- 从存储引擎得到错误28