Upgrade 3CX to v18 and get it hosted free!

MySQL Failover in Dialplan

Author image

Synopsis

Addressing MySQL Database failover in the Asterisk Dialplan

Introduction

HA installations require failover in event of a database failure. Included here are some dial plan configurations that can facilitate switchover inside of the dialplan.

Extensions.conf

[globals]
DBDefaultHost=192.168.170.21
DBBackupHost=192.168.170.22
DBCurrentHost=192.168.170.21
DBname=astdb
DBuser=root
DBpass=stellance

[macro-db-connect]
exten => s,1,MYSQL(Connect connid ${DBCurrentHost} ${DBuser} ${DBpass} ${DBname})
exten => s,n,GotoIf($[“${connid}” = “”]?changedb)
exten => s,n,MacroExit
exten => s,n(changedb),NoOp(“Switching DB Servers”)
exten => s,n,GotoIf($[“${DBCurrentHost}” = “${DBDefaultHost}”]?backup)
exten => s,n,Set(DBCurrentHost=${DBDefaultHost}|g)
exten => s,n,Macro(write-cdr_mysql)
exten => s,n,MacroExit
exten => s,n(backup),Set(DBCurrentHost=${DBBackupHost}|g)
exten => s,n,Macro(write-cdr_mysql)
exten => s,n,MacroExit

[macro-write-cdr_mysql]
exten => s,1,System(/bin/echo “‘[global]'” > /etc/asterisk/cdr_mysql.conf)
exten => s,n,System(/bin/echo “‘hostname=${DBCurrentHost}'” >> /etc/asterisk/cdr_mysql.conf)
exten => s,n,System(/bin/echo “‘dbname=astdb'” >> /etc/asterisk/cdr_mysql.conf)
exten => s,n,System(/bin/echo “‘table=cdr'” >> /etc/asterisk/cdr_mysql.conf)
exten => s,n,System(/bin/echo “‘password=stellance'” >> /etc/asterisk/cdr_mysql.conf)
exten => s,n,System(/bin/echo “‘user=root'” >> /etc/asterisk/cdr_mysql.conf)
exten => s,n,System(/bin/echo “‘port=3306′” >> /etc/asterisk/cdr_mysql.conf)
exten => s,n,System(/bin/echo “‘sock=/etc/asterisk/mysql.sock'” >> /etc/asterisk/cdr_mysql.conf)
exten => s,n,System(/bin/echo “‘userfield=1′” >> /etc/asterisk/cdr_mysql.conf)
exten => s,n,Wait(1)
exten => s,n,System(asterisk -r -x “reload cdr_addon_mysql.so”)

 

Improvements and Explanations

The initial connection will still fail, however the connection macro can be modified to try to connect to the backup database. You will have to be wary of loops.

 

See also


Article Reviews

Write a Review

Your email address will not be published. Required fields are marked *

Required Field. Minimum 5 characters.

Required Field. Minimum 5 characters, maximum 50.

Required field.There is an error with this field.

Required Field.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

There are no reviews for this article. Be the first one to write a review.

Related Posts:

Get 3CX - Absolutely Free!
Link up your team and customers Phone System Live Chat Video Conferencing

Hosted or Self-managed. Up to 10 users free forever. No credit card. Try risk free.

3CX
A 3CX Account with that email already exists. You will be redirected to the Customer Portal to sign in or reset your password if you've forgotten it.