Error Establishing a Database Connection

I hadn’t started up my virtual test server since changing the network configuration to get port forwarding to work properly. Today when I tried it out, I got the dreaded “Error Establishing a Database Connection” message on all my local WordPress sites.

Even the mysql client couldn’t connect to the database, and of course neither could phpmyadmin. The database server was running and I had even restarted it multiple times to make sure. Removing it and installing it again didn’t help either.

I finally found the problem in the mysql config file in /etc/mysql/my.cnf. The bind-address setting was set to the old fixed IP address I had been using. I’m not sure why it hadn’t broken before when I first changed from a fixed IP to the Virtualbox NAT. I changed it back to ‘localhost’, restarted mysql server, and all my sites started working again.

EDIT: I realised from WordPress.com’s statistics page that there are quite some visitors landing here by searching for the title. The information above is probably not what you’re looking for.

If you’re getting this error on your WordPress.org self hosted install, check the settings in the wp-config.php file. If you’re unsure what the values should be, ask your hosts. Every host configures their servers in their own way. Some have the database on the same machine, so the DB_SERVER setting is ‘localhost’. Others might put it on a dedicated database server with its own hostname. Also make sure that your hosting plan lets you use a database. Some of the cheapest ones may not.

If you’re still stuck, post a question in the Installation or How-to and Troubleshooting sections of the WordPress.org forum.

Networking Woes

After changing ISP, I got a new modem with a built in wireless router. I managed to configure port forwarding and get it to work with my test server running on VirtualBox. Just for the record, the port forwarding interface on the device required me to select from a list of devices. I had to port forward to the host machine’s IP.

After some weeks of using the built in wireless router, I got tired of the weak signal strength. To be fair, it might be because it is placed in a less centralized location due to wiring constraints. I decided to connect my Linksys router to it and use the Linksys device for wireless service.

I had previously tried connecting a LAN cable from the local Ethernet port of the ISP provided modem to the WAN port of the Linksys device. That didn’t work at all. However, it does work when it’s connected to a LAN port on the Linksys device. I’m not sure why that’s the case as I thought putting it to WAN would just send all the packets through another layer of address translation.

Next step was to disable the wireless network on the built in modem and make sure that the Linksys router was configured with the desired settings. This was accomplished with the provided web administration interfaces.

When I tested the network, some devices could get Internet access, while some couldn’t. I had to disable and re-enable the wireless network adapter to get it to work.

Unfortunately, all these steps broke port forwarding to my guest OS. I had VirtualBox set up to use a bridged network, but it looked like the modem didn’t even recognize the guest as its IP did not show up in the list of connected devices.

I looked through the other network options in VirtualBox and looked up the documentation.

It turns out that with the NAT option, I could run a server on the guest OS if I configured port forwarding with VBoxManage. There’s even an example on the page.

Shut down the guest OS, change the networking option to NAT, and run the following command.

VBoxManage modifyvm "guest-name" --natpf1 "guest-https,tcp,,443,,443"

Restart the guest. As no IP address was specified with modifyvm, it is necessary to use DHCP on the guest. Change the networking settings, restart the eth interface, and try to access the web server.

A request to the web server now goes to the modem, where it is forwarded to the host machine, which then forwards it onwards to the guest! A little convoluted, but once again my test server is publicly accessible so I can ask friends for feedback.

Hiding the Editor Part 2

A previous post explained how to hide the WordPress editor for custom post types.

I just noticed that this no longer works in WordPress 3.3.1. It looks like the CSS Ids have changed for the editor. The whole editor is now enclosed by a div with the id ‘postdivrich’.

Thus the hide_editor() function now reads as below. Much simpler.

	function hide_editor() {
		?>
			<style>
				#postdivrich {display:none;}
			</style>
		<?php
	}

Adding Announcements

As the new year came round, a requirement to post announcements on the home page of the site came up. I figured that the easiest way to do this is to edit the page content directly. Using the built in HTML editor, I put the text in an ‘announcements’ div.

<div id='announcements'>
Announcement text here</div>

A simple addition to the theme’s stylesheet ensured that the text would look different and stand out.

#announcements {
	background-color: #21759B;
	color: #FFFFFF;
	clear: both;
	padding: 6px 12px 6px 12px;
	-moz-border-radius: 12px;
	-khtml-border-radius: 12px;
	-webkit-border-radius: 12px;
	border-radius: 12px;
	margin-bottom: 20px;
}

Of course, this only looks nice if the announcements area isn’t too long. Future work may include adding ‘Announcements’ as a custom post type so other people can make the changes easily. With more announcements, it may also be necessary to use a ticker style, showing only one at a time and allowing the user to scroll through the list.