Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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: enter image description here

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...

like image 425
Casey Flynn Avatar asked Sep 23 '11 02:09

Casey Flynn


People also ask

Can we use multiple database in CodeIgniter?

it is very easy to configure multiple databases in CodeIgniter app. you can simply add database query, join etc with multiple databases. As we know well, in today we may need to add multiple databases on our application. all then framework provides multiple database connections.

Can I use MongoDB with CodeIgniter?

To connect our CodeIgniter application to MongoDB, we'll need to install the MongoDB PHP Driver. The driver consists of two components—the extension and the library.


1 Answers

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.

like image 109
thomaux Avatar answered Sep 29 '22 05:09

thomaux