markdown 星号实时MySQL

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了markdown 星号实时MySQL相关的知识,希望对你有一定的参考价值。

Asterisk Realtime MySQL
=======================

Requirements
------------

- unixodbc and unixodbc-dev installed
- Asterisk 12+ installed
- cdr_odbc and cdr_adaptive_odbc modules installed and selected
- pjproject installed and all necessary asterisk pjsip modules installed 
(see Install Asterisk 13 on Debian 7/8 from source)

Install MySQL and the MySQL ODBC connector
------------------------------------------

```
$ [sudo] apt-get update \
> && [sudo] apt-get upgrade \
> && [sudo] apt-get install \
>       mysql-server 
>       libmyodbc
```

*The following extra packages will be installed :*

```
- libaio1
- libdbd-mysql-perl
- libdbi-perl
- libhtml-template-perl
- libmysqlclient18
- libterm-readkey-perl
- mysql-client-5.5
- mysql-common
- mysql-server-5.5
- mysql-server-core-5.5
 ```

### Secure MySQL

```
$ [sudo] mysql_secure_installation

Change the root password? [Y/n] n

Remove anonymous users? [Y/n] Y

Disallow root login remotely? [Y/n] Y

Remove test database and access to it? [Y/n] Y

Reload privilege tables now? [Y/n] Y
```

### Connect to MySQL

```
$ [sudo] mysql -u root -p
```

### Create asterisk MySQL user

```
> CREATE USER 'asterisk'@'%' IDENTIFIED BY 'myasteriskmysqlpassword';
```

### Create asterisk MySQL daatabase

```
> CREATE DATABASE asterisk;
```

### Grant permission for the asterisk user to the asterisk database

```
> GRANT ALL PRIVILEGES ON asterisk.* TO 'asterisk'@'%';
```

### Install ODBC and the MySQL ODBC connector

```
$ [sudo] apt-get install unixodbc unixodbc-dev libmyodbc
```

*The following extra packages will be installed*

```
- autotools-dev
- libltdl-dev
- libltdl7
- libodbc1
- libtool
- odbcinst
- odbcinst1debian2
```
### Configure MySQL ODBC Driver

*To find where the librairies are :*

```
$ [sudo] updatedb \
> && locate \
>   libmyodbc.so \
>   libodbcmyS.so
```

file : `/etc/odbcinst.ini`

```
[MySQL]
Description = OBDC for MySQL
Driver      = /usr/lib/x86_64-linux-gnu/odbc/libmyodbc.so
Setup       = /usr/lib/x86_64-linux-gnu/odbc/libodbcmyS.so
FileUsage   = 1
```


### Verify that the system is able to see the driver

```
$ odbcinst -q -d
[MySQL]
```

### Configure Asterisk ODBC connector

*Find the mysql socket:*

```
$ grep -m1 'sock$' /etc/mysql/my.cnf
```

file : `/etc/odbc.ini`

```
[asterisk-connector]
Description             = asterisk MySQL connector to asterisk DB
Driver                  = MySQL
Database                = asterisk
Server                  = localhost
UID                     = asterisk
Password                = myasteriskmysqlpassword
Port                    = 3306
Socket                  = /var/run/mysqld/mysqld.sock
```

### Validate the ODBC connector

```
$ echo "select 1" | isql -v asterisk-connector
+---------------------------------------+
| Connected!                            |
|                                       |
| sql-statement                         |
| help [tablename]                      |
| quit                                  |
|                                       |
+---------------------------------------+
SQL> select 1
+---------------------+
| 1                   |
+---------------------+
| 1                   |
+---------------------+
SQLRowCount returns 1
1 rows fetched
```

### Configure res_odbc to Allow Asterisk to connect through ODBC

file : `/etc/asterisk/res_odbc.conf`

```
[asterisk]
enabled         => yes
dsn             => asterisk-connector
username        => asterisk
password        => myasteriskmysqlpassword
pooling         => no
limit           => 1
pre-connect     => yes
```

Restart Asterisk Service and connect to it
------------------------------------------

```
$ [sudo] systemctl restart asterisk.service && \
> [sudo] asterisk -rvvvvvvvvvv
```

Check ODBC
----------

```
*CLI> odbc show

ODBC DSN Settings
-----------------

  Name:   asterisk
  DSN:    asterisk-connector
    Last connection attempt: 1970-01-01 00:00:00
  Pooled: No
  Connected: Yes

*CLI> exit
```

Install Alembic
---------------

Since version 12 Asterisk uses Alembic to help manage Asterisk Realtime Database schemas

```
$ [sudo] apt-get update && \
> [sudo] apt-get install python-dev python-pip python-mysqldb && \
> [sudo] pip install alembic
```

*The following extra packages will be installed :*

```
- libexpat1-dev
- libpython-dev
- libpython2.7
- libpython2.7-dev
- python-cffi
- python-colorama
- python-cryptography
- python-distlib
- python-html5lib
- python-ndg-httpsclient
- python-openssl
- python-ply
- python-pyasn1
- python-pycparser
- python-requests
- python-setuptools
- python-urllib3
- python-wheel
- python2.7-dev
```

### Configure Alembic and use it to setup the asterisk database tables

```
$ cd /usr/src/asterisk-13.*/contrib/ast-db-manage/ && \
> [sudo] cp config.ini{.sample,} && \
> [sudo] cp cdr.ini{.sample,} && \
> [sudo] sed -i \
> -e 's/user\:pass/asterisk\:myasteriskmysqlpassword/' \
> -e 's/localhost\/cdr$/localhost\/asterisk/' \
> config.ini cdr.ini && \
> alembic -c config.ini upgrade head
```
*We move to asterisk source directory, rename the .sample files to simply .ini, change the sqlalchemy.url option in both files and launch the configuration.*

### Connect to MySQL and drop unnecessary tables

```
$ mysql -u asterisk -p -D asterisk

> DROP TABLE
-> alembic_version,
-> iaxfriends,
-> meetme,
-> musiconhold,
-> queue_members,
-> queue_rules,
-> queues,
-> voicemail;
Query OK, 0 rows affected (0.02 sec)
```

Notes :

- *The alembic_version table is used by alembic to store migration revision, we need to drop it to create the cdr table*  

### Use Alembic to setup the asterisk CDR tables

```
$ alembic -c cdr.ini upgrade head
```

### Connect to MySQL to verify the existing tables

```
$ mysql -u asterisk -p -D asterisk

> show tables;
+--------------------+
| Tables_in_asterisk |
+--------------------+
| cdr                |
| extensions         |
| ps_auths           |
| ps_endpoints       |
| ps_globals         |
| ps_registrations   |
| ps_systems         |
| sippeers           |
| static_config      |
+--------------------+
9 rows in set (0.00 sec)
```

Configure CDR Adaptive ODBC
---------------------------

Move to asterisk configuration directory, copy cdr_adaptive_odbc.conf to sample, then empty the file :

```
$ cd /etc/asterisk && \
> [sudo] cp cdr_adaptive_odbc.{conf,conf.sample} && \
> [sudo sh -c "] \
> > cdr_adaptive_odbc.conf \
> ["]
```

Edit the file:

```
[adaptive_connection]
connection      = asterisk
table           = cdr
```


Connect PJSIP Sorcery to the Realtime Database
----------------------------------------------

file : `/etc/asterisk/sorcery.conf`

Move to asterisk configuration directory, rename sorcery.conf to sample, then copy only none commented lines to a new sorcery.conf file :

```
$ cd /etc/asterisk && \
> [sudo] cp sorcery.{conf,sample} && \
> [sudo sh -c "] \
> cat sorcery.sample | grep -v ';' | sed '/^$/d' > sorcery.conf \
> ["]
```

Add the following lines to the top:

```
; Realtime PJSIP configuration wizard
[res_pjsip]
endpoint        = realtime,ps_endpoints
auth            = realtime,ps_auths
```

The items use the following nomenclature:

```
object_type = sorcery_wizard_name,wizard_arguments
```

*In our case, the sorcery_wizard_name is realtime, and the wizard_arguments are the name of the database connector to associate with our object types.*

Configure Realtime
------------------

Move to asterisk configuration directory, copy extconfig.conf to sample, then empty the file:

```
$ cd /etc/asterisk && \
> [sudo] cp extconfig.{conf,sample} && \
> [sudo sh -c "] \
> > extconfig.conf \
> ["]
```

Add the following lines:

```
[settings]
ps_endpoints            => odbc,asterisk
ps_auths                => odbc,asterisk
```

Configure Asterisk Startup
--------------------------

Move to asterisk configuration directory, rename modules.conf to sample, then copy only none commented lines to a new modules.conf file:

```
$ cd /etc/asterisk && \
> [sudo] cp modules.{conf,sample} && \
> [sudo sh -c "] \
> cat modules.sample | grep -v ';' | sed '/^$/d' > modules.conf \
> ["]
```

We need to tell Asterisk to do two things at startup :
1. Load ODBC driver early so that it's available when any other modules might need to take advantage of it.
2. Prevent the old chan_sip channel driver from loading, since we're only worried about PJSIP.

Add the following lines in the [modules] section:

```
preload         => res_odbc.so
preload         => res_config_odbc.so
noload          => chan_sip.so
```

Configure PJSIP transport
-------------------------

PJSIP transport object types will not be stored in realtime as unexpected results can occur.

Move to asterisk configuration directory, rename pjsip.conf to sample, then empty the file:

```
$ cd /etc/asterisk && \
> [sudo] cp pjsip.{conf,conf.sample} && \
> [sudo sh -c "]
> > pjsip.conf \
> ["]
```

Add the following lines:

```
[transport-udp]
type        = transport
protocol    = udp
bind        = 0.0.0.0
```

Populate endpoint with two test users
-------------------------------------

Here, we'll be populating data directly into the database using the MySQL interactive tool.

Connect to MySQL:

```
$ mysql -u asterisk -p -D asterisk
```

Execute the following queries:

```
> INSERT INTO
-> ps_auths (id, auth_type, password, username)
-> VALUES
-> (101, 'userpass', 101, 101),
-> (102, 'userpass', 102, 102);

> INSERT INTO
-> ps_endpoints
-> (id, transport, aors, auth, context, disallow, allow, direct_media)
-> VALUES
-> (101, 'transport-udp', '101', '101', 'testing', 'all', 'g722', 'no'),
-> (102, 'transport-udp', '102', '102', 'testing', 'all', 'g722', 'no');
```

Restart Asterisk Service and connect to it
------------------------------------------
```
$ [sudo] systemctl restart asterisk.service && \
> [sudo] asterisk -rvvvvvvvvvv
```

Check PJSIP endpoints
---------------------

```
*CLI> pjsip show endpoints

 Endpoint:  <Endpoint/CID.....................................>  <State.....>  <Channels.>
    I/OAuth:  <AuthId/UserName...........................................................>
        Aor:  <Aor............................................>  <MaxContact>
      Contact:  <Aor/ContactUri...............................>  <Status....>  <RTT(ms)..>
  Transport:  <TransportId........>  <Type>  <cos>  <tos>  <BindAddress..................>
   Identify:  <Identify/Endpoint.........................................................>
        Match:  <ip/cidr.........................>
    Channel:  <ChannelId......................................>  <State.....>  <Time(sec)>
        Exten: <DialedExten...........>  CLCID: <ConnectedLineCID.......>
 =========================================================================================

 Endpoint:  101                                                  Unavailable   0 of inf
     InAuth:  101/101
        Aor:  101                                                1
  Transport:  transport-udp             udp      0      0  0.0.0.0:5060

 Endpoint:  102                                                  Unavailable   0 of inf
     InAuth:  102/102
        Aor:  102                                                1
  Transport:  transport-udp             udp      0      0  0.0.0.0:5060

```

Finally we're gonna create the static Realtime table and add the rest of the configuration needed there

```
$ mysql -u asterisk -p -D asterisk
> CREATE TABLE `static_config` (
-> `id` int(11) NOT NULL auto_increment,
-> `cat_metric` int(11) NOT NULL default '0',
-> `var_metric` int(11) NOT NULL default '0',
-> `commented` int(11) NOT NULL default '0',
-> `filename` varchar(128) NOT NULL default '',
-> `category` varchar(128) NOT NULL default 'default',
-> `var_name` varchar(128) NOT NULL default '',
-> `var_val` varchar(128) NOT NULL default '',
-> PRIMARY KEY (`id`),
-> KEY `filename_comment` (`filename`,`commented`)
-> ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
Query OK, 0 rows affected (0.00 sec)

```

The easiest way to get existing .conf files into the database is by using the perl script ast2sql

Now register two SIP phones using the 101/101 and 102/102 credentials, and make a call.

以上是关于markdown 星号实时MySQL的主要内容,如果未能解决你的问题,请参考以下文章

MarkDown基础学习

MarkDown入门

Markdown学习

Markdown学习

Markdown语法,写博客

Markdown学习