How to configure environments.php, database.php to use different dbs for different environments

Hello,

QUESTION:
How can I configure to use one database connection for the environment "development"
and
ANOTHER database connection for the environment "production".

=====================================
config/database.php

$active_group = 'default';
$query_builder = TRUE;

$db['default'] = array(
'dsn' => '',
'hostname' => 'server-livesystem.de',
'username' => 'username-livesystem',
'password' => 'pw-livesystem',
'database' => 'db-livesystem',
'dbdriver' => 'mysqli',
'dbprefix' => '',
'pconnect' => FALSE,
'db_debug' => (ENVIRONMENT !== 'production'),
'cache_on' => FALSE,
'cachedir' => '',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'encrypt' => FALSE,
'compress' => FALSE,
'stricton' => FALSE,
'failover' => array(),
'save_queries' => TRUE
);

$db['dev'] = array(
'dsn' => '',
'hostname' => '127.0.0.1',
'username' => 'username-development',
'password' => '',
'database' => 'db-development',
'dbdriver' => 'mysqli',
'dbprefix' => '',
'pconnect' => FALSE,
'db_debug' => (ENVIRONMENT !== 'development'),
....

======================================
config/environments.php
(https://docs.getfuelcms.com/general/environments)

$environments = array(
'development' => array(??????),
'production' => array(???????),
);

Comments

  • edited December 2020

    This may help:
    https://stackoverflow.com/questions/8268853/codeigniter-multiple-database-connections

    For FUEL, the default database connection should be relegated to the fuel tables. For models using a different database connection, there is a dsn protected property you can specify on the model to point to the other database:
    https://docs.getfuelcms.com/libraries/my_model

  • edited December 2020

    Hello admin ... thanks for your quick reply ...
    ... I KNOW howto to use different databases ... as described in the links you provided.
    ...sorry ( I was not clear enough in my initial posting )


    BUT ...
    I THOUGHT there is a way to define different ENVIRONMENTS:
    a) development-env
    b) production-env.

    because .. my development stuff is on localhost...with different dbuser and dbpassword
    BUT ... my production system is on a remote server ...

    It would be nice to define 2 environments (development and production) with different database configs ( localhost and productionserver) ...and use a switch (e.g. a parameter in one of the config files ) to define, which environment should be used....


    /fuel/application/config/environments.php

    the userguide tells not much about the usage/meaning of:

    $environments = array(
    'development' => array('localhost', '192.', '*.dev'),
    );


    /fuel/application/config/database.php

    I defined different database groups to use different databases in my modules

    $active_group = 'default';
    $query_builder = TRUE;

    $db['default'] = array(
    'dsn' => '',
    'hostname' => 'localhost',
    'username' => 'dev-user_1',
    'password' => 'dev-paswd_1',
    'database' => 'dev-db_1',
    ...);

    $db['second'] = array(
    'dsn' => '',
    'hostname' => 'localhost',
    'username' => 'dev-user_2',
    'password' => 'dev-paswd_2',
    'database' => 'dev-db_2',
    ....

    The production-system has different hostnames, username and password.

    QUESTION

    Is there a way to define environment-dependend database settings?

    At the moment my procedure is, that the development settings ( see above ) are checked-In my source-control (SVN) ... and I manually changed these settings on the production system.

    QUESTION
    I assume it is bad practice to check-in production-hostnames, -username and -password in my source-control (SVN).

  • FUEL does not have a .env file that it uses (either does CodeIgniter 3... 4 does). You could setup conditional code in your database.php file to look for a database-local.php file and include it:

    if (is_environment('production') AND file_exists(APPPATH.'/config/database-local.php')) {
         include(APPPATH.'/config/database-local.php');
    } else {
    ....
    }
    
Sign In or Register to comment.