【1213】redis(下)慢查詢日誌,php安裝redis擴展,儲存session,主從配置
21.18 redis慢查詢日誌
21.19 php安裝redis擴展
21.20 redis存儲session
21.21 redis主從配置
21.18 redis慢查詢日誌
針對慢查詢日誌,可以設置兩個參數,一個是執行時長,單位是微秒,另一個是慢查詢日誌的長度。當一個新的命令被寫入日誌時,最老的一條會從命令日誌隊列中被移除
1、編輯配置文件vim /etc/redis.conf
slowlog-log-slower-than 1000 //單位us,表示慢於1000us則記錄日誌
slowlog-max-len 128 //定義日誌長度,表示最多存128條
2、測試
1)將 slowlog-log-slower-than 設置爲10微秒,重啓 redis
[root@arslinux-01 ~]# vim /etc/redis.conf
slowlog-log-slower-than 10 //實驗環境任何一個查詢都是大於10us的
[root@arslinux-01 ~]# killall redis-server
[root@arslinux-01 ~]# redis-server /etc/redis.conf
[root@arslinux-01 ~]# redis-cli -a 'arslinux>com'
127.0.0.1:6379> SLOWLOG get
1) 1) (integer) 0
2) (integer) 1563117351
3) (integer) 738
4) 1) "COMMAND"
5) "127.0.0.1:52340"
6) ""
127.0.0.1:6379> keys *
1) "k3"
2) "hash1"
3) "list2"
4) "hash2"
5) "seta"
6) "set1"
7) "list1"
8) "setd"
9) "setc"
10) "mykey"
11) "k1"
12) "sete"
13) "set3"
127.0.0.1:6379> get k3
"a"
127.0.0.1:6379> SLOWLOG get
1) 1) (integer) 2
2) (integer) 1563117380
3) (integer) 47
4) 1) "keys"
2) "*"
5) "127.0.0.1:52340"
6) ""
2) 1) (integer) 1
2) (integer) 1563117356
3) (integer) 34
4) 1) "SLOWLOG"
2) "get"
5) "127.0.0.1:52340"
6) ""
3) 1) (integer) 0
2) (integer) 1563117351
3) (integer) 738
4) 1) "COMMAND"
5) "127.0.0.1:52340"
6) ""
127.0.0.1:6379> SLOWLOG get 2 //只列出最新的2條
1) 1) (integer) 4
2) (integer) 1563117461
3) (integer) 20
4) 1) "SLOWLOG"
2) "get"
3) "1"
5) "127.0.0.1:52340"
6) ""
2) 1) (integer) 3
2) (integer) 1563117386
3) (integer) 48
4) 1) "SLOWLOG"
2) "get"
5) "127.0.0.1:52340"
6) ""
127.0.0.1:6379> SLOWLOG len //查看慢查詢日誌條數
(integer) 6
21.19 php安裝redis擴展
1、下載、解壓
[root@arslinux-01 ~]# cd /usr/local/src/
[root@arslinux-01 src]# wget https://coding.net/u/aminglinux/p/yuanke_centos7/git/raw/master/21NOSQL/phpredis.zip
解壓 unzip phpredis.zip
2、生成 .config 文件
[root@arslinux-01 src]# cd phpredis-develop/
[root@arslinux-01 phpredis-develop]# /usr/local/php-fpm/bin/phpize
\Configuring for:
PHP Api Version: 20131106
Zend Module Api No: 20131226
Zend Extension Api No: 220131226
3、編譯安裝
[root@arslinux-01 phpredis-develop]# ./configure --with-php-config=/usr/local/php-fpm/bin/php-config
[root@arslinux-01 phpredis-develop]# make && make install
4、編輯 php.ini,增加一行 extension=redis.so
[root@arslinux-01 phpredis-develop]# vim /usr/local/php-fpm/etc/php.ini
extension=redis.so
5、檢查 redis 模塊是否安裝成功
[root@arslinux-01 phpredis-develop]# /usr/local/php-fpm/bin/php -m |grep redis
redis
6、重啓 php-fpm 服務
[root@arslinux-01 phpredis-develop]# /etc/init.d/php-fpm restart
Gracefully shutting down php-fpm . done
Starting php-fpm done
21.20 redis存儲session
1、根據之前的經驗,直接在 php-fpm 配置對應的 pool 裏增加
[root@arslinux-01 phpredis-develop]# vim /usr/local/php-fpm/etc/php-fpm.d/arslinux.conf
php_value[session.save_handler] = redis
php_value[session.save_path] = "tcp://127.0.0.1:6379"
2、重啓 php-fpm 和 去除密碼配置 重啓redis
[root@arslinux-01 phpredis-develop]# /etc/init.d/php-fpm restart
Gracefully shutting down php-fpm . done
Starting php-fpm done
[root@arslinux-01 phpredis-develop]# killall redis-server
[root@arslinux-01 phpredis-develop]# redis-server /etc/redis.conf
3、下載測試文件
因爲之前已經下載了,在 /data/wwwroot/default/下並改名爲 1.php
4、直接 curl
[root@arslinux-01 default.com]# curl localhost/1.php
1563118794<br><br>1563118794<br><br>5tih1g88dm4amd6614iihfuvb2
[root@arslinux-01 default.com]# curl localhost/1.php
1563118796<br><br>1563118796<br><br>70rnt7unik8eie33co32pdg155
[root@arslinux-01 default.com]# curl localhost/1.php
1563118799<br><br>1563118799<br><br>i9pmk3ht9nccqro6vh1c6bnh16
[root@arslinux-01 default.com]# curl localhost/1.php
1563118800<br><br>1563118800<br><br>3sgv8fmn2csf5vmcsbgbaqf351
[root@arslinux-01 default.com]# curl localhost/1.php
1563118801<br><br>1563118801<br><br>1lrs39of4hashv86t2rntr5tc0
5、進入 redis,查看 keys 對應的 value是否匹配
[root@arslinux-01 default.com]# redis-cli
127.0.0.1:6379> keys *
1) "set3"
2) "PHPREDIS_SESSION:i9pmk3ht9nccqro6vh1c6bnh16"
3) "setc"
4) "PHPREDIS_SESSION:1lrs39of4hashv86t2rntr5tc0"
5) "list1"
6) "sete"
7) "list2"
8) "hash1"
9) "PHPREDIS_SESSION:3sgv8fmn2csf5vmcsbgbaqf351"
10) "seta"
11) "PHPREDIS_SESSION:70rnt7unik8eie33co32pdg155"
12) "PHPREDIS_SESSION:5tih1g88dm4amd6614iihfuvb2"
13) "setd"
14) "mykey"
15) "set1"
16) "k1"
17) "k3"
18) "hash2"
127.0.0.1:6379> get PHPREDIS_SESSION:5tih1g88dm4amd6614iihfuvb2
"TEST|i:1563118794;TEST3|i:1563118794;"
127.0.0.1:6379> get PHPREDIS_SESSION:70rnt7unik8eie33co32pdg155
"TEST|i:1563118796;TEST3|i:1563118796;"
6、成功!!
還有兩種方法:
1、vim /usr/local/php-fpm/etc/php.ini//更改或增加
session.save_handler = “redis”
session.save_path = “tcp://127.0.0.1:6379”
2、apache虛擬主機配置文件中也可以這樣配置:
php_value session.save_handler " redis" php_value session.save_path " tcp://127.0.0.1:6379"
如果想用 php 連接 redis cluster,需要使用 predis 擴展
安裝方法類似 phpredis,predis 擴展地址 https://github.com/nrk/predis(針對集羣的)
21.21 redis主從配置
爲了節省資源,我們可以在一臺機器上啓動兩個redis服務
1、拷貝配置文件並根據實際更改,以區分兩個 redis
[root@arslinux-01 ~]# cp /etc/redis.conf /etc/redis2.conf
[root@arslinux-01 ~]# vim /etc/redis2.conf
port 6380
pidfile /var/run/redis_6380.pid
logfile "/var/log/redis2.log"
dir /data/redis2/
slaveof 127.0.0.1 6379 //增加一行主服務器的ip,端口號
如果主配置了 requirepass ,那麼從上也需要配置同樣的設置
2、創建 redis2 的目錄
[root@arslinux-01 ~]# mkdir /data/redis2/
3、啓動 reids2
[root@arslinux-01 ~]# redis-server /etc/redis2.conf
[root@arslinux-01 ~]# ps aux|grep redis
root 8128 0.3 0.9 147348 9856 ? Ssl 12:03 0:00 redis-server 127.0.0.1:6379
root 8151 0.0 0.9 147348 9736 ? Ssl 12:03 0:00 redis-server 127.0.0.1:6380
root 8158 0.0 0.0 112724 988 pts/0 R+ 12:03 0:00 grep --color=auto redis
[root@arslinux-01 ~]# netstat -lntp |grep redis
tcp 0 0 127.0.0.1:6379 0.0.0.0:* LISTEN 8128/redis-server 1
tcp 0 0 127.0.0.1:6380 0.0.0.0:* LISTEN 8151/redis-server 1
4、測試
[root@arslinux-01 ~]# redis-cli -h 127.0.0.1 -p 6379
127.0.0.1:6379> keys *
1) "set1"
2) "setd"
3) "set3"
4) "mykey"
5) "sete"
6) "list2"
7) "setc"
8) "hash2"
9) "hash1"
10) "list1"
11) "seta"
12) "k3"
13) "k1"
[root@arslinux-01 ~]# redis-cli -h 127.0.0.1 -p 6380
127.0.0.1:6380> keys *
1) "set1"
2) "k1"
3) "seta"
4) "hash2"
5) "mykey"
6) "hash1"
7) "setc"
8) "sete"
9) "list1"
10) "k3"
11) "list2"
12) "set3"
13) "setd"
127.0.0.1:6380> CONFIG GET dir
(error) ERR unknown command 'CONFIG'
127.0.0.1:6380> arslinux get dir
1) "dir"
2) "/data/redis2"
127.0.0.1:6380> arslinux get dbfilename
1) "dbfilename"
2) "dump.rdb"
127.0.0.1:6380> arslinux get slaveof
1) "slaveof"
2) "127.0.0.1 6379"
因爲配置文件中 slave-read-only yes 限制了只能讀不可寫,改爲 no 則可寫
127.0.0.1:6380> set key19 111
(error) READONLY You can't write against a read only slave.
注意: redis主從和mysql主從不一樣,redis主從不用事先同步數據,它會自動同步過去