MySQL全局遍歷替換特征字符串的實(shí)現(xiàn)方法
需求:將一個MySQL實(shí)例(如10.10.10.1:3306)范圍內(nèi)所有字段數(shù)據(jù)中的 .letssing.net 替換為 .kaixinvv.com。
實(shí)現(xiàn):
1. 確定替換規(guī)則
replace .letssing.net/ -> .kaixinvv.com/ where column like (%http://%.letssing.net/% or %https://%.letssing.net/%)
2. 找出實(shí)例中所有符合特征的庫表字段
(1)生成查詢SQL語句
$cat find_db_table_column.sh
mysql -uroot -p123456 -h10.10.10.1 -P3306 -e "
select concat('select ','''',t1.TABLE_SCHEMA, '''',', ','''',t1.TABLE_NAME, '''',', ', '''',t1.COLUMN_NAME,'''','
from ', t1.TABLE_SCHEMA,'.',t1.TABLE_NAME,' where \`',t1.COLUMN_NAME,'\` like \'%://%.letssing.net/%\' limit 1;')
from information_schema.columns t1, information_schema.tables t2
where t1.DATA_TYPE in ('varchar','longtext','text','mediumtext','char')
and t1.TABLE_SCHEMA not in ('information_schema','mysql','performance_schema','sys')
and (t2.data_length+t2.index_length)/1024/1024/1024 < 1
and t2.table_name not like '%log%' and t2.table_name not like '%idempotent%'
and t1.table_schema= t2.table_schema
and t1.table_name= t2.table_name
order by t1.TABLE_SCHEMA, t1.TABLE_name, t1.column_name;" -N > query.sql說明:
- mysql命令行執(zhí)行查詢,將查詢結(jié)果輸出到文件,-N參數(shù)用于去掉表頭。
- 通過查詢數(shù)據(jù)字典視圖 information_schema.columns 和 information_schema.tables 生成查詢所有包含特征字符串的庫表字段的SQL語句。查詢條件為:只查詢字符串類型的字段;不查詢系統(tǒng)庫表;只查詢1G以下的小表;不查詢某些特殊用途(日志、冪等性)的超大表。
(2)執(zhí)行查詢并生成結(jié)果文件
mysql -uroot -p123456 -h10.10.10.1 -P3306 -N < query.sql > result.txt
result.txt文件內(nèi)容示例:
db1 table1 column1
db1 table1 column2
db2 table2 column1
db2 table2 column2
3. 對上一步每個庫表字段,查詢n條特征數(shù)據(jù),用以人工采樣確認(rèn)
(1)導(dǎo)入庫表字段數(shù)據(jù)
mysql -uwxy -p -h127.0.0.1 -p123456 -P3306 --local-infile -Ddomain -e " truncate table t1; load data local infile '/home/mysql/domain_name/rule/result.txt' into table t1(dbname,tablename,columnname);"
說明:將前一步生成的結(jié)果文件導(dǎo)入一個表中,用于下一步生成查詢SQL語句。
(2)生成查詢數(shù)據(jù)的SQL語句
mysql -uwxy -p -h127.0.0.1 -p123456 -P3306 -Ddomain -e "
select concat('select ',instance,',''',dbname,''',''',tablename,''',\`',columnname,'\` from ',dbname,'.',tablename, ' where \`',columnname,'\` like \'%://%.letssing.net/%\' limit 5;') from t1 order by instance,dbname,tablename;" -N > query_domain.sql
說明:這里對于每個符合條件的庫表字段,查詢出5條數(shù)據(jù)用于人工確認(rèn)。
(3)執(zhí)行查詢并生成結(jié)果文件
mysql -uroot -p123456 -h10.10.10.1 -P3306 < query_domain.sql > result_domain.txt
result_domain.txt文件內(nèi)容示例:
db1 table1 column1
db1 table1 http://txcdn-song-mvbox-cn.letssing.net/mka/16/90461116-0.mka
db1 table1 http://txcdn-song-mvbox-cn.letssing.net/mka/16/90461116-0.mka
db1 table1 column2
db1 table1 http://txcdn-song-mvbox-cn.letssing.net/ksc/90/16/90461116-0.ksc
db1 table1 http://txcdn-song-mvbox-cn.letssing.net/ksc/90/16/90461116-0.ksc
db2 table2 column1
db2 table2 http://txcdn-song-mvbox-cn.letssing.net/mka/16/90461116-0.mka
db2 table2 http://txcdn-song-mvbox-cn.letssing.net/mka/16/90461116-0.mka
db2 table2 column2
db2 table2 http://txcdn-song-mvbox-cn.letssing.net/ksc/90/16/90461116-0.ksc
db2 table1 http://txcdn-song-mvbox-cn.letssing.net/ksc/90/16/90461116-0.ksc
4. 更新特征域名數(shù)據(jù)
(1)生成字符串替換的更新SQL語句
mysql -uwxy -p -h127.0.0.1 -p123456 -P3306 --local-infile -Ddomain -e "
select concat('update ',dbname,'.',tablename,' set \`',columnname,'\` = ','replace(\`',columnname,'\`,','\'.letssing.net/\',\'.kaixinvv.com/\')',
' where \`',columnname,'\` like \'%http://%.letssing.net/%\' or \`', columnname,'\` like \'%https://%.letssing.net/%\';')
from t1 where instance = 1 order by instance,dbname,tablename;" -N > update.sql
(2)執(zhí)行更新
mysql -uroot -p123456 -h10.10.10.1 -P3306 < update.sql
到此這篇關(guān)于MySQL全局遍歷替換特征字符串的實(shí)現(xiàn)方法的文章就介紹到這了,更多相關(guān)MySQL全局遍歷替換特征字符串內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
MySQL核心參數(shù)優(yōu)化文件my.ini實(shí)現(xiàn)
本文主要介紹了MySQL核心參數(shù)優(yōu)化文件my.ini實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-01-01
MySQL數(shù)據(jù)庫InnoDB引擎主從復(fù)制同步經(jīng)驗(yàn)總結(jié)
這篇文章主要介紹了MySQL數(shù)據(jù)庫InnoDB引擎主從復(fù)制同步經(jīng)驗(yàn)總結(jié),本文總結(jié)了設(shè)置主從復(fù)制時遇到的一些錯誤和解決方法,需要的朋友可以參考下2015-01-01
mysql5.7.18安裝時mysql服務(wù)啟動失敗的解決方法
這篇文章主要為大家詳細(xì)介紹了mysql5.7.18安裝時mysql服務(wù)啟動失敗的解決方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-03-03
MySQL優(yōu)化全攻略-相關(guān)數(shù)據(jù)庫命令
MySQL優(yōu)化全攻略-相關(guān)數(shù)據(jù)庫命令...2006-11-11
mysql-connector-java與mysql版本的對應(yīng)關(guān)系說明
這篇文章主要介紹了mysql-connector-java與mysql版本的對應(yīng)關(guān)系說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-02-02
Mysql復(fù)制表三種實(shí)現(xiàn)方法及grant解析
這篇文章主要介紹了Mysql復(fù)制表三種實(shí)現(xiàn)方法及grant解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-09-09
mysql server 5.7.20 安裝配置方法圖文教程
這篇文章主要為大家詳細(xì)介紹了mysql server 5.7.20 安裝配置方法圖文教程,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-09-09

