Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I find and replace individual strings in a MySQL database?

Tags:

mysql

I am switching my Wordpress-powered website to a new server and changing domain names at the same time, which means I have to find and replace many fields in my MySQL database.

Specifically, I must replace "olddomain.com" with "newdomain.com" whenever they appear in a field in MySQL such as "http://olddomain.com/?p=34".

How can I do a massive find a replace in my MySQL database? I know that the LIKE SQL command can find fields with "olddomain.com" in them, but how do I replace specific portions of fields?

like image 295
dangerChihuahua007 Avatar asked Dec 07 '25 07:12

dangerChihuahua007


1 Answers

You can use this search and replace script for this purpose

http://interconnectit.com/124/search-and-replace-for-wordpress-databases/

As the author said raw replace is not good in these cases

When you’re migrating WordPress (or any other platform using serialized PHP strings in the database) between domains, you must use a safe search and replace method that preserves the integrity of the serialized string lengths. A simple of a dump file for localhost to, for example, thenewdomain.com is problematic because the length of the string changes but the indexes for the serialized strings does not. Consequently settings are lost and widgets disappear. Not good.

Referred in Wordpress.org doc itself.

like image 192
Prasenjit Kumar Nag Avatar answered Dec 08 '25 20:12

Prasenjit Kumar Nag



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!