Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to search and replace all instances of a string within a database?

I have a string that is contained inside of a wordpress install (the name of a server) thousands of times, across multiple columns, records and tables.

I'd like to update it with the location of another server - we are moving the content over.

So the source would be something like http://my-server1/some/link/to/something, and I'd want to replace it with http://my-other-server/some/link/to/something. I'm essentially looking to repeat this process for every instance of http://my-server1.

Is there an easy way to do this in MySQL? A tool? Or do I sadly have to update every record problematically?

Thank you,

like image 860
barfoon Avatar asked May 07 '09 17:05

barfoon


People also ask

How do I change all SQL instances of a word?

In SQL Server, you can use the T-SQL REPLACE() function to replace all instances of a given string with another string. For example, you can replace all occurrences of a certain word with another word.

How do I find and replace in a database?

Go to phpMyAdmin and to the database you want to update. Select the required table name and go to “Search” tab. Click on the “Find and Replace” button. Enter the word to be found, and the replacement word.

How do I replace multiple characters in a string in SQL?

If you wanted to replace the words with blank string, go with REGEXP_REPLACE() . If you want to replace the words with other words, for example replacing & with and then use replace() . If there are multiple words to be replaced, use multiple nested replace() .

How replace all occurrences of a string in SQL?

SQL Server REPLACE() Function The REPLACE() function replaces all occurrences of a substring within a string, with a new substring. Note: The search is case-insensitive.


3 Answers

A crude (but effective) way of doing it would be to dump the schema into a file, carefully apply the search-and-replace and then re-import.

As a matter of fact I did that today :)

like image 187
cherouvim Avatar answered Oct 23 '22 21:10

cherouvim


Came across this in a google search, but this may help some people. If you know the tables and columns (you could find this using the wildcard search in phpMyAdmin),

UPDATE table_name SET column_name = REPLACE(column_name, 'http://oldsite.com','http://newsite.com');

Replace bold parts with your own.

If you had a large database you could apply this into a script that could loop through each table and column.

like image 22
Enbee Avatar answered Oct 23 '22 21:10

Enbee


The MySQL dump method would be the best bet if you're happy to re-import the whole database. For anyone that doesn't want to do this - WordPress core installation only actually consists of 11 tables, of which few are content columns, so doing a replace by column would be equally easy. Assuming you don't have loads of plugin tables referencing your link or string this would be your SQL:

UPDATE wp_commentmeta SET meta_value = REPLACE(meta_value,'xcurrentx','xreplacementx');
UPDATE wp_comments SET comment_content = REPLACE(comment_content,'xcurrentx','xreplacementx');
UPDATE wp_links SET link_description = REPLACE(link_description,'xcurrentx','xreplacementx');
UPDATE wp_options SET option_value = REPLACE(option_value,'xcurrentx','xreplacementx');
UPDATE wp_postmeta SET meta_value = REPLACE(meta_value,'xcurrentx','xreplacementx');
UPDATE wp_posts SET post_content = REPLACE(post_content,'xcurrentx','xreplacementx');
UPDATE wp_posts SET post_title = REPLACE(post_title,'xcurrentx','xreplacementx');
UPDATE wp_posts SET post_excerpt = REPLACE(post_excerpt,'xcurrentx','xreplacementx');
UPDATE wp_term_taxonomy SET description = REPLACE(description,'xcurrentx','xreplacementx');
UPDATE wp_usermeta SET meta_value = REPLACE(meta_value,'xcurrentx','xreplacementx');
like image 18
Code Synthesis Avatar answered Oct 23 '22 22:10

Code Synthesis