Redis 是完全开源免费的,遵守BSD协议,是一个高性能的key-value类型的nosql数据库。
一、Redis 服务的安装使用:
redis官网:https://redis.io/download/
1、下载Redis4.0.4.tar.gz
[root@localhost ~]# wget -O redis-4.0.4.tar.gz http://download.redis.io/releases/redis-4.0.4.tar.gz
2、转移到安装目录
[root@localhost ~]# mv redis-4.0.4.tar.gz /usr/local/src/
3、切换目录
[root@localhost ~]# cd /usr/local/src/
4、然后执行make编译源码:
[root@localhost src]# tar -zxvf redis-4.0.4.tar.gz
[root@localhost src]# cd redis-4.0.4/
[root@localhost redis-4.0.4]# make
make完后 redis-4.0.4目录下会出现编译后的redis服务程序redis-server,还有用于测试的客户端程序redis-cli,两个程序位于安装目录 src 目录下
5、编译完成后启动
启动服务
[root@localhost redis-4.0.4]# cd src/
[root@localhost src]# ./redis-server
8172:C 01 Dec 17:20:18.415 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
8172:C 01 Dec 17:20:18.415 # Redis version=4.0.4, bits=64, commit=00000000, modified=0, pid=8172, just started
8172:C 01 Dec 17:20:18.415 # Warning: no config file specified, using the default config. In order to specify a config file use ./redis-server /path/to/redis.conf
8172:M 01 Dec 17:20:18.416 * Increased maximum number of open files to 10032 (it was originally set to 1024).
_._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 4.0.4 (00000000/0) 64 bit
.-`` .-```. ```\/ _.,_ ''-._
( ' , .-` | `, ) Running in standalone mode
|`-._`-...-` __...-.``-._|'` _.-'| Port: 6379
| `-._ `._ / _.-' | PID: 8172
`-._ `-._ `-./ _.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' | http://redis.io
`-._ `-._`-.__.-'_.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-'
8172:M 01 Dec 17:20:18.417 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
8172:M 01 Dec 17:20:18.417 # Server initialized
8172:M 01 Dec 17:20:18.417 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
8172:M 01 Dec 17:20:18.417 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
8172:M 01 Dec 17:20:18.417 * Ready to accept connections
注意这种方式启动redis 使用的是默认配置。也可以通过启动参数告诉redis使用指定配置文件使用下面命令启动。
$ cd src
$ ./redis-server redis.conf
redis.conf是一个默认的配置文件。我们可以根据需要使用自己的配置文件。
启动redis服务进程后,就可以使用测试客户端程序redis-cli和redis服务交互了
6、测试效果:另起一个命令行窗口
[root@localhost /]# cd /usr/local/src/redis-4.0.4/src/
127.0.0.1 是本机 IP ,6379 是 redis 服务端口。现在我们输入 PING 命令。
[root@localhost src]# ./redis-cli ping
PONG
[root@localhost src]# ./redis-cli
127.0.0.1:6379> set name cuikai
OK
127.0.0.1:6379> get name
"cuikai"
127.0.0.1:6379>
make命令执行完成后,会在src目录下生成6个可执行文件,分别是redis-server、redis-cli、redis-benchmark、redis-check-aof、redis-check-dump、redis-sentinel
redis-server is the Redis Server itself.(Redis服务器本身)
redis-sentinel is the Redis Sentinel executable (monitoring and failover).(Redis集群的管理工具)
redis-cli is the command line interface utility to talk with Redis.(与Redis进行交互的命令行客户端)
redis-benchmark is used to check Redis performances.(Redis性能测试工具)
redis-check-aof and redis-check-dump are useful in the rare event of corrupted data files.(AOF文件修复工具和RDB文件检查工具)
7、将redis-server redis-cli 复制到 /usr/local/bin/
[root@localhost src]# cp ./redis-server /usr/local/bin/
[root@localhost src]# cp ./redis-cli /usr/local/bin/
这样的话,只要/usr/local/bin/在PATH环境变量里,我们就可以直接使用redis-server和redis-cli而不需要指定全路径了。
[root@localhost ~]# vim /etc/profile
export PATH=$PATH:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/usr/local/mysql/bin:/usr/local/php7/bin:/usr/local/bin/redis-server:/usr/local/bin/redis-cli
重读环境变量
[root@localhost ~]# source /etc/profile
测试快捷启动redis
1、根目录启动redis服务
[root@localhost ~]# redis-server
2、新打开一命令行窗口
[root@localhost ~]# redis-cli
127.0.0.1:6379> ping
PONG
127.0.0.1:6379> set name ck
OK
127.0.0.1:6379> get name
"ck"
127.0.0.1:6379>
[root@localhost ~]# redis-cli shutdown #使用如下命令可以关闭redis服务
8、配置初始化脚本,以服务方式启动redis
初始化脚本到/etc/init.d目录,并重命名文件为:redis
[root@localhost local]# cd /usr/local/src/redis-4.0.4/
[root@localhost redis-4.0.4]# cp utils/redis_init_script /etc/init.d/redis
[root@localhost redis-4.0.4]# vim /etc/init.d/redis
确保redis文件内的REDISPORT变量是你使用的端口号
#!/bin/sh
#
# Simple Redis init.d script conceived to work on Linux systems
# as it does use of the /proc filesystem.
PATH=/usr/local/bin/:/sbin:/usr/bin:/bin
REDISPORT=6379
EXEC=/usr/local/bin/redis-server
CLIEXEC=/usr/local/bin/redis-cli
#PIDFILE=/var/run/redis_${REDISPORT}.pid
#CONF="/etc/redis/${REDISPORT}.conf"
PIDFILE=/etc/redis/pid/redis_6379.pid
CONF="/etc/redis/redis_6379.conf"
case "$1" in
start)
if [ -f $PIDFILE ]
then
echo "$PIDFILE exists, process is already running or crashed"
else
echo "Starting Redis server..."
$EXEC $CONF
fi
;;
stop)
if [ ! -f $PIDFILE ]
then
echo "$PIDFILE does not exist, process is not running"
else
PID=$(cat $PIDFILE)
echo "Stopping ..."
$CLIEXEC -p $REDISPORT shutdown
while [ -x /proc/${PID} ]
do
echo "Waiting for Redis to shutdown ..."
sleep 1
done
echo "Redis stopped"
fi
;;
*)
echo "Please use start or stop as first argument"
;;
esac
给予执行权限
[root@localhost redis-4.0.4]# chmod a+x /etc/init.d/redis
新建文件夹/etc/redis/ ,并拷贝redis-4.0.0下的 redis.conf 文件到到改目录下,使用端口号作为文件名
[root@localhost redis-4.0.4]# mkdir /etc/redis
[root@localhost redis-4.0.4]# ls
00-RELEASENOTES BUGS CONTRIBUTING COPYING deps INSTALL Makefile MANIFESTO README.md redis.conf runtest runtest-cluster runtest-sentinel sentinel.conf src tests utils
[root@localhost redis-4.0.4]# cp redis.conf /etc/redis/redis_6379.conf
创建用来存储redis持久化文件的目录(6379为端口号)
[root@localhost redis-4.0.4]# mkdir /etc/redis/log #存放日志文件
[root@localhost redis-4.0.4]# mkdir /etc/redis/db #存放数据文件
[root@localhost redis-4.0.4]# mkdir /etc/redis/pid #存放进程文件
编辑redis.conf文件,修改如下几个参数:
[root@localhost redis-4.0.4]# vim /etc/redis/redis_6379.conf
设置 daemonize
搜索 :daemonize 将这个值 改为 yes
daemonize yes
设置 pidfile 的位置
搜索:pidfile
pidfile /etc/redis/pid/redis_6379.pid #存放pid进程文件的位置
设置日志等级及存放位置(可以设置日志等级,注释上有说明)
logfile /etc/redis/log/redis_6379.log #设置日志文件路径。需要在log下新建redis文件夹
设置 redis 数据存放的工作目录
dir /etc/redis/db
测试
[root@localhost redis]# service redis start
Starting Redis server...
[root@localhost redis]# redis-cli
127.0.0.1:6379> ping
PONG
127.0.0.1:6379> set name cuikai
OK
127.0.0.1:6379> get name
"cuikai"
127.0.0.1:6379>
9、设置redis服务访问限制(根据需要)
[root@localhost ~]# vim /etc/redis/redis_6379.conf
搜索 bind ,
将 #bind 127.0.0.1 前面的 # 去掉,改为 bind 0.0.0.0 (允许所有ip 访问)
10、设置开机自动启动
默认启动级别,级别有0-6共7个级别。
等级0表示:表示关机
等级1表示:单用户模式
等级2表示:无网络连接的多用户命令行模式
等级3表示:有网络连接的多用户命令行模式
等级4表示:不可用
等级5表示:带图形界面的多用户模式
等级6表示:重新启动
10是启动优先级,90是停止优先级,优先级范围是0-100,数字越大,优先级越低
将redis放入linux启动管理体系中
[root@localhost ~]# chkconfig --level 2345 redis on
服务 redis 不支持 chkconfig
注: 报错:service redis does not support chkconfig,解决方法:
编辑/etc/init.d/redis文件,在#!/bin/bash 之后添加如下两行。
[root@localhost ~]# vim /etc/init.d/redis
# chkconfig: 2345 10 90
# description: redis_6379 service manage...
[root@localhost ~]# chkconfig --level 2345 redis on
[root@localhost ~]# chkconfig --add redis
查看redis_6379服务在各运行级状态
[root@localhost ~]# chkconfig --list
注:该输出结果只显示 SysV 服务,并不包含
原生 systemd 服务。SysV 配置数据
可能被原生 systemd 配置覆盖。
要列出 systemd 服务,请执行 'systemctl list-unit-files'。
查看在具体 target 启用的服务请执行
'systemctl list-dependencies [target]'。
mysqld 0:关 1:关 2:开 3:开 4:开 5:开 6:关
netconsole 0:关 1:关 2:关 3:关 4:关 5:关 6:关
network 0:关 1:关 2:开 3:开 4:开 5:开 6:关
redis 0:关 1:关 2:开 3:开 4:开 5:开 6:关
[root@localhost ~]#
重启服务器测试效果:
[root@localhost ~]# reboot #`reboot` 或者 `init 6`
重启完成后,直接使用redis-cli连接redis,效果如下:
Connecting to 192.168.216.154:22...
Connection established.
To escape to local shell, press 'Ctrl+Alt+]'.
Last login: Sun Dec 3 13:26:00 2017
[root@localhost ~]# redis-cli
127.0.0.1:6379> get name
"cuikai"
127.0.0.1:6379>
配置成功,完!
二、LNMP 环境 PHP7.0.21 安装 Redis扩展:
php扩展官方地址:http://pecl.php.net/package/redis
1、下载phpredis安装包
[root@localhost ~]# git clone https://github.com/phpredis/phpredis.git
2、编译安装
[root@localhost ~]# mv phpredis/ /usr/local/src
[root@localhost ~]# cd /usr/local/src/
[root@localhost src]# ls
[root@localhost src]# cd phpredis/
查找php-config位置
[root@localhost phpredis]# find / -name php-config
/usr/local/src/php-7.1.7/scripts/php-config
/usr/local/php7/bin/php-config
[root@localhost phpredis]# /usr/local/php7/bin/phpize
[root@localhost phpredis]# ./configure --with-php-config=/usr/local/php7/bin/php-config
[root@localhost phpredis]# make && make install
安装完成后,会出现:
See any operating system documentation about shared libraries for
more information, such as the ld(1) and ld.so(8) manual pages.
----------------------------------------------------------------------
Build complete.
Don't forget to run 'make test'.
Installing shared extensions: /usr/local/php7/lib/php/extensions/no-debug-non-zts-20160303/
[root@localhost phpredis]# make test
编辑 php.ini文件:添加 : extension=redis.so
[root@localhost phpredis]# vim /etc/php.ini
[redis]
extension=redis.so
重新启动php.ini
[root@localhost phpredis]# systemctl restart php-fpm.service
查看扩展是否已经添加
[root@localhost phpredis]# /usr/local/php7/bin/php -m
或者phpinfo()
完成.
测试php操作redis:
[root@localhost phpredis]# vim /usr/local/nginx/html/www.laravel.com/test.php
//连接本地的 Redis 服务 $redis = new Redis(); $redis->connect('127.0.0.1', 6379); echo "Connection to server sucessfully"; //设置 redis 字符串数据 $redis->set("tutorial-name", "Redis tutorial"); // 获取存储的数据并输出 echo "Stored string in redis:: " . $redis->get("tutorial-name");
访问 http://www.laravel.com/test.php:
输出结果:
Array ( [0] => Array ( [word] => C++ [times] => 1 [weight] => 9.5 [attr] => nz ) )
Connection to server sucessfullyStored string in redis:: Redis tutorial
本文为崔凯原创文章,转载无需和我联系,但请注明来自冷暖自知一抹茶ckhttp://www.cksite.cn