|
mysql数据库中有一个参数--init_connect
根据文档描述该参数是在非super privilege用户连接数据库时隐式执行的动作(也就是sql语句)。
比如:
- [mysqld]
- init_connect='SET autocommit=0'
复制代码
表示,一般用户连接数据库时隐式执行“SET autocommit=0”这条语句。
并且还说明,服务器会丢弃语句执行产生的结果。
##更深层研究
1:建监控连接信息的表
- use dba;
- create table accesslog(
- `thread_id` int primary key auto_increment,
- `time` timestamp,
- `localname` varchar(40),
- `machine_name` varchar(40)
- );
复制代码
2:设置变量init_connect
- mysql> show variables like 'init%';
- +---------------+-------+
- | Variable_name | Value |
- +---------------+-------+
- | init_connect | |
- | init_file | |
- | init_slave | |
- +---------------+-------+
- 3 rows in set (0.00 sec)
-
- mysql> set global init_connect='insert into dba.accesslog(thread_id,time,localname,machine_name) values(connection_id(),now(),user(),current_user());';
- Query OK, 0 rows affected (0.00 sec)
-
- mysql> show variables like 'init%';
- +---------------+-----------------------------------------------------------------------------------------------------------------------+
- | Variable_name | Value |
- +---------------+-----------------------------------------------------------------------------------------------------------------------+
- | init_connect | insert into dba.accesslog(thread_id,time,localname,machine_name) values(connection_id(),now(),user(),current_user()); |
- | init_file | |
- | init_slave | |
- +---------------+-----------------------------------------------------------------------------------------------------------------------+
- 3 rows in set (0.00 sec)
复制代码
3:分配用户权限
- mysql> grant select,insert,update on dba.accesslog to baidandan@'192.168.9.45' identified by 'baidandan';
- Query OK, 0 rows affected (0.00 sec)
-
- --为了做实验,给baidandan赋予操作dba.t表的权限
- mysql> grant select,delete on dba.t to baidandan@'192.168.9.45';
- Query OK, 0 rows affected (0.00 sec)
复制代码
4:测试
--客户端连接进行测试
C:\Users\dandan>mysql -u baidandan -p -h 192.168.6.51
Enter password: *********
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 371
Server version: 5.6.20-r5436-log Source distribution |
|