Want to Write for Us?Read This | LoginBecome a Member
mysql search and replace php

MySQL Search & Replace Tool

This past weekend I had a WordPress website migration to make.

It wasn’t your traditional migration, as the domain changed. With well over 500 blog posts, it wasn’t a small move, but I did learn a few things that I would like to share with you all.

After importing the old database info into it’s new place, I went ahead and changed the WordPress settings via MySQL (maybe will cover this more later). I thought I was done, until I realized that all internal links were wrong. From images to URLs, the blog was essentially broken.

Knowing that I could run some MySQL magic and update all of these in one routine, I hit-up Google on the deets.

Here’s what I learned:

“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 dump file for http://localhost to, for example, http://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.”

Solution?

Serialized PHP Search & Replace Tool

mysql search and replace php

This was ridiculously awesome.

Not only did this update everything the correct way, it was quick and easy!

Here’s what I did:

  • Downloaded Search Replace DB v2.1.0 (a simple PHP file)
  • Renamed it with a cryptic name
  • Uploaded it to my root WordPress install (the usual httpdocs)
  • Jumped to my browser and dialed it in: http://newurl.com/crypticname.php
  • Followed the prompts
  • Ska-DOOSH! <– (the sound I made in my head when it finished)
  • Deleted the crypticname.php file from the server

And that’s it!

All the internal URLs and image file links were updated and now correct. Plus, I had the security that the strings in the database were not harmed in any way.

Pretty cool, right?

You can download the latest version and learn more on the Interconnect/it website.

[via Internconnectit.com]

11 Responses to “MySQL Search & Replace Tool”

  1. October 22, 2012 at #

    I’ve used this the last few times I’ve had to migrate from development to a live environment. It’s amazing!

    • October 23, 2012 at #

      Ah, yes. You’ve experienced the joy, too. :)

  2. Peter
    October 22, 2012 at #

    Eric, I’d love to hear how you did this migration. I did a WP migration this past weekend too and all of my images worked just fine. But then again, I wasn’t changing hosts, everything was on the same server but it was a new domain and new WP installation. I also exported my old database and imported it into the new one.

    • October 23, 2012 at #

      Dude! You’re a pro!

      • Peter
        October 23, 2012 at #

        Ha ha, thanks :-)

        We host with WP Engine and they have great documentation online. I used their self migration process to do all of my migrations because I found that their process works with any host. If you’re interested Eric, you can check out the documentation here:

        http://support.wpengine.com/migration-process/

        • October 24, 2012 at #

          Cool! Thanks!

          (Pricy! But nice :) )

  3. October 23, 2012 at #

    I just went through this headache recently too. Search/Replace’s are easy. But maintaining serialization is where it gets tricky. I used this pretty sweet utility called Peach on Github: http://petesaia.github.com/Peach/. My help you next time too.

    • October 24, 2012 at #

      Very cool, Scott, thanks for the link love!

  4. Cory
    February 16, 2013 at #

    Why not just open the .sql file in notepad+ find the old domain URL and choose replace all with new domain URL? I do this all the time. It’s priceless.

    • February 18, 2013 at #

      I would rethink that. As stated in the post:

      “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 dump file for http://localhost to, for example, http://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.”

      I’m not going to risk it.

Trackbacks/Pingbacks:

  1. A Peachy SQL Tool for WordPress Domain Migrations | ChurchMag - January 9, 2013

    [...] personally dealt with this using a different solution (you can read about that here), but I’ve since found an online solution that’s really [...]

Leave a Reply

Gravatar Image