Changes WordPress database prefix by exporting database, updated references and then re-importing database. Be sure to update db_prefix_new
to a new database prefix.
db_prefix=$( wp db prefix )
db_prefix_new="wp_random_"
db_file="mysql-database-backup.sql"
wp db export $db_file
sed --in-place --expression "s#\`${db_prefix}#\`${db_prefix_new}#g" $db_file
sed --in-place --expression "s#'${db_prefix}user_roles#'${db_prefix_new}user_roles#g" $db_file
sed --in-place --expression "s#'${db_prefix}capabilities#'${db_prefix_new}capabilities#g" $db_file
sed --in-place --expression "s#'${db_prefix}user_level#'${db_prefix_new}user_level#g" $db_file
wp db reset --yes
wp db import $db_file
wp config set table_prefix $db_prefix_new --skip-plugins --skip-themes