{"id":2701,"date":"2018-09-08T13:39:53","date_gmt":"2018-09-08T13:39:53","guid":{"rendered":"http:\/\/dbtut.com\/?p=2701"},"modified":"2018-12-06T08:47:01","modified_gmt":"2018-12-06T08:47:01","slug":"script-for-mysql-replication-monitoring","status":"publish","type":"post","link":"https:\/\/dbtut.com\/index.php\/2018\/09\/08\/script-for-mysql-replication-monitoring\/","title":{"rendered":"Script for MySQL Replication Monitoring"},"content":{"rendered":"<p>&nbsp;<\/p>\n<p>As a Database Administrator, Monitoring of database is the most important part. As all we know Prevention is better than cure,<\/p>\n<p>So. I am in this article comes with Replication Monitoring.<\/p>\n<p>In replication monitoring, mainly we need to look into three things &#8220;sql_thread_running&#8221;, &#8220;io_thread_running&#8221; and the lag between master and slave.<\/p>\n<p>The below script will help you in replication monitoring.<\/p>\n<pre class=\"\">#!\/bin\/bash\r\n#change this in order to indicate which slave you are monitoring now\r\nserver_name=\"server1@example.com\"\r\n#email of the database administrator to recieve notification\r\nadmin_email='dba@example.com'\r\nadmin_email1='manager@example.com'\r\n# Add mysql credentials in the following commands.\r\n\r\nsql_thread_running=$(mysql -uuser -ppassword -e \"show slave status\\G\" | awk -F\":\" '\/Slave_SQL_Running\/ { print $2 }' | tr -d ' ')\r\nio_thread_running=$(mysql -uuser -ppassword -e \"show slave status\\G\" | awk -F\":\" '\/Slave_IO_Running\/ { print $2 }' | tr -d ' ')\r\nseconds_late=$(mysql -uuser -ppassword -e \"show slave status\\G\" | awk -F\":\" '\/Seconds_Behind_Master\/ { print $2 }' | tr -d ' ')\r\nseconds_late=$(($seconds_late+0))\r\n\r\nif [ \"$sql_thread_running\" = \"No\" ] || [ \"$io_thread_running\" = \"No\" ] || [ $seconds_late -gt 3600 ]; then\r\n\r\nlog_file=\"\/tmp\/log_slave_status_$(date +%m-%d-%Y-%H:%M)\"\r\necho \"Replication is stopped between master and (Slave) on $(date +%m-%d-%Y-%H:%M)\" &gt;&gt; $log_file\r\nif [ \"$sql_thread_running\" = \"No\" ]; then\r\necho \"-SQL Thread not running\" &gt;&gt; $log_file\r\nfi\r\n\r\nif [ \"$io_thread_running\" = \"No\" ]; then\r\necho \"-IO thread not running\" &gt;&gt; $log_file\r\nfi\r\n\r\nif [ $seconds_late -gt 3600 ]; then #formattting how the latency of the slave behind master should be displayed\r\ndisplay_late=\"$seconds_late seconds\"\r\nif [ $seconds_late -gt 60 ]; then\r\ndisplay_late=\"$display_late = $(($seconds_late\/60)) minutes\"\r\nfi\r\n\r\nif [ $seconds_late -gt 3600 ]; then\r\ndisplay_late=\"$display_late = $(($seconds_late\/3600)) hours\"\r\nfi\r\n\r\necho \"slave is behind master by $display_late\" &gt;&gt; $log_file\r\nfi\r\n\r\n#echo send alerts to the admin\r\nmail -s \"[Replication stop ALERT] Replication is stopped between  master and Slave on $(date +%m-%d-%Y-%H:%M)\" $admin_email $admin_email1 &lt; $log_file\r\necho \"Slave not running, alerts sent to the admins...\"\r\nelse\r\necho \"slave is running normally, no problem detected :)\"\r\nfi\r\n\r\n<\/pre>\n<p>Thank You.<\/p>\n\n<div class=\"pvc_clear\"><\/div>\n<p id=\"pvc_stats_2701\" class=\"pvc_stats all  \" data-element-id=\"2701\" style=\"\"><i class=\"pvc-stats-icon medium\" aria-hidden=\"true\"><svg aria-hidden=\"true\" focusable=\"false\" data-prefix=\"far\" data-icon=\"chart-bar\" role=\"img\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" viewBox=\"0 0 512 512\" class=\"svg-inline--fa fa-chart-bar fa-w-16 fa-2x\"><path fill=\"currentColor\" d=\"M396.8 352h22.4c6.4 0 12.8-6.4 12.8-12.8V108.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v230.4c0 6.4 6.4 12.8 12.8 12.8zm-192 0h22.4c6.4 0 12.8-6.4 12.8-12.8V140.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v198.4c0 6.4 6.4 12.8 12.8 12.8zm96 0h22.4c6.4 0 12.8-6.4 12.8-12.8V204.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v134.4c0 6.4 6.4 12.8 12.8 12.8zM496 400H48V80c0-8.84-7.16-16-16-16H16C7.16 64 0 71.16 0 80v336c0 17.67 14.33 32 32 32h464c8.84 0 16-7.16 16-16v-16c0-8.84-7.16-16-16-16zm-387.2-48h22.4c6.4 0 12.8-6.4 12.8-12.8v-70.4c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v70.4c0 6.4 6.4 12.8 12.8 12.8z\" class=\"\"><\/path><\/svg><\/i> <img loading=\"lazy\" decoding=\"async\" width=\"16\" height=\"16\" alt=\"Loading\" src=\"https:\/\/dbtut.com\/wp-content\/plugins\/page-views-count\/ajax-loader-2x.gif\" border=0 \/><\/p>\n<div class=\"pvc_clear\"><\/div>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp; As a Database Administrator, Monitoring of database is the most important part. As all we know Prevention is better than cure, So. I am in this article comes with Replication Monitoring. In replication monitoring, mainly we need to look into three things &#8220;sql_thread_running&#8221;, &#8220;io_thread_running&#8221; and the lag between master and slave. The below script &hellip;<\/p>\n<div class=\"pvc_clear\"><\/div>\n<p id=\"pvc_stats_2701\" class=\"pvc_stats all  \" data-element-id=\"2701\" style=\"\"><i class=\"pvc-stats-icon medium\" aria-hidden=\"true\"><svg aria-hidden=\"true\" focusable=\"false\" data-prefix=\"far\" data-icon=\"chart-bar\" role=\"img\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" viewBox=\"0 0 512 512\" class=\"svg-inline--fa fa-chart-bar fa-w-16 fa-2x\"><path fill=\"currentColor\" d=\"M396.8 352h22.4c6.4 0 12.8-6.4 12.8-12.8V108.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v230.4c0 6.4 6.4 12.8 12.8 12.8zm-192 0h22.4c6.4 0 12.8-6.4 12.8-12.8V140.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v198.4c0 6.4 6.4 12.8 12.8 12.8zm96 0h22.4c6.4 0 12.8-6.4 12.8-12.8V204.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v134.4c0 6.4 6.4 12.8 12.8 12.8zM496 400H48V80c0-8.84-7.16-16-16-16H16C7.16 64 0 71.16 0 80v336c0 17.67 14.33 32 32 32h464c8.84 0 16-7.16 16-16v-16c0-8.84-7.16-16-16-16zm-387.2-48h22.4c6.4 0 12.8-6.4 12.8-12.8v-70.4c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v70.4c0 6.4 6.4 12.8 12.8 12.8z\" class=\"\"><\/path><\/svg><\/i> <img loading=\"lazy\" decoding=\"async\" width=\"16\" height=\"16\" alt=\"Loading\" src=\"https:\/\/dbtut.com\/wp-content\/plugins\/page-views-count\/ajax-loader-2x.gif\" border=0 \/><\/p>\n<div class=\"pvc_clear\"><\/div>\n","protected":false},"author":186,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_uf_show_specific_survey":0,"_uf_disable_surveys":false,"footnotes":""},"categories":[7],"tags":[1481,1415],"class_list":["post-2701","post","type-post","status-publish","format-standard","","category-mysql","tag-monitoring","tag-mysql-replication"],"aioseo_notices":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.9 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Script for MySQL Replication Monitoring - Database Tutorials<\/title>\n<meta name=\"description\" content=\"Script for MySQL Replication Monitoring\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/dbtut.com\/index.php\/2018\/09\/08\/script-for-mysql-replication-monitoring\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Script for MySQL Replication Monitoring - Database Tutorials\" \/>\n<meta property=\"og:description\" content=\"Script for MySQL Replication Monitoring\" \/>\n<meta property=\"og:url\" content=\"https:\/\/dbtut.com\/index.php\/2018\/09\/08\/script-for-mysql-replication-monitoring\/\" \/>\n<meta property=\"og:site_name\" content=\"Database Tutorials\" \/>\n<meta property=\"article:published_time\" content=\"2018-09-08T13:39:53+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2018-12-06T08:47:01+00:00\" \/>\n<meta name=\"author\" content=\"Rahul Shekhawat\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Rahul Shekhawat\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/dbtut.com\/index.php\/2018\/09\/08\/script-for-mysql-replication-monitoring\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/dbtut.com\/index.php\/2018\/09\/08\/script-for-mysql-replication-monitoring\/\"},\"author\":{\"name\":\"Rahul Shekhawat\",\"@id\":\"https:\/\/dbtut.com\/#\/schema\/person\/91f82db029899f02a5ef2e689f4b818f\"},\"headline\":\"Script for MySQL Replication Monitoring\",\"datePublished\":\"2018-09-08T13:39:53+00:00\",\"dateModified\":\"2018-12-06T08:47:01+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/dbtut.com\/index.php\/2018\/09\/08\/script-for-mysql-replication-monitoring\/\"},\"wordCount\":75,\"commentCount\":2,\"publisher\":{\"@id\":\"https:\/\/dbtut.com\/#organization\"},\"keywords\":[\"Monitoring\",\"MySQL Replication\"],\"articleSection\":[\"MySQL-MariaDB\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/dbtut.com\/index.php\/2018\/09\/08\/script-for-mysql-replication-monitoring\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/dbtut.com\/index.php\/2018\/09\/08\/script-for-mysql-replication-monitoring\/\",\"url\":\"https:\/\/dbtut.com\/index.php\/2018\/09\/08\/script-for-mysql-replication-monitoring\/\",\"name\":\"Script for MySQL Replication Monitoring - Database Tutorials\",\"isPartOf\":{\"@id\":\"https:\/\/dbtut.com\/#website\"},\"datePublished\":\"2018-09-08T13:39:53+00:00\",\"dateModified\":\"2018-12-06T08:47:01+00:00\",\"description\":\"Script for MySQL Replication Monitoring\",\"breadcrumb\":{\"@id\":\"https:\/\/dbtut.com\/index.php\/2018\/09\/08\/script-for-mysql-replication-monitoring\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/dbtut.com\/index.php\/2018\/09\/08\/script-for-mysql-replication-monitoring\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/dbtut.com\/index.php\/2018\/09\/08\/script-for-mysql-replication-monitoring\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/dbtut.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Script for MySQL Replication Monitoring\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/dbtut.com\/#website\",\"url\":\"https:\/\/dbtut.com\/\",\"name\":\"Database Tutorials\",\"description\":\"MSSQL, Oracle, PostgreSQL, MySQL, MariaDB, DB2, Sybase, Teradata, Big Data, NOSQL, MongoDB, Couchbase, Cassandra, Windows, Linux\",\"publisher\":{\"@id\":\"https:\/\/dbtut.com\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/dbtut.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/dbtut.com\/#organization\",\"name\":\"dbtut\",\"url\":\"https:\/\/dbtut.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/dbtut.com\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/dbtut.com\/wp-content\/uploads\/2021\/02\/dbtutlogo.jpg\",\"contentUrl\":\"https:\/\/dbtut.com\/wp-content\/uploads\/2021\/02\/dbtutlogo.jpg\",\"width\":223,\"height\":36,\"caption\":\"dbtut\"},\"image\":{\"@id\":\"https:\/\/dbtut.com\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/dbtut.com\/#\/schema\/person\/91f82db029899f02a5ef2e689f4b818f\",\"name\":\"Rahul Shekhawat\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/dbtut.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/29fd43cfafd9d52ad4cd0745a900cd1e53c5f993bc2c1b1a7aeb3e1b1b86e8d2?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/29fd43cfafd9d52ad4cd0745a900cd1e53c5f993bc2c1b1a7aeb3e1b1b86e8d2?s=96&d=mm&r=g\",\"caption\":\"Rahul Shekhawat\"},\"url\":\"https:\/\/dbtut.com\/index.php\/author\/rahulshekhawat\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Script for MySQL Replication Monitoring - Database Tutorials","description":"Script for MySQL Replication Monitoring","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/dbtut.com\/index.php\/2018\/09\/08\/script-for-mysql-replication-monitoring\/","og_locale":"en_US","og_type":"article","og_title":"Script for MySQL Replication Monitoring - Database Tutorials","og_description":"Script for MySQL Replication Monitoring","og_url":"https:\/\/dbtut.com\/index.php\/2018\/09\/08\/script-for-mysql-replication-monitoring\/","og_site_name":"Database Tutorials","article_published_time":"2018-09-08T13:39:53+00:00","article_modified_time":"2018-12-06T08:47:01+00:00","author":"Rahul Shekhawat","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Rahul Shekhawat","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/dbtut.com\/index.php\/2018\/09\/08\/script-for-mysql-replication-monitoring\/#article","isPartOf":{"@id":"https:\/\/dbtut.com\/index.php\/2018\/09\/08\/script-for-mysql-replication-monitoring\/"},"author":{"name":"Rahul Shekhawat","@id":"https:\/\/dbtut.com\/#\/schema\/person\/91f82db029899f02a5ef2e689f4b818f"},"headline":"Script for MySQL Replication Monitoring","datePublished":"2018-09-08T13:39:53+00:00","dateModified":"2018-12-06T08:47:01+00:00","mainEntityOfPage":{"@id":"https:\/\/dbtut.com\/index.php\/2018\/09\/08\/script-for-mysql-replication-monitoring\/"},"wordCount":75,"commentCount":2,"publisher":{"@id":"https:\/\/dbtut.com\/#organization"},"keywords":["Monitoring","MySQL Replication"],"articleSection":["MySQL-MariaDB"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/dbtut.com\/index.php\/2018\/09\/08\/script-for-mysql-replication-monitoring\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/dbtut.com\/index.php\/2018\/09\/08\/script-for-mysql-replication-monitoring\/","url":"https:\/\/dbtut.com\/index.php\/2018\/09\/08\/script-for-mysql-replication-monitoring\/","name":"Script for MySQL Replication Monitoring - Database Tutorials","isPartOf":{"@id":"https:\/\/dbtut.com\/#website"},"datePublished":"2018-09-08T13:39:53+00:00","dateModified":"2018-12-06T08:47:01+00:00","description":"Script for MySQL Replication Monitoring","breadcrumb":{"@id":"https:\/\/dbtut.com\/index.php\/2018\/09\/08\/script-for-mysql-replication-monitoring\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/dbtut.com\/index.php\/2018\/09\/08\/script-for-mysql-replication-monitoring\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/dbtut.com\/index.php\/2018\/09\/08\/script-for-mysql-replication-monitoring\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/dbtut.com\/"},{"@type":"ListItem","position":2,"name":"Script for MySQL Replication Monitoring"}]},{"@type":"WebSite","@id":"https:\/\/dbtut.com\/#website","url":"https:\/\/dbtut.com\/","name":"Database Tutorials","description":"MSSQL, Oracle, PostgreSQL, MySQL, MariaDB, DB2, Sybase, Teradata, Big Data, NOSQL, MongoDB, Couchbase, Cassandra, Windows, Linux","publisher":{"@id":"https:\/\/dbtut.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/dbtut.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/dbtut.com\/#organization","name":"dbtut","url":"https:\/\/dbtut.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/dbtut.com\/#\/schema\/logo\/image\/","url":"https:\/\/dbtut.com\/wp-content\/uploads\/2021\/02\/dbtutlogo.jpg","contentUrl":"https:\/\/dbtut.com\/wp-content\/uploads\/2021\/02\/dbtutlogo.jpg","width":223,"height":36,"caption":"dbtut"},"image":{"@id":"https:\/\/dbtut.com\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/dbtut.com\/#\/schema\/person\/91f82db029899f02a5ef2e689f4b818f","name":"Rahul Shekhawat","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/dbtut.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/29fd43cfafd9d52ad4cd0745a900cd1e53c5f993bc2c1b1a7aeb3e1b1b86e8d2?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/29fd43cfafd9d52ad4cd0745a900cd1e53c5f993bc2c1b1a7aeb3e1b1b86e8d2?s=96&d=mm&r=g","caption":"Rahul Shekhawat"},"url":"https:\/\/dbtut.com\/index.php\/author\/rahulshekhawat\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/dbtut.com\/index.php\/wp-json\/wp\/v2\/posts\/2701","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dbtut.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dbtut.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dbtut.com\/index.php\/wp-json\/wp\/v2\/users\/186"}],"replies":[{"embeddable":true,"href":"https:\/\/dbtut.com\/index.php\/wp-json\/wp\/v2\/comments?post=2701"}],"version-history":[{"count":0,"href":"https:\/\/dbtut.com\/index.php\/wp-json\/wp\/v2\/posts\/2701\/revisions"}],"wp:attachment":[{"href":"https:\/\/dbtut.com\/index.php\/wp-json\/wp\/v2\/media?parent=2701"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dbtut.com\/index.php\/wp-json\/wp\/v2\/categories?post=2701"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dbtut.com\/index.php\/wp-json\/wp\/v2\/tags?post=2701"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}