1、配置.env文件
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=database_name
DB_USERNAME=root
DB_PASSWORD=1234
DB_HOST_CENTER=127.0.0.1
DB_PORT_CENTER=3306
DB_DATABASE_CENTER=database_center
DB_USERNAME_CENTER=root
DB_PASSWORD_CENTER=1234
2、配置config/database.php
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
'engine' => null,
],
'mysql_center' => [
'driver' => 'mysql',
'host' => env('DB_HOST_CENTER', 'localhost'),
'port' => env('DB_PORT_CENTER', '3306'),
'database' => env('DB_DATABASE_CENTER', 'forge'),
'username' => env('DB_USERNAME_CENTER', 'forge'),
'password' => env('DB_PASSWORD_CENTER', ''),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
'engine' => null,
],
3、创建model
// 这个model将采用默认的'mysql'连接
class UserModel extends Model
{
// 数据库'database'中的users表
protected $table = "users";
}
// 这个model将使用mysql_center连接
class UserModel extends Model
{
// 数据库'dadtabase_center'中的users表
protected $connection = 'mysql_center';
protected $table = "users";
}
4、其他
$user1=DB::connection(′pgsql′)−>select("SQL语句");user1=DB::connection(′pgsql′)−>select("SQL语句");user2 = DB::connection('pgsql2')->update("SQL语句");
$user3 = DB::connection('pgsql3')->select("SQL语句");
DB::connection('pgsql3')->delete("delete from changeinfo where id=1");
DB::connection('pgsql3')->insert("insert into changeinfo(name,idcard,iccid) values ('maxin′,′maxin′,′cardid','$iccid')");
DB::connection('pgsql')->update("update radcheck SET value='data′whereusername=′data′whereusername=′phone'");
DB::connection('pgsql3')->select("select * from changeinfo where iccid='$iccid'");
//事务相关
DB::connection('pgsql1')->beginTransaction(); //开启
DB::connection('pgsql')->beginTransaction();
rs1=DB::connection(′pgsql1′)−>update("updateradcheckSETvalue=′rs1=DB::connection(′pgsql1′)−>update("updateradcheckSETvalue=′data' where username='phone′");phone′");rs2 = DB::connection('pgsql')->update("update radcheck SET value='data′whereusername=′data′whereusername=′phone'");
if(rs1&&rs1&&rs2)
{
DB::connection('pgsql')->commit(); //提交
DB::connection('pgsql1')->commit();
return true;
}
else
{
DB::connection('pgsql')->rollback(); //回滚
DB::connection('pgsql1')->rollback();
return false;
}