Nagios: Quick MySQL replication for your Nagios installation

MySQL is the favorite database utility for opensource software. Nagios and Nagios XI use MySQL to store data for retrieval by end-users. The retrieval process becomes evident when you install NDOutils and create organizational structure diagrams. However, why MySQL replication?

Replication means exactly what is stated in the dictionary. MySQL provides asynchronous replication between a master and one or more slaves. This article will explain a method I use to create slave instances of MySQL in support of Nagios. These instruction are designed to create a slave instance that we can fail-over to in the future if our master instance and or host fails.

Our scenario involves two physical hosts (in my case I have created two virtual hosts using Xen). One host will house the master (node1) and the other the slave (node2). If you already have an instance of MySQL running in support of Nagios, this procedure will enable you to create a slave without interrupting your current installation and established permissions.

NOTE: Read through these instructions to make sure you understand them, because you will want to perform this procedure as quickly as possible. Nagios is going to be trying to writing to the master server while the instance is stopped during our data migration process.

  1. Identify node1.
  2. On node1 log into MySQL: sudo mysql (provide password).
  3. Create a replication user: CREATE USER ‘repl’@'%.your domain.com’ IDENTIFIED BY ‘your password’; . Make sure you set up a separate account for the replication user, because the userid and password will be in plain text in your master.info and my.cnf files.
  4. Grant the repl user rights: GRANT REPLICATION ON ‘*.*’ TO ‘repl’@'%.your domain.com’; The reason why we use the ‘*.*’ is to allow replication of all databases and hosts on the master instance. We use ‘%.your domain.com’ so we can create a slave anywhere on our networks.
  5. Once that has been completed we need to proceed to node2 in order to modify the my.cnf file. If MySQL is running on the potential slave instance, stop MySQL.
  6. Set the server-id = 2 (or the next number in your series of slaves).
  7. Set the master-host = node1 hostname (I use the IP address of the master host).
  8. Set the master-user = repl.
  9. Set the master-password = your password (remember, I mentioned earlier that the userid and password for replication will be in plain text).
  10. Set the log-bin = mysql-bin (you need to set this flag in order to have the ability to fail-over to this MySQL instance).
  11. Now you want to start node2′s instance of MySQL. You will receive some warnings, but the instance should start and try to begin replication.
  12. Log into node2′s instance of MySQL: sudo mysql and run: “SHOW SLAVE STATUS;”  You will see that the MySQL instance is now a slave.
  13. Now, run “STOP SLAVE;” This will stop the slave on node2 in preparation for our next steps. You can quit the MySQL session on node2 and return to a command prompt.
  14. On node1′s r instance of MySQL run: “FLUSH TABLES WITH READ LOCK;” This will prevent the tables from updating so we can obtain a snapshot.
  15. Log into another session on node1 so we can perform a backup of the databases. From the command prompt run: mysqldump –all-databases –master-data > mydbdump.db. The “–master-data” flag will capture all our required log markers.
  16. We will now restore the data from node1 to node2: sudo mysql < mydbdump.db. Remember, we froze node1, so we are restoring up-to-date data to node2.
  17. Once the restore has completed, log into the MySQL instance on node2 and run: “START SLAVE;”
  18. On node1 run: “UNLOCK TABLES;” This will begin the master’s data processing once more.

That’s all there is too it. I use a restore because I want my slave (node2) to spend as little time as possible updating or syncing to node1. This will also save on server load and network bandwidth. To test if replication is working properly perform this test:

  • node1: Log into MySQL and run: “CREATE DATABASE testing;”, then “SHOW DATABASES;” and you should see the new database “testing”.
  • node2: Log into MySQL and run: “SHOW DATABASES;” if replication is working you will now see the “testing” database.

I hope that helps with your MySQL instance on Nagios or Nagios XI. For more information on MySQL replication please visit: MySQL Replication .

Comments and or other solutions? You can always shoot me an email for questions at mikhail@ebusinessjuncture.com.

Bookmark and Share

0 Response to “Nagios: Quick MySQL replication for your Nagios installation”


  • No Comments

Leave a Reply

You must login to post a comment.