redis慢查询日志默认开启,在配置文件中有两行配置参数:
[root@linux ~]# grep slowlog /etc/redis.conf
#超时时间,单位微秒,执行超时的命令将会被记录到慢查询日志中
slowlog-log-slower-than 10000
#定义日志长度,表示最多保留的慢查询记录数量
slowlog-max-len 128
#当慢查询日志记录数量达到上限,新增的慢查询记录被写入日志时,最老的一条记录会从日志队列移除
列出所有慢查询日志:
127.0.0.1:6379> SLOWLOG get
只列出5条慢查询日志:
127.0.0.1:6379> SLOWLOG get 5
获取慢查询日志的数量:
127.0.0.1:6379> SLOWLOG len
下载php的redis拓展模块包:
[root@linux ~]# cd /usr/local/src/
[root@linux src]# wget https://pecl.php.net/get/redis-5.1.1.tgz
解压:
[root@linux src]# tar -zxvf redis-5.1.1.tgz
生成configure文件:
[root@linux src]# cd redis-5.1.1/
[root@linux redis-5.1.1]# /usr/local/php-fpm/bin/phpize
配置编译参数:
[root@linux redis-5.1.1]# ./configure --with-php-config=/usr/local/php-fpm/bin/php-config
编译安装:
[root@linux redis-5.1.1]# make && make install
编辑php.ini配置文件:
[root@linux ~]# vim /usr/local/php-fpm/etc/php.ini
添加以下内容:
extension=redis.so
查看redis拓展模块是否已被加载:
[root@linux ~]# /usr/local/php-fpm/bin/php -m|grep redis
redis
重启php-fpm服务:
[root@linux ~]# /etc/init.d/php-fpm restart
编辑php-fpm.conf配置文件:
[root@linux ~]# vi /usr/local/php-fpm/etc/php-fpm.d/www.conf
添加以下内容:
php_value[session.save_handler] = redis
php_value[session.save_path] = " tcp://127.0.0.1:6379 "
补充: 配置redis存储session的另外两种方法
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-fpm:
[root@linux ~]# /etc/init.d/php-fpm restart
新建php脚本:
[root@linux ~]# vi se_test.php
添加以下内容:
session_start();
if (!isset($_SESSION['TEST'])) {
$_SESSION['TEST'] = time();
}
$_SESSION['TEST3'] = time();
print $_SESSION['TEST'];
print "
";
print $_SESSION['TEST3'];
print "
";
print session_id();
?>
将测试脚本放到网站目录下,请求该文件:
[root@linux ~]# curl localhost/se_test.php
1576226429<br><br>1576226429<br><br>df5d41226jf31v8j7v1at7sen3
连接redis查看:
[root@linux ~]# redis-cli
127.0.0.1:6379> keys *
1) "PHPREDIS_SESSION:df5d41226jf31v8j7v1at7sen3"
127.0.0.1:6379> get PHPREDIS_SESSION:df5d41226jf31v8j7v1at7sen3
"TEST|i:1576226429;TEST3|i:1576226429;"
#session已被redis存储
补充: 如果php连接的是redis集群,需要用predis拓展模块的包,安装方法类似phpredis,predis拓展模块地址 https://github.com/nrk/predis
配置redis主从时,主不变,编辑从机器的redis配置文件即可:
[root@linux02 ~]# vim /etc/redis.conf
添加以下内容:
replicaof 192.168.234.128 6379
#指定master的ip和端口(master配置文件中bind定义的ip)
如果master redis设置了连接密码,需要在添加一行配置指定密码:
masterauth test123
#假设密码为test123
还需要注意一行配置:
replica-read-only yes
#表示slave redis为只读
启动slave redis:
[root@linux02 ~]# redis-server /etc/redis.conf
连接后查看master redis的数据是否已经同步到slave redis:
[root@linux02 ~]# redis-cli
127.0.0.1:6379> keys *
1) "key2"
#已成功同步
查看日志:
11586:S 15 Dec 2019 14:32:09.860 * Connecting to MASTER 192.168.234.128:6379
11586:S 15 Dec 2019 14:32:09.861 * MASTER <-> REPLICA sync started
11586:S 15 Dec 2019 14:32:09.862 * Non blocking connect for SYNC fired the event.
11586:S 15 Dec 2019 14:32:09.864 * Master replied to PING, replication can continue...
11586:S 15 Dec 2019 14:32:09.865 * Partial resynchronization not possible (no cached master)
11586:S 15 Dec 2019 14:32:09.867 * Full resync from master: acc4e63222b46b508a9398f11e0399343984b4c0:0
11586:S 15 Dec 2019 14:32:09.917 * MASTER <-> REPLICA sync: receiving 191 bytes from master
11586:S 15 Dec 2019 14:32:09.917 * MASTER <-> REPLICA sync: Flushing old data
11586:S 15 Dec 2019 14:32:09.917 * MASTER <-> REPLICA sync: Loading DB in memory
11586:S 15 Dec 2019 14:32:09.917 * MASTER <-> REPLICA sync: Finished with success
在slave redis上创建key:
127.0.0.1:6379> set key5 test
(error) READONLY You can't write against a read only replica.
#由于配置文件中定义只读的配置为yes,所以slave redis不可写入
补充: redis主从与mysql主从不同,redis主从不用事先同步好数据,配置完成后即可自动同步