Analysis of php Framework CodeIgniter Master Slave Database Configuration Method

  • 2021-10-11 17:51:48
  • OfStack

This paper describes the configuration method of php framework CodeIgniter master-slave database with examples. Share it for your reference, as follows:

CodeIgniter abbreviated as CI is one of the most popular php MVC frameworks. I will write a series of practical experience from the actual project use, which is different from other theoretical explanation articles, and will attach practical process and code.

This article is to configure multiple databases. The usage scenarios are cluster and distributed, and the database reads and writes are separated. Only one of the multiple master-slave standby databases is a read-write database, and the others are read-only databases.

Tools/environment:

php Development Environment
CodeIgniter

Methods/Steps:

One more database source is configured in config/database. php. default is the default localhost or IP, and writedb is a read-write database. Because the writing needs to be called remotely by IP, I configure it as:


$active_group = 'default';
$active_record = TRUE;
$db['default']['hostname'] = 'localhost';
$db['default']['username'] = 'default_username';
$db['default']['password'] = 'default_password';
$db['default']['database'] = 'default_dbname';
$db['default']['dbdriver'] = 'mysql';
$db['default']['dbprefix'] = '';
$db['default']['pconnect'] = TRUE;
$db['default']['db_debug'] = FALSE;
$db['default']['cache_on'] = FALSE;
$db['default']['cachedir'] = '';
$db['default']['char_set'] = 'utf8';
$db['default']['dbcollat'] = 'utf8_general_ci';
$db['default']['swap_pre'] = '';
$db['default']['autoinit'] = TRUE;
$db['default']['stricton'] = FALSE;
$db['writedb']['hostname'] = '202.187.194.160';
$db['writedb']['username'] = 'writedb_name';
$db['writedb']['password'] = 'writedb_password';
$db['writedb']['database'] = 'writedb_db';
$db['writedb']['dbdriver'] = 'mysql';
$db['writedb']['dbprefix'] = '';
$db['writedb']['pconnect'] = TRUE;
$db['writedb']['db_debug'] = TRUE;
$db['writedb']['cache_on'] = FALSE;
$db['writedb']['cachedir'] = '';
$db['writedb']['char_set'] = 'utf8';
$db['writedb']['char_names'] = 'utf8';
$db['writedb']['dbcollat'] = 'utf8_general_ci';
$db['writedb']['swap_pre'] = '';
$db['writedb']['autoinit'] = TRUE;
$db['writedb']['stricton'] = FALSE;

M (Model) needs to use Model to configure two data sources, only one for reading, and of course, if it is only for writing, it can also configure one for writing separately.

An example of my feedback on a question feedbackmodel. php:


<?php
class Feedbackmodel extends CI_Model {
function __construct() {
parent::__construct ();
$this->db = $this->load->database ('default',true);
$this->writedb = $this->load->database ('writedb',true);
}
public function add($data)
{
$this->writedb->insert('feedback',$data);
if($this->writedb->affected_rows() == 1){
return true;
}
return false ;
}
}
?>

C (Controller) controller call with ordinary 1, as long as the introduction of Model can, my example:


<?php 
class Feedback extends CI_Controller {
function __construct(){
parent::__construct();
}
function index(){
$this->load->model('feedbackmodel'); 
$this->load->helper('url'); 
$data['name'] = "feedback";
$this->load->view('feedbackview',$data);
}
}
?>

Precautions:

① It is called remotely with IP because of the need of mutual backup between master and slave

② default can be localhost or IP by default

More readers interested in CodeIgniter can check the topics of this site: "codeigniter Introduction Tutorial", "CI (CodeIgniter) Framework Advanced Tutorial", "php Excellent Development Framework Summary", "ThinkPHP Introduction Tutorial", "Zend Common Methods Summary", "Zend FrameWork Framework Introduction Tutorial", "php Object-Oriented Programming Introduction Tutorial", "php+mysql Database Operation Introduction Tutorial" and "php Common Database Operation Skills Summary"

I hope this article is helpful to the PHP programming based on CodeIgniter framework.


Related articles: