Use the Command Line Search Replace DB Script

Share on facebook
Share on twitter
Share on pinterest

You might have heard me mention this script in my earlier articles as I wholeheartedly recommend it for changing those pesky links that still point to old URLs. I have no idea how people move or copy their WordPress site without the Search Replace DB tool. Its strength lies in replacing serialized values deep in the database. Furthermore, it’s not dependent on WordPress although it works with it (and it’s even officially recommended). This article focuses on the command line version of Search Replace DB, which is more robust and less prone to errors.

The GUI can fail!

I’ve only used the graphical UI until the day it seemed to fail on me (with an AJAX error). The problem came from moving a WordPress site I did not create. There was a plugin called Duplicator that used a BLOB, a binary object data type in the database, which I admit I’ve never seen before. I had to dig into server logs on Amazon RDS, after seeing nothing on the web server. The only thing I found was “Aborted connection” (Got an error reading communication packets). Long story short, I decided to give command line Search Replace DB a go, and I’m happy I tried it.

How does the command line Search Replace DB differ?

The operation of actual search and replace is much faster, not that it matters much as I’m not doing this every day. But it seemed that the beautiful interface that works with a bunch of AJAX calls is an overhead. This tool uses PHP CLI, which is entirely different from what serves your pages. It might not even be the same version!

The only cumbersome thing I noticed is that it doesn’t automatically load credentials from wp-config.php. I had to specify everything directly.  However, I was finally able to see the PHP error: Class __PHP_Incomplete_Class has no unserializer. After that, I turned to the project’s community on GitHub for a suitable pull request that promptly addressed my problem.

Why is it more secure?

I think it’s excellent that you don’t need to put the tool into a publicly accessible folder under some obscure name to use it (from the browser). If you accidentally leave the GUI version there, it’s an open door for attackers if they find the tool. On the contrary, you can place the command line Search Replace DB anywhere on your server! You interact with it over SSH, and you don’t even need to remove it after you are done.

How to use the CLI?

The readme is very detailed, so I’d rather not repeat it here. The line I’ve been using with great success for changing the host of a site is:

php srdb.cli.php -h example.rds.amazonaws.com -n database_name -u user_name -p your_password -s old.example.com -r example.com -v true -z

(Obviously, you need to put in your details.) As you can see, it requires the host and database name/user/pass. The -s is the search and -r is the value you replace it with. My advice is to keep verbose mode on with -v true to see what’s going on. Very important: at first, use the dry mode with the -z option. Once you are confident, remove it to turn it live and rerun the command. As an example, this is what it looks like:

results: The dry-run option was selected. No replacements will be made.
wp_commentmeta: replacing old.example.com with example.com
wp_commentmeta: 0 rows, 0 changes found, 0 updates made in 0.00045514 seconds
wp_comments: replacing old.example.com with example.com
wp_comments: 1 rows, 0 changes found, 0 updates made in 0.00049305 seconds
wp_links: replacing old.example.com with example.com
wp_links: 0 rows, 0 changes found, 0 updates made in 0.00015807 seconds
wp_options: replacing old.example.com with example.com
wp_options: 125 rows, 3 changes found, 0 updates made in 0.00367403 seconds
wp_postmeta: replacing old.example.com with example.com
wp_postmeta: 6 rows, 0 changes found, 0 updates made in 0.00053310 seconds
wp_posts: replacing old.example.com with example.com
wp_posts: 4 rows, 6 changes found, 0 updates made in 0.00090098 seconds
wp_term_relationships: replacing old.example.com with example.com
wp_term_relationships: 1 rows, 0 changes found, 0 updates made in 0.00029993 s...
wp_term_taxonomy: replacing old.example.com with example.com
wp_term_taxonomy: 1 rows, 0 changes found, 0 updates made in 0.00040007 seconds
wp_termmeta: replacing old.example.com with example.com
wp_termmeta: 0 rows, 0 changes found, 0 updates made in 0.00014710 seconds
wp_terms: replacing old.example.com with example.com
wp_terms: 1 rows, 0 changes found, 0 updates made in 0.00082612 seconds
wp_usermeta: replacing old.example.com with example.com
wp_usermeta: 48 rows, 0 changes found, 0 updates made in 0.00122905 seconds
wp_users: replacing old.example.com with example.com
wp_users: 3 rows, 0 changes found, 0 updates made in 0.00074220 seconds

Replacing old.example.com with example.com on 12 tables with 190 rows
9 changes would have been made
0 updates were actually made
It took 0.02472520 seconds

And we're done!

Have you ever used Search Replace DB?

Related Posts

Leave a Reply

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

Join Our Newsletter!

Get secret tips and valuable details about making your site more successful.

Follow Us!

We Recommend

This site is powered by Elementor

100% Free  WP Website Builder  All-in-One Solution