# Redis Advanced Kiến thức nâng cao về Redis # Redis backup - Tạo bản backup trong Redis ##### Backup Redis data Sử dụng Redis SAVE command để thực hiện tạo một bản backup cho Redis Cú pháp: ```shell 127.0.0.1:6379> SAVE OK ``` Thực thi command này sẽ tạo 1 file dump.rdb trong thư mục Redis. ##### Restore Redis Data Để thực hiện khôi phục Redis data, move file Redis backup (dump.rdb) vào thư mục Redis và start Redis server. Để lấy đường dẫn thư mục Redis, sử dụng command CONFIG: ```shell 127.0.0.1:6379> CONFIG get dir 1) "dir" 2) "/user/laptrinhvn/redis-2.8.13/src" ``` ##### Bgsave Một cách khác để tạo một bản backup của Redis, đó là sử dụng command BGSAVE. Đây là command sẽ start tiến trình backup và được chạy background. Ví dụ: ```shell 127.0.0.1:6379> BGSAVE Background saving started ``` # Redis Security - Bảo mật trong Redis database Redis database có cơ chế bảo mật xác thực khi client kết nối và thực thi lệnh trên Redis. Để bật tính năng bảo mật, bạn cần thiết lập mật khẩu trong config file **Kiểm tra cấu hình xác thực:** ```shell 127.0.0.1:6379> CONFIG get requirepass 1) "requirepass" 2) "" ``` **Thiết lập xác thực:** ```shell 127.0.0.1:6379> CONFIG set requirepass "tutorialspoint" OK 127.0.0.1:6379> CONFIG get requirepass 1) "requirepass" 2) "matkhau" ``` Sau khi thiết lập mật khẩu, client kết nối đến Redis mà không xác thực sẽ nhận được thông báo lỗi: `(error) NOAUTH Authentication required` **Cú pháp xác thực:** ```shell 127.0.0.1:6379> AUTH password ``` Ví dụ: ```shell 127.0.0.1:6379> AUTH "tutorialspoint" OK 127.0.0.1:6379> SET mykey "Test value" OK 127.0.0.1:6379> GET mykey "Test value" ``` # Redis Benchmarks - Đo hiệu năng của Redis Redis benchmark là công cụ để thực hiện đo tốc độ xử lý đồng thời của Redis database. **Cú pháp:** ```shell redis-benchmark [option] [option value] ``` Ví dụ: Thực hiện đồng thời 100000 lệnh ```shell redis-benchmark -n 100000 PING_INLINE: 141043.72 requests per second PING_BULK: 142857.14 requests per second SET: 141442.72 requests per second GET: 145348.83 requests per second INCR: 137362.64 requests per second LPUSH: 145348.83 requests per second LPOP: 146198.83 requests per second SADD: 146198.83 requests per second SPOP: 149253.73 requests per second LPUSH (needed to benchmark LRANGE): 148588.42 requests per second LRANGE_100 (first 100 elements): 58411.21 requests per second LRANGE_300 (first 300 elements): 21195.42 requests per second LRANGE_500 (first 450 elements): 14539.11 requests per second LRANGE_600 (first 600 elements): 10504.20 requests per second MSET (10 keys): 93283.58 requests per second ``` **Các tham số:**
**Sr.No** | **Option** | **Description** | **Default Value** |
1 | -h | Specifies server host name | 127.0.0.1 |
2 | -p | Specifies server port | 6379 |
3 | -s | Specifies server socket | |
4 | -c | Specifies the number of parallel connections | 50 |
5 | -n | Specifies the total number of requests | 10000 |
6 | -d | Specifies data size of SET/GET value in bytes | 2 |
7 | -k | 1=keep alive, 0=reconnect | 1 |
8 | -r | Use random keys for SET/GET/INCR, random values for SADD | |
9 | -p | Pipeline <numreq> requests | 1 |
10 | -h | Specifies server host name | |
11 | -q | Forces Quiet to Redis. Just shows query/sec values | |
12 | --csv | Output in CSV format | |
13 | -l | Generates loop, Run the tests forever | |
14 | -t | Only runs the comma-separated list of tests | |
15 | -I | Idle mode. Just opens N idle connections and wait |
STT | Command | Mô tả |
---|---|---|
1 | **CLIENT LIST** | Trả về danh sách client đang kết nối đến Redis server |
2 | **CLIENT SETNAME** | Thiết lập tên của connection hiện tại |
3 | **CLIENT GETNAME** | Trả về tên của connection hiện tại |
4 | **CLIENT PAUSE** | Dừng (suspend) các kết nối theo khoảng thời gian xác định (in milliseconds) |
5 | **CLIENT KILL** | Đóng (close) một kết nối |