开发者

Possible for PHP app built on top of codeigniter to connect to a MySQL AND a mongoDB database at the same time?

开发者 https://www.devze.com 2023-04-07 01:46 出处:网络
I have a web application built in codeigniter and hosted with cloudcontrol. I use a normal MySQL database for all of my data persistance, and now I want to use a mongodb database in addition to the My

I have a web application built in codeigniter and hosted with cloudcontrol. I use a normal MySQL database for all of my data persistance, and now I want to use a mongodb database in addition to the MySQL database.

I want to use mongodb as a job queue to persist messages between my workers and my application servers. I've gotten the inspiration to do this from this tutorial: http://www.captaincodeman.com/2011/05/28/simple-service-bus-message-queue-mongodb/

  1. Is this possible (using two different types of databases simultaniously -- with/without hacking codeigniter, I would assume it is)

  2. Any tips for accomplishing this?

---- EDIT ----

I've included this library in my CI project: https://github.com/alexbilbie/codeigniter-mongodb-library

And when I attempt to load it via: $this->load->library('mongo_db'); I run into this:

Possible for PHP app built on top of codeigniter to connect to a MySQL AND a mongoDB database at the same time?

开发者_JAVA百科

I'm not sure how I can get mysql and mongodb working together...

---- EDIT ----

Nevermind my above question, I improperly set up the config file and confused the error...


Yes this is possible, out of the box.

You need to define two groups in your config, one for mysql and one for mongodb. In your application you can then load in these databases by group name.

In your confugration.php:

$db['mysql']['hostname'] = "localhost";
$db['mysql']['username'] = "root";
$db['mysql']['password'] = "";
$db['mysql']['dbdriver'] = "mysql";
//... (full config omitted for brevity)

$db['mongodb']['hostname'] = "localhost";
$db['mongodb']['username'] = "root";
$db['mongodb']['password'] = "";
$db['mongodb']['dbdriver'] = "mongodb";
//... (full config omitted for brevity)

And then you would load in your databases as follows:

$mysqlDB = $this->load->database('mysql', TRUE);
$mongoDB = $this->load->database('mongodb', TRUE); 

Take a look at the user guide on how to connect to multiple databases and on how to specify configuration groups.

0

精彩评论

暂无评论...
验证码 换一张
取 消