深圳全飞鸿

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 534|回复: 0
打印 上一主题 下一主题

mysql数据库中有一个参数--init_connect

[复制链接]

800

主题

1379

帖子

7704

积分

版主

Rank: 7Rank: 7Rank: 7

积分
7704
跳转到指定楼层
楼主
发表于 2021-9-11 19:21:05 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
mysql数据库中有一个参数--init_connect   


根据文档描述该参数是在非super privilege用户连接数据库时隐式执行的动作(也就是sql语句)。   

比如:   

  1.     [mysqld]   
  2.     init_connect='SET autocommit=0'   
复制代码


表示,一般用户连接数据库时隐式执行“SET autocommit=0”这条语句。  
并且还说明,服务器会丢弃语句执行产生的结果。   



##更深层研究   


1:建监控连接信息的表  
  1. use dba;  
  2. create table accesslog(
  3. `thread_id` int primary key auto_increment,
  4. `time` timestamp,
  5. `localname` varchar(40),
  6. `machine_name` varchar(40)
  7. );  
复制代码

2:设置变量init_connect  
  1. mysql> show variables like 'init%';  
  2. +---------------+-------+  
  3. | Variable_name | Value |  
  4. +---------------+-------+  
  5. | init_connect  |       |  
  6. | init_file     |       |  
  7. | init_slave    |       |  
  8. +---------------+-------+  
  9. 3 rows in set (0.00 sec)  
  10.    
  11. mysql> set global init_connect='insert into dba.accesslog(thread_id,time,localname,machine_name) values(connection_id(),now(),user(),current_user());';  
  12. Query OK, 0 rows affected (0.00 sec)  
  13.    
  14. mysql> show variables like 'init%';  
  15. +---------------+-----------------------------------------------------------------------------------------------------------------------+  
  16. | Variable_name | Value                                                                                                                 |  
  17. +---------------+-----------------------------------------------------------------------------------------------------------------------+  
  18. | init_connect  | insert into dba.accesslog(thread_id,time,localname,machine_name) values(connection_id(),now(),user(),current_user()); |  
  19. | init_file     |                                                                                                                       |  
  20. | init_slave    |                                                                                                                       |  
  21. +---------------+-----------------------------------------------------------------------------------------------------------------------+  
  22. 3 rows in set (0.00 sec)  
复制代码
  
3:分配用户权限  
  1. mysql> grant select,insert,update on dba.accesslog to baidandan@'192.168.9.45' identified by 'baidandan';  
  2. Query OK, 0 rows affected (0.00 sec)  
  3.    
  4. --为了做实验,给baidandan赋予操作dba.t表的权限  
  5. mysql> grant select,delete on dba.t to baidandan@'192.168.9.45';  
  6. 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  
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|nagomes  

GMT+8, 2025-5-5 03:11 , Processed in 0.023559 second(s), 21 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表