thinkphp Connection database and master-slave database setup tutorial, thinkphp master-slave _php Tutorial

Source: Internet
Author: User

thinkphp Connection database and master-slave database setup tutorial, thinkphp master-Slave


This paper describes in detail the thinkphp connection database and the master-slave database setup method, which is very practical in the thinkphp project development. The implementation method is as follows:

First, the project root directory to establish config.php

The code looks like this:

<?php if (!defined (' Think_path ')) exit (); Return Array (  ' db_type '    = '  mysql ',//database type    ' db_host '    = '  localhost ',//host  ' Db_ ') Name '    = '  aoli ',//database name  ' db_user ' +  ' root ',//database user name  ' db_pwd '     =  ',//Database Password  ' db_prefix ' + '  ,//data table prefix  ' db_charset '   = '  utf8 ',//website code  ' db_port '    = '  3306 ',//Database port);? >

Second, set the project configuration file

The \home\conf\config.php file code is as follows:

<?php $arr 1=array{  ' Url_model ' =>2,//pathinfo access mode}; $arr 2=include './config.php '; return Array_merge ($ ARR1, $arr 2); Array Integration?>

The \admin\conf\config.php file code is as follows:

<?php $arr 1=array{  ' Url_model ' =>1,//normal access mode get way}; $arr 2=include './config.php '; return Array_merge ($arr 1, $arr 2); Array Integration?>

Third, master-slave database settings

This setting is more suitable for large web sites with high concurrency and high load
Readers can view the default system constant settings in the \thinkphp\common\convention.php

The config.php file is set as follows:

<?php  return Array (  //' config item ' = = ' config value '  //Backstage  ' Url_mode ' =>0,  ' db_type ' = ' mysql ',  ' Db_host ' = ' localhost,192.168.1.2 ',//Two database servers  ' db_port ' + ' 3306 ',  ' db_name ' = ' thinkphptest ',// If the database name is the same, do not define multiple, if not the same with the server in turn ' db_user ' + '  root ',  ' db_pwd ' = ' password ',  //Table prefix  ' db_ PREFIX ' + ' think_ ',  ///Next Configure the master-slave database  ' Db_deploy_type ' =>1,//open the Distributed Database  ' Db_rw_separate ' =>ture, Read/write separation, default to the first server to write to the server, the other only read not write  ); >

To read the database file parameters in an action:

  $hh =c (' db_host '); C can read the value in the configuration file  $pp =c (' Db_prefix ');  $this->assain (' h ', $hh);  $this->assain (' P ', $pp);  $this->display ();

The TPL under this action:

  Database server address: {$h}  database table prefix: {$p}

It is hoped that the method described in this article will be helpful to the thinkphp program design.


thinkphp the type of master-slave database can be multiple?

As far as I know. Multiple databases can be set, but not as you set ....

It helps to see more help documents. , the help document should have the settings for this multi-database:

How to connect a database in thinkphp [go]

thinkphp How to connect database operations database, we will create a model. Before you say the model and action, explain the location where the model and action are saved. The model is saved in the Lib/model folder in the program directory, and the action is saved in the Lib/action folder in the program directory. thinkphp system default Model rule is this: Model file Civilization name is similar to "model class name +model.class.php, and the model default operation database table name is our definition in config.php db_prefix+ Model class name, model class name and file name need to be capitalized "in the model file, define a class, extend the model class, the general syntax is as follows class name model extends model{} Well, now let's define a model. Because our database table name is Cms_article,class Articlemodel extends model{} file is saved as ArticleModel.class.php. Do not write anything, a model has been defined to complete. So now, let's continue with our action knowledge. The action and model many of the rules are very close, the difference is that the action does not directly manipulate the database, but need to use model to operate the database. Now let's define an action to complete the operation. Class Indexaction extends Action{function index () {$Article = D ("article");}} Save the file as IndexAction.class.php. OK, now we refresh the homepage, if there is no hint, then congratulations, the database connection model, the action definition is normal. The D method in action is to call Model,article the model class in the ArticleModel.class.php that we just defined-that is, while defining the model, we have completed the connection to the database and the operation of the database table. Ready ~
 

http://www.bkjia.com/PHPjc/868235.html www.bkjia.com true http://www.bkjia.com/PHPjc/868235.html techarticle thinkphp Connection database and master-slave database setup tutorial, thinkphp master and slave This text in detail the thinkphp connection database and master-slave database setup method, in the thinkphp project open ...

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.