C/C++ program that connects to a MySQL server and checks intrusion attempts every 5 minutes

This program is developed in ubuntu gccto execute this program use the following command
“gcc  $(mysql_config –cflags) fmysql.c $(mysql_config –libs)”
for output “./a.out”

C program that connects to a MySQL server and checks intrusion attempts every 5 minutes. If an intrusion attempt is detected beep the internal speaker to alert the administrator.

int cnt=0;       //count the no of while(1) loop execution
int ct=0;
int gt=0;
int x=0;        //hold the total result of table
int i=0;        //hold th current result of table
int fval=0;     //hold the no. of new instruction in table
char *lcl_server;//mysql server location
char *mysql_user;     //mysql user name
char *mysql_password; //mysql password
char *mysql_dd = "mysql";          //mysql database


MYSQL *mysql_con; //for make the connection with mysql
MYSQL_RES *mysql_res;// get the result
MYSQL_ROW rw;          //for get the no. of row to fetched
system("clear");      //clear the screen
printf("\n\n********** MySQL Intrusion detection in C **********");
printf("\n             Server Location and Login");
lcl_server = (char *) malloc (35);    //create the memory
mysql_user=(char *)malloc(50);       //create the memory
mysql_password=(char *)malloc(50);   //create the memory
printf("\n           Enter Server Location::");
printf("\n           Enter user name::");
printf("\n           Enter %s password::",mysql_user);

while(1)                //loop for checking the new instruction will arrive in mysql server


mysql_con = mysql_init('\0');  //init the server to null
printf("\n\n********** MySQL Intrusion detection in C **********");
//connect the server and check the connection and error
printf("\n\n           Try To Build connection with '%2s' Database...",mysql_dd);
if(!mysql_real_connect(mysql_con, lcl_server,mysql_user, mysql_password, mysql_dd, 0, NULL, 0)) {

printf("\n           %s", mysql_error(mysql_con)); //show the error
exit(0);                                 //exit




printf("\n\n           '%s' Database is Ready to use...",mysql_dd);  //display the msg


printf("\n\n           Changing the log variable value....");

if(mysql_query(mysql_con,"set global log_output='TABLE'"))//setting the varible in


printf("\n%s", mysql_error(mysql_con));



printf("\n\n           Log variable value is set to 'Table'...");


printf("\n\n           Detecting Intrusion...");
if(mysql_query(mysql_con, "select * from general_log")) //execute the select query
//__in general_log table in mysql database

printf("\n%s", mysql_error(mysql_con));


mysql_res = mysql_use_result(mysql_con);            //get the result of query


while((rw = mysql_fetch_row(mysql_res)) != NULL)   //reading the recored at last


i=i+1;        //increment the i counter


cnt=cnt+1;                   //increment the cnt counter
fval = i-(cnt*4);           //fval hold the count of new instruction in mysql_
//_not this program intrusion executed in mysql
if(x!=fval-1)                //compare the count of intrusion if new instruction__
//__condition is true will found then

printf("\a\a\n\n            Instruction has been Detected...\a\a"); //give the msg and beep
//to administrator
flag();                                             //get the current count of_
//_intrusion in x

mysql_free_result(mysql_res);// free the result varible
mysql_close(mysql_con);        //close the connection
printf("\n\nPress ctrl+z to exit...");  //msg for exit
//      sleep(18000);           //5 Min. wait






MYSQL *mysql_con;
MYSQL_RES *mysql_res;
mysql_con = mysql_init(NULL);
if(!mysql_real_connect(mysql_con, lcl_server,mysql_user, mysql_password, mysql_dd, 0, NULL, 0))

printf("%s\n", mysql_error(mysql_con));

mysql_query(mysql_con,"set global log_output='TABLE'");
if (mysql_query(mysql_con, "select * from general_log"))
printf("%s\n", mysql_error(mysql_con));
mysql_res = mysql_use_result(mysql_con);
while ((rw = mysql_fetch_row(mysql_res)) != NULL)

4 thoughts on “C/C++ program that connects to a MySQL server and checks intrusion attempts every 5 minutes

  1. sir where i have to write these code.i was writing in turbo c.but this gives error that what is mysql.h lib.tell me first step to do and execute this program

    • Hi adarsh,
      Basically I used ubuntu GCC for developing this program first you need to install my-sql in Ubuntu by running this command

        “sudo apt-get install mysql-server”

      . Then you need to include the


      header file.

  2. sir thank you..one company asking me to write c/c++ code to connect to sql server….only select command should work….i have window 7.can you give me full code and path to run that program…
    if i will give him than only they will select me….my email id@ piyushanand26@gmail.com

  3. I tried out this program but i got error: expected ‘;’ before ‘{’ token at line num 94 can u help me out to solve this…

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.