{"id":6014,"date":"2015-03-02T01:36:57","date_gmt":"2015-03-01T17:36:57","guid":{"rendered":"https:\/\/webhostinggeeks.com\/howto\/?p=6014"},"modified":"2023-06-23T13:12:54","modified_gmt":"2023-06-23T13:12:54","slug":"how-to-change-the-wordpress-urls-in-mysql-database","status":"publish","type":"post","link":"https:\/\/webhostinggeeks.com\/howto\/how-to-change-the-wordpress-urls-in-mysql-database\/","title":{"rendered":"How to Change the WordPress URLs in MySQL Database"},"content":{"rendered":"<p>In WordPress, the URLs of your website are stored in the MySQL database. If you need to change the URLs of your WordPress site, whether due to a domain change or moving from a local development environment to a live server, you can make the necessary changes directly in the database. In this guide, we will walk you through the steps to change the WordPress URLs in the MySQL database using the command line.<\/p>\n<h2>Step 1: Access MySQL:<\/h2>\n<p>First, you need to access your MySQL database using the command line. Open your terminal or SSH into your server and execute the following command:<\/p>\n<pre>\r\nmysql -u username -p\r\n<\/pre>\n<p>Replace &#8216;username&#8217; with your MySQL username. You will be prompted to enter your MySQL password.<\/p>\n<h2>Step 2: Select the WordPress Database:<\/h2>\n<p>Once you are logged into MySQL, you need to select the WordPress database. If you know the name of your WordPress database, use the following command:<\/p>\n<pre>\r\nuse database_name;\r\n<\/pre>\n<p>Replace &#8216;database_name&#8217; with the actual name of your WordPress database.<\/p>\n<p>For example:<\/p>\n<pre>\r\nmysql> use wordpressdb;\r\nReading table information for completion of table and column names\r\nYou can turn off this feature to get a quicker startup with -A\r\n\r\nDatabase changed\r\n<\/pre>\n<p>Check current value for &#8216;siteurl&#8217; and &#8216;home&#8217;:<\/p>\n<pre>\r\nmysql> SELECT option_name, option_value FROM wp_options WHERE option_name IN ('siteurl', 'home');\r\n+-------------+-----------------------------------+\r\n| option_name | option_value                      |\r\n+-------------+-----------------------------------+\r\n| home        | https:\/\/www.webhostinggeeks.local |\r\n| siteurl     | https:\/\/www.webhostinggeeks.local |\r\n+-------------+-----------------------------------+\r\n2 rows in set (0.00 sec)\r\n<\/pre>\n<p>If you are not sure about the database name, you can list all the available databases using the following command:<\/p>\n<pre>\r\nshow databases;\r\n<\/pre>\n<p>Then, select the appropriate database using the use command as shown above.<\/p>\n<h2>Step 3: Update the URLs:<\/h2>\n<p>To change the URLs in the database, you need to update two specific tables: wp_options and wp_posts. Execute the following commands:<\/p>\n<pre>\r\nUPDATE wp_options SET option_value = replace(option_value, 'old_url', 'new_url') WHERE option_name = 'home' OR option_name = 'siteurl';\r\n<\/pre>\n<p>Replace &#8216;old_url&#8217; with the current URL of your WordPress site and &#8216;new_url&#8217; with the new URL you want to set.<\/p>\n<p>For example:<\/p>\n<pre>\r\nmysql> UPDATE wp_options SET option_value = 'https:\/\/webhostinggeeks.com\/howto' WHERE option_name IN ('siteurl', 'home');\r\nQuery OK, 2 rows affected (0.00 sec)\r\nRows matched: 2  Changed: 2  Warnings: 0\r\n<\/pre>\n<p>Check the updated value for &#8216;siteurl&#8217; and &#8216;home&#8217; :<\/p>\n<pre>\r\nmysql> SELECT option_name, option_value FROM wp_options WHERE option_name IN ('siteurl', 'home');\r\n\r\n+-------------+-----------------------------------+\r\n| option_name | option_value                      |\r\n+-------------+-----------------------------------+\r\n| home        | https:\/\/webhostinggeeks.com\/howto |\r\n| siteurl     | https:\/\/webhostinggeeks.com\/howto |\r\n+-------------+-----------------------------------+\r\n2 rows in set (0.00 sec)\r\n<\/pre>\n<p>Next, execute the following command to update the URLs in the wp_posts table:<\/p>\n<pre>\r\nUPDATE wp_posts SET post_content = replace(post_content, 'old_url', 'new_url');\r\n<\/pre>\n<p>Again, replace &#8216;old_url&#8217; with the current URL and &#8216;new_url&#8217; with the new URL.<\/p>\n<h2>Step 4: Confirm the Changes:<\/h2>\n<p>To ensure that the changes have been made successfully, execute the following commands:<\/p>\n<pre>\r\nSELECT option_value FROM wp_options WHERE option_name = 'home' OR option_name = 'siteurl';\r\nSELECT guid FROM wp_posts WHERE post_type = 'post' OR post_type = 'page';\r\n<\/pre>\n<p>These commands will display the updated URLs in the wp_options table and the guid column of the wp_posts table.<\/p>\n<h2>Step 5: Exit MySQL:<\/h2>\n<p>Once you have confirmed the changes, you can exit the MySQL prompt by typing:<\/p>\n<pre>\r\nexit\r\n<\/pre>\n<h2>Commands Mentioned:<\/h2>\n<ul>\n<li><span class=\"fw-bold\">sudo htpasswd<\/span> &#8211; Command to create a password file and set a password for a user.<\/li>\n<li><span class=\"fw-bold\">cat<\/span> &#8211; Command to display the contents of a file.<\/li>\n<li><span class=\"fw-bold\">sudo service apache2 restart<\/span> &#8211; Command to restart Apache web server on Ubuntu\/Debian systems.<\/li>\n<li><span class=\"fw-bold\">sudo systemctl restart httpd<\/span> &#8211; Command to restart Apache web server on CentOS\/RHEL systems.<\/li>\n<li><span class=\"fw-bold\">sudo apachectl restart<\/span> &#8211; Command to restart Apache web server on general systems.<\/li>\n<li><span class=\"fw-bold\">mysql -u username -p<\/span> &#8211; Command to access MySQL database using the command line.<\/li>\n<li><span class=\"fw-bold\">use database_name;<\/span> &#8211; Command to select a specific MySQL database.<\/li>\n<li><span class=\"fw-bold\">show databases;<\/span> &#8211; Command to display all available MySQL databases.<\/li>\n<li><span class=\"fw-bold\">UPDATE wp_options SET option_value = replace(option_value, &#8216;old_url&#8217;, &#8216;new_url&#8217;) WHERE option_name = &#8216;home&#8217; OR option_name = &#8216;siteurl&#8217;;<\/span> &#8211; Command to update the URLs in the `wp_options` table of a WordPress database.<\/li>\n<li><span class=\"fw-bold\">UPDATE wp_posts SET post_content = replace(post_content, &#8216;old_url&#8217;, &#8216;new_url&#8217;);<\/span> &#8211; Command to update the URLs in the `wp_posts` table of a WordPress database.<\/li>\n<li><span class=\"fw-bold\">SELECT option_value FROM wp_options WHERE option_name = &#8216;home&#8217; OR option_name = &#8216;siteurl&#8217;;<\/span> &#8211; Command to display the updated URLs in the `wp_options` table of a WordPress database.<\/li>\n<li><span class=\"fw-bold\">SELECT guid FROM wp_posts WHERE post_type = &#8216;post&#8217; OR post_type = &#8216;page&#8217;;<\/span> &#8211; Command to display the updated URLs in the `guid` column of the `wp_posts` table of a WordPress database.<\/li>\n<li><span class=\"fw-bold\">exit<\/span> &#8211; Command to exit the MySQL prompt.<\/li>\n<\/ul>\n<h2>Conclusion:<\/h2>\n<p>In this guide, we have learned how to change the WordPress URLs in the MySQL database using the command line. By following these steps, you can update the URLs of your WordPress site directly in the database, which is useful when you need to change domains or migrate your site. Remember to take a backup of your database before making any changes and double-check the URLs to avoid any errors.<\/p>\n<p>Feel free to comment below if you have any questions or suggestions for improvement.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In WordPress, the URLs of your website are stored in the MySQL database. If you need to change the URLs of your WordPress site, whether due to a domain change&#8230;<\/p>\n","protected":false},"author":6,"featured_media":6188,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"wds_primary_category":0,"footnotes":""},"categories":[2056,1008],"tags":[1585,1933],"class_list":["post-6014","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-cms","category-mysql","tag-mysql","tag-wordpress"],"_links":{"self":[{"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/posts\/6014","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/comments?post=6014"}],"version-history":[{"count":0,"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/posts\/6014\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/media\/6188"}],"wp:attachment":[{"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/media?parent=6014"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/categories?post=6014"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/tags?post=6014"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}