MySQL uses init-connect to increase the implementation of access audit function

MySQL uses init-connect to increase the implementation of access audit function

The mysql connection must first be initialized through init-connect and then connected to the instance.

We take advantage of this and implement the db access audit function by recording the user's thread_id, username and user address during init-connect.

Implementation steps

1. Create a library table for auditing.

In order to avoid conflicts with business libraries, create your own library separately:

#Create database table code create database db_monitor;
use db_monitor ;
CREATE TABLE accesslog
(thread_id int(11) DEFAULT NULL, #process id
 log_time datetime default null, #login time localname varchar(50) DEFAULT NULL, #login name with detailed IP
 matchname varchar(50) DEFAULT NULL, #Login user key idx_log_time(log_time)
)ENGINE=InnoDB DEFAULT CHARSET=utf8; 

2. Configure init-connect parameters

This parameter can be adjusted dynamically. Please also note that it must be added to the configuration file my.cnf, otherwise it will become invalid after the next restart.

mysql> show variables like 'init_connect%';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| init_connect | |
+---------------+-------+
1 row in set (0.00 sec

mysql> set global init_connect='insert into db_monitor.accesslog(thread_id,log_time,localname,matchname) values(connection_id(),now(),user(),current_user());'; 

3. Grant ordinary users insert permissions to the accesslog table

This point is important

This parameter is only effective for ordinary users and will not work for users with super permissions.

If you are a normal user, you must authorize after adding this function:

grant insert on db_monitor.accesslog to user@'xx.xx.xx.%';

The consequence of not authorizing is that connecting to the database will fail:

Users who do not have insert permission on the accesslog table:

mysql> show databases;
ERROR 2006 (HY000): MySQL server has gone away
No connection. Trying to reconnect...
Connection id: 7
Current database: *** NONE ***

ERROR 1184 (08S01): Aborted connection 7 to db: 'unconnected' user: 'user2' host: 'localhost' (init_connect command failed) 

4. Verify audit function

A user deleted a table in the test database. Let's see if we can track the user with the help of binlog log:


View binlog:


It can be seen which user performed the operation, thus completing the audit.

The above article about how to use init-connect to increase the access audit function in MySQL is all I have to share with you. I hope it can give you a reference, and I also hope that you will support 123WORDPRESS.COM.

You may also be interested in:
  • Spring Data JPA's Audit function audits database changes
  • Sample code for AuditorAware auditing function based on Spring Data
  • Solve the problem of springdataJPA supporting native sql
  • SpringDataJPA native sql query encapsulation operation
  • Analysis of Spring Data JPA's Audit function: Audit database changes

<<:  Open the app on the h5 side in vue (determine whether it is Android or Apple)

>>:  Tips for organizing strings in Linux

Recommend

KVM virtualization installation, deployment and management tutorial

Table of contents 1.kvm deployment 1.1 kvm instal...

Example of using #include file in html

There are two files a.htm and b.htm. In the same d...

Detailed explanation of various join summaries of SQL

SQL Left Join, Right Join, Inner Join, and Natura...

MySQL uses custom sequences to implement row_number functions (detailed steps)

After reading some articles, I finally figured ou...

How to reset the root password in CentOS7

There are various environmental and configuration...

Detailed process of Vue front-end packaging

Table of contents 1. Add packaging command 2. Run...

Why MySQL does not recommend using null columns with default values

The answer you often hear is that using a NULL va...

Implementation of single process control of Linux C background service program

introduce Usually a background server program mus...

MySQL 8.0 Window Function Introduction and Summary

Preface Before MySQL 8.0, it was quite painful to...

Detailed tutorial on running Tomcat in debug mode in IDEA Maven project

1. Add the following dependencies in pom.xml <...

How to use uni-app to display buttons and search boxes in the top navigation bar

Recently, the company is preparing to develop an ...

Detailed explanation of docker compose usage

Table of contents Docker Compose usage scenarios ...

Drawing fireworks effect of 2021 based on JS with source code download

This work uses the knowledge of front-end develop...

Today I encountered a very strange li a click problem and solved it myself

...It's like this, today I was going to make a...

Node script realizes automatic sign-in and lottery function

Table of contents 1. Introduction 2. Preparation ...