一:作用与目的
1:消除读写的冲突,提高性能
2:提升数据库的并发负载能力
官方说明:
有时候你可能会希望使用一个数据库连接来执行 SELECT 语句,
而 INSERT、UPDATE 和 DELETE 语句则由另一个数据库连接来执行。
在 Laravel 中,无论你是使用原生 SQL 查询、查询构造器 或是 Eloquent ORM,都能轻松实现读写分离。
配置:
一:单 读库 配置(一主一从)
1:config/database.php文件中mysql配置项
'mysql' => [
'read' => [
'host' => '192.168.0.84',
'password' => env('DB_READ_PASSWORD', ''),
],
'write' => [
'host' => '192.168.0.83',
'password' => env('DB_WRITE_PASSWORD', ''),
],
'driver' => 'mysql',
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => true,
'engine' => null,
],
2:.env文件配置
DB_READ_PASSWORD=123456
DB_WRITE_PASSWORD=123456
二:多 读库配置(一主多从)
1:config/database.php文件中mysql配置项
'mysql' => [
'read' => [
['host' => '192.168.0.84','password' => env('DB_READ1_PASSWORD', '')],
['host' => '192.168.0.85','password' => env('DB_READ2_PASSWORD', '')],
],
'write' => [
'host' => '192.168.0.83',
'password' => env('DB_WRITE_PASSWORD', ''),
],
'driver' => 'mysql',
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => true,
'engine' => null,
],
2:.env文件配置
DB_READ1_PASSWORD=123456
DB_READ2_PASSWORD=123456
DB_WRITE_PASSWORD=123456
三:多读多写 配置(多主多从)
1:config/database.php文件中mysql配置项
'mysql' => [
'read' => [
['host' => '192.168.0.84','password' => env('DB_READ1_PASSWORD', '')],
['host' => '192.168.0.85','password' => env('DB_READ2_PASSWORD', '')],
],
'write' => [
['host' => '192.168.0.82','password' => env('DB_WRITE1_PASSWORD', '')],
['host' => '192.168.0.83','password' => env('DB_WRITE2_PASSWORD', '')],
],
'driver' => 'mysql',
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => true,
'engine' => null,
],
2:.env文件配置
DB_READ1_PASSWORD=123456
DB_READ2_PASSWORD=123456
DB_WRITE1_PASSWORD=123456
DB_WRITE2_PASSWORD=123456
四:主从数据同步延迟(初级方案)
注:laravel提供了一个针对分布式数据库系统中,主从数据同步延迟的初级的解决方案,也即是访问量不高的中小网站的解决方案,大流量高并发网站不能使用。
官方解释:sticky 是一个 可选 值,它用于允许 Laravel 立即读取在当前请求周期内写入到数据库的记录。 若 sticky选项被启用,且在当前请求周期中执行过「写」操作,那么在这之后的所有「读」操作都将使用「写」连接。 这样可以确保同一个请求周期中写入的数据库可以被立即读取到,从而避免主从同步延迟导致的数据不一致。不过是否启用它取决于项目的实际需求。
'mysql' => [
'read' => [
['host' => '192.168.0.84','password' => env('DB_READ1_PASSWORD', '')],
['host' => '192.168.0.85','password' => env('DB_READ2_PASSWORD', '')],
],
'write' => [
['host' => '192.168.0.82','password' => env('DB_WRITE1_PASSWORD', '')],
['host' => '192.168.0.83','password' => env('DB_WRITE2_PASSWORD', '')],
],
'driver' => 'mysql',
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => true,
'engine' => null,
'sticky' => true,
],
五:数据库实时同步
1:主库(写库,主服务器)配置:
1): Windows下MySQL的配置文件是my.ini,一般会在安装目录的根目录; Linux下MySQL的配置文件是my.cnf,一般会方在/etc/my.cnf,/etc/mysql/my.cnf。如果找不到,可以用find命令查找;
在[mysqld]节点下面添加如下内容:
[mysqld]
server-id=1
log-bin=c:\log-bin.log
2):创建远程访问用户(执行以下命令)
grant all on *.* to root@'192.168.0.84' identified by '1234'
3):设置用户远程访问权限
grant all on *.* to root@'%' identified by ‘1234’
4):重启数据库(主数据库配置完成)
mysql server
2:从库(读库,从服务器)配置
1): Windows下MySQL的配置文件是my.ini,一般会在安装目录的根目录; Linux下MySQL的配置文件是my.cnf,一般会方在/etc/my.cnf,/etc/mysql/my.cnf。如果找不到,可以用find命令查找;
在[mysqld]节点下面添加如下内容:
[mysqld]
server-id=2
master-host=192.168.0.83
master-user=root
master-password=1234
replicate-do-db=root
2):重启服务器(执行以下命令)
mysql server
3):启动主从机线程(配置完成,现在即完成全部配置,主库写入时,会同步到从库)
slave start
参考文章: 参考1 参考2 官方文档
|