The ‘search-replace’ Function of WP-CLI Returns a ‘Segmentation Fault’

Here at SatelliteWP, we use WP-CLI extensively! If you’re unfamiliar with the tool, WP-CLI lets you control your WordPress website directly from the command line. While some might find it unnecessary or inconceivable not to use the visual interface (User interface or UI), it’s very useful when you want to execute several tasks in a row, all without clicking!

WP-CLI usage example

For instance, when creating a development environment for the “” website, you need to modify the links contained in the database. The average user will probably use a plugin to accomplish this. First, you need to search for the plugin in the WordPress directory, install it, activate it, set up the address modification, and then run it. Phew!

With WP-CLI, you simply run the search-replace command with the right parameters, and you’re done. The parameters are always the same, so it’s easy to remember.

Your website is a target…

We’re all in hackers’ crosshairs. Get your free analysis of your current situation in less than 5 minutes.

WP-CLI allows you to do almost everything:

  • Add a user
  • Change a setting
  • Backup the database
  • Delete a page
  • Update WordPress
  • Disable a plugin
  • Install a theme
  • Etc.

The list of possibilities is endless!

‘Segmentation Fault’

Recently, we wanted to change our site’s address in the database as follows:

wp search-replace '' 'www.satellitewp.local'

This command is tasked with changing all “” to “www.satellitewp.local” after copying the site to a local environment.

This command is used several times a day here and had never caused a problem. But not this time…

Segmentation fault (core dumped)

The message ‘Segmentation fault’ means a segmentation error that causes the application to crash… without further details.

Fixing a segmentation error in WP-CLI

When such a message appears, the details are too sparse to fix the problem. We need more information! Fortunately, the flag –debug allows for the display of information during execution. Thus, the command becomes:

wp search-replace '' 'www.satellitewp.local' --debug

Once executed, it is now possible to see the reason for this fatal error:

PHP Fatal error: Uncaught TypeError: mysqli::real_connect(): Argument #5 ($port) must be of type ?int, string given in /home/websites/satellitewp/public/wp-includes/class-wpdb.php:2431

Error: A critical error has occurred on your site. Learn more about WordPress debugging.

In this case, with a bit of research, it seems that the MySQL connection port is not specified. To rectify this, simply modify the wp-config.php file to change the following code:

/** MySQL hostname */
define( 'DB_HOST', 'localhost' );

… to this (which now includes the port) :

/** MySQL hostname */
define( 'DB_HOST', 'localhost:3306' );

Once the modification is completed, it is possible to retry the initial command and see if execution goes to the end:

Success: Made 18920 replacements.

Voila! It works!

Should this modification have been necessary? The answer is no! However, this workaround can be handy when you don’t control the environment where WP-CLI has been installed and you want to replace data in the database!

WP-CLI is an incredibly useful tool that drastically speeds up a host of tasks related to development, maintenance, and debugging of WordPress websites. Happy coding!

Similar Posts