Login/Register
Installatron Server
API Documentation

Database Automation in Installatron Server

When using Installatron Server via API, databases must be created prior to invoking the API (using whatever method is available to users on your server).

NOTE: When manually creating a database it's important to remember to give the database-user full privileged access to the database. This is not always the default privilege access level.

When using Installatron Server via GUI, there are two ways to handle database creation:

  • Add database automation via 'panel.php'

    The preferred solution is to implement a PHP class (described in detail below) that Installatron Server then uses to list, create, and delete databases.

    Using this solution's default configuration, this will create a new database for each application that Installatron Server installs.

  • Keep the pre-created databases requirement

    The simpler solution is to keep the API model's approach to database management: requiring pre-created databases.

    Installatron can be informed of externally created databases using the POST /users/{id} API, and the user will then have them available, to use as the application's database, when they install an application from the GUI.

    A warning: users are able to choose databases that already have applications using them (ie. database sharing). Most applications installed by Installatron support database sharing through the use of table prefixes, however there are applications (eg. SugarCRM, Tiki Wiki) that don't support table prefixes so installing those applications into a database that already contains an application that doesn't use a table prefix is likely to cause table collisions. This is why option 1 is the preferred database creation solution.

    But if you need to use this method, option 2, then we recommend disabling any applications that don't support table prefixes to avoid long term problems.

Add Database Automation Via 'panel.php'

To use solution 1, implement these PHP methods:

public function generateDB($type)
public function createDB($name, $user, $pass, $type, $host)
public function removeDB($name, $type)

-in this file:

/usr/local/installatron/etc/panel.php

Sample Code

Here is an example implementation of these methods in panel.php, to get you started:


<?php
class i_custom extends i_panel
{
    /**
     * This method enables you to format the database name Installatron Server will expect to be created.
     *
     * @param   string  Database type (typically "mysql", can also be "mssql")
     * @return  array   (Database name, database user, database password, database host, database table prefix)
     */
    public function generateDB($type)
    {
        //@note
        // itron::$session is a PHP array referencing the selected website. If you define "_some_prefix"
        // when calling "guixfer" for the selected website it will be available in this array.
        // All custom variable names must begin with an underscore (_).
        $prefix = itron::$session["_some_prefix"]."_";

        // Maximum database name length, including prefix.
        $len_max = 16;
        $len_less_prefix = $len_max-strlen($prefix);

        //@note
        // This must generate a UNIQUE database name that can be passed to createDB to be created.
        // PHP's uniqid function uses the system microtime, and this is often good enough
        $db_name = $prefix.substr(uniqid(), 0, $len_less_prefix);

        $db_host = "localhost";
        $db_user = $db_name;//same as the db name

        $db_pass = null;//let Installatron create a randomized value.
        $db_table_prefix = null;//let Installatron manage table prefixes

        return array($db_name, $db_user, $db_pass, $db_host, $db_table_prefix);
    }

    /**
     * Create a database
     *
     * @param   string  New database name. This is typically set by generateDB.
     * @param   string  New database username. This is typically set by generateDB.
     * @param   string  New database username's password. This is typically set by generateDB.
     * @param   string  Database type (typically "mysql", can also be "mssql")
     * @param   string  New database's server. This is typically set by generateDB.
     * @return  bool    true if successful; false otherwise
     */
    public function createDB($name, $user, $pass, $type, $host)
    {
        switch($type)
        {
        case "mysql":

            //@todo
            // create the database here

            return true;
        }
        return false;
    }

    /**
     * Remove a database.
     *
     * If database usernames are tied to database names,
     * the database username can also be removed.
     *
     * @param   string  Name of database to be removed
     * @param   string  Database type
     * @return  bool    true if successful; false otherwise
     */
    public function removeDB($name, $type)
    {
        switch($type)
        {
        case "mysql":

            //@todo
            // delete the database here
            // this might be a call to your control panel's API,
            // or could be something like this:
            $db = mysql_connect("localhost", "root", "rootspass");
            mysql_query("DROP DATABASE ".mysql_real_escape_string($name, $db), $db);
            mysql_close($db);

            return true;
        }
        return false;
    }
}

With those methods performing their required tasks and returning the required information, Installatron Server is able to fully automate database handling on your server.

Don't hesitate to contact Installatron Support if you have questions.



© 2004 - 2025 Installatron LLC. All rights reserved. Privacy Policy.