[Asterisk-Dev] cdr_mysql and MySQL socket

Tamas Jalsovszky thomasj at eworld.hu
Mon Aug 4 06:52:33 MST 2003


Hello,

        I found, that cdr_mysql connects to database through TCP
socket and doesn't allow to connect to Unix socket (in case mysql is
running on the same box as asterisk).
        If both (hostname and socket) options are set, cdr_mysql will
connect through hostname (according to mysql API:
http://www.mysql.com/doc/en/mysql_real_connect.html)

        I include a patch which adds unix socket functionality.

        Regards,
                Thomas

--- configs/cdr_mysql.conf.sample.orig  Mon Aug  4 15:46:21 2003
+++ configs/cdr_mysql.conf.sample       Mon Aug  4 15:34:04 2003
@@ -3,3 +3,4 @@
 ;dbname=asteriskcdrdb
 ;password=password
 ;user=asteriskcdruser
+;socket=/var/run/mysqld/mysqld.sock


--- cdr/cdr_mysql.c.orig        Thu Jul 24 19:45:32 2003
+++ cdr/cdr_mysql.c     Mon Aug  4 15:32:21 2003
@@ -94,7 +94,7 @@
   struct ast_config *cfg;
   struct ast_variable *var;

-  char *hostname, *dbname, *dbuser, *password;
+  char *hostname, *dbname, *dbuser, *password, *unix_socket;

   cfg = ast_load(config);
   if (!cfg) {
@@ -112,15 +112,16 @@
   dbname = ast_variable_retrieve(cfg,"global","dbname");
   dbuser = ast_variable_retrieve(cfg,"global","user");
   password = ast_variable_retrieve(cfg,"global","password");
+  unix_socket = ast_variable_retrieve(cfg,"global","socket");
   ast_log(LOG_DEBUG,"cdr_mysql: got hostname of %s\n",hostname);
   ast_log(LOG_DEBUG,"cdr_mysql: got user of %s\n",dbuser);
   ast_log(LOG_DEBUG,"cdr_mysql: got dbname of %s\n",dbname);
   ast_log(LOG_DEBUG,"cdr_mysql: got password of %s\n",password);
+  ast_log(LOG_DEBUG,"cdr_mysql: got unix_socket of %s\n",unix_socket);

   if (hostname == NULL)
     {
-      ast_log(LOG_ERROR,"Database server hostname not specified.\n");
-      return -1;
+      ast_log(LOG_DEBUG,"Database server hostname not specified.\n");
     }
   if (dbuser == NULL)
     {
@@ -137,11 +138,19 @@
       ast_log(LOG_ERROR,"Database password not specified.\n");
       return -1;
     }
-
+  if (unix_socket == NULL)
+    {
+      ast_log(LOG_DEBUG,"Database unix_socket not specified.\n");
+    }
+  if (unix_socket == NULL && hostname == NULL)
+    {
+      ast_log(LOG_ERROR,"Database hostname and unix_socket not specified.\n");
+      return -1;
+    }

   mysql = mysql_init(NULL);

-  mysql = mysql_real_connect(mysql, hostname, dbuser, password, dbname, 0, NULL, 0);
+  mysql = mysql_real_connect(mysql, hostname, dbuser, password, dbname, 0, unix_socket, 0);

   if (mysql == NULL) {
     ast_log(LOG_ERROR, "Failed to connect to mysql database.\n");
@@ -164,7 +173,7 @@
   struct ast_config *cfg;
   struct ast_variable *var;

-  char *hostname, *dbname, *password, *dbuser;
+  char *hostname, *dbname, *password, *dbuser, *unix_socket;

   mysql_close(mysql);

@@ -185,15 +194,16 @@
   dbname = ast_variable_retrieve(cfg,"global","dbname");
   dbuser = ast_variable_retrieve(cfg,"global","user");
   password = ast_variable_retrieve(cfg,"global","password");
+  unix_socket = ast_variable_retrieve(cfg,"global","socket");
   ast_log(LOG_DEBUG,"cdr_mysql: got hostname of %s\n",hostname);
   ast_log(LOG_DEBUG,"cdr_mysql: got dbname of %s\n",dbname);
   ast_log(LOG_DEBUG,"cdr_mysql: got dbuser of %s\n",dbuser);
   ast_log(LOG_DEBUG,"cdr_mysql: got password of %s\n",password);
+  ast_log(LOG_DEBUG,"cdr_mysql: got unix_socket of %s\n",unix_socket);

   if (hostname == NULL)
     {
-      ast_log(LOG_ERROR,"Database server hostname not specified.\n");
-      return -1;
+      ast_log(LOG_DEBUG,"Database server hostname not specified.\n");
     }
   if (dbname == NULL)
     {
@@ -205,16 +215,24 @@
       ast_log(LOG_ERROR,"Database dbuser not specified.\n");
       return -1;
     }
-
   if (password == NULL)
     {
       ast_log(LOG_ERROR,"Database password not specified.\n");
       return -1;
     }
+  if (unix_socket == NULL)
+    {
+      ast_log(LOG_DEBUG,"Database unix_socket not specified.\n");
+    }
+  if (unix_socket == NULL && hostname == NULL)
+    {
+      ast_log(LOG_ERROR,"Database hostname and unix_socket not specified.\n");
+      return -1;
+    }

  mysql = mysql_init(NULL);

- mysql = mysql_real_connect(mysql, hostname, dbuser, password, dbname, 0, NULL, 0);
+ mysql = mysql_real_connect(mysql, hostname, dbuser, password, dbname, 0, unix_socket, 0);

  if (mysql == NULL) {
    ast_log(LOG_ERROR, "Failed to connect to mysql database.\n");




More information about the asterisk-dev mailing list