三、建立mysql连接以及连接检查
1、打开src/events.c,在46行左右,增加下面的定义:
1MYSQL *my_conn;
2、找到void SetupEvents(void)函数体,修改代码如下:1
2 /* Start events */
3 //add by oldhawk
4 EventAddEx(NULL, "check_mysql_conn", 300, 0, check_mysql_conn, NULL);
5 //add end
6 EventAddEx(NULL, "tunefile", 300, 0, save_tunefile, NULL);
7 EventAddEx(NULL, "garbage", GARBAGE_COLLECT_EVERY, 0, garbage_collect, NULL);
8
添加此行意义为让系统在每过300秒(5分钟)执行指定的函数来检查mysql的连接状况。2 /* Start events */
3 //add by oldhawk
4 EventAddEx(NULL, "check_mysql_conn", 300, 0, check_mysql_conn, NULL);
5 //add end
6 EventAddEx(NULL, "tunefile", 300, 0, save_tunefile, NULL);
7 EventAddEx(NULL, "garbage", GARBAGE_COLLECT_EVERY, 0, garbage_collect, NULL);
8
3、在src/events/c最后,加入对check_mysql_conn的详细代码,如下:
1 //add by oldhawk
2 EVENT(check_mysql_conn)
3 {
4 if(strcmp(MYSQL_HOST,"0")){
5 //ircd_log(LOG_SERVER,"in event check_mysql_conn");
6 if(!my_conn){ //数据库未连接,可能是首次起动
7 ircd_log(LOG_CLIENT,"Begin to connect to Mysql");
8
9 my_conn=my_mysql_connect();
10 if(!my_conn){
11 sendto_ops("Mysql 数据库服务器出现故障,请通知管理员排除!");
12 ircd_log(LOG_SERVER,"Mysql Connect : ERROR!");
13 return;
14 }else{
15 mysql_query(my_conn,"SET NAMES 'gb2312'");
16 ircd_log(LOG_SERVER,"Mysql Connect : OK!");
17 }
18 }else{ //数据库已连接,则测试激活
19 if(my_mysql_ping(my_conn)!=0){
20 ircd_log(LOG_SERVER,"Mysql Ping : mysql ping error");
21 my_conn=my_mysql_connect();
22 if(!my_conn){
23 sendto_ops("Mysql 数据库服务器出现Ping故障,请通知管理员排除!");
24 ircd_log(LOG_SERVER,"Mysql Ping reConnect : ERROR!");
25 return;
26 }else{
27 mysql_query(my_conn,"SET NAMES 'gb2312'");
28 ircd_log(LOG_SERVER,"Mysql Ping reConnect : OK!");
29 }
30 }/*else{
31 ircd_log(LOG_SERVER,"Mysql Ping: OK!");
32 }*/
33 }
34 }
35 }
36 //add end
37
4、上面的函数只会在5分钟后才会起动一次,但系统起动时应该就需要执行此函数体一次以保证系统在起动时就对mysql进行连接,所以请打开src/ircd.c,在1058行左右,修改代码如下:2 EVENT(check_mysql_conn)
3 {
4 if(strcmp(MYSQL_HOST,"0")){
5 //ircd_log(LOG_SERVER,"in event check_mysql_conn");
6 if(!my_conn){ //数据库未连接,可能是首次起动
7 ircd_log(LOG_CLIENT,"Begin to connect to Mysql");
8
9 my_conn=my_mysql_connect();
10 if(!my_conn){
11 sendto_ops("Mysql 数据库服务器出现故障,请通知管理员排除!");
12 ircd_log(LOG_SERVER,"Mysql Connect : ERROR!");
13 return;
14 }else{
15 mysql_query(my_conn,"SET NAMES 'gb2312'");
16 ircd_log(LOG_SERVER,"Mysql Connect : OK!");
17 }
18 }else{ //数据库已连接,则测试激活
19 if(my_mysql_ping(my_conn)!=0){
20 ircd_log(LOG_SERVER,"Mysql Ping : mysql ping error");
21 my_conn=my_mysql_connect();
22 if(!my_conn){
23 sendto_ops("Mysql 数据库服务器出现Ping故障,请通知管理员排除!");
24 ircd_log(LOG_SERVER,"Mysql Ping reConnect : ERROR!");
25 return;
26 }else{
27 mysql_query(my_conn,"SET NAMES 'gb2312'");
28 ircd_log(LOG_SERVER,"Mysql Ping reConnect : OK!");
29 }
30 }/*else{
31 ircd_log(LOG_SERVER,"Mysql Ping: OK!");
32 }*/
33 }
34 }
35 }
36 //add end
37
1
2 write_pidfile();
3 Debug((DEBUG_NOTICE, "Server ready"));
4 SetupEvents();
5 //add by oldhawk
6 check_mysql_conn(NULL);
7 //add end
8 #ifdef THROTTLING
9 init_throttling_hash();
10 #endif
11 #ifdef NEWCHFLOODPROT
12
2 write_pidfile();
3 Debug((DEBUG_NOTICE, "Server ready"));
4 SetupEvents();
5 //add by oldhawk
6 check_mysql_conn(NULL);
7 //add end
8 #ifdef THROTTLING
9 init_throttling_hash();
10 #endif
11 #ifdef NEWCHFLOODPROT
12
在经过上面的步聚以后,系统就会在起动后连接mysql,并且每5分钟就检测一次mysql,如果连接不正常会自动重连,如果连接正常则发送ping命令以让此连接一直保持活动。
为Unrealircd增加Mysql的数据库支持(三)