溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

MySQL全局遍歷替換特征字符串如何實(shí)現(xiàn)

發(fā)布時(shí)間:2023-03-25 14:26:16 來源:億速云 閱讀:83 作者:iii 欄目:開發(fā)技術(shù)

這篇“MySQL全局遍歷替換特征字符串如何實(shí)現(xiàn)”文章的知識(shí)點(diǎn)大部分人都不太理解,所以小編給大家總結(jié)了以下內(nèi)容,內(nèi)容詳細(xì),步驟清晰,具有一定的借鑒價(jià)值,希望大家閱讀完這篇文章能有所收獲,下面我們一起來看看這篇“MySQL全局遍歷替換特征字符串如何實(shí)現(xiàn)”文章吧。

需求:將一個(gè)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í)例中所有符合特征的庫(kù)表字段

(1)生成查詢SQL語句

$cat find_db_table_column.sh
mysql -uroot -p123456 -h20.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 生成查詢所有包含特征字符串的庫(kù)表字段的SQL語句。查詢條件為:只查詢字符串類型的字段;不查詢系統(tǒng)庫(kù)表;只查詢1G以下的小表;不查詢某些特殊用途(日志、冪等性)的超大表。

(2)執(zhí)行查詢并生成結(jié)果文件

mysql -uroot -p123456 -h20.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. 對(duì)上一步每個(gè)庫(kù)表字段,查詢n條特征數(shù)據(jù),用以人工采樣確認(rèn)

(1)導(dǎo)入庫(kù)表字段數(shù)據(jù)

mysql -uwxy -p -h227.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)入一個(gè)表中,用于下一步生成查詢SQL語句。

(2)生成查詢數(shù)據(jù)的SQL語句

mysql -uwxy -p -h227.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

說明:這里對(duì)于每個(gè)符合條件的庫(kù)表字段,查詢出5條數(shù)據(jù)用于人工確認(rèn)。

(3)執(zhí)行查詢并生成結(jié)果文件

mysql -uroot -p123456 -h20.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 -h227.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 -h20.10.10.1 -P3306 < update.sql

以上就是關(guān)于“MySQL全局遍歷替換特征字符串如何實(shí)現(xiàn)”這篇文章的內(nèi)容,相信大家都有了一定的了解,希望小編分享的內(nèi)容對(duì)大家有幫助,若想了解更多相關(guān)的知識(shí)內(nèi)容,請(qǐng)關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI