Finding and replace text in MySQL

I’ve been slowly moving images into Amazon S3. My main purpose to do offload images to another server. This current server is quite badly hit due to hot-linking actually. All the old images hot-linked will be broken. I am not too concern about that. I have updated all my blog posts to reflect the new image urls.

These are the MySQL find and replace commands for URL address changes:

  • UPDATE `wp_posts` SET `post_content` = replace(`post_content`, "'http://beconfused.com/images/", "'http://beconfused.com/new-address/")
  • UPDATE `wp_posts` SET `post_content` = replace(`post_content`, "\"http://beconfused.com/images/", "'http://beconfused.com/new-address/")
  • UPDATE `wp_posts` SET `post_content` = replace(`post_content`, "'http://www.beconfused.com/images/", "'http://beconfused.com/new-address/")
  • UPDATE `wp_posts` SET `post_content` = replace(`post_content`, "\"http://www.beconfused.com/images/", "'http://beconfused.com/new-address/")

Not that my WordPress posts table is called ‘wp_posts’, yours may be called something else.

The above code changes all HTML links and images to the new address. Notice it starts with a quotation, this is to ensure what is being changed is part of a HTML attribute. Single and double quotations will be changed.

You can execute it in phpMyAdmin.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Please leave these two fields as-is: