21.18 redis慢查詢日誌

21.18 redis慢查詢日誌

編輯配置文件/etc/redis.conf
針對慢查詢日誌,能夠設置兩個參數,一個是執行時長,單位是微秒,另外一個是慢查詢日誌的長度。當一個新的命令被寫入日誌時,最老的一條會從命令日誌隊列中被移除。
slowlog-log-slower-than 1000 //單位ms,表示慢於1000ms則記錄日誌
slowlog-max-len 128 //定義日誌長度,表示最多存128條
slowlog get //列出全部的慢查詢日誌
slowlog get 2 //只列出2條
slowlog len //查看慢查詢日誌條數php

##redis慢查詢日誌默認是開啓的:
[root@Dasoncheng ~]# vim /etc/redis.conf
# The following time is expressed in microseconds, so 1000000 is equivalent
# to one second. Note that a negative number disables the slow log, while
# a value of zero forces the logging of every command.
slowlog-log-slower-than 10000

# There is no limit to this length. Just be aware that it will consume memory.
# You can reclaim memory used by the slow log with SLOWLOG RESET.
slowlog-max-len 128

這裏咱們將執行時常修改成10微秒,看看效果:mysql

# The following time is expressed in microseconds, so 1000000 is equivalent
# to one second. Note that a negative number disables the slow log, while
# a value of zero forces the logging of every command.
slowlog-log-slower-than 10  ##正常實驗環境下,查詢都會超過10微秒

# There is no limit to this length. Just be aware that it will consume memory.
# You can reclaim memory used by the slow log with SLOWLOG RESET.
slowlog-max-len 128
#保存退出
[root@Dasoncheng ~]# killall redis-server
[root@Dasoncheng ~]# redis-server /etc/redis.conf 
[root@Dasoncheng ~]# redis-cli 
127.0.0.1:6379> slowlog get   ##這裏顯示0爲1條慢查詢日誌
1) 1) (integer) 0
   2) (integer) 1507942013
   3) (integer) 899
   4) 1) "COMMAND"
   5) "127.0.0.1:40790"
   6) ""
127.0.0.1:6379> keys *
 1) "k3"
 2) "key2"
 3) "setc"
 4) "hash1"
 5) "s1"
 6) "lista"
 7) "set1"
 8) "l1"
 9) "setd"
10) "set2"
11) "list"
12) "key10"
13) "setb"
14) "key1"
15) "k2"
16) "sete"
17) "k1"
18) "seta"
19) "set3"
127.0.0.1:6379> get k3
"3"
127.0.0.1:6379> get key2
"200"
127.0.0.1:6379> slowlog get
1) 1) (integer) 3
   2) (integer) 1507942047
   3) (integer) 58
   4) 1) "get"
      2) "key2"
   5) "127.0.0.1:40790"
   6) ""
2) 1) (integer) 2
   2) (integer) 1507942031
   3) (integer) 29
   4) 1) "keys"
      2) "*"
   5) "127.0.0.1:40790"
   6) ""
3) 1) (integer) 1
   2) (integer) 1507942021
   3) (integer) 22
   4) 1) "slowlog"
      2) "get"
   5) "127.0.0.1:40790"
   6) ""
4) 1) (integer) 0
   2) (integer) 1507942013
   3) (integer) 899
   4) 1) "COMMAND"
   5) "127.0.0.1:40790"
   6) ""
127.0.0.1:6379> slowlog get 2  ##只獲取最近兩條慢查詢日誌;
1) 1) (integer) 4
   2) (integer) 1507942052
   3) (integer) 41
   4) 1) "slowlog"
      2) "get"
   5) "127.0.0.1:40790"
   6) ""
2) 1) (integer) 3
   2) (integer) 1507942047
   3) (integer) 58
   4) 1) "get"
      2) "key2"
   5) "127.0.0.1:40790"
   6) ""
127.0.0.1:6379> slowlog len  ##顯示全部慢查詢日誌條數;
(integer) 6

21.19 php安裝redis擴展模塊

cd /usr/local/src
 wget https://coding.net/u/aminglinux/p/yuanke_centos7/git/raw/master/21NOSQL/phpredis.zip
 unzip phpredis.zip
 cd phpredis-develop
 /usr/local/php-fpm/bin/phpize
 ./configure --with-php-config=/usr/local/php-fpm/bin/php-config
 make 
 make install
 vim /usr/local/php.ini//增長extension=redis.so
 /usr/local/php-fpm/bin/php -m|grep redis//看是否有redis模塊
 重啓php-fpm服務

21.20 redis存儲session

  • vim /usr/local/php-fpm/etc/php.ini//更改或增長
    session.save_handler = "redis"
    session.save_path = "tcp://127.0.0.1:6379"
  • 或者apache虛擬主機配置文件中也能夠這樣配置:
    php_value session.save_handler " redis"
    php_value session.save_path " tcp://127.0.0.1:6379
  • 或者php-fpm配置文件對應的pool中增長:
    php_value[session.save_handler] = redis
    php_value[session.save_path] = " tcp://127.0.0.1:6379 "

wgt http://study.lishiming.net/.mem_se.txt
mv .mem_se.txt /usr/local/apache2/htdocs/session.php
其中session.php內容能夠參考https://coding.net/u/aminglinux/p/yuanke_centos7/git/blob/master/21NOSQL/session.php
curl localhost/session.php //結果相似於1443702394<br><br>1443702394<br><br>i44nunao0g3o7vf2su0hnc5440
命令行鏈接redis,也能夠查看到該key以及對應的值
若是想用php鏈接redis cluster,須要使用predis擴展
安裝方法相似phpredis,predis擴展地址https://github.com/nrk/predislinux

21.21 redis主從配置

爲了節省資源,咱們能夠在一臺機器上啓動兩個redis服務
cp /etc/redis.conf /etc/redis2.conf
vim /etc/redis2.conf //須要修改port,dir,pidfile,logfile
還要增長一行
slaveof 127.0.0.1 6379
若是主上設置了密碼,還須要增長
masterauth aminglinux>com //設置主的密碼
啓動以前不要忘記建立新的dir目錄
redis-server /etc/redis2.conf
測試:在主上建立新的key,在從上查看
注意:redis主從和mysql主從不同,redis主從不用事先同步數據,它會自動同步過去git

[root@Dasoncheng ~]# vim /etc/redis2.conf 
[root@Dasoncheng ~]# mkdir /data/redis2
[root@Dasoncheng ~]# redis-server /etc/redis2.conf
[root@Dasoncheng ~]# ps aux |grep redis
root       6411  0.2  0.2 142920  2284 ?        Ssl  08:46   0:02 redis-server 127.0.0.1:6379
root       6498  0.1  0.2 142788  2136 ?        Ssl  09:06   0:00 redis-server 127.0.0.1:6333
root       6505  0.0  0.0 112660   964 pts/2    S+   09:06   0:00 grep --color=auto redis
[root@Dasoncheng ~]# redis-cli -h 127.0.0.1 -p 6333
127.0.0.1:6333> keys *
 1) "set1"
 2) "seta"
 3) "key2"
 4) "l1"
 5) "key10"
 6) "lista"
 7) "s1"
 8) "k1"
 9) "k2"
10) "set3"
11) "key1"
12) "k3"
13) "setd"
14) "set2"
15) "sete"
16) "list"
17) "setc"
18) "setb"
19) "hash1"
相關文章
相關標籤/搜索