Synchronise cPanel DNS Cluster from Command Line

If you make use of cPanel’s DNS cluster or cPanel DNS Only then you might find this of use.

Typical example where you would use this is scripting, and you want to synchronise your DNS zone files with other servers in the cluster. For example, you may want to automate the deployment of new servers in the DNS cluster. In your script, you want to have  the zone files synchronised.

A second scenario – you have a domain where records are added and removed automatically by a front end system. The system makes modifications to the zone file directly and synchronised changes in a cron every 5 minutes.

The script and path is as follows. Usage is included for your convenience.

$ /scripts/dnscluster
Usage: dnscluster <action> <zone> --help
Actions:
 syncall - make sure all dns zones are
 in sync within the cluster. If the zones
 are out out of sync, the one with the largest
 serial number will be copied to all servers.

 syncalllocal - make sure all dns zones are
 in sync within the cluster. If the zones
 are out out of sync, the one with the largest
 serial number will be copied to all servers.
 Only the local server's zone database will be updated.

 synczone <zone> - sync one zone
 If the zone is out out of sync, the one with the largest
 serial number will be copied to all servers.

 synczonelocal <zone> - sync one zone
 If the zone is out out of sync, the one with the largest
 serial number will be copied to the local server.

'--help' or no args at all will give usage

In the first example I mentioned (sync all zones), the following command would be used:

$ /scripts/dnscluster syncall

For the second example (just a single DNS zone to remote server) you would use:

$ /scripts/dnscluster synczone sysadminspot.com

Note that cPanel DNS synchronisation works on the zone serial. If you’re making direct modifications to the DNS zone file then you must update the serial number to a larger number than the previous. Typically the ten (10) digit serials are the format of YYYYMMDDNN. That is:

  • YYYY = Year in full (2009)
  • MM = Month in full (09)
  • DD = Day in full (26)
  • NN = Version number for that day. Start at 01 and increment. When a new date starts, return to 01.

So if your DNS is not syncing correctly, it’s likely because you are not updating your serials correctly OR your DNS clustering (sync directions, etc) are not setup correctly in cPanel. Login to WHM and setup under Cluster Setup.

For more information on cPanel DNS Only and DNS clustering, check out Understanding cPanel clustered DNS and Setup.

Leave a comment

Understanding cPanel clustered DNS and Setup

What is DNS and why do we care?

DNS is the service that resolves domain names like sysadminspot.com to IP addresses. So instead of memorising all the possible IP addresses in the world and who they belong to, we use domain names (example.com). DNS resolution takes place on a DNS server. The server takes the domain name given to it and looks to see if it has an IP address for it. If it doesn’t, it may forward to it another DNS server. So if your memory is like mine, then you care about DNS.

Why cluster DNS?

Well firstly it sounds really cool, however increased performance is also another key factor.

Explain how said performance increase would work…

As mentioned before, DNS resolves domain names (sysadminspot.com) to an IP address. This is one step in a bucket full of steps when it comes to loading a website. But if just one fails or is slow – websites don’t load!

Here’s an example:

You have three servers running in the United States. However your customers and their visitors are geographically scattered.  Customers in the UK and Australia often complain that their websites load slowly but they load really quickly for you in the United States.

This is because of network latency. It generally takes longer to talk to a network that is cross continental than one in the same country as you. Same applies if you wanted to go shopping and went to Europe instead of down the road – it’s gonna take longer!

How about this though:

  1. You deploy several virtual servers (low cost) in key geographic locations around the world (e.g. Sydney and London).
  2. You install cPanel DNS Only
  3. All your servers are configured to synchronise DNS with the new DNS servers.

So what has changed? Well now your customers in Europe are talking with the DNS server in London instead of the United States. One way communication drops from 300ms to 150ms and hence, resolution takes place faster. Yes, it is certainly the case that now that customer now has to go back to talking with the US server to grab the files. But you’ve collectively saved a lot time for all your customers.

You can also distribute some of your server load to other servers. Let your web and database servers focus on serving dynamic page content. Outsource your DNS to distributed, cheaper and less busy servers. You’ll save bandwidth, reduce congestion and lower disk I/O.

Redundancy… sounds cool right?

After a scheduled reboot your DNS service doesn’t start. Under standalone DNS circumstances, domain names cannot be resolved to IP addresses and therefore websites are down.

However with the clustered DNS setup customers just attempt to connect to another server, even if it’s in another country. This isn’t something you need to setup either, it’s build into the standards and protocols of DNS.

Okay, I want it, give it to me! How do I set it up?

Yes that’s what I thought. Check out the cPanel documentation below which includes all the sexy looking screenshots you could want.

Installation Guide – Install cPanel DNSONLY
cPanel DNSONLY Summary

Let me know if the link stops working. cPanel are constantly breaking their documentation links with redesigns.

EDIT 27 Dec 2015: Thanks Felix for the heads up, links fixed. 🙂

2 Comments

Can’t connect to local MySQL server through socket ‘/var/lib/mysql/mysql.sock’ (13)

Background

MySQLIt wasn’t working! But in more detail:

  • An R1Soft restore had taken place, restoring from the backup server to the live server
  • The files were MySQL data files originally from /var/lib/mysql/
  • The files were restored to /root/temp/db_wordpress/ and checked
  • Files were relocated from /root/temp/db_wordpress/ to /var/lib/mysql/db_wordpress2/
  • Permissions were adjusted on /var/lib/mysql/db_wordpress2/ and /var/lib/mysql
  • MySQL was restarted to load the new database
  • A repair on the new database was completed
  • I’d been asleep for a total of 3 hours then woken to deal with an issue, and it was now 5am.

What happened next came to us by surprise. Every single website using MySQL (all WordPress sites),  went from working perfectly to “Error establishing a database connection”. There were no further errors.

  1. MySQL was restarted. It reported start and stop successfully. There was however no change in the websites.
  2. We checked the MySQL error logs. To my absolute shock there was nothing more logged beyond a successful stop/start of MySQL.
  3. MySQL was stopped again and the new database data files were removed from the /var/lib/mysql/ directory, then MySQL was started again. No change.

We’d removed what we thought to be damaged data files, the only thing foreign we introduced to the environment. What was more annoying was that WordPress was giving nothing more than “Error establishing a database connection”.

The Plot

Here’s where the mental head spin occurred. Stay tuned:

$ mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2 to server version: 4.1.22
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
mysql> show processlist;
+----+------+-----------+------+---------+------+-------+------------------+
| Id | User | Host      | db   | Command | Time | State | Info             |
+----+------+-----------+------+---------+------+-------+------------------+
|  2 | root | localhost | NULL | Query   |    0 | NULL  | show processlist |
+----+------+-----------+------+---------+------+-------+------------------+
1 row in set (0.00 sec)

MySQL connects from the command line perfectly and runs a command.

mysql> use db_wordpress2;
Database changed
mysql> show tables;
+-------------------------+
| Tables_in_db_wordpress2 |
+-------------------------+
| wp_comments
| wp_links
| wp_options
| wp_postmeta
| wp_posts
| wp_term_relationships
| wp_term_taxonomy
| wp_terms
| wp_usermeta
| wp_users
+-----------------------+
10 rows in set (0.00 sec)

MySQL just accessed a database and listed the available tables – all present and correct.

mysql> select * from wp_users;
+----+------------+------------------------------------+---------------+--------------------------+--------------------------+---------------------+---------------------+-------------+--------------+
| ID | user_login | user_pass                          | user_nicename | user_email               | user_url                 | user_registered     | user_activation_key | user_status | display_name |
+----+------------+------------------------------------+---------------+--------------------------+--------------------------+---------------------+---------------------+-------------+--------------+
...
+----+------------+------------------------------------+---------------+--------------------------+--------------------------+---------------------+---------------------+-------------+--------------+
10 row in set (0.20 sec)

MySQL just accessed a database and listed data from a table, but all the WordPress websites were still down with “Error establishing a database connection”. Head-spin!

Absolutely furious and in rage from the long day, lack of sleep, the sole and meaningless WordPress errors, and the zero errors MySQL gave. To top it, I couldn’t see the screen properly with my blotchy tired eyes and I couldn’t type without missing keys and putting spaces in the wrong place. I knew I had to escape the WordPress environment. But it felt like I was looking up at a large mountain.

On a side note, I recommend anyone and everyone who wants to learn the basics of a programming language to W3Schools. I simply don’t have time to teach everyone, so it’s a great resource. I knew they had a great PHP snippet for connecting to a MySQL database.

<?php
$con = mysql_connect("localhost","peter","abc123");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

// some code
echo "done";
?>

Source: http://www.w3schools.com/PHP/php_mysql_connect.asp

I replaced the variables with the login details from the wp-config.php file and ran it in the browser. I could have cried with joy at this exhausted point.

Warning: mysql_connect() [function.mysql-connect]: Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (13) in /var/www/db.php on line 2              Could not connect: Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (13)

Typically when you see this type of error it means MySQL isn’t running, is hanging or in the process of shutting down. But it’s been clearly established that is not the case.

It took me a little while to realise the little bit of information I was overlooking. In fact I admit, it took me way too long, but I put that down to the lack of sleep. Oh that magic little number was staring right at me.

...sql/mysql.sock' (13)

What is the significance of the number 13? There are three important factors:

  1. It’s one more than 12
  2. It’s one less than 14
  3. And its an operating system error code relating to something specific.

It was as simple as looking it up to point me in the right direction:

$ perror 13
OS error code  13:  Permission denied

Because it’s an operating system error code, it rules out any permissions issues within MySQL. While I’d removed the database I thought was causing the issues, I didn’t revert the permissions changes I had made, more specifically and stupidly to the /var/lib/mysql directory.

The correct permissions were 755 and look as follows:

$ ls -alh /var/lib | grep mysql
drwxr-xr-x  8 mysql mysql   4.0K Sep  3 18:02 mysql

Conclusion

So what is the lesson learned here? There are two very key points:

  • Pay more attention to the error codes you are give. The messages are standard, but the number give it more specific meaning.
  • Never opt to do third shift on a weekend where you’ve done the first and second shifts as well.

All the servers lived happily ever after.

5 Comments