Take a look at the things that interest us.

Storing Asterisk call records in a Mysql database

Wesley Wesley 2 years

In this post, I would like to talk about the Asterisk Call Detail Records function, more commonly known as CDR. I will tell you what CDR records are, why using the CDR function is useful, and how to set the CDR function up.


  • Asterisk 16 or 18
  • Asterisk ODBC connection
  • Basic sip accounts for Asterisk
  • Mysql Server

What Are Call Detail Records?

Call Detail Records provides an administrator with access to historical data sets such as call times, durations, and numbers. This information can be used to develop other functions within the Asterisk PBX.

Configuring the Asterisk CDR for mysql

The simplest way to start is with the cdr.conf. To access the cdr.conf, enter your Asterisk console as root and use the following command.

$ cd /etc/asterisk
$ vi /cdr.conf

This will open the configuration file and documentation which can be used as a reference point. In this configuration file, we will write the following.

dsn                       = asterisk
loguniqueid               = yes
dispositionstring         = yes
usegmtime                 = no        
newcdrcolumns             = yes 

After this, we will save the configuration file and restart our Asterisk PBX server with the following command.

$ systemctl restart asterisk

The Asterisk PBX has now been set up to log call detail records. The last step is to add our cdr table to our MySQL database.

    accountcode VARCHAR(80), 
    src VARCHAR(80), 
    dst VARCHAR(80), 
    dcontext VARCHAR(80), 
    clid VARCHAR(80), 
    channel VARCHAR(80), 
    dstchannel VARCHAR(80), 
    lastapp VARCHAR(80), 
    lastdata VARCHAR(80), 
    start DATETIME, 
    answer DATETIME, 
    end DATETIME, 
    duration INTEGER, 
    billsec INTEGER, 
    disposition VARCHAR(45), 
    amaflags VARCHAR(45), 
    userfield VARCHAR(256), 
    uniqueid VARCHAR(150), 
    linkedid VARCHAR(150), 
    peeraccount VARCHAR(80), 
    sequence INTEGER

After importing the tables, you are able to test the Asterisk CDR by making a normal extensions call. After the call is finished, you'll see that a new record has been added with the call record details to your CDR table.

It is important to understand that per default, Asterisk writes the CDR records after the hangup in the dialplan.

Storing Asterisk call records in a Mysql database 2021-08-24 08:08:35

There are no comments.


Have questions about our services?

Contact our Sales team to get answers.

Contact Us