Mysql uses init-connect to add the implementation of the access audit function, mysqlinit-connect

Source: Internet
Author: User

Mysql uses init-connect to add the implementation of the access audit function, mysqlinit-connect

Mysql is first initialized through init-connect and then connected to the instance.

We use this to record the user's thread_id, user name, and user address during init-connect to implement the Database Access Audit function.

Steps

1. Create a database table for audit.

To avoid conflicts with the Business Database, create your own database separately:

# Create database db_monitor; use db_monitor; create table accesslog (thread_id int (11) default null, # process id log_time datetime default null, # Logon 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 the init-connect Parameter

This parameter can be dynamically adjusted and 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 secmysql> set global init_connect='insert into db_monitor.accesslog(thread_id,log_time,localname,matchname) values(connection_id(),now(),user(),current_user());'; 

3. Grant normal users the insert permission on the accesslog table.

This is important.

This parameter takes effect only for common users.

If you are a common user, you must grant permissions after adding this function:

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

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

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

mysql> show databases;ERROR 2006 (HY000): MySQL server has gone awayNo connection. Trying to reconnect...Connection id:  7Current database: *** NONE ***ERROR 1184 (08S01): Aborted connection 7 to db: 'unconnected' user: 'user2' host: 'localhost' (init_connect command failed) 

4. Verify the audit function

A user deletes a table from the test database to see if the user can be tracked using binlog:


View binlog:


You can see which user performed the operation to complete the audit.

The above implementation of the Access Audit function added by mysql using init-connect is all the content that I have shared with you. I hope to give you a reference and support for the customer's house.

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.