MySQL replication problem

My MySQL replication slave stopped replicating the master. Checked the log file it has this error:

Error reading packet from server: log event entry exceeded max_allowed_packet; Increase max_allowed_packet on master (server_errno=1236)

Got fatal error 1236: ‘log event entry exceeded max_allowed_packet; Increase max_allowed_packet on master’ from master when reading data from binary log

Naturally I started increasing the max_allowed_packet in my.cnf. No help. So I looked up the web and found this bug documented. Bingo I thought…oh wait, the bug was discovered a few years back and my version of MySQL should have included the fix already. Ok back to square one.

Just to poke around I ran “show master status” on the master server and compare bin file name and position with what’s in the logs on slave server. The files do not match. It may or may not cause the error 1236 but it’s definitely not right. So I went ahead ran the following command to re-sync:

On the slave:

mysql -u root -pxxxxx





Now step two to load data from mater created a lot of error logs since my slave DB had had data so I really should have clean up the DB before run the commands.

So the replication is back to work now. Not sure what had really caused error 1236. Get my fingers crossed.

Posted in mysql | Comments Off

home wiring 2.0

We moved into our new home a year ago and back then the builder asked me about the network wiring of the house. Basically the home will be wired with cat5 cables for phone service. If I want a network (data) jack additionally, more cables will be added – and of course the cost. Buying a new home was financially demanding so I went on the cheap side of me and opted for voice only.

I have been using wireless network since then and I was generally pretty happy about it. However I’m moving my office to the basement and if the router follows me the wireless access will be very spotty from the second floor. Adding more access point is not a good option in my opinion so I decided to re-configure the wiring so I can have the wired data access in rooms that I want.

One big pre-condition for this setup is that I DON”T have a landline and don’t intend to get one. Like 20% of American household (and growing) we only use cellphones. I do have a SIP phone (basically a VOIP solution) and it needs a data port. Currently it is connected to my wireless router through a VOIP adapter. My VOIP phone is another good reason to have more data ports around the house. I have a cordless phone set which only needs one line to the base station (all the other handsets are cord free).

So I made the decision to go ahead re-wire cat5 to support data only.

One alternative – if you need voice jack – is to split the cat5 cable to support 2 jacks, one for voice and the other for data. Obviously that requires more work. Since this is not my configuration I’ll briefly mention it at the end of the post.

What is cat 5 cable

Here we need to have a little bit of explanation on cat5. “Category 5″ is a cable specification.

The cable looks familiar? Yep, you see it everyday if you look under the desk, around your ethernet switches.

A cat 5 cable includes 8 wires, every two of which are twisted together, known as twisted pairs. The wires are color coded, they are: orange, white orange, green, white green, blue, white blue, brown and white brown.

These wires carry voice or data signal. As a matter of fact, only 2 pairs (4 wires) are needed to support a 10/100 BASE-T network; only 1 pair is needed for a voice line. That’s why it’s possible to split one cat 5 for both data and voice. Again, in my case, I’m going all data.


A good planning always pays. Here is a list of things you need to check and prepare before start cutting wires.

  • You house is wired with cat 5 or cat 5e. Usually this is done in newer house and it can be confirmed by checking the outlet. It should say cat5 on it. If this is not the case, stop reading and go visit
  • You have “star” wiring. Meaning every single wire branches out from the central panel. If they are in a loop, you can stop now and go watch a movie.
  • You can access your switch board and it is either inside the house or in your basement. Because you’ll put cable modem and network switch there it needs to be securely located.
  • Equipments, tools and parts.

1. The cat 5 jacks. I bought 10 for $29 and didn’t even need one at the end.


2. The Leviton data board.


3. A bunch of ethernet cables.

4. A wired router


5. Some tools


  • Internet downtime.

The internet will be broken for a few hours and you’ll need to let the wife know.

Let’s roll

1. Re-wire the patch panels

Note: I chose to do this first – instead of the jack – was because there is a chance the wiring of the jacks will work without any changes.

The following photo shows the patch board where all the cat 5 cables are originally terminated. Notice the third from the left is the new data board that I added. The builder’s contractor did a great job labeling the cable so I didn’t have any problem to locate the one that I want to rewire.


So I basically cut of the wires and punch them in to the new data panel (3rd one on the left). Remember the color codes for those twisted pairs? Now it’s the time to use them. Below is my color code mapping.

Wire color Board slot
Green Green
White green White green
Orange Orange
White orange White orange

Although the color codes seem to be a straight one to one mapping there is somewhat a standard to follow. And it matters because both ends of the cable need to follow the same standard to get the data juice flowing. Just remember T-568A is the standard we are going by here. You may or may not need to rewire the jack to T-568A, depending on what your builder used originally. To learn more about the standards you can visit this page.

The Leviton patch board has instruction on the color mapping on each punch slot so it was super easy to find the correct punch slot. It’s the first time that I work on these punchdowns and I have to say it was pretty easy to get a hang of it. Just keep a steady hand and enjoy that little clicks when you push the wire in. The plastic tool that comes with the patch panel was good enough for me and I don’t think you need to invest a $30 punch tool unless you are planning to do this to make a living. Again, I’m cheap.

This photo shows the two wires are terminated into data panel. The cables coming out of the black ports go into the router.


2. Connect the patch panel to a router

Now is the time to notify your family that the internet will be down. I took the cable modem and connect it directly to cable company’s video feed. Did I mention I don’t have cable TV? If you do, you probably need a splitter. Then, connect the cable modem to a wired Linksys router (I left the wireless router upstairs and it will be my access point). Now it’s also a good time to configure/check the router if it’s a new one. Once you verified it is online, connect it to your patch board and make sure you use the right port where the wires were terminated.

3. Check and re-wire the jack, if needed.

If you are as lucky as me your jack is a data port already. If not, it is possible it’s not wired using T568A standard so you’ll need to rewire it that way. The cat 5 jack should have a label on it that clearly indicates which color goes where for A or B standard so it should be pretty easy to punch and attach. Here is the color code mapping:

Wire Jack slot
Green Green
White green White green
Orange Orange
White orange White orange

As I mentioned I didn’t even need to rewire the jack but I did one anyway. Here is a picture:


At this point you should have one room wired. So plug in a your computer you should have the internet. If not, check the wires, connections.

4. Take a break and finish up

Now it’s a good time to take a break and give yourself a pad on the back. If you have more outlets to convert, repeat the same steps. Once you are done, tidy up the wires and outlets.

The old wireless router can now be used as a simple access point. Just make sure to re-configure it to a different static IP ( for example) and turn off DHCP (The wired router is doing that now). Connect one of the LAN port to a network switch or a data jack you are done.

Voice + Data over one cat 5

As I mentioned I don’t use this kind of setup but since I did some research on it I might as well write it down.

Remember there are 4 pairs of wires in a cat 5 cable? We just used 2 pair. The 2 pairs left can be used for phone lines, and as matter of fact, two phone lines if you wish.

1. Make a phone jack

You can use a cheaper voice jack for this one. Here is the color code mapping:

Wire color Jack slot
Blue Blue
White blue White blue
Brown Brown
White brown White brown

2. Terminate phone jacks

Since I don’t have a land line of my own so I don’t get a chance to practice this. Basically one twisted pair can support one voice line so you can play with it and see what’s the best fit for you.

One suggestion is that you should use a regular voice jack, which is smaller than cat 5 jack so you can’t accidentally plug in data cable in a voice jack. Since there is always a chance the wires are not done correctly, the high voltage generated from the phone rings can toast your network adapter if the cable is plugged into the wrong jack.

Some think there will be data transfer degradation when the voice line is in use. Based on what I read and talking to friend who actually did this setup, it seems more like just a theory.

Document what you did

Once you get it working please make sure finishing the last mile before start surfing the web: documenting what you did. Label the wires, mark the jacks. Or write a blog post, maybe.

So that’s how I rewired my home to version 2.

As landline becomes less and less popular these days I really think this is a setup that may become more and more popular. As we are going to see more and more “network intelligent” electronic devices in homes in the future. Devices to stream internet video to TV is a good example and is already making the front run. With more bandwidth hungry devices in your home wireless access point can become a bottleneck. In that case, the more data port, the merrier. Of course if you are building a new home and have the chance and means, you can pimp the wiring up by doubling cat 5 or even adding cat 6. But for most of the existing homes, it’s not that easy to drag wires behind the walls and this might be a more feasible solution.

Posted in my 2 cents | Comments Off

Configure sendmail to work with Gmail smtp relay

Ok this one was really a thinker. I spent at least 5 hours to get this to work and finally I was able to use Sendmail to relay through my Gmail account.

A little background:

I have a Linux VPS with CentOS installed. The only email MTA is the default Sendmail. Everything else is pretty much the standard CentOS 4 installation. I don’t intend to use this box as a mail server or any other type of email processor. What I was trying to do is to add some basic capability to send out email from the box using my existing email accounts hosted in Gmail. And I didn’t want to install any additional software such as Postfix for this.

That being said, let me continue to take you down the path that I have gone through, without the stumbling blocks.

My approach was basically: problem -> Google for solutions -> trouble shoot -> Google again. So I found a lot of useful content on the web during the process.

1. Check sendmail

Since gmail uses TLS, you will need to make sure your sendmail is compiled with TLS (for encryption) and SASL (for authentication). This is the command to use to check it:

/usr/sbin/sendmail -d0.1 -bv root

In my case, sendmail does have the necessary compilation flags so I was good. If yours doesn’t, you’ll need to re-compile sendmail and update the binary that is used to start the sendmail service, which is not covered here.

2. Upgrading Cyrus SASL

If your SASL installation doesn’t have the “plain” and “login” lib you will have authentication problem with Gmail. You can see why when you get to the sendmail configuration in the later steps. The common error in the /var/log/maillog is this:

AUTH=client, available mechanisms do not fulfill requirements

It was a vague error and I was so frustrated with it at one point I was ready to give up. However, this article about setting up Postfix with Gmail casted some light and helped me figured out the cause.

The problem is that SASL doesn’t have all the necessary plugins. The “login” and “plain” are the plugins necessary to talk to Gmail smtp. So I had to upgrade SASL to fix the problem. Here is what I did:

$ wget
$ tar -xzf cyrus-sasl-2.1.21.tar.gz
$ cd cyrus-sasl-2.1.21
$ ./configure
$ make
$ make install

$ mv /usr/lib/sasl2 /usr/lib/sasl2.orig
$ ln -s /usr/local/lib/sasl2 /usr/lib/sasl2

Note: if you have issue installing Cyrus SASL around compiling digestmd5.c, it’s because your compiler is too new. Read here to find out how to patch it.

Since I just switched out the old sasl2 lib without recompiling sendmail, I was concerned sendmail would poop during runtime. Luckily that didn’t happen. Dynamic lib rocks!

3. Generate SSL certificate

I made a directory called certs under /etc/mail. Here are the commands that I used to generate the SSL certificates.

openssl req -new -x509 -keyout cakey.pem -out cacert.pem -days 3650
openssl req -nodes -new -x509 -keyout sendmail.pem -out sendmail.pem -days 3650

Notice I made the certificates good for almost 10 years. I didn’t needed the cacert.pem to be exact.

I also copied /usr/share/ssl/ca-bundle.crt to /ect/mail/certs and included it in the sendmail configuration file. Other wise you’ll see some error like this:

unable to get local issuer certificate

The reason is that the ca bundle file has the Gmail certificate issuer. Although I read it somewhere that email still goes out with this error. Nonetheless, we don’t need to see this if we can fix it.

4. Configure sendmail

With the preparations above we are ready to configure sendmail. I found this tutorial very useful in terms of getting the correct sendmail configurations.

In summary, I have the /etc/mail/auth/client-info looks like this: “U:root” “” “P:password” “M:PLAIN” “U:root” “” “P:password” “M:PLAIN”

If you use Gmail hosted email with your own domain name, you will have username@hostname.tld in there.

Make sure run:

$ makemap -r hash client-info.db < client-info

and chmod 600 on client info files.

Essential lines in my

FEATURE(`authinfo’,`hash /etc/mail/auth/client-info.db’)dnl
define(`RELAY_MAILER_ARGS’, `TCP $h 587′)
define(`ESMTP_MAILER_ARGS’, `TCP $h 587′)

define(`CERT_DIR’, `/etc/mail/certs’)
define(`confCACERT_PATH’, `CERT_DIR’)
define(`confCACERT’, `CERT_DIR/ca-bundle.crt’)
define(`confCRL’, `CERT_DIR/ca-bundle.crt’)
define(`confSERVER_CERT’, `CERT_DIR/sendmail.pem’)
define(`confSERVER_KEY’, `CERT_DIR/sendmail.pem’)
define(`confCLIENT_CERT’, `CERT_DIR/sendmail.pem’)
define(`confCLIENT_KEY’, `CERT_DIR/sendmail.pem’)


NOTE: Be aware that smart-quotes used in the code examples will not be recognised if pasted into your files! Ensure replacing smart-quotes by regular quotes (see comments below for further detail). – thanks Johnny for the suggestion.

The certificate files are generated/copied from the previous step. I’m no sendmail expert so the configuration lines may not be perfect. But it works. Let me know if you have better settings.

One tip I found very useful is to use the debugging feature. You can set a high log level in the to see at which step sendmail choked and for what reason.

Also, make sure run “make” or m4 every time you touch the

m4 >

So that’s pretty much it. I restart the sendmail service and out goes my email.


Recently I installed Sendmail on a brand new VPS and had hard time get the authentication working. It turned out saslauthd was not running. So a note for new system is to make sure saslauthd has to be up and running (better use chkconfig to make sure it starts up at run level 3) in order to get Sendmail authentication working. This may help resolve some issues in the comments.

Posted in server setup | 56 Comments

A follow up on using Amazon A3

Last week Amazon A3 was down for 4 hours and made a lot of webmasters unhappy. It further proves that it is quite risky to design your site solely relying on A3 to provide the essential functionalities, at least for now.

One way to reduce the risk is to have a copy of the files, for example images, saved in your server and design a flag in your code to pull the file from your own server if A3 downtime was detected. The flag can be controlled by some parameter in a configuration file so it can be easily switched.

One might argu this defeats the purpose of using AWS storage since the load copies take up the space. But I believe the storage cost will worth it in the event of the A3 hiccups. By using A3 service when it’s up and running, you will still save the bandwidth when serving those files, which is a lot more expensive than the storage cost.

Posted in my 2 cents | 2 Comments

Document your site with a wiki

Good documentation can save a lot of time to look for the information that you have once obtained. I had been looking for some documentation system that can help me to write down notes and finally decided to use a wiki.

Using a wiki can provide some great benefits:

  • It is web based can I can access anywhere.
  • My documents can be organized by sites and category so I have a central place to look for things that I have done but not necessarily memorized.
  • Easy collaboration with others.

There are several free wiki softwares and I chose DocuWiki after some research and test run. Since I only need a simple interface to create structured content DocuWiki was a good choice for me. This is a nice comparison on some popular wiki software that I also used to help me make my decision.

Posted in my 2 cents | Comments Off

A PHP boolean variable note

I am by no means a PHP know-all so this might have been documented.

If I have a boolean variable, say

boolean x = true;

and if I use it in a if statement like:

if ( x == ‘abc’)

The result will always return “true”.

I believe the PHP version I am using is 5.2.

Posted in PHP development | Comments Off

Is Yahoo making the wrong move?

There are a lot of talks around Yahoo’s move recently. Y’s decisions to reject the Microsoft deal and partner with Google on search advertising market don’t sit well with a lot of people. Some think Yahoo is making stupid decisions.

I beg to differ.

Being bought by Microsoft will make a lot of shareholders happy today. But if bought by Microsoft, 3 years from now, will Yahoo still be relevant at all? Choosing to remain independent will give Yahoo a chance. But what about the shareholders? Well shareholders don’t RUN the company. They can make their influence by choosing the board, or cast their vote by selling the stock. I don’t own YHOO so this might sound insensitive. But company shareholders having disagreement with management team is not new and opinions from shareholders who want to sell the stock and cash in should not be used to judge the company’s direction.

Will Yahoo+Google deal hurt Yahoo in the long run? It might hurt Yahoo’s advertising network. But Google is already so dominant in search market and Yahoo may never be able to catch on with its technology today. The key is for Yahoo to innovate and find new ways to establish a stronger brand, not necessary all in the search market though.

Some also have concerns that Yahoo is helping to create a monopolistic Google which will turn evil in no time. Google has 60% search market share because so far its search technology is the best. And it is not so dominant in other areas and Yahoo’s deal will not make it be. I think the key is to believe that technology is alway moving forward, today’s big players will be replaced if they stay still, and there will always be newer and better things coming out. Technology is simply not a business that can be established upon monopoly.

Having said that, the challenges ahead of Yahoo are obvious and I wish them the best of the luck.

Posted in my 2 cents | Comments Off

Use JQuery to adjust the iframe height

Although frame is generally not recommended on most of the web pages, iFrame can still be useful in some occasions, especially as an Ajax alternative. One strength of Ajax is that it greatly reduces (or at least it appears so) the page response time by only changing a small part of the page. Using Ajax to submit a form is a great example. This usability improvement doesn’t come free though. In order to use Ajax to send the form data back to the server, the JavaScript code has to be written to collect the form data and append them to the request URL as GET or POST parameters. Frameworks like JQuery or Prototype make this process a lot easier.

However there is some limitation to use Ajax to post a multi-part form. Although it might be possible, it is definitely not a clean implementation, and it is not supported by most of the browsers. In this case, iFrame may be a good alternative. The inner frame page will handle the multi-part form and the parent page will have the similar Ajax effect. That being said, iFrame’s biggest problem is that its width and height are set right from the start and it won’t adjust based on the source content. This will leave the ugly scrolling bars around the iFrame, or some content will be hidden if the scroll bar is disabled. For most of the pages, the width is somewhat less of a problem but the height is harder to control and set correctly.

The good news is there are ways to dynamically adjust the height of the iFrame based on the inner content. There are different ways to achieve this using JavaScript. I found this approach is the best:

Using jQuery, we can add the following code in the iframe source content:

<script type=”text/javascript”>
$(document).ready(function() {
var theFrame = $(“#iFrameToAdjust”, parent.document.body);
theFrame.height($(document.body).height() + 30);

The JavaScript will get the iframe object from the parent DOM and change its height according to the size of the current document after the document is loaded (very important to get the true size). I like it because it is less intrusive to the page where the iframe is on, and the iframe source kind of “take care of its own size” when being displayed. If the source page is displayed as regular page, there simply will not be any adjustment. With the help of jQuery the code is quite clean and simple. Of course the iframe source has to be an internal page and the developer has the permission to add the code.

From my testing result, this works in FF2, IE7 and Opera 9.25, not in Safari 3.1 for Windows though.

Some updates:

I found it work better to put the JavaScript which adjusts the frame height in the body onLoad attribute. Basically “ready” function will be kicked off when the DOM is loaded, at which point the page may or may not be completely loaded. The onLoad event will be a better bet in this case since we need the actual size of the page including all the images.

Posted in javascript | 23 Comments

Amazon AWS web service

Amazon’s AWS webservice has been around for a while now and recently I implemented one of my web site to use the Simple Storage Service (A3). The idea is to utilize the storage space in Amazon’s computing cloud to ease the actual loads on my own web server. In this case, I store the user uploaded images into Amazon A3 storage as backup; and when the images are loaded on a page, they are pulled directly from Amazon A3. By using A3 the web site uses the distributed computing/storage resources and save the bandwidth.

Some of the good reasons to use A3:

  • The service is cheap. We are talking about GBs on the pennies here.
  • The service is quite reliable. I know earlier this year when A3 went down and a bunch of sites that were built using A3 as part of core infrastructure went down with it. But overall the down time is very rare. And A3 load time has also been pretty fast based on my experience.
  • Easy development. The A3 development community is a great place to find resources. For example, this standalone S3 php class pretty much has everything you need to start using the A3 storage.

Although we have a lot of good reasons to use AWS, I still wouldn’t embed too much of it into the infrastructure. The A3 storage in my case is used as a backup source and the actual data can be retrieved from local server by simply flipping a property value in the configuration file. The data update is also initialized by the scheduled cron jobs instead of building into the code. The benefit of this is that A3 is never something that my site has to reply on to stay up. And A3′s performance will not heavily influence the web site’s performance.

I have to say AWS is a brilliant idea and the fact that it came out from an online retail site makes it even more interesting. Here is a brief interview on Jeff Bezos who talked about Amazon’s cloud computing service during the D6 conference in May 2008.

Posted in programming in general | Comments Off

FCKEditor vs. TinyMCE

I am preparing to add WYSIWYG editor to one of my project and looking for some library code to use. The selections narrowed down to FCKEditor and TinyMCE. Both of them look great and perform nicely in the demo. Now comes to the question which one to pick. This article has a nice comparison between the two.

Just found out that default editor in WordPress is also based on TinyMCE, which is quite an endorsement. I particularly like the capability to resize the editor’s size in TinyMCE.

Posted in usability design | Comments Off