Asterisk - Realtime Installation Guide
By default, you rely heavily on Asterisk .conf file for creating your dialplans, iax and sip users and peers, as well as voicemail users. And after making changes to the configuration files, you have to reload Asterisk to apply them. However, the guys at Asterisk have made it easier. With Asterisk Realtime, you can add new users and modify your dialplans on the fly. Everything is neatly stored in a database, and loaded on the fly. So in this article, we will discuss how to get started so that you can make use of Asterisk Realtime.
You are going to begin by installing Asterisk. You can check out my quick install guide here.
Once you have that all complete, you will want to make sure that MYSQL is running. So we can start it by issuing the following command from ssh:
/etc/init.d/mysqld start
We need to create our Asterisk database, and setup our tables now, from the mysql command line:
CREATE DATABASE asterisk;
GRANT ALL PRIVILEGES ON asterisk.* TO 'asterisk'@'localhost' IDENTIFIED BY 'yourpassword' WITH GRANT OPTION;
A table for your sip users and peers:
CREATE TABLE `asterisk`.`sip_buddies` (
`id` int(11) NOT NULL auto_increment,
`name` varchar(80) NOT NULL default '',
`accountcode` varchar(20) default NULL,
`amaflags` varchar(7) default NULL,
`callgroup` varchar(10) default NULL,
`callerid` varchar(80) default NULL,
`canreinvite` char(3) default 'yes',
`context` varchar(80) default NULL,
`defaultip` varchar(15) default NULL,
`dtmfmode` varchar(7) default NULL,
`fromuser` varchar(80) default NULL,
`fromdomain` varchar(80) default NULL,
`fullcontact` varchar(80) default NULL,
`host` varchar(31) NOT NULL default '',
`insecure` varchar(20) default NULL,
`language` char(2) default NULL,
`mailbox` varchar(50) default NULL,
`md5secret` varchar(80) default NULL,
`nat` varchar(5) NOT NULL default 'no',
`deny` varchar(95) default NULL,
`permit` varchar(95) default NULL,
`mask` varchar(95) default NULL,
`pickupgroup` varchar(10) default NULL,
`port` varchar(5) NOT NULL default '',
`qualify` char(3) default NULL,
`restrictcid` char(1) default NULL,
`rtptimeout` char(3) default NULL,
`rtpholdtimeout` char(3) default NULL,
`secret` varchar(80) default NULL,
`type` varchar(6) NOT NULL default 'friend',
`username` varchar(80) NOT NULL default '',
`disallow` varchar(100) default 'all',
`allow` varchar(100) default 'g729;ilbc;gsm;ulaw;alaw',
`musiconhold` varchar(100) default NULL,
`regseconds` int(11) NOT NULL default '0',
`ipaddr` varchar(15) NOT NULL default '',
`regexten` varchar(80) NOT NULL default '',
`cancallforward` char(3) default 'yes',
PRIMARY KEY (`id`),
UNIQUE KEY `name` (`name`),
KEY `name_2` (`name`)
) ENGINE=MyISAM;
And a table for extensions - the backbone of Asterisk:
CREATE TABLE `asterisk`.`extensions` (
`id` int(11) NOT NULL auto_increment,
`context` varchar(20) NOT NULL default '',
`exten` varchar(20) NOT NULL default '',
`priority` tinyint(4) NOT NULL default '0',
`app` varchar(20) NOT NULL default '',
`appdata` varchar(128) NOT NULL default '',
PRIMARY KEY (`context`,`exten`,`priority`),
KEY `id` (`id`)
) ENGINE=MyISAM;
And a table for the voicemail users:
CREATE TABLE `asterisk`.`voicemessages` (
`id` int(11) NOT NULL auto_increment,
`msgnum` int(11) NOT NULL default '0',
`dir` varchar(80) default '',
`context` varchar(80) default '',
`macrocontext` varchar(80) default '',
`callerid` varchar(40) default '',
`origtime` varchar(40) default '',
`duration` varchar(20) default '',
`mailboxuser` varchar(80) default '',
`mailboxcontext` varchar(80) default '',
`recording` longblob,
PRIMARY KEY (`id`),
KEY `dir` (`dir`)
) ENGINE=MyISAM;
And the table for queues:
CREATE TABLE`asterisk`.`queues` ( `name` varchar(128) NOT NULL, `musiconhold` varchar(128) default NULL, `announce` varchar(128) default NULL, `context` varchar(128) default NULL, `timeout` int(11) default NULL, `monitor_type` varchar(50) NOT NULL, `monitor_format` varchar(128) default NULL, `queue_youarenext` varchar(128) default NULL, `queue_thereare` varchar(128) default NULL, `queue_callswaiting` varchar(128) default NULL, `queue_holdtime` varchar(128) default NULL, `queue_minutes` varchar(128) default NULL, `queue_seconds` varchar(128) default NULL, `queue_lessthan` varchar(128) default NULL, `queue_thankyou` varchar(128) default NULL, `queue_reporthold` varchar(128) default NULL, `announce_frequency` int(11) default NULL, `announce_round_seconds` int(11) default NULL, `announce_holdtime` varchar(128) default NULL, `retry` int(11) default NULL, `wrapuptime` int(11) default NULL, `maxlen` int(11) default NULL, `servicelevel` int(11) default NULL, `strategy` varchar(128) default NULL, `joinempty` varchar(128) default NULL, `leavewhenempty` varchar(128) default NULL, `eventmemberstatus` varchar(4) default NULL, `eventwhencalled` varchar(4) default NULL, `reportholdtime` tinyint(1) default NULL, `memberdelay` int(11) default NULL, `weight` int(11) default NULL, `timeoutrestart` tinyint(1) default NULL, `periodic_announce` varchar(50) default NULL, `periodic_announce_frequency` int(11) default NULL, `ringinuse` tinyint(1) default NULL, PRIMARY KEY (`name`) ) ENGINE=MyISAM
And the queue members:
CREATE TABLE`asterisk`.`queue_members` ( `uniqueid` int(10) unsigned NOT NULL auto_increment, `membername` varchar(40) default NULL, `queue_name` varchar(128) default NULL, `interface` varchar(128) default NULL, `penalty` int(11) default NULL, `paused` tinyint(1) default NULL, PRIMARY KEY (`uniqueid`), UNIQUE KEY `queue_interface` (`queue_name`,`interface`) ) ENGINE=MyISAM
So now we need to make the needed configuration changes so Asterisk knows to use the database. Assuming that you followed my installation guide (or that you have asterisk-addons installed) we need to copy the res_mysql.conf file, so:
cd /usr/src/asterisk-addons/configs/
cp res_mysql.conf.sample /etc/asterisk/res_mysql.conf
And we can edit that new /etc/asterisk/res_mysql.conf file to show your database settings:
[general]
dbhost = localhost
dbname = asterisk
dbuser = asterisk
dbpass = yourpassword
dbport = 3306
dbsock = /var/lib/mysql/mysql.sock
Next we’ll edit the /etc/asterisk/extconfig.conf to show as follows:
[settings]
sipusers => mysql,asterisk,sip_buddies
sippeers => mysql,asterisk,sip_buddies
extensions => mysql,asterisk,extensions
voicemail => mysql,asterisk,voicemessages
queues => mysql,asterisk,queues
queue_members => mysql,asterisk,queue_members
For extensions to be loaded of the database, we need to edit the /etc/asterisk/extensions.conf file as well. Each context that will be getting their information from the database, will need the following: switch => Realtime/@extensions - For example:
[incoming]
switch => Realtime/@extensions
Restart Asterisk and you’re all set. Now you just need to load your data into the database. Let’s have a look at an example of that. So here’s my extension:
exten => 105,1,Dial(SIP/105)
Here’s my database insert for the above extension:
INSERT into extensions (id, context, exten, priority, app, appdata)
VALUES ('','incoming','105','1','Dial','SIP/105');
And for an example of my sip user, I had the following in my sip.conf at one time:
[105]
type=friend
context=internal
callerid=105
host=dynamic
secret=password
canreinvite=no
insecure=port,invite
allow=all
nat=yes
Here is my insert code for the database:
INSERT into sip_buddies (id, name, callerid, context, canreinvite, insecure, type, host, secret, allow, nat)
VALUES ('','105','Robert','incoming','no','port,invite','friend','dynamic','bob123','all','yes');
I hope this example works well for you. I got it working fairly easy. It is important to note, that I haven’t been able to successfully setup the connection I have with my carrier in the realtime database. So I have to define those in my sip.conf or iax.conf - which isn’t a big deal. Another note is that any general conf settings, I always set those within the conf file, instead of within the realtime database.

Best example I’ve seen for real time asterisk!!! Thank you!!!
hi
where did you write the insert code to the database?I mean how tables full?
My tutorial outlines how to create the tables and setup Asterisk for
realtime use. However, what you put in the tables is entirely based on
your configuration. If you know some php mysql, you can write some php
scripts to make inputting new data to the database easier. Otherwise you
will have to use mysql CLi, or an application like phpMyAdmin.
This is a good idea though. Perhaps I will write an article shortly about
a quick php script to insert data into the realtime database tables.
Thanks for your comments and interest in our site.
When you create the table for sip_buddies change the insecure field from varchar(4) to varchar(20) so it can accept values for Asterisk 1.4 and later (e.g. port - invite - port,invite)
Thanks Trevor for this change. You are right. With v1.4, insecure field accepts new values. I have updated the above tutorial.
Well written tutorial but using Asterisk Real Time where to put the DID in the MySQL?
Joel, you would simply setup your DID and dialplan in the extensions table. For example, so my DID was: 1234561212, I might put something like below in there.
Standard conf setup:
exten => 1234561212,1,Answer()
exten => 1234561212,2,Playback(greeting-file)
exten => 1234561212,3,Playback(good-bye)
exten => 1234561212,4,Hangup()
I would enter it into my extensions table like so:
INSERT into sip_buddies (id, context, exten, priority, app, appdata)
VALUES (”,’incoming’,'1234561212′,’1′,’Answer’,”), (”,’incoming’,'1234561212′,’2′,’Playback’,'greeting-file’), (”,’incoming’,'1234561212′,’3′,’Playback’,'good-bye’), (”,’incoming’,'1234561212′,’4′,’Hangup’,”);
Its simply a great post. It has almost solved my problem. For two days now, I have been scanning the web, as I am a PHP/MySQL coder and I want to run a website to assign SIPs to my visitors through a Asterisk server
I can now easily write the web application which will insert SIPs into the mysql database.
These SIP will be instantly usable? Will I need to restart Asterisk after every SIP created in mysql?
Thanks
Ashwani
Ashwani,
If you have followed my instructions outlined above, you are running Asterisk realtime.
That’s one of the greatest things with Asterisk Realtime, you don’t have to reload Asterisk for your new users to be usable. Isn’t Asterisk Realtime so much better?
Good Work
Hi! I want to congratulate you for this great tutorial…
I’m trying now to configure a queue using ARA, but I don’t know why Asterisk doesn’t retrieve the queue data from the database instead of the queues.conf file. I modified extconfig.conf but it isn’t working. Can you please post somethign related to Queues Real Time?
Horacio,
I have made adjustments to this article to include Queue and QueueMembers via Asterisk Realtime.
I hope this is helpful to you too!
[...] In addition to the above, you can always port voicemail.conf to MySQL via Asterisk Realtime. You can learn more Asterisk Realtime here. [...]
Thank you very much for adjusting your article!
I followed the steps described here but I still can’t get queues real time working. Please, can you post some examples of what data goes into the “queues” and queue_members” tables and a way to test it? And one more question: where are stored the passwords of the queue agents?
Again, thanks a lot!
Best regards
Very nice tutorial. I must agree, this is the best tutorial on asterisk real-time. Good work. Thanks!
Excellent work. Got realtime working in notime
Excellent tutorial. Thank you for your hard work.
I do have one question however. Do you have a table layout for iaxusers, iaxpeers and sipregs?
Thank you for your help.
Hi Gabe,
For IAX, you will need to the following within /etc/asterisk/extconfig.conf:
iaxusers => mysql,asterisk,iax_buddies
iaxpeers => mysql,asterisk,iax_buddies
Here’s the database structure:
CREATE TABLE iax_buddies (
name varchar(30) primary key NOT NULL,
username varchar(30),
type varchar(6) NOT NULL,
secret varchar(50),
md5secret varchar(32),
dbsecret varchar(100),
notransfer varchar(10),
inkeys varchar(100),
outkey varchar(100),
auth varchar(100),
accountcode varchar(100),
amaflags varchar(100),
callerid varchar(100),
context varchar(100),
defaultip varchar(15),
host varchar(31) NOT NULL default ‘dynamic’,
language char(5),
mailbox varchar(50),
deny varchar(95),
permit varchar(95),
qualify varchar(4),
disallow varchar(100),
allow varchar(100),
ipaddr varchar(15),
port integer default 0,
regseconds integer default 0
);
CREATE UNIQUE INDEX iax_buddies_username_idx ON iax_buddies(username);
I haven’t tested IAX with Asterisk Realtime. I personally prefer SIP, because it’s a more universally accepted protocol. In addition, SIP can carry video and text. But that’s for another post altogether.
Agreed.
The setup I am testing is with Asterisk 1.6 with 2 servers linked via IAX trunks and I hope to use IAX in realtime as well.
Again, keep up the good work and thank you.
Hi,
Actually am using Asterisk 1.4. I want to call from one asterisk to another asterisk, but I can’t do that using this realtime.
Plz help me out from this…
Rocky,
The same rules apply. You’re simply taking your dialplans and porting them to Asterisk Realtime. Perhaps your dialplans for dialing between the two servers aren’t correct? I’ll publish an article about connecting two Asterisk servers shortly.