This can be easily achieved with a simple SQL statement using MySQL's replace()
function. Before we do that, you should definitely do a database dump or whatever you use for backups. It's not only that it's The Right Thing To Do™, but if you make a mistake on your substitution, it might prove difficult to undo it (yes, you could rollback, but you might only figure out your mistake later on.)
To create a database dump from MySQL, you can run something like this --
mysqldump -h hostname -u username -p databasename > my_sql_dump.sql
Where (and you probably know this, but for the sake of completeness for future generations...) --
- hostname is a placeholder for the database hostname. If the database server is running on your own machine, then you can either use "localhost" or simply leave the "-h hostname" off entirely
- username is a placeholder for the user with permission to run a dump on the database. This is often an admin, but if it's a shared db, it might simply be you.
- databasename is the name of the whole database containing your tables. (Note that the "-p" has nothing to do with this database name. "-p" indicates that MySQL should ask you for a password in order to log in.)
Now that we got that out of the way, you can log in to the MySQL database using:
mysql -h hostname -u username -p databasename
And simply run this statement:
UPDATE `wp-posts` SET `post-content` = REPLACE(`post-content`, "http://oldurl.com", "http://newurl.com");
And that should do it!
If you make a mistake, you can often rerun the statement with the original and new texts inverted (if the new text -- in your case the new URL -- didn't already exist in the text before you did the replace.) Sometimes this is not possible depending on what the new text was (again, not likely in your case.) Anyway, you can always try recovering the sql dump --
cat my_sql_dump.sql | mysql -h hostname -u username -p databasename
And voilà.