検索
ホームページデータベースmysql チュートリアルMYSQL 数据库状态检查脚本(Python版)_MySQL

python

bitsCN.com #原shell版View Code
  1 #!/bin/bash   2   3 # Script Name: mysql_status_check.sh   4 # Description: check mysql servers status   5 # Author: Xinggang Wang - OpsEye.com   6 # Create Date: 2012/3/30   7   8 #获取MySQL所在服务器IP/端口/用户名/密码   9 read -p "Host=" HOST  10 read -p "Port=" PORT  11 read -p "User=" USER  12 read -sp "Password=" PASSWORD  13 echo  14  15 #默认为127.0.0.1/3306/root  16 if [ "${HOST}" = "" ]  17 then  18 HOST='127.0.0.1'  19 fi  20  21 if [ "${PORT}" = "" ]  22 then  23 PORT='3306'  24 fi  25  26 if [ "${USER}" = "" ]  27 then  28 USER='root'  29 fi  30  31 #注意密码为空的时候的格式  32 mysql_list="  33 $HOST:$PORT:$USER:$PASSWORD  34 "  35 #计算函数,提高脚本效率  36 compute(){  37 formula="$1"  38 awk 'BEGIN{printf("%.2f",'$formula')}' 2>/dev/null &&  39 echo $value || echo NULL  40 }  41  42 for mysql in $mysql_list  43 {  44 host=${mysql%%:*}  45 port=$(echo $mysql|awk -F: '{print $2}')  46 user=$(echo $mysql|awk -F: '{print $3}')  47 passwd=${mysql##*:}  48  49 [ -z "$passwd" ] && mysql="mysql -h$host -P$port -u$user" ||  50 mysql="mysql -h$host -P$port -u$user -p$passwd"  51  52 unset Uptime  53 # 把show global status的值赋给相应的参数名称(这里相当于大量的变量赋值操作)  54 eval $( $mysql -e "show global status" | awk '{print $1"=/x27"$2"/047"}')  55 [ X = X"$Uptime" ] && continue  56  57 # Mysql VER  58 VER=`$mysql -e"status;"|grep 'Server version'|awk '{print $3}'`  59  60 # Uptime  61 UPTIME=`compute "$Uptime/3600/24"`  62  63 # Threads_connected  64 threads_connected=`compute "$Threads_connected"`  65  66 # QPS Questions/Uptime  67 qps=`compute "$Questions/$Uptime"`  68  69 # TPS (Com_commit + Com_rollback)/Uptime  70 tps=`compute "($Com_commit+$Com_rollback)/$Uptime"`  71  72 # Reads Com_select + Qcache_hits  73 reads=`compute "$Com_select+$Qcache_hits"`  74  75 # Writes Com_insert + Com_update + Com_delete + Com_replace  76 writes=`compute "$Com_insert+$Com_update+$Com_delete+$Com_replace"`  77  78 # Read/Writes Ratio reads/writes*100%  79 rwratio=`compute "$reads/$writes*100"`%  80  81 # MyISAM Key_buffer_read_hits (1 - Key_reads/Key_read_requests) * 100  82 key_buffer_read_hits=`compute "(1-$Key_reads/$Key_read_requests)*100"`%  83  84 # MyISAM Key_buffer_write_hits (1 - Key_writes/Key_write_requests) * 100  85 key_buffer_write_hits=`compute "(1-$Key_writes/$Key_write_requests)*100"`%  86  87 # Query_cache_hits (Qcache_hits / (Qcache_hits + Qcache_inserts)) * 100%  88 query_cache_hits=`compute "$Qcache_hits/($Qcache_hits+$Qcache_inserts)*100"`%  89  90 # Innodb_buffer_read_hits (1 - Innodb_buffer_pool_reads/Innodb_buffer_pool_read_requests) * 100  91 innodb_buffer_read_hits=`compute "(1-$Innodb_buffer_pool_reads/$Innodb_buffer_pool_read_requests)*100"`%  92  93 # Thread_cache_hits (1 - Threads_created / Connections) * 100%  94 thread_cache_hits=`compute "(1-$Threads_created/$Connections)*100"`%  95  96 # Slow_queries_per_second Slow_queries / Uptime * 60  97 slow_queries_per_second=`compute "$Slow_queries/$Uptime"`  98  99 # Select_full_join_per_second Select_full_join / Uptime * 60 100 select_full_join_per_second=`compute "$Select_full_join/$Uptime*60"` 101 102 # select_full_join_in_all_select (Select_full_join / Com_select) * 100 103 select_full_join_in_all_select=`compute "($Select_full_join/$Com_select)*100"`% 104 105 # MyISAM Lock Contention (Table_locks_waited / Table_locks_immediate) * 100 106 myisam_lock_contention=`compute "($Table_locks_waited/$Table_locks_immediate)*100"`% 107 108 # Temp_tables_to_disk (Created_tmp_disk_tables / Created_tmp_tables) * 100 109 temp_tables_to_disk_ratio=`compute "($Created_tmp_disk_tables/$Created_tmp_tables)*100"`% 110 111 # print formated MySQL status report 112 title="******************** MySQL--${HOST}--${PORT} ***********************" 113 width=$((`echo "$title"|wc -c`-1)) 114 115 echo "$title" 116 117 export IFS=':' 118 while read name value ;do 119 printf "%36s :/t%10s/n" $name $value 120 done 99%):$key_buffer_read_hits 130 MyISAM Key buffer write hits:$key_buffer_write_hits 131 Query cache hits:$query_cache_hits 132 InnoDB buffer read hits(>95%):$innodb_buffer_read_hits 133 Thread cache hits(>90%):$thread_cache_hits 134 Slow queries per second:$slow_queries_per_second 135 Select full join per second:$select_full_join_per_second 136 Select full join in all select:$select_full_join_in_all_select 137 MyiSAM lock contention(
#Python版<img class="code_img_closed lazy" src="/static/imghwm/default1.png" data-src="http://img.bitscn.com/upimg/allimg/c140719/1405L9330U940-33564.jpg" id="code_img_closed_547cb232-79f2-4f40-9763-d38b744e9424" alt=""><img class="code_img_opened lazy" src="/static/imghwm/default1.png" data-src="http://img.bitscn.com/upimg/allimg/c140719/1405L9331093P-423U.jpg" onclick="cnblogs_code_hide('547cb232-79f2-4f40-9763-d38b744e9424',event)" id="code_img_opened_547cb232-79f2-4f40-9763-d38b744e9424"   style="max-width:90%" alt="">View Code
<pre class="brush:php;toolbar:false">  1 #!/usr/bin/env python  2   3 #-*- coding: utf-8 -*-  4   5 # Script Name: mysql_status_check.py  6   7 # Description: check mysql servers status  8   9 # Author: Bruce.Zuo  10  11 # Create Date: 2012/06/05 12  13 import os,sys 14  15 import MySQLdb 16  17 import getpass 18  19  20  21 host=raw_input("host:") 22  23 user=raw_input("user:") 24  25 password=getpass.getpass() 26  27  28  29 try: 30  31    conn = MySQLdb.connect(host = host, user=user ,passwd = password, db = 'test') 32  33 except MySQLdb.ERROR,e: 34  35    print "Error %d:%s"%(e.args[0],e.args[1]) 36  37    exit(1) 38  39 cursor=conn.cursor() 40  41  42  43 cursor.execute('show global status;') 44  45 result_set=cursor.fetchall() 46  47 cursor.close() 48  49 conn.close() 50  51  52  53 def get_value(key_name): 54  55         for rows in result_set: 56  57                 if rows[0]==key_name: 58  59                         return float(rows[1]) 60  61  62  63 print ('MySQL-'+host+'-3306').center(60,'*') 64  65 print 'Uptime:'.rjust(40),get_value('Uptime') 66  67 print 'Threads_connected:'.rjust(40),get_value('Threads_connected') 68  69 print 'QPS:'.rjust(40),round(get_value('Questions') / get_value('Uptime'),2) 70  71 print 'TPS:'.rjust(40),round(get_value('Com_commit')+get_value('Com_rollback') / get_value('Uptime'),2) 72  73 reads=get_value('Com_select')+ get_value('Qcache_hits') 74  75 writes=get_value('Com_insert')+get_value('Com_update')+get_value('Com_delete')+get_value('Com_replace') 76  77 print 'Reads:'.rjust(40),get_value('Com_select')+ get_value('Qcache_hits') 78  79 print 'Writes:'.rjust(40),get_value('Com_insert')+get_value('Com_update')+get_value('Com_delete')+get_value('Com_replace') 80  81 print 'Read/Writes Ratio:'.rjust(40),round(reads / writes,2),'%' 82  83 print 'MyISAM Key buffer read hits(>99%):'.rjust(40),round(1-get_value('Key_reads') / (get_value('Key_read_requests')*100),2),'%' 84  85 print 'MyISAM Key buffer write hits:'.rjust(40),round(1-get_value('Key_writes') / (get_value('Key_write_requests')*100),2),'%' 86  87 print 'Query cache hits:'.rjust(40),round(get_value('Qcache_hits') / (get_value('Qcache_hits')+get_value('Qcache_inserts'))*100,2),'%' 88  89 print 'InnoDB buffer read hits(>95%):'.rjust(40),round(1-get_value('Innodb_buffer_pool_reads') / (get_value('Innodb_buffer_pool_read_requests')*100),2),'%' 90  91 print 'Thread cache hits(>90%):'.rjust(40),round(1-get_value('Threads_created') / (get_value('Connections')*100),2),'%' 92  93 print 'Slow queries per second:'.rjust(40),round(get_value('Slow_queries') / get_value('Uptime'),2) 94  95 print 'Select full join per second:'.rjust(40),round(get_value('Select_full_join') / get_value('Uptime'),2) 96  97 print 'Select full join in all select:'.rjust(40),round(get_value('Select_full_join') / (get_value('Com_select')*100),2),'%' 98  99 print 'MyiSAM lock contention(
<p>#根据这个方法,可以添加更多的状态项。</p>
<p>#效果图</p>
<p><img src="/static/imghwm/default1.png" data-src="http://img.bitscn.com/upimg/allimg/c140719/1405L933135940-593L.jpg" class="lazy" alt=""></p>
bitsCN.com
    
声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
MySQL:初心者が習得するための必須スキルMySQL:初心者が習得するための必須スキルApr 18, 2025 am 12:24 AM

MySQLは、初心者がデータベーススキルを学ぶのに適しています。 1.MySQLサーバーとクライアントツールをインストールします。 2。selectなどの基本的なSQLクエリを理解します。 3。マスターデータ操作:テーブルを作成し、データを挿入、更新、削除します。 4.高度なスキルを学ぶ:サブクエリとウィンドウの関数。 5。デバッグと最適化:構文を確認し、インデックスを使用し、選択*を避け、制限を使用します。

MySQL:構造化データとリレーショナルデータベースMySQL:構造化データとリレーショナルデータベースApr 18, 2025 am 12:22 AM

MySQLは、テーブル構造とSQLクエリを介して構造化されたデータを効率的に管理し、外部キーを介してテーブル間関係を実装します。 1.テーブルを作成するときにデータ形式と入力を定義します。 2。外部キーを使用して、テーブル間の関係を確立します。 3。インデックス作成とクエリの最適化により、パフォーマンスを改善します。 4.データベースを定期的にバックアップおよび監視して、データのセキュリティとパフォーマンスの最適化を確保します。

MySQL:説明されている主要な機能と機能MySQL:説明されている主要な機能と機能Apr 18, 2025 am 12:17 AM

MySQLは、Web開発で広く使用されているオープンソースリレーショナルデータベース管理システムです。その重要な機能には、次のものが含まれます。1。さまざまなシナリオに適したInnodbやMyisamなどの複数のストレージエンジンをサポートします。 2。ロードバランスとデータバックアップを容易にするために、マスタースレーブレプリケーション機能を提供します。 3.クエリの最適化とインデックスの使用により、クエリ効率を改善します。

SQLの目的:MySQLデータベースとの対話SQLの目的:MySQLデータベースとの対話Apr 18, 2025 am 12:12 AM

SQLは、MySQLデータベースと対話して、データの追加、削除、変更、検査、データベース設計を実現するために使用されます。 1)SQLは、ステートメントの選択、挿入、更新、削除を介してデータ操作を実行します。 2)データベースの設計と管理に作成、変更、ドロップステートメントを使用します。 3)複雑なクエリとデータ分析は、ビジネス上の意思決定効率を改善するためにSQLを通じて実装されます。

初心者向けのMySQL:データベース管理を開始します初心者向けのMySQL:データベース管理を開始しますApr 18, 2025 am 12:10 AM

MySQLの基本操作には、データベース、テーブルの作成、およびSQLを使用してデータのCRUD操作を実行することが含まれます。 1.データベースの作成:createdatabasemy_first_db; 2。テーブルの作成:createTableBooks(idintauto_incrementprimarykey、titlevarchary(100)notnull、authorvarchar(100)notnull、published_yearint); 3.データの挿入:InsertIntoBooks(タイトル、著者、公開_year)VA

MySQLの役割:WebアプリケーションのデータベースMySQLの役割:WebアプリケーションのデータベースApr 17, 2025 am 12:23 AM

WebアプリケーションにおけるMySQLの主な役割は、データを保存および管理することです。 1.MYSQLは、ユーザー情報、製品カタログ、トランザクションレコード、その他のデータを効率的に処理します。 2。SQLクエリを介して、開発者はデータベースから情報を抽出して動的なコンテンツを生成できます。 3.MYSQLは、クライアントサーバーモデルに基づいて機能し、許容可能なクエリ速度を確保します。

MySQL:最初のデータベースを構築しますMySQL:最初のデータベースを構築しますApr 17, 2025 am 12:22 AM

MySQLデータベースを構築する手順には次のものがあります。1。データベースとテーブルの作成、2。データの挿入、および3。クエリを実行します。まず、createdAtabaseおよびcreateTableステートメントを使用してデータベースとテーブルを作成し、InsertINTOステートメントを使用してデータを挿入し、最後にSelectステートメントを使用してデータを照会します。

MySQL:データストレージに対する初心者向けのアプローチMySQL:データストレージに対する初心者向けのアプローチApr 17, 2025 am 12:21 AM

MySQLは、使いやすく強力であるため、初心者に適しています。 1.MYSQLはリレーショナルデータベースであり、CRUD操作にSQLを使用します。 2。インストールは簡単で、ルートユーザーのパスワードを構成する必要があります。 3.挿入、更新、削除、および選択してデータ操作を実行します。 4. Orderby、Where and Joinは複雑なクエリに使用できます。 5.デバッグでは、構文をチェックし、説明を使用してクエリを分析する必要があります。 6.最適化の提案には、インデックスの使用、適切なデータ型の選択、優れたプログラミング習慣が含まれます。

See all articles

ホットAIツール

Undresser.AI Undress

Undresser.AI Undress

リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover

AI Clothes Remover

写真から衣服を削除するオンライン AI ツール。

Undress AI Tool

Undress AI Tool

脱衣画像を無料で

Clothoff.io

Clothoff.io

AI衣類リムーバー

AI Hentai Generator

AI Hentai Generator

AIヘンタイを無料で生成します。

ホットツール

Dreamweaver Mac版

Dreamweaver Mac版

ビジュアル Web 開発ツール

PhpStorm Mac バージョン

PhpStorm Mac バージョン

最新(2018.2.1)のプロフェッショナル向けPHP統合開発ツール

MantisBT

MantisBT

Mantis は、製品の欠陥追跡を支援するために設計された、導入が簡単な Web ベースの欠陥追跡ツールです。 PHP、MySQL、Web サーバーが必要です。デモおよびホスティング サービスをチェックしてください。

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Eclipse を SAP NetWeaver アプリケーション サーバーと統合します。

WebStorm Mac版

WebStorm Mac版

便利なJavaScript開発ツール