How to configure multiple database in elgg

Hello Elgger,

I came across the problem where i need to run two databases in ELGG. First database is default ELGG database where as second database into which i want to do insert, update and delete operation for a item. and each item belongs to user.

How to connect to secondary database? How to use resource of secondary database to make a operation on it?

Please share your thought for it.

Many thanks in advance!

Mitesh

 

  • mysql db splits ? you'll need to be *more than fairly comfortable working with mysql at a pretty techie internals level to replicate db`s and then allow the elgg builtin logic to affect the split read/updates. it's not for the faint-hearted !

    w.r.t

    /*
     * Standard configuration
     *
     * You will use the same database connection for reads and writes.
     * This is the easiest configuration, and will suit 99.99% of setups. However, if you're
     * running a really popular site, you'll probably want to spread out your database connections
     * and implement database replication.  That's beyond the scope of this configuration file
     * to explain, but if you know you need it, skip past this section.
     */

  • //Connect to another database
    $r2 =mysql_connect( $host,$user, $pass) OR die(mysql_error());
    mysql_select_db($db,$r2) OR die(mysql_error()); 

    //Do some sql
    $sql = 'INSERT INTO ...';
    $res=mysql_query($sql,$r2);

    //get back to elgg database
    mysql_select_db($GLOBALS['CONFIG']->dbname) OR die(mysql_error());

  • I would recommend a more modern API like PDO or at least Mysqli.

  • @DhrupDeScoop: I didn't get you. Could you please share code if you have any?

    @tobi: This what i have done at the moment. but i am looking for a code which does not use core php mysql connection code.

    @Steve: It would better if you please share link if you have any for a reference.

    Mit

  • Dhrup posted some text found in the engine/settings.php file. It's not relevant to what you're trying to do.

    You need to start a new MySQL connection and as Steve says, it's better to use PDO than the deprecated mysql_*() functions. You'll do something like this to connect to your database:

    $db = new PDO('mysql:host=localhost;dbname=YOUR_DATABASE', 'user', 'password');

    $r = $db->query('SELECT * FROM your_table');

    foreach ($r as $row) {

        var_dump($row);

    }

    A great resource is php.net/pdo

  • @Mitesh:

    What exactly are you trying to achieve ?

    You said before " .. run two databases in ELGG. First database is default ELGG database where as second database.." 

    Are these 2 databases both Elg DBs ? or different for Elgg ? The answers for you will depend on your aims.. And therefore -- maybe you're looking to use MySql replication which Elgg has catered for to some degree; or if you want to simply access anither Db - solution will be different.

    So -- what are the end results you seek ?

     

  • How to add a new tabel to the existing database with all the features like comment, like and download

  • @Brett: This is what exactly i want to do. And it worked for me. Thanks a lot!

    @Dhrup: Both are different databases. I have used PDO now and it working very fine as per my need.