2019-11-24sad creeper
!注意:标红的要注意区分开
第一步、配置.env文件(同一服务器上多个数据库)
DB_CONNECTION=pgsql
DB_HOST=IP(例如:127.0.0.1)
DB_PORT=端口号(例如:5432)
DB_DATABASE=连接的第一个数据库名(例如:user)
DB_USERNAME=账号(例如:root)
DB_PASSWORD=密码(例如:root)
DB_DATABASE2=连接的第二个数据库名(例如:user2)
DB_DATABASE3=连接的第三个数据库名(例如:user3)
第一步、配置.env文件(多个服务器上的数据库)
第二部分把不相同的改改就行了,注意区分
第二部、配置config/database.php(上面两个第一步的配置是差不多的,注意区分好就行了)
//这个是默认的
'pgsql' => [
'driver' => 'pgsql',
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '5432'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'prefix' => '',
'schema' => 'public',
'sslmode' => 'prefer',
],
//根据需求要连几个数据库就重复写下面的配置几遍,注意这里面'database'要跟 .env 里标红的相对应
'pgsql2' => [
'driver' => 'pgsql',
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '5432'),
'database' => env('DB_DATABASE2', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'prefix' => '',
'schema' => 'public',
'sslmode' => 'prefer',
],
'pgsql3' => [
'driver' => 'pgsql',
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '5432'),
'database' => env('DB_DATABASE3', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'prefix' => '',
'schema' => 'public',
'sslmode' => 'prefer',
],
第三步、使用方法
方法1:
$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','$cardid','$iccid')");
DB::connection('pgsql')->update("update radcheck SET value='$data' where username='$phone'");
DB::connection('pgsql3')->select("select * from changeinfo where iccid='$iccid'");
方法2:
protected $connection = 'pgsql'; //指明你要使用的连接
protected $connection = 'pgsql2'; //指明你要使用的连接
protected $connection = 'pgsql3'; //指明你要使用的连接
第四步、多个数据库操作事务处理
use socket;
DB::connection('pgsql1')->beginTransaction(); //开启
DB::connection('pgsql')->beginTransaction();
$rs1 = DB::connection('pgsql1')->update("update radcheck SET value='$data' where username='$phone'");
$rs2 = DB::connection('pgsql')->update("update radcheck SET value='$data' where username='$phone'");
if($rs1&&$rs2)
{
DB::connection('pgsql')->commit(); //提交
DB::connection('pgsql1')->commit();
return true;
}
else
{
DB::connection('pgsql')->rollback(); //回滚
DB::connection('pgsql1')->rollback();
return false;
}
阅读 969 评论
p
priligy amazon
I have been exploring for a little for any high-quality articles or blog posts on this kind of space . Exploring in Yahoo I ultimately stumbled upon this website. Reading this information So i'm glad to exhibit that I've an incredibly excellent uncanny feeling I discovered just what I needed. I so much definitely will make sure to do not forget this web site and give it a look regularly.
1个月前 ·