来张图
通过rpm包装的redis内容
1、首先通过rpm包安装的redis来看下都有哪些内容
[root@localhost ~]#yum info redis Loaded plugins: fastestmirror, refresh-packagekit, security Loading mirror speeds from cached hostfile Available Packages Name : redis Arch : x86_64 Version : 2.4.10 Release : 1.el6 Size : 213 k Repo : epel Summary : A persistent key-value database URL : http://redis.io License : BSD Description : Redis is an advanced key-value store. It is similar to memcached but the data : set is not volatile, and values can be strings, exactly like in memcached, but : also lists, sets, and ordered sets. All this data types can be manipulated with : atomic operations to push/pop elements, add/remove elements, perform server side : union, intersection, difference between sets, and so forth. Redis supports : different kind of sorting abilities.
看下具体都装了哪些内容
[root@localhost ~]#rpm -ql redis /etc/logrotate.d/redis /etc/rc.d/init.d/redis /etc/redis.conf /usr/bin/redis-benchmark /usr/bin/redis-check-aof /usr/bin/redis-check-dump /usr/bin/redis-cli /usr/sbin/redis-server /usr/share/doc/redis-2.4.10 /usr/share/doc/redis-2.4.10/00-RELEASENOTES /usr/share/doc/redis-2.4.10/BUGS /usr/share/doc/redis-2.4.10/CONTRIBUTING /usr/share/doc/redis-2.4.10/COPYING /usr/share/doc/redis-2.4.10/README /usr/share/doc/redis-2.4.10/TODO /var/lib/redis /var/log/redis /var/run/redis
查看日志滚动配置
[root@localhost ~]#cat /etc/logrotate.d/redis /var/log/redis/redis.log { weekly rotate 10 copytruncate delaycompress compress notifempty missingok }
查看创建的用户
[root@localhost ~]#id redis uid=496(redis) gid=492(redis) groups=492(redis)
查看服务文件
[root@localhost ~]#cat /etc/init.d/redis #!/bin/sh # # redis init file for starting up the redis daemon # # chkconfig: - 20 80 # description: Starts and stops the redis daemon. # Source function library. . /etc/rc.d/init.d/functions name="redis-server" exec="/usr/sbin/$name" pidfile="/var/run/redis/redis.pid" REDIS_CONFIG="/etc/redis.conf" [ -e /etc/sysconfig/redis ] && . /etc/sysconfig/redis lockfile=/var/lock/subsys/redis start() { [ -f $REDIS_CONFIG ] || exit 6 [ -x $exec ] || exit 5 echo -n $"Starting $name: " daemon --user ${REDIS_USER-redis} "$exec $REDIS_CONFIG" retval=$? echo [ $retval -eq 0 ] && touch $lockfile return $retval } stop() { echo -n $"Stopping $name: " killproc -p $pidfile $name retval=$? echo [ $retval -eq 0 ] && rm -f $lockfile return $retval } restart() { stop start } reload() { false } rh_status() { status -p $pidfile $name } rh_status_q() { rh_status >/dev/null 2>&1 } case "$1" in start) rh_status_q && exit 0 $1 ;; stop) rh_status_q || exit 0 $1 ;; restart) $1 ;; reload) rh_status_q || exit 7 $1 ;; force-reload) force_reload ;; status) rh_status ;; condrestart|try-restart) rh_status_q || exit 0 restart ;; *) echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart}" exit 2 esac exit $?
查看日志目录
因为以redis用户身份运行,日志目录要对redis有写权限 [root@localhost /var/lib]#ll /var/log/redis/ -d drwxr-xr-x 2 redis root 4096 Apr 1 2012 /var/log/redis/ [root@localhost /var/lib]#ll /var/run/redis/ -d drwxr-xr-x 2 redis root 4096 Apr 1 2012 /var/run/redis/
比较简单
===================================================================
下面是通过编译安装的redis
1、安装说明:
操作系统:centos6.8
redis版本:3.0.7 稳定版
redis安装目录:/usr/local/redis
redis日志目录:/var/log/redis
监听端口:6379
如果要测试的话,最好将php,nginx都安装好,便于测试
2、准备工作
[root@localhost ~]#useradd -r redis -s /sbin/nologin [root@localhost ~]#id redis uid=498(redis) gid=498(redis) groups=498(redis) [root@localhost ~]#yi gcc tcl jemalloc-devel #安装,缺什么安装什么
3、下载:
[root@localhost ~]#wget http://download.redis.io/releases/redis-3.0.7.tar.gz [root@localhost ~]#ls redis-3.0.7.tar.gz [root@localhost ~]#tar xf redis-3.0.7.tar.gz [root@localhost ~]#ls redis-3.0.7 redis-3.0.7.tar.gz [root@localhost ~]#cd redis-3.0.7 [root@localhost ~/redis-3.0.7]#ls 00-RELEASENOTES COPYING Makefile redis.conf runtest-sentinel tests BUGS deps MANIFESTO runtest sentinel.conf utils CONTRIBUTING INSTALL README runtest-cluster src
4、编译安装
[root@localhost ~/redis-3.0.7]#make [root@localhost ~/redis-3.0.7]#make PREFIX=/usr/local/redis install #指定可执行文件目录 [root@localhost ~/redis-3.0.7]#cd /usr/local [root@localhost /usr/local]#ls bin etc games include lib lib64 libexec redis rmfiles.sh [root@localhost /usr/local]#cd redis/ [root@localhost /usr/local/redis]#ls bin [root@localhost /usr/local/redis]#cd bin/ [root@localhost /usr/local/redis/bin]#ls redis-benchmark redis-check-aof redis-check-dump redis-cli redis-sentinel redis-server
5、修改配置文件
[root@localhost /usr/local/redis]#mkdir conf [root@localhost /usr/local/redis]#cd conf/ [root@localhost /usr/local/redis/conf]#cp /root/redis-3.0.7/redis.conf ./ #编译过程中没有修改过这个文件 [root@localhost /usr/local/redis/conf]#ls redis.conf [root@localhost /usr/local/redis/conf]#vim redis.conf #修改配置文件 # 修改一下配置 # redis以守护进程的方式运行 # no表示不以守护进程的方式运行(会占用一个终端) daemonize yes pidfile /usr/local/redis/run/redis.pid # 客户端闲置多长时间后断开连接,默认为0关闭此功能 timeout 300 # 数据库文件名,这里最好以所在的端口为名字,如果有多个redis的话会搞混 dbfilename redis-6379.rdb # 设置数据存放目录 dir /data/redis # 设置redis日志级别,默认级别:notice loglevel verbose # 设置日志文件的输出方式,如果以守护进程的方式运行redis 默认:"" # 并且日志输出设置为stdout,那么日志信息就输出到/dev/null里面去了 logfile "/var/log/redis/redis.log"
6、创建必要的目录,权限,因为进程是以redis用户运行的,这里的目录都要让redis有访问权限
[root@localhost /usr/local/redis]#mkdir run #创建pid文件目录 [root@localhost /usr/local/redis]#mkdir run/locl #创建lock文件目录 [root@localhost /usr/local/redis]#chown -R redis run/ [root@localhost /usr/local/redis]#ll total 12 drwxr-xr-x. 2 root root 4096 Jun 9 21:19 bin drwxr-xr-x. 2 root root 4096 Jun 10 01:18 conf drwxr-xr-x. 3 redis root 4096 Jun 9 21:28 run [root@localhost /var/log]#mkdir redis #创建日志目录文件 [root@localhost /var/log]#chown -R redis redis [root@localhost /var/log]#ll -d redis/ drwxr-xr-x. 2 redis root 4096 Jun 10 01:12 redis/ [root@localhost ~]#chown -R redis /data/redis #创建数据目录 [root@localhost ~]#cd /data/ [root@localhost /data]#ll total 4 drwxr-xr-x. 2 redis root 4096 Jun 10 01:36 redis
添加环境变量
[root@localhost ~]#cat /etc/profile.d/redis.sh #redis可执行目录添加到PATH环境变量中 PATH=/usr/local/redis/bin:$PATH export PATH [root@localhost ~]#source /etc/profile.d/redis.sh
7、为redis添加service服务文件,因为源码文件中没有提供service文件参考,这里根据上面rpm包修改,如果目录不同,需要根据实际情况修改
[root@localhost ~]#cat /etc/init.d/redis #!/bin/sh # # redis init file for starting up the redis daemon # # chkconfig: - 20 80 # description: Starts and stops the redis daemon. # Source function library. . /etc/rc.d/init.d/functions name="redis-server" exec="/usr/local/redis/bin/$name" pidfile="/usr/local/redis/run/redis.pid" REDIS_CONFIG="/usr/local/redis/conf/redis.conf" lockfile="/usr/local/redis/run/lock/redis" #以上内容需要根据实际情况修改 start() { [ -f $REDIS_CONFIG ] || exit 6 [ -x $exec ] || exit 5 echo -n $"Starting $name: " daemon --user ${REDIS_USER-redis} "$exec $REDIS_CONFIG" retval=$? echo [ $retval -eq 0 ] && touch $lockfile return $retval } stop() { echo -n $"Stopping $name: " killproc -p $pidfile $name retval=$? echo [ $retval -eq 0 ] && rm -f $lockfile return $retval } restart() { stop start } reload() { false } rh_status() { status -p $pidfile $name } rh_status_q() { rh_status >/dev/null 2>&1 } case "$1" in start) rh_status_q && exit 0 $1 ;; stop) rh_status_q || exit 0 $1 ;; restart) $1 ;; reload) rh_status_q || exit 7 $1 ;; force-reload) force_reload ;; status) rh_status ;; condrestart|try-restart) rh_status_q || exit 0 restart ;; *) echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart}" exit 2 esac exit $?
8、启动脚本配置
[root@localhost ~]#cd /etc/init.d/ [root@localhost /etc/init.d]#chmod +x redis [root@localhost /etc/init.d]#chkconfig --add redis [root@localhost /etc/init.d]#chkconfig --list redis redis 0:off 1:off 2:off 3:off 4:off 5:off 6:off [root@localhost /etc/init.d]#chkconfig redis on [root@localhost /etc/init.d]#chkconfig --list redis redis 0:off 1:off 2:on 3:on 4:on 5:on 6:off
添加日志滚动配置
[root@localhost ~]#cat /etc/logrotate.d/redis /var/log/redis/redis.log { weekly rotate 10 copytruncate delaycompress compress notifempty missingok }
9、服务启动
[root@localhost ~]#service redis start Starting Redis server: [ OK ] [root@localhost ~]#ss -tnl State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 128 *:6379 *:* LISTEN 0 128 :::6379 :::* [root@localhost /usr/local/redis/bin]#./redis-cli 127.0.0.1:6379> ping PONG
可以看到下面的目录都是要对redis有写权限的
[root@localhost ~]#ps aux | grep redis redis 5889 0.2 0.3 137452 7520 ? Ssl 01:51 0:00 /usr/local/redis/bin/redis-server *:6379 root 5910 0.0 0.0 103312 876 pts/1 S+ 01:54 0:00 grep redis [root@localhost ~]#ll /var/log/redis/ total 4 -rw-r--r--. 1 redis redis 2430 Jun 10 01:54 redis.log [root@localhost ~]#ll /usr/local/redis/run/ total 8 drwxr-xr-x. 2 redis root 4096 Jun 10 01:51 lock -rw-r--r--. 1 redis redis 5 Jun 10 01:51 redis.pid [root@localhost /data/redis]#ll total 4 -rw-r--r--. 1 redis redis 18 Jun 10 01:50 redis-6379.rdb
10、防护墙配置
# 打开防火墙规则 vim /etc/sysconfig/iptables # 增加内容 -A INPUT -m state --state NEW -m tcp -p tcp --dport 6379 -j ACCEPT # 重启火墙规则立即生效 service iptables restart
11、安装PHP Redis 扩展包,这样,php程序就可以操作redis中的数据了。注意文件安装路径,与前面编译安装php有关系,根据自己实际情况来操作
git官网:https://github.com/phpredis/phpredis
[root@bogon ~]#wget https://github.com/phpredis/phpredis/archive/develop.zip [root@bogon ~]#unzip develop.zip [root@bogon ~]#cd phpredis-develop/ [root@bogon ~/phpredis-develop]#ls #没有configure文件 arrays.markdown COPYING mkdeb-apache2.sh redis_array_impl.c redis_session.c cluster_library.c crc16.h mkdeb.sh redis_array_impl.h redis_session.h cluster_library.h CREDITS package.xml redis.c rpm cluster.markdown debian php_redis.h redis_cluster.c serialize.list common.h debian.control README.markdown redis_cluster.h tests config.m4 library.c redis_array.c redis_commands.c config.w32 library.h redis_array.h redis_commands.h [root@bogon /usr/local/php/bin]#ls pear peardev pecl phar phar.phar php php-cgi php-config phpdbg phpize [root@bogon ~/phpredis-develop]#/usr/local/php/bin/phpize #生成configure文件 Configuring for: PHP Api Version: 20151012 Zend Module Api No: 20151012 Zend Extension Api No: 320151012 [root@bogon ~/phpredis-develop]#ls acinclude.m4 config.h.in debian.control package.xml redis_commands.c aclocal.m4 config.m4 install-sh php_redis.h redis_commands.h arrays.markdown config.sub library.c README.markdown redis_session.c autom4te.cache configure library.h redis_array.c redis_session.h build configure.in ltmain.sh redis_array.h rpm cluster_library.c config.w32 Makefile.global redis_array_impl.c run-tests.php cluster_library.h COPYING missing redis_array_impl.h serialize.list cluster.markdown crc16.h mkdeb-apache2.sh redis.c tests common.h CREDITS mkdeb.sh redis_cluster.c config.guess debian mkinstalldirs redis_cluster.h [root@bogon ~/phpredis-develop]#./configure --with-php-config=/usr/local/php/bin/php-config [root@bogon ~/phpredis-develop]#make && make install #生成一个动态文件到下面的这个目录中 Installing shared extensions: /usr/local/php/lib/php/extensions/no-debug-non-zts-20131226/ [root@bogon /usr/local/php/conf/]#vim php.ini #将这个动态文件写入配置文件中 732 extension_dir = "/usr/local/php/lib/php/extensions/no-debug-non-zts-20131226/" 733 ; On windows: 734 extension = redis.so [root@localhost ~]#service php-fpm restart Gracefully shutting down php-fpm: [ OK ] Starting php-fpm: [ OK ]
用php测试,可以获取到redis中的值,测试正确,当用户访问网站,php会去lo网卡请求redis数据,将请求到的数据处理后返回给用户
[root@localhost /web]#cat redis.php <?php $redis = new Redis(); $redis->connect('127.0.0.1', 6379); $redis->set('key', 'test'); echo $redis->get('key'); [root@localhost /web]#cd [root@localhost ~]#curl localhost/redis.php test[root@localhost ~]#
上面内容只适合一台机器只有一个redis进程的方法,当有多个redis进程的时候,需要做修改。
–
–
–
评论前必须登录!
注册