2011-08-24 67 views
0

我需要一個連接到MySQL服務器並檢查InnoDB數據存儲是否安裝在其上的C/C++程序。用於檢查MySQL連接和InnoDB插件的C/C++程序

程序應該打印MySQL寫入InnoDB存儲的總磁盤寫入次數。

+0

如果可以,請詳細說明,需要找到沒有。磁盤寫入由MySql。如果你能提供一個背景,會更好。它將幫助人們更好地將場景想象爲更接近您需要的答案,並且這將成爲其他讀者的一個很好的信息 – Aniket

回答

1
/* 
Below program is compiled using GCC and tested in Ubuntu 11.04 and Maynot run properly run in windows because of header file. 
Make sure MySQL is installed properly. 
*/ 
#include<stdio.h> 
#include<string.h> 
#include<mysql.h> 
//#include<conio.h> //Uncomment if you are using windows 
int main() 
{ 
    MYSQL *connection;//pointer to connection string, MYSQL structure pointer 
    MYSQL_RES *result;//pointer holds result 
    MYSQL_ROW row;//stores a row in result 

    char *server = "localhost";//server is localhost since MySQL runs in same machine 
    char *user = "root";//username to connect to DB 
    char *password = "pass";//Password 
    char *database = "blog";//DB name 

    connection = mysql_init(NULL);//Getting ready for MySQL Connection 

    if(!mysql_real_connect(connection, server, user, password, database, 
    0, NULL, 0))//Establishing connection with parameters 
    { 
     fprintf(stderr, "%s\n", mysql_error(connection));//PRINT Error message 
     exit(1);//In case unable to connect quit 
    } 

    /* 
     show engines will display all the engines available.Sample Output 
     mysql> show engines; 
     +------------+---------+------------------------------------ ----------------------------+--------------+------+------------+ 
     | Engine  | Support | Comment               | Transactions | XA | Savepoints | 
     +------------+---------+------------------------------------ ----------------------------+--------------+------+------------+ 
     | InnoDB  | YES  | Supports transactions, row-level locking, and foreign keys  | YES   | YES | YES  | 
     | MRG_MYISAM | YES  | Collection of identical MyISAM tables       | NO   | NO | NO   | 
     | BLACKHOLE | YES  | /dev/null storage engine (anything you write to it disappears) | NO   | NO | NO   | 
     | CSV  | YES  | CSV storage engine             | NO   | NO | NO   | 
     | MEMORY  | YES  | Hash based, stored in memory, useful for temporary tables  | NO   | NO | NO   | 
     | FEDERATED | NO  | Federated MySQL storage engine         | NULL   | NULL | NULL  | 
     | ARCHIVE | YES  | Archive storage engine           | NO   | NO | NO   | 
     | MyISAM  | DEFAULT | Default engine as of MySQL 3.23 with great performance   | NO   | NO | NO   | 
     +------------+---------+----------------------------------------------------------------+--------------+------+------------+ 
     8 rows in set (0.00 sec) 


     */ 
     if(mysql_query(connection, "show engines")) 
     { 
      fprintf(connection, "%s\n", mysql_error(connection)); 
      exit(1);//EXit when query failed 
     } 

     result = mysql_use_result(connection);//fetch result pointer 
     printf("\n --- Output ---- \n"); 
     while((row = mysql_fetch_row(result)) != NULL)//iterating over the result 
     { 
      if((strcmp(row[0], "InnoDB") == 0) && strcmp(row[1], "YES") == 0) 
      printf("InnoDB engine is supported\n"); 
      break; 
     } 
     mysql_free_result(result);//Free the buffer 

     //To fetch Total writes 
     if(mysql_query(connection, "show global status")) 
     { 
      fprintf(connection, "%s\n", mysql_error(connection)); 
      exit(1);//EXit when query failed 
     } 

     result = mysql_use_result(connection);//fetch result pointer 
     while((row = mysql_fetch_row(result)) != NULL)//iterating over the result 
     { 
       if(strcmp(row[0], "Handler_write") == 0) 
       printf("\nTotal Writes : %d\n", row[1]); 
     } 

     mysql_free_result(result);//Free the buffer 
     mysql_close(connection);//close the connection 

     //getch(); //uncomment this line if you are using windows. 
     return 0; 
    } 
0
/* 
Program :InnoDB_checker.c 
Description:Write a C/C++ program that connects to a MySQL server and checks if the    InnoDB plug-in is installed on it. 
     If so, your program should print the total number of disk writes by MySQL. 
Date  :7/09/2011 
Version :1.0 
Author  :Venugopal Madathil*/ 
/*Command to run the program: gcc -o output -I/usr/include/mysql/ -lmysqlclient InnoDB_checker.c -w */ 

#include<stdio.h> 
#include<mysql.h> 
#include<string.h> 
int main(int argc,char *argv[]) 
{ 
     MYSQL *connection; 
     MYSQL_RES *result; 
     MYSQL_ROW *row; 


//printf("\nI'm here\n");//Debugger 
char *server = "localhost";//Since system itself acts as the server 
//Credentials initialization 
char *user = "root"; 
char *password = "pass";//Must 
char *database = "webyog";//Must be an existing database name 
int numfields,Inno_flag=0,CSV_flag=0,c; 
char choice; 

do 
{ 
    system("clear");//To clear the terminal screen 
    printf("----------------------\nPlug-in checker\n"); 
    printf("----------------------\n"); 
    connection = mysql_init(NULL);//Allocates or initializes a MYSQL object suitable for mysql_real_connect,if NULL it returs a new object 
    if(!mysql_real_connect(connection,server,user,password,database,0,NULL,0))//Connection establishment 
    { 
     printf("\nConnection error:"); 
     fprintf(stderr,"%s\n",mysql_error(connection));//Error 
    } 
    if(mysql_query(connection,"show engines")) 
    { 
     printf("\nQuery execution failed:"); 
     fprintf(stderr,"%s\n",mysql_error(connection));//Error 
    } 
    result = mysql_use_result(connection);//Fetching result 
    numfields = mysql_num_fields(result); 
    //printf("\nNumber of fields:%d\n",numfields);//Number of fields 
    printf("What do you want to check?\nPress 1 for InnoDB\nPress 2 for CSV support\nYour choice: "); 
    scanf("%d",&c); 
    // 
    while((row = mysql_fetch_row(result)) != NULL)//Iterating result row 
      { 
       if((strcmp(row[0], "InnoDB") == 0) && strcmp(row[1], "YES") == 0)//Innodb plug in checking 
         Inno_flag=1; 
        if((strcmp(row[0], "CSV") == 0) && strcmp(row[1], "YES") == 0)//CSV support checking 
         CSV_flag=1; 
      } 
    switch(c) 
    { 
     case 1: 

      if(!Inno_flag) 
       printf("InnoDB plug-in is not installed\n"); 
      else 
       printf("InnoDB plug-in is installed\n"); 
       mysql_free_result(result);//Freeing the buffer 
       //To fetch Total Disk writes by MySQL 
       if(mysql_query(connection, "show global status")) 
       { 
       printf("\nQuery execution failed:"); 
        fprintf(connection, "%s\n", mysql_error(connection)); 
        exit(1); 
       } 

       result = mysql_use_result(connection);//fetching result 
       while((row = mysql_fetch_row(result)) != NULL)//Iterating result row 
       { 
        if(strcmp(row[0], "Handler_write") == 0) 
        printf("Total number of Disk Writes by MySQL: %d\n", row[1]); 
       } 
      break; 
     case 2: 
      if(!CSV_flag) 
            printf("CSV support is not there.\n"); 
          else 
            printf("CSV support is there.\n"); 

      break; 
     default:printf("\nInvalid choice\n"); 

    } 
    mysql_free_result(result);//Freeing the buffer 
     mysql_close(connection);//closing the connection 
    printf("\nDo you want to continue?(Y/N):"); 
    scanf("%s",&choice); 
    if(choice!='Y'|| choice!='y'||choice!='n'||choice!='N') 
    { 
     printf("\nInvalid option. Program exits.\n"); 
     exit(0); 
    } 
    printf("\n\n\n"); 
}while(choice =='Y' || choice =='y'); 
return 0; 
    } 
    //Command to run the program: gcc -o output -I/usr/include/mysql/ -lmysqlclient InnoDB_checker.c -w