I once owned astronomytime.com. It was a wordpress site focused on astronomy, space and telescopes.
But I let the domain expire in 2017 🙁
I still had all the WordPress files and database. And decided I wanted to continue having that site but the domain was no longer available.
So I bought a new domain, astronomytips.com.
I was thinking adjusting the siteurl and home variables in the wp_options table would do the trick – becuase that’s what WordPress wants you to do when you adjust the domain name. Like when you are changing DNS and need to launch a new website to go live.
Turns out that does still need to be done there were tons of mentions to the old domain name in the content and in other parts of the database. Too time consuming to have to log into the database and update those by hand.
So what to do?
Vi to the rescue to do a search and replace!
Sure I could download the database from my web browser PHPMyAdmin tool my hosting service provides to my desktop… but I find that sometimes creates extra characters and unwanted results when I open it up and start editing. Better to keep things clean when making database edits.
First, I SSH-ed into my web server. Since I’m on my Windows PC I prefer using WSL (Windows subsystem for Linux) where I have Ubuntu installed. That let’s me easily access the linux command line and run the ssh command. You could also just use PowerShell, which also has an SSH command (but not the same as linux):
And then once I’m logged into my web server, then did a mysqldump of the database (The database connection info is kept in wp-config.php). Notice how the arrow (to the left of dump.sql) is pointing to the right indicating the database should be exporting into dump.sql:
mysqldump -u <username> -p -h <host> <database_name> > dump.sql
Next, I opened up that dump.sql file in vi like this
Then did a search and replace across the entire file like this. The % indicates we want to replace within the entire file (all lines). The “g” indicates to replace all instances within any particular line. And we are replacing “astronomytime” with “astronomytips”:
Now save and exit the file. To do that I just hit esc and then do:
And bang! Done! Database file is ready to go…
Now just need to reload that same database file into the WordPress mysql database, like using the following mysql command. Notice how the arrow is pointing left instead of right, that means the file should be read into the database.
mysql -u <username> -p -h <host> <database_name> < dump.sql
And that does it, the new database is loaded into the database server and the website now has all instances of the old domain replaced with the new one!
This means not just the links but also the images and styling elements affected also began working as they should. Super awesome.