# Databases

Ubuntu provides two popular database servers. They are:

- MySQL

- PostgreSQL

They are available in the main repository. This section explains how to install and configure these database servers.

## MySQL

MySQL is a fast, multi-threaded, multi-user, and robust SQL database server. It is intended for mission-critical, heavy-load production systems as well as for embedding into mass-deployed software.

### Installation

To install MySQL, run the following command from a terminal prompt:

    sudo apt-get install mysql-server

During the installation process you will be prompted to enter a password for the MySQL root user.

Once the installation is complete, the MySQL server should be started automatically. You can run the following command from a terminal prompt to check whether the MySQL server is running:

    sudo netstat -tap | grep mysql

When you run this command, you should see the following line or something similar:

    tcp        0      0 localhost:mysql         *:*                     LISTEN      2556/mysqld

If the server is not running correctly, you can type the following command to start it:

    sudo /etc/init.d/mysql restart

### Configuration

You can edit the `/etc/mysql/my.cnf` file to configure the basic settings -- log file, port number, etc. For example, to configure MySQL to listen for connections from network hosts, change the bind-address directive to the server's IP address:

    bind-address            = 192.168.0.5

Replace 192.168.0.5 with the appropriate address.

After making a change to `/etc/mysql/my.cnf` the mysql daemon will need to be restarted:

    sudo /etc/init.d/mysql restart

If you would like to change the MySQL root password, in a terminal enter:

    sudo dpkg-reconfigure mysql-server-5.1

The mysql daemon will be stopped, and you will be prompted to enter a new password.

### Resources

- See the MySQL Home Page for more information.

- The MySQL Handbook is also available in the mysql-doc-5.0 package. To install the package enter the following in a terminal:

      sudo apt-get install mysql-doc-5.0

  The documentation is in HTML format, to view them enter `file:///usr/share/doc/mysql-doc-5.0/refman-5.0-en.html-chapter/index.html` in your browser's address bar.

- For general SQL information see Using SQL Special Edition by Rafe Colburn.

- The Apache MySQL PHP Ubuntu Wiki page also has useful information.

## PostgreSQL

PostgreSQL is an object-relational database system that has the features of traditional commercial database systems with enhancements to be found in next-generation DBMS systems.

### Installation

To install PostgreSQL, run the following command in the command prompt:

    sudo apt-get install postgresql

Once the installation is complete, you should configure the PostgreSQL server based on your needs, although the default configuration is viable.

### Configuration

By default, connection via TCP/IP is disabled. PostgreSQL supports multiple client authentication methods. By default, IDENT authentication method is used for postgres and local users. Please refer the PostgreSQL Administrator's Guide.

The following discussion assumes that you wish to enable TCP/IP connections and use the MD5 method for client authentication. PostgreSQL configuration files are stored in the `/etc/postgresql/<version>/main` directory. For example, if you install PostgreSQL 8.4, the configuration files are stored in the `/etc/postgresql/8.4/main` directory.

To configure ident authentication, add entries to the `/etc/postgresql/8.4/main/pg_ident.conf` file.

To enable TCP/IP connections, edit the file `/etc/postgresql/8.4/main/postgresql.conf`

Locate the line #listen_addresses = 'localhost' and change it to:

    listen_addresses = 'localhost'

To allow other computers to connect to your PostgreSQL server replace 'localhost' with the IP Address of your server.

You may also edit all other parameters, if you know what you are doing! For details, refer to the configuration file or to the PostgreSQL documentation.

Now that we can connect to our PostgreSQL server, the next step is to set a password for the postgres user. Run the following command at a terminal prompt to connect to the default PostgreSQL template database:

    sudo -u postgres psql template1

The above command connects to PostgreSQL database template1 as user postgres. Once you connect to the PostgreSQL server, you will be at a SQL prompt. You can run the following SQL command at the psql prompt to configure the password for the user postgres.

    ALTER USER postgres with encrypted password 'your_password';

After configuring the password, edit the file `/etc/postgresql/8.4/main/pg_hba.conf` to use MD5 authentication with the postgres user:

    local   all         postgres                          md5

Finally, you should restart the PostgreSQL service to initialize the new configuration. From a terminal prompt enter the following to restart PostgreSQL:

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

The above configuration is not complete by any means. Please refer the PostgreSQL Administrator's Guide to configure more parameters.

### Resources

- As mentioned above the Administrator's Guide is an excellent resource. The guide is also available in the postgresql-doc-8.4 package. Execute the following in a terminal to install the package:

      sudo apt-get install postgresql-doc-8.4

  To view the guide enter `file:///usr/share/doc/postgresql-doc-8.4/html/index.html` into the address bar of your browser.

- For general SQL information see Using SQL Special Edition by Rafe Colburn.

- Also, see the PostgreSQL Ubuntu Wiki page for more information.