Installing and Configuring PostgreSQL and MySQL

In the following sections we will show how to install and configure PostgreSQL and MySQL on both CentOS and Ubuntu.[139] It is recommended that you only install one database at a time while working through this section. Pick the database you are most comfortable with, as there is no wrong choice.

Installing PostgreSQL for CentOS

The following command can be used to install the PostgreSQL server and its dependencies from the console:

$ sudo yum install -y postgresql-server
Install       3 Package(s)
Upgrade       0 Package(s)

Total download size: 6.9 M
Is this ok [y/N]: y

Then start the database, which will take a few seconds to initialize for the first time:

$ sudo service postgresql start

Now head to the section called “Configuring PostgreSQL” for instructions on how to perform the initial configuration.

Installing PostgreSQL for Ubuntu

To install PostgreSQL on Ubuntu, run the following command. You will be prompted to also install any additional packages that are dependencies of the application. Press Enter to accept the list of dependencies, at which point the packages will be installed and PostgreSQL will be automatically started and initialized:

$ sudo apt-get install postgresql
...
After this operation, 19.1MB of additional disk space will be used.
Do you want to continue [Y/n]? y

Now head to the section called “Configuring PostgreSQL” for instructions on how to perform the initial configuration.

Installing MySQL for CentOS

To install MySQL on CentOS, run the following command. You will be prompted to install several dependencies. Press Enter to accept, and the MySQL server and dependency packages will be installed:

$ sudo yum install mysql-server
Install       5 Package(s)
Upgrade       0 Package(s)

Total download size: 27 M
Is this ok [y/N]: y

Then start the MySQL database by running:

$ sudo service mysqld start

Now head to the section called “Configuring MySQL” to perform the initial configuration.

Installing MySQL for Ubuntu

To install MySQL on Ubuntu, run the following command. You will be prompted to install several dependencies. Press Enter to accept, and the MySQL server and its dependency packages will be installed:

$ sudo apt-get install mysql-server
Need to get 24.0MB of archives.
After this operation, 60.6MB of additional disk space will be used.
Do you want to continue [Y/n]? y

During the installation, you will be placed into a configuration wizard to help you through the initial configuration of the database. You will be prompted to enter a new password for the root user. Type in a strong password and press Enter. You will then be asked to confirm the password. Type your strong password again, followed by Enter. You will then be returned to the console, where the installation will complete. The MySQL service will now be running.

Now head to the section called “Configuring MySQL” to perform the initial configuration.

Configuring PostgreSQL

Next, create a user called asterisk, which you will use to connect to and manage the database. You can switch to the postgres user by using the following command:

$ sudo su - postgres

Note

At the time of this writing, PostgreSQL version 8.1.x is utilized on CentOS, and 8.4.x on Ubuntu.

Then run the following commands to create the asterisk user in the database and set up permissions:

$ createuser -P
Enter name of user to add: asterisk
Enter password for new user:
Enter it again:
Shall the new role be a superuser? (y/n) n
Shall the new user be allowed to create databases? (y/n) y
Shall the new user be allowed to create more new users? (y/n) n
CREATE ROLE

Now, edit the pg_hba.conf file in order to allow the asterisk user you just created to connect to the PostgreSQL server over the TCP/IP socket.

On CentOS, this file will be located at /var/lib/pgsql/data/pg_hba.conf. On Ubuntu, you will find it at /etc/postgresql/8.4/main/pg_hba.conf.

At the end of the file, replace everything below this line:

# TYPE DATABASE USER CIDR-ADDRESS METHOD

with the following:

# TYPE  DATABASE USER     CIDR-ADDRESS  METHOD
host    all      asterisk 127.0.0.1/32  md5
local   all      asterisk               trust

Now you can create the database that we will use throughout this chapter. Call the database asterisk and set the owner to your asterisk user:

$ createdb --owner=asterisk asterisk
CREATE DATABASE

You can set the password for the asterisk user like so:

$ psql -d template1
template1=# "ALTER USER asterisk WITH PASSWORD 'password'"
template1=# \q

Exit from the postgres user:

$ exit

Then restart the PostgreSQL server. On CentOS:

$ sudo service postgresql restart

Note

You need to restart the PostgreSQL service because you made changes to pg_hba.conf, not because you added a new user or changed the password.

On Ubuntu:

$ sudo /etc/init.d/postgresql-8.4 restart

Note

On Ubuntu 10.10 and newer the version number seems to be dropped, so it may just be /etc/init.d/postgresql restart.

You can verify your connection to the PostgreSQL server via TCP/IP, like so:

$ psql -h 127.0.0.1 -U asterisk 
Password for user asterisk:

Welcome to psql 8.1.21, the PostgreSQL interactive terminal.

Type:  \copyright for distribution terms
       \h for help with SQL commands
       \? for help with psql commands
       \g or terminate with semicolon to execute query
       \q to quit

asterisk=>

You’re now ready to move on to the section called “Installing and Configuring ODBC”.

Configuring MySQL

With the MySQL database now running, you should secure your installation. Conveniently, there is a script you can execute that will allow you to enter a new password[140] for the root user, along with some additional options. The script is pretty straightforward, and after entering and confirming your root password you can continue to select the defaults unless you have a specific reason not to.

Execute the following script:

$ sudo /usr/bin/mysql_secure_installation

Then connect to the database console so you can create your asterisk user and set up permissions:

$ mysql -u root -p
Enter password:

After entering the password, you will be presented with the mysql console prompt. You can now create your asterisk user by executing the CREATE USER command. The % is a wildcard indicating the asterisk user can connect from any host and is IDENTIFIED BY the password some_secret_password (which you should obviously change). Note the trailing semicolon:

mysql> CREATE USER 'asterisk'@'%' IDENTIFIED BY 'some_secret_password';
Query OK, 0 rows affected (0.00 sec)

Let’s also create the initial database you’ll use throughout this chapter:

mysql> CREATE DATABASE asterisk;
Query OK, 1 rows affected (0.00 sec)

Now that you’ve created your user and database, you need to assign permissions for the asterisk user to access the asterisk database:

mysql> GRANT ALL PRIVILEGES ON asterisk.* TO 'asterisk'@'%';
Query OK, 0 rows affected (0.00 sec)

Finally, exit from the console and verify that your permissions are correct by logging back into the asterisk database as the asterisk user:

mysql> exit
Bye
# mysql -u asterisk -p asterisk
Enter password:

mysql>

You’re now ready to move on to the section called “Installing and Configuring ODBC”.



[139] On a large, busy system you will want to install the database on a completely separate box from your Asterisk system.

[140] If you installed on Ubuntu, you will have already set the root password. You will have to enter that password while executing the script, at which point it will say you’ve already set a root password, so you don’t need to change it.