Welcome Guest, Not a member yet? Register   Sign In
Dual Connection and Database Connectivity Failure

HI, I have write a code with dual connection in codeigniter. I am facing problem that it loses the database connection

Message "Unable to connect database using provided setting"

I am wondering, why is it going on ? Is there any bug or I am doing any mistake.

Database file setting is given below.

$active_group = "db_write";
$active_group = "db_read";
$active_record = TRUE;

$db['db_write']['hostname'] = "root";
$db['db_write']['username'] = "sb110";
$db['db_write']['password'] = "sb110";

$db['db_read']['hostname'] = "root";
$db['db_read']['username'] = "sb110";
$db['db_read']['password'] = "sb110";

$db['db_write']['database'] = "sb_dev";
$db['db_read']['database'] = "sb_dev";

$db['db_write']['dbdriver'] = "mysql";
$db['db_read']['dbdriver'] = "mysql";

$db['db_write']['dbprefix'] = "";
$db['db_read']['dbprefix'] = "";

$db['db_write']['pconnect'] = TRUE;
$db['db_read']['pconnect'] = TRUE;

$db['db_write']['db_debug'] = TRUE;
$db['db_read']['db_debug'] = TRUE;

$db['db_write']['cache_on'] = FALSE;
$db['db_read']['cache_on'] = FALSE;

$db['db_write']['cachedir'] = "";
$db['db_read']['cachedir'] = "";

$db['db_write']['char_set'] = "utf8";
$db['db_read']['char_set'] = "utf8";

$db['db_write']['dbcollat'] = "utf8_general_ci";
$db['db_read']['dbcollat'] = "utf8_general_ci";

$db['db_write']['dbprefix'] = "";
$db['db_read']['dbprefix'] = "";

$db['db_write']['pconnect'] = FALSE;
$db['db_read']['pconnect'] = FALSE;

$db['db_write']['db_debug'] = TRUE;
$db['db_read']['db_debug'] = TRUE;

$db['db_write']['cache_on'] = FALSE;
$db['db_read']['cache_on'] = FALSE;

$db['db_write']['cachedir'] = "";
$db['db_read']['cachedir'] = "";

This is no code, it's just the database config file.

You can only define one active group (which is the config loaded when you do $this->load->database() ), which in this case is 'db_read'.

How do you connect to both databases (in this case the two configs are identical, so it's a bit pointless right now. I assume you have other plans?).

Are you talking about this code?

$this->DBr = $this->load->database('db_read', TRUE);
$this->DBw = $this->load->database('db_write', TRUE);

$this->DBr->query('select * from table');

Nothing wrong with this code.

So if you get 'unable to connect', either your PHP installation doesn't have any mysql drivers installed, or your configuration is not correct. Are you sure the server name 'root' is ok?

actually server name is "localhost" because it is shared server. I just change it.
Site is working fine but when this problem occur, I am unable to trace out. This site have not much traffic. But I have an other site which have 20k to 25k user per day, but it is not using the dual connection. And I do not get this type of error on it

So sometimes you have this error, sometimes you don't?

You get this error when this call
// function db_connect(), in /system/database/drivers/mysql/mysql_driver.php
@mysql_connect($this->hostname, $this->username, $this->password, TRUE);

It could fail because one of the parameters is wrong (unlikely if it only happens sometimes), or because the server doesn't accept connections (for whatever reason). You could remove the @ in the command, to see the actual error MySQL throws you, or call
$this->DBr = $this->load->database(‘db_read’, TRUE);
echo $this->DBr->_error_message();
$this->DBw = $this->load->database(‘db_write’, TRUE);
echo $this->DBw->_error_message();

Thanks for this awesome solution, Let me do it and what is wrong parameters, you are talking about? Let me know about it.

hi man i have remove @ from mysql_connect() and in database.php ( db config ) file, i have change the connection type from pconnect to normal connect. I m getting this error " Too many connections".

I am closing connection in models destruct-er, method given below

function __destruct() {



Any solution?

Yes, get your hoster to up the number of connections supported, or get yourself another hoster.

This is simply the database server reprorting it has reached it's maximum number of concurrent connections. If it is your server, up the number of connections in the MySQL config, if not, complain to your hoster.

Theme © iAndrew 2016 - Forum software by © MyBB