Use the audit function of mysql to record user operation information

Source: Internet
Author: User

MysqlIf you want to record user operation information in the database, you can useAuditAudit function. This function is automatically triggered. You can see more detailed definitions in the plugin_audit.h file. In the audit plug-in, controllable variables include THD and events.

There are two types of event structures, which can be forced conversion:

First:

 
 
  1. struct mysql_event_general   
  2.  
  3. {  
  4.  
  5. unsigned int event_subclass;   
  6.  
  7. int general_error_code;   
  8.  
  9. unsigned long general_thread_id;   
  10.  
  11. const char *general_user;   
  12.  
  13. unsigned int general_user_length;   
  14.  
  15. const char *general_command;   
  16.  
  17. unsigned int general_command_length;   
  18.  
  19. const char *general_query;   
  20.  
  21. unsigned int general_query_length;   
  22.  
  23. struct charset_info_st *general_charset;   
  24.  
  25. unsigned long long general_time;   
  26.  
  27. unsigned long long general_rows;   
  28.  
  29. }; 

Trigger condition:

# Define MYSQL_AUDIT_GENERAL_LOG 0: triggered before being submitted to the general query log.

# Define MYSQL_AUDIT_GENERAL_ERROR 1: triggered before an error is sent to the user.

# Define MYSQL_AUDIT_GENERAL_RESULT 2: triggered when the result set is sent to the user.

# Define MYSQL_AUDIT_GENERAL_STATUS 3: triggered when a result set is sent or an error occurs.

Second:

 
 
  1. struct mysql_event_connection   
  2.  
  3. {  
  4.  
  5. unsigned int event_subclass;   
  6.  
  7. int status;   
  8.  
  9. unsigned long thread_id;   
  10.  
  11. const char *user;   
  12.  
  13. unsigned int user_length;   
  14.  
  15. const char *priv_user;   
  16.  
  17. unsigned int priv_user_length;   
  18.  
  19. const char *external_user;   
  20.  
  21. unsigned int external_user_length;   
  22.  
  23. const char *proxy_user;   
  24.  
  25. unsigned int proxy_user_length;   
  26.  
  27. const char *host;   
  28.  
  29. unsigned int host_length;   
  30.  
  31. const char *ip;   
  32.  
  33. unsigned int ip_length;   
  34.  
  35. const char *database;   
  36.  
  37. unsigned int database_length;   
  38.  
  39. }; 

Trigger condition:

# Define MYSQL_AUDIT_CONNECTION_CONNECT 0: triggered after authentication is completed.

# Define MYSQL_AUDIT_CONNECTION_DISCONNECT 1: triggered when the connection is interrupted.

# Define MYSQL_AUDIT_CONNECTION_CHANGE_USER 2: triggered after the COM_CHANGE_USER command is executed.

From the above analysis, we can see that a wealth of information is stored in the event, and the notify function is modified as follows:

 
 
  1. static void audit_null_notify(MYSQL_THD thd __attribute__((unused)),   
  2.  
  3. unsigned int event_class,  
  4.  
  5. const void *event)  
  6.  
  7. {   
  8.  
  9. const struct mysql_event_general *pEvent;  
  10.  
  11. if (log_fp == NULL)  
  12.  
  13. log_fp = fopen("/tmp/rec.log", "a");  
  14.  
  15. number_of_calls++;  
  16.  
  17. if (event_class == MYSQL_AUDIT_GENERAL_CLASS && log_fp != NULL){  
  18.  
  19. pEvent = (const struct mysql_event_general *) event;  
  20.  
  21. if ( pEvent->event_subclass == MYSQL_AUDIT_GENERAL_RESULT &&  
  22.  
  23. pEvent->general_query != NULL  
  24.  
  25. && *(pEvent->general_query) != '\0') {   
  26.  
  27. // fprintf(log_fp, "user:%s,host:%s,command:%s\n",&thd->security_ctx->priv_user[0],   
  28.  
  29. // (char *) thd->security_ctx->host_or_ip ,  
  30.  
  31. // pEvent->general_query);  
  32.  
  33. time_t cur = time(NULL);  
  34.  
  35. fprintf(log_fp, "%s %s\n%s\n", ctime(&cur) , pEvent->general_user , pEvent->general_query);  
  36.  
  37. fflush(log_fp);  
  38.  
  39. }  
  40.  
  41. }  
  42.  

In this way, we can record user name, user host information, SQL operations, and other related information.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.