指尖上的记忆指尖上的记忆
首页
  • 基础
  • Laravel框架
  • Symfony框架
  • 基础
  • Gin框架
  • 基础
  • Spring框架
  • 命令
  • Nginx
  • Ai
  • Deploy
  • Docker
  • K8s
  • Micro
  • RabbitMQ
  • Mysql
  • PostgreSsql
  • Redis
  • MongoDb
  • Html
  • Js
  • 前端
  • 后端
  • Git
  • 知识扫盲
  • Golang
🌟 gitHub
首页
  • 基础
  • Laravel框架
  • Symfony框架
  • 基础
  • Gin框架
  • 基础
  • Spring框架
  • 命令
  • Nginx
  • Ai
  • Deploy
  • Docker
  • K8s
  • Micro
  • RabbitMQ
  • Mysql
  • PostgreSsql
  • Redis
  • MongoDb
  • Html
  • Js
  • 前端
  • 后端
  • Git
  • 知识扫盲
  • Golang
🌟 gitHub

关于keyDB的使用

1.服务配置

  • docker-compose 文件

    vim docker-compose.yml
    
    version: "3.9"
    services:
    keydb-master:
    image: eqalpha/keydb:latest
    container_name: keydb-master
    restart: unless-stopped
    ports:
    - "6381:6379"
    volumes:
    - ./master-data:/data
    - ./master.conf:/etc/keydb/keydb.conf
    command: ["keydb-server", "/etc/keydb/keydb.conf"]
    
    keydb-replica:
    image: eqalpha/keydb:latest
    container_name: keydb-replica
    restart: unless-stopped
    ports:
    - "6382:6379"
    volumes:
    - ./replica-data:/data
    - ./replica.conf:/etc/keydb/keydb.conf
    depends_on:
    - keydb-master
    command: ["keydb-server", "/etc/keydb/keydb.conf"]
    
  • 创建目录

    mkdir -p master-data replica-data
    
  • 主配置

    vim master.conf
    
  • KeyDB 主节点配置

    port 6379
    bind 0.0.0.0
    requirepass yourpassword
    masterauth yourpassword
    
  • 数据持久化

    save 900 1
    save 300 10
    save 60 10000
    dir /data
    dbfilename keydb-master.rdb
    
  • 日志

    loglevel notice
    logfile ""
    
  • 其他配置

    timeout 0
    tcp-keepalive 300
    supervised no
    pidfile /var/run/keydb_6379.pid
    databases 16
    
  • 从配置

    vim replica.conf
    
  • KeyDB 副本节点配置

    port 6379
    bind 0.0.0.0
    requirepass yourpassword
    masterauth yourpassword
    
  • 主从复制配置

    replicaof keydb-master 6379
    replica-read-only yes
    replica-serve-stale-data yes
    replica-priority 100
    
  • 数据持久化

    save 900 1
    save 300 10
    save 60 10000
    dir /data
    dbfilename keydb-replica.rdb
    
  • 日志

    loglevel notice
    logfile ""
    
  • 其他配置

    timeout 0
    tcp-keepalive 300
    supervised no
    pidfile /var/run/keydb_6379.pid
    databases 16
    

启动服务

$ docker-compose up -d
WARN[0000] /home/mdpi/code/code-new/my-keyDB/docker-compose.yml: `version` is obsolete
[+] Running 3/3
✔ Network my-keydb_default  Created                                                                                                                                                                          0.1s
✔ Container keydb-master    Started                                                                                                                                                                          0.0s
✔ Container keydb-replica   Started

查看运行状态

$ docker ps
CONTAINER ID   IMAGE                    COMMAND                  CREATED         STATUS         PORTS                                                                                  NAMES
0e1a6ca9d96e   eqalpha/keydb:latest     "docker-entrypoint.s…"   8 seconds ago   Up 8 seconds   0.0.0.0:6382->6379/tcp, :::6382->6379/tcp                                              keydb-replica
ade18b7c5898   eqalpha/keydb:latest     "docker-entrypoint.s…"   8 seconds ago   Up 8 seconds   0.0.0.0:6381->6379/tcp, :::6381->6379/tcp                                              keydb-master

查看配置是否生效

$ docker exec -it keydb-master keydb-cli -a yourpassword INFO replication
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
role:master
connected_slaves:1
slave0:ip=172.22.0.3,port=6379,state=online,offset=37,lag=1
master_failover_state:no-failover
master_replid:f214c2411a60042d8e85679c3f3f7648943a245c
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:37
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:37


$ docker exec -it keydb-replica keydb-cli -a yourpassword INFO replication
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
role:slave
master_global_link_status:up
connected_masters:1
master_host:keydb-master
master_port:6379
master_link_status:up
master_last_io_seconds_ago:7
master_sync_in_progress:0
slave_read_repl_offset:51
slave_repl_offset:51
slave_priority:100
slave_read_only:1
replica_announced:1
connected_slaves:0
master_failover_state:no-failover
master_replid:f214c2411a60042d8e85679c3f3f7648943a245c
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:51
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:51

测试

$ docker exec -it keydb-master keydb-cli -a yourpassword SET test "hello world"
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
OK


$ docker exec -it keydb-replica keydb-cli -a yourpassword GET test
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
"hello world"

配置分析

requirepass yourpassword
作用: 设置当前节点的访问密码
用途: 客户端连接到这个节点时需要提供的密码
相当于: "进入这个节点需要密码"

masterauth yourpassword
作用: 设置连接主节点时使用的密码
用途: 当前节点作为副本连接主节点时的认证密码
相当于: "连接主节点时用这个密码"

主节点上也配置了: masterauth yourpassword 虽然是主节点,但配置上以防角色切换

结论: 顺利启动


简单运行一个laravel12项目,用来测试上面的 keyDB 读写分离

  • 1.运行comoser 创建命令
    $ composer create-project laravel/laravel laravel12-project "12.*"
    
    Problem 1
    - Root composer.json requires laravel/pint ^1.24 -> satisfiable by laravel/pint[v1.24.0, v1.25.0, v1.25.1].
    - laravel/pint[v1.24.0, ..., v1.25.1] require ext-xml * -> it is missing from your system. Install or enable PHP's xml extension.
    Problem 2
    - phpunit/phpunit[11.5.3, ..., 11.5.41] require ext-dom * -> it is missing from your system. Install or enable PHP's dom extension.
    - Root composer.json requires phpunit/phpunit ^11.5.3 -> satisfiable by phpunit/phpunit[11.5.3, ..., 11.5.41].
    
    To enable extensions, verify that they are enabled in your .ini files:
    - /etc/php/8.4/cli/php.ini
    - /etc/php/8.4/cli/conf.d/10-opcache.ini
    - /etc/php/8.4/cli/conf.d/10-pdo.ini
    - /etc/php/8.4/cli/conf.d/20-calendar.ini
    - /etc/php/8.4/cli/conf.d/20-ctype.ini
    - /etc/php/8.4/cli/conf.d/20-exif.ini
    - /etc/php/8.4/cli/conf.d/20-ffi.ini
    - /etc/php/8.4/cli/conf.d/20-fileinfo.ini
    - /etc/php/8.4/cli/conf.d/20-ftp.ini
    

这个报错的意思是 PHP 缺少一些扩展,Laravel 12 的 pint 和 phpunit 需要用到 ext-xml 和 ext-dom。

具体解决方法如下:

1. 安装缺失的 PHP 扩展

根据你的操作系统不同,安装方式也不一样。你用的是 PHP 8.4(报错里显示 /etc/php/8.4/cli/php.ini),假设是 Ubuntu / Debian 系统:

sudo apt update
sudo apt install php8.4-xml
  • php8.4-xml 会包含 ext-xml 和 ext-dom。
  • 安装后,重启 PHP-FPM / CLI:
sudo service php8.4-fpm restart

2. 确认扩展已启用

php -m | grep -E 'xml|dom'

如果输出:

dom
xml

就说明安装成功。


3. 重新执行 Composer

composer install
由于使用的是phpredis 客户端,所以还需要 sudo apt install php8.4-redis
还需要:
sudo apt install php8.4-mbstring, 否则报:Call to undefined function Illuminate\Support\mb_split()
sudo apt install php8.4-mysql 否则报: could not find driver (Connection: mysql
  • 2.配置redis 'write' => [ 'host' => env('REDIS_HOST_1', '127.0.0.1'), 'password' => env('REDIS_PASSWORD', null), 'port' => env('REDIS_PORT_1', 6379), // KeyDB 主节点 'database' => 0, ], 'read' => [ 'host' => env('REDIS_HOST_2', '127.0.0.1'), 'password' => env('REDIS_PASSWORD', null), 'port' => env('REDIS_PORT_2', 6380), // KeyDB 从节点 'database' => 0, ],

  • 3.通过php-cli启动服务

    php artisan serve (默认8000端口)
    
    也可以手动指定端口
    php artisan serve --port=8080
    
    也可以手动指定 IP + 端口(比如想局域网访问)
    php artisan serve --host=0.0.0.0 --port=8081
    
  • 4.测试:

    <?php
    
    namespace App\Helpers;
    
    use Illuminate\Support\Facades\Redis;
    
    class RedisHelper
    {
        /**
         * redis 写数据库
         *
         * @return \Illuminate\Redis\Connections\Connection
         */
        public static function write()
        {
            return Redis::connection('write');
        }
    
        /**
         * Redis 读数据库
         *
         * @return \Illuminate\Redis\Connections\Connection
         */
        public static function read()
        {
            return Redis::connection('read');
        }
    }
    
    
    Route::get('/redis-cluster-test', function () {
        RedisHelper::write()->set('address', 'wuhan'.time());
        $value = RedisHelper::read()->get('address');
    
        return response()->json([
            'master_write' => 'success',
            'slave_read' => $value
        ]);
    });
    
    
    
    http://localhost:8082/redis-cluster-test
    {
    "master_write": "success",
    "slave_read": "wuhan1759033136"
    }
    
  • 5.问题: 还是需要手动控制 读写 连接