{"id":19102,"date":"2025-09-26T09:52:18","date_gmt":"2025-09-26T01:52:18","guid":{"rendered":"https:\/\/blog.ywdevops.cn\/?p=19102"},"modified":"2025-09-26T09:52:25","modified_gmt":"2025-09-26T01:52:25","slug":"golang-6","status":"publish","type":"post","link":"https:\/\/blog.ywdevops.cn\/index.php\/2025\/09\/26\/golang-6\/","title":{"rendered":"Golang+\u591c\u9e70\u76d1\u63a7\u5224\u65admysql\u4e3b\u4ece\u662f\u5426\u65ad\u5f00"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">\u672c\u4f8b\u5b50\u901a\u8fc7golang\u811a\u672c+\u591c\u83ba\u76d1\u63a7v3\u7248\u672c\u6765\u5224\u65admysql\u4e3b\u4ece\uff0c\u539f\u7406\u5c31\u662f\u901a\u8fc7\u83b7\u53d6SlaveIORunning\u548cSlaveSQLRunning\u7684\u503c\uff0c\u6765\u5224\u65ad\u662f\u5426\u4e3ayes\uff0c\u53ea\u8981\u6709\u4e00\u4e2a\u4e0d\u662fyes\uff0c\u90a3\u4e48\u5c31\u8bc1\u660e\u4e3b\u4ece\u5df2\u7ecf\u7aef\u5f00\uff0c\u6700\u540e\u8f93\u51fa\u590d\u5408\u591c\u83ba\u76d1\u63a7\u89c4\u5219\u7684\u683c\u5f0f\uff0c\u5373\u53ef\u88ab\u76d1\u63a7\u8bc6\u522b<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u811a\u672c\u5185\u5bb9\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport (\n\t\"fmt\"\n\t\"gorm.io\/driver\/mysql\"\n\t\"gorm.io\/gorm\"\n\t\"log\"\n\t\"time\"\n)\n\n\/\/ \u6b64\u5904\u7684\u7ed3\u6784\u4f53\u540d\u53ef\u81ea\u5b9a\u4e49\ntype slavestatus struct {\n\tSlaveIORunning      string `gorm:\"column:Slave_IO_Running\"`\n\tSlaveSQLRunning     string `gorm:\"column:Slave_SQL_Running\"`\n\tSecondsBehindMaster int    `gorm:\"column:Seconds_Behind_Master\"`\n}\n\nvar db *gorm.DB\n\nfunc init() {\n\tuser := \"root\"\n\tpassword := \"abcd@2020\"\n\tdbName := \"mysql\"\n\thost := \"192.168.12.172\"\n\tport := \"3306\"\n\tdsn := fmt.Sprintf(\"%s:%s@tcp(%s:%s)\/%s?charset=utf8&amp;parseTime=True&amp;loc=Local\", user, password, host, port, dbName)\n\tvar err error\n\tdb, err = gorm.Open(mysql.Open(dsn), &amp;gorm.Config{})\n\tif err != nil {\n\t\tlog.Fatal(err)\n\t}\n\tsqlDB, err := db.DB()\n\t\/\/SetMaxIdleConns \u8bbe\u7f6e\u7a7a\u95f2\u8fde\u63a5\u6c60\u4e2d\u8fde\u63a5\u7684\u6700\u5927\u6570\u91cf\n\tsqlDB.SetMaxIdleConns(10)\n\t\/\/SetMaxOpenConns \u8bbe\u7f6e\u6253\u5f00\u6570\u636e\u5e93\u8fde\u63a5\u7684\u6700\u5927\u6570\u91cf\n\tsqlDB.SetMaxOpenConns(100)\n\t\/\/SetConnMaxLifetime \u8bbe\u7f6e\u4e86\u8fde\u63a5\u53ef\u590d\u7528\u7684\u6700\u5927\u65f6\u95f4\n\tsqlDB.SetConnMaxLifetime(time.Hour)\n}\nfunc main() {\n\tout := `\n          &#91; \n            {\n               \"endpoint\": \"192.168.12.172\",\n               \"tags\": \"\",\n               \"timestamp\": 0,\n               \"metric\": \"slave_status_12_172\",\n               \"value\": %d\n            }\n          ]\n        `\n\tvar ss slavestatus\n\tdb.Raw(\"show slave status\").Scan(&amp;ss)\n\t\/\/fmt.Println(\"\u4ece\u5e93IO\u7ebf\u7a0b:\", ss.SlaveIORunning)\n\t\/\/fmt.Println(\"\u4ece\u5e93SQL\u7ebf\u7a0b:\", ss.SlaveSQLRunning)\n\t\/\/fmt.Println(\"\u4ece\u5e93\u843d\u540e\u4e3b\u5e93\u65f6\u95f4:\", ss.SecondsBehindMaster)\n\tif ss.SlaveIORunning != \"Yes\" || ss.SlaveSQLRunning != \"Yes\" {\n\t\tfmt.Println(fmt.Sprintf(out, 0))\n\t} else {\n\t\tfmt.Println(fmt.Sprintf(out, 1))\n\t}\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e0a\u9762\u811a\u672c\u6267\u884c\u540e\u7684\u7ed3\u679c\u4e3a\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"651\" height=\"253\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2025\/09\/7-2.png\" alt=\"\" class=\"wp-image-19105\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2025\/09\/7-2.png 651w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2025\/09\/7-2-300x117.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2025\/09\/7-2-600x233.png 600w\" sizes=\"auto, (max-width: 651px) 100vw, 651px\" \/><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>\u672c\u4f8b\u5b50\u901a\u8fc7golang\u811a\u672c+\u591c\u83ba\u76d1\u63a7v3\u7248\u672c\u6765\u5224\u65admysql\u4e3b\u4ece\uff0c\u539f\u7406\u5c31\u662f\u901a\u8fc7\u83b7\u53d6SlaveIORunning [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":19107,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[111,35],"tags":[],"class_list":["post-19102","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-go","category-mysql"],"blocksy_meta":{"styles_descriptor":{"styles":{"desktop":"","tablet":"","mobile":""},"google_fonts":[],"version":8}},"_links":{"self":[{"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/posts\/19102","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/comments?post=19102"}],"version-history":[{"count":2,"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/posts\/19102\/revisions"}],"predecessor-version":[{"id":19106,"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/posts\/19102\/revisions\/19106"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/media\/19107"}],"wp:attachment":[{"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/media?parent=19102"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/categories?post=19102"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/tags?post=19102"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}