i would like to do a find and replace inside an entire database not just a table.
How can i alter the script below to work?
update [table_name] set
MySQL Search & Replace Tool
Very useful web-based tool written in PHP which makes it easy to search and replace text strings in a MySQL database.
If you are in phpMyAdmin and you have only a minimal change, you can do this in an easy way.
Click on the search option
You can always select either all the tables or any. Remember to give the search keyword, it will be used as wildcard(%).
Now you can open each table one by one and perform the update A sample query generated may look like the following.
SELECT * FROM sibeecst_passion
.wp_ewwwio_images
WHERE (CONVERT(id
USING utf8) LIKE '%sibee%' OR CONVERT(path
USING utf8) LIKE '%sibee%' OR CONVERT(image_md5
USING utf8) LIKE '%sibee%' OR CONVERT(results
USING utf8) LIKE '%sibee%' OR CONVERT(gallery
USING utf8) LIKE '%sibee%' OR CONVERT(image_size
USING utf8) LIKE '%sibee%' OR CONVERT(orig_size
USING utf8) LIKE '%sibee%' OR CONVERT(updates
USING utf8) LIKE '%sibee%' OR CONVERT(updated
USING utf8) LIKE '%sibee%' OR CONVERT(trace
USING utf8) LIKE '%sibee%' OR CONVERT(attachment_id
USING utf8) LIKE '%sibee%' OR CONVERT(resize
USING utf8) LIKE '%sibee%' OR CONVERT(converted
USING utf8) LIKE '%sibee%' OR CONVERT(level
USING utf8) LIKE '%sibee%' OR CONVERT(pending
USING utf8) LIKE '%sibee%' OR CONVERT(backup
USING utf8) LIKE '%sibee%')
I had the same issue on MySQL. I took the procedure from symcbean and adapted her to my needs.
Mine is only replacing textual values (or any type you put in the SELECT FROM information_schema) so if you have date fields, you will not have an error in execution.
Mind the collate in SET @stmt, it must match you database collation.
I used a template request in a variable with multiple replaces but if you have motivation, you could have done it with one CONCAT().
Anyway, if you have serialized data in your database, don't use this. It will not work unless you replace your string with a string with the same lenght.
Hope it helps someone.
DELIMITER $$
DROP PROCEDURE IF EXISTS replace_all_occurences_in_database$$
CREATE PROCEDURE replace_all_occurences_in_database (find_string varchar(255), replace_string varchar(255))
BEGIN
DECLARE loop_done integer DEFAULT 0;
DECLARE current_table varchar(255);
DECLARE current_column varchar(255);
DECLARE all_columns CURSOR FOR
SELECT
t.table_name,
c.column_name
FROM information_schema.tables t,
information_schema.columns c
WHERE t.table_schema = DATABASE()
AND c.table_schema = DATABASE()
AND t.table_name = c.table_name
AND c.DATA_TYPE IN('varchar', 'text', 'longtext');
DECLARE CONTINUE HANDLER FOR NOT FOUND
SET loop_done = 1;
OPEN all_columns;
table_loop:
LOOP
FETCH all_columns INTO current_table, current_column;
IF (loop_done > 0) THEN
LEAVE table_loop;
END IF;
SET @stmt = 'UPDATE `|table|` SET `|column|` = REPLACE(`|column|`, "|find|", "|replace|") WHERE `|column|` LIKE "%|find|%"' COLLATE `utf8mb4_unicode_ci`;
SET @stmt = REPLACE(@stmt, '|table|', current_table);
SET @stmt = REPLACE(@stmt, '|column|', current_column);
SET @stmt = REPLACE(@stmt, '|find|', find_string);
SET @stmt = REPLACE(@stmt, '|replace|', replace_string);
PREPARE s1 FROM @stmt;
EXECUTE s1;
DEALLOCATE PREPARE s1;
END LOOP;
END
$$
DELIMITER ;
Another option (depending on the use case) would be to use DataMystic's TextPipe and DataPipe products. I've used them in the past, and they've worked great in the complex replacement scenarios, and without having to export data out of the database for find-and-replace.
This isn't possible - you need to carry out an UPDATE for each table individually.
WARNING: DUBIOUS, BUT IT'LL WORK (PROBABLY) SOLUTION FOLLOWS
Alternatively, you could dump the database via mysqldump and simply perform the search/replace on the resultant SQL file. (I'd recommend offlining anything that might touch the database whilst this is in progress, as well as using the --add-drop-table and --extended-insert flags.) However, you'd need to be sure that the search/replace text wasn't going to alter anything other than the data itself (i.e.: that the text you were going to swap out might not occur as a part of SQL syntax) and I'd really try doing the re-insert on an empty test database first.)
sqldump to a text file, find/replace, re-import the sqldump.
Dump the database to a text file
mysqldump -u root -p[root_password] [database_name] > dumpfilename.sql
Restore the database after you have made changes to it.
mysql -u root -p[root_password] [database_name] < dumpfilename.sql