Skip to content

Instantly share code, notes, and snippets.

@shahriarhasib
Last active January 29, 2025 20:08
Show Gist options
  • Save shahriarhasib/0f7b810b2874d31db105faee679d6b7c to your computer and use it in GitHub Desktop.
Save shahriarhasib/0f7b810b2874d31db105faee679d6b7c to your computer and use it in GitHub Desktop.
Asterisk CDR in database | via ODBC | MySQL
## Configuring Asterisk CDR to update into database with MySQL via Open Database Connectivity (ODBC)
## Resources:
## Ubuntu 24.04 LTS, Asterisk 20.6.0
## Last update: 2024.08.21
##At once to install:
apt install mysql-server php libapache2-mod-php php-mysql php-curl php-gd php-json php-zip apache2 phpmyadmin asterisk-mysql
# Response 'No' for asterisk files replacement during these installation, this will preserve your current asterisk configurations.
#Loging to web:
http://ip/phpmyadmin
#Username: phpmyadmin
#password: As set during installation of phpmyadmin
#Login to mysql console:
mysql -u root -p
#Login without password or with password if set
#Create a database for CDR:
#sql:
CREATE DATABASE asteriskcdrdb;
#Create a MySQL user and grant privileges:
#sql:
CREATE USER 'asteriskuser'@'localhost' IDENTIFIED BY '7AEmMN76dADwn2#UqyY3';
GRANT ALL PRIVILEGES ON asteriskcdrdb.* TO 'asteriskuser'@'localhost';
GRANT ALL PRIVILEGES ON asteriskcdrdb.* TO 'phpmyadmin'@'localhost';
FLUSH PRIVILEGES;
#Switch to the new database:
#sql:
USE asteriskcdrdb;
#Create the cdr table:
#Ref: https://docs.asterisk.org/Configuration/Reporting/Call-Detail-Records-CDR/CDR-Specification/#fields
#sql:
CREATE TABLE cdr (
calldate datetime NOT NULL default CURRENT_TIMESTAMP,
clid varchar(80) NOT NULL default '',
src varchar(80) NOT NULL default '',
dst varchar(80) NOT NULL default '',
dcontext varchar(80) NOT NULL default '',
channel varchar(80) NOT NULL default ste'',
dstchannel varchar(80) NOT NULL default '',
lastapp varchar(80) NOT NULL default '',
lastdata varchar(80) NOT NULL default '',
duration int(11) NOT NULL default '0',
billsec int(11) NOT NULL default '0',
disposition varchar(45) NOT NULL default '',
amaflags int(11) NOT NULL default '0',
accountcode varchar(20) NOT NULL default '',
uniqueid varchar(32) NOT NULL default '',
userfield varchar(255) NOT NULL default ''
);
#Exit MySQL:
#sql:
quit;
# Install development files for UnixODBC, a driver manager for accessing SQL databases
apt-get install unixodbc-dev
# Install UnixODBC, which provides a uniform interface for ODBC drivers
apt install unixodbc
# Install UnixODBC binary utilities like 'isql', useful for testing ODBC connections
apt install unixodbc-bin
# Install GTK+ 3.0, a multi-platform toolkit for creating graphical user interfaces, required by some applications
apt -y install libgtk-3-0
# Install GTK+ 2.0, an older version of GTK+ that might be required by legacy applications
apt -y install libgtk2.0-0
#Library
#Download files from https://downloads.mysql.com/archives/ according to your operating system.
# 1. mysql-community-client-plugins_* (https://downloads.mysql.com/archives/community/),
(e.g. For ubuntu22: https://cdn.mysql.com/archives/mysql-9.1/mysql-community-client-plugins_9.1.0-1ubuntu22.04_amd64.deb)
# 2. mysql-connector-odbc (https://downloads.mysql.com/archives/c-odbc/)
# 3. mysql-connector-odbc-setup (https://downloads.mysql.com/archives/c-odbc/)
#Change directory where you want to download
cd /usr/local/src/
#Download example for Ubuntu 24.04
wget https://cdn.mysql.com/archives/mysql-9.1/mysql-community-client-plugins_9.1.0-1ubuntu24.04_amd64.deb
wget https://cdn.mysql.com/archives/mysql-connector-odbc-9.1/mysql-connector-odbc_9.1.0-1ubuntu24.04_amd64.deb
wget https://cdn.mysql.com/archives/mysql-connector-odbc-9.1/mysql-connector-odbc-setup_9.1.0-1ubuntu24.04_amd64.deb
#Install downloaded package
dpkg -i mysql-community-client-plugins_9.1.0-1ubuntu24.04_amd64.deb
dpkg -i mysql-connector-odbc_9.1.0-1ubuntu24.04_amd64.deb
dpkg -i mysql-connector-odbc-setup_9.1.0-1ubuntu24.04_amd64.deb
#apt install will check the dependencies of the MySQL packages. Even if the core files were installed by dpkg -i, there might be other libraries or configuration steps required. apt will automatically install those missing pieces.
apt update
apt install mysql-community-client-plugins
apt install mysql-connector-odbc
#Verify Installation
odbcinst -q -d -n "MySQL"
#After the driver is installed, you can proceed with configuring ODBC and Asterisk:
#This lib need to be downloaded from MYSQL SITE according to OS: https://dev.mysql.com/downloads/connector/odbc/
vi /etc/odbcinst.ini
[MySQL]
Description = ODBC for MySQL
Driver = /usr/lib/x86_64-linux-gnu/odbc/libmyodbc9a.so
Setup = /usr/lib/x86_64-linux-gnu/odbc/libmyodbc9S.so
FileUsage = 1
#Configure DSN in /etc/odbc.ini:
vi /etc/odbc.ini
[asterisk-connector]
Description = MySQL connection to ‘asterisk’ database
Driver = MySQL
Database = asteriskcdrdb
Server = localhost
UserName = asteriskuser
Password = 7AEmMN76dADwn2#UqyY3
Port = 3306
Socket = /var/run/mysqld/mysqld.sock
vi /etc/asterisk/cdr_adaptive_odbc.conf
[asterisk-connector]
connection=asterisk-connector
table=cdr
alias start => calldate
vi /etc/asterisk/res_odbc.conf
[asterisk-connector]
enabled => yes
dsn => asterisk-connector
username=asteriskuser
password=7AEmMN76dADwn2#UqyY3
pre-connect => yes
#Test the ODBC Connection
isql -v asterisk-connector asteriskuser 7AEmMN76dADwn2#UqyY3
#Restart Asterisk
service asterisk restart
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment