背景:从某种意义来讲用memcache做缓存已经相当成熟,对于中小型的一些高并发也是可以的,但是伴随微博的兴起(现在好像不行了),有了No-sql这样一种东西出来,但究其从底层来讲和memcache不同在memcache是基于libevent的主进程下用pipe进行通讯的多线程机制,可能是不需要锁的还提供原子锁,而那个redis呢是基于epoll啥的事件驱动的异步的(好像是柱塞的,我再查下:是非阻塞IO ,epoll加自己实现的事件框架,redis实际上是采用了线程封闭的观念,把任务封闭在一个线程,自然避免了线程安全问题,不过对于需要依赖多个redis操作的复合操作来说,依然需要锁,而且有可能是分布式锁,这块不太明白,反正有利有弊吧,有了锁性能肯定不是太好。)一个单进程在跑,其规避了memcache的全放内存,而是隔一会儿会刷新到那个较为活跃的数据到硬盘里,还支持一部分的数据结构,结构化数据可能会影响其性能,其稳定性和高效来讲,我个人觉得和memcache差不多(mm略高一丝),但对大的粒度的存储来讲,可能mm性能更好,而对于小粒度那当然是redis了,这是因为一个是基于多线程和单进程,二是epoll这玩意好像生来就有这个对大粒度的东西不是太在行,比如:apache之于用epoll写的nginx,对于长时间的重逻辑处理来讲还是apache稳定,至于为什么,这个是实践出来的。获取数据和设置数据在协议上memcache是字符的,redis是二进制???
好了,下载一个最新稳定版本的吧:
http://download.redis.io/releases/redis-3.0.3.tar.gz
http://download.redis.io/releases/redis-stable.tar.gz
解压缩: tar -zxvf redis-3.0.3.tar.gz
cd redis-3.0.3
$ make
$make install
$cp redis.conf /etc/
Q:默认目录:/usr/local/bin 我不想装这儿啊。怎么办?
A:make完 不要install了 手工做
make完了之后,cd utils,里面有个install_server.sh,执行的时候会问你要安装到哪个路径
./install_server.sh
参数介绍:
make install命令执行完成后,会在/usr/local/bin目录下生成本个可执行文件,分别是redis-server、redis-cli、redis-benchmark、redis-check-aof 、redis-check-dump
redis-server:Redis服务器的daemon启动程序
redis-cli:Redis命令行操作工具。也可以用telnet根据其纯文本协议来操作
redis-benchmark:Redis性能测试工具,测试Redis在当前系统下的读写性能
redis-check-aof:数据修复
redis-check-dump:检查导出工具
修改系统配置文件,执行命令
a) echo vm.overcommit_memory=1 >> /etc/sysctl.conf
b) sysctl vm.overcommit_memory=1 或执行echo vm.overcommit_memory=1 >>/proc/sys/vm/overcommit_memory
修改redis配置文件
a) $ cd /etc
b) vi redis.conf
c) 修改daemonize yes---目的使进程在后台运行
启动redis
a) $ cd /usr/local/bin
b) ./redis-server /etc/redis.conf
/usr/local/bin/redis-server /etc/redis.conf
/usr/local/redis/bin/redis-server /usr/local/redis/etc/redis.conf
7. 检查是否启动成功
a) $ ps -ef | grep redis
果然是单进程且是epoll:
[root@localhost bin]# ps -ef | grep redis
root 317 1 0 17:20 ? 00:00:00 ./redis-server *:6379
[root@localhost bin]# strace -f -p 317
Process 317 attached with 3 threads - interrupt to quit
[pid 325] futex(0x6e6c54, FUTEX_WAIT_PRIVATE, 1, NULL <unfinished ...>
[pid 324] futex(0x6e6c24, FUTEX_WAIT_PRIVATE, 1, NULL <unfinished ...>
[pid 317] epoll_wait(3, {}, 10128, 3) = 0
[pid 317] open("/proc/317/stat", O_RDONLY) = 6
[pid 317] read(6, "317 (redis-server) R 1 317 317 0"..., 4096) = 233
[pid 317] close(6) = 0
[pid 317] epoll_wait(3, {}, 10128, 100) = 0
[pid 317] open("/proc/317/stat", O_RDONLY) = 6
[pid 317] read(6, "317 (redis-server) R 1 317 317 0"..., 4096) = 233
[pid 317] close(6) = 0
[pid 317] epoll_wait(3, {}, 10128, 100) = 0
[pid 317] open("/proc/317/stat", O_RDONLY) = 6
[pid 317] read(6, "317 (redis-server) R 1 317 317 0"..., 4096) = 233
[pid 317] close(6) = 0
[pid 317] epoll_wait(3, <unfinished ...>
PHP是最好的语言,那安装个PHP扩展,选个自己的PHP版本的扩展:
https://github.com/nicolasff/phpredis/downloads
[root@localhost software]# php -v
PHP 5.3.10 (cli) (built: Jun 4 2013 11:51:25)
Win:https://github.com/downloads/phpredis/phpredis/php_redis-5.3-vc9-ts-73d99c3e.zip
*nix: https://github.com/phpredis/phpredis
https://github.com/phpredis/phpredis/releases
Linux代码 收藏代码
$ tar zxvf nicolasff-phpredis-2.2.2-48-g7dfac44.tar.gz
$ cd nicolasff-phpredis-7dfac44/
$ /usr/local/webserver/php/bin/phpize
$ ./configure --with-php-config=/usr/local/webserver/php/bin/php-config
$ make
$ make install
3 修改php.ini
查看/usr/local/webserver/php/lib/php/extensions/no-debug-non-zts-20060613是否有redis.so
[root@localhost phpredis-2.2.7]# make install
Installing shared extensions: /usr/local/php/lib/php/extensions/no-debug-non-zts-20090626/
在php.ini中 添加
extension=redis.so
/usr/local/php/lib/php/extensions/no-debug-non-zts-20090626/redis.so
[root@localhost phpredis-2.2.7]# php -i|grep php.ini
Configuration File (php.ini) Path => /usr/local/php/etc
Loaded Configuration File => /usr/local/php/etc/php.ini
[root@localhost phpredis-2.2.7]# vi /usr/local/php/etc/php.ini
4 重启php进程
$ /usr/local/webserver/php/sbin/php-fpm restart
5 查看phpinfo() 是否有redis扩展
6 用PHP文件测试
Php代码
输出:Hello World
成功!
参考来自:http://alfred-long.iteye.com/blog/1685092
redis server安装部分摘自并参考:http://www.cnblogs.com/zhuhongbao/archive/2013/06/04/3117997.html
开机启动,没试过,需要实践:
把上述代码存为redis,放到/etc/init.d/下面
[html] view plaincopy在CODE上查看代码片派生到我的代码片
chmod +x /etc/init.d/redis
设定开机启动服务
sudo chkconfig redis on
启动,停止redis
service redis start #或者 /etc/init.d/redis start
service redis stop #或者 /etc/init.d/redis stop
测试redis
redis-cli
redis 127.0.0.1:6379> set foo 123
OK
redis 127.0.0.1:6379> get foo
"123"
redis 127.0.0.1:6379> exit
好了,下载一个最新稳定版本的吧:
http://download.redis.io/releases/redis-3.0.3.tar.gz
http://download.redis.io/releases/redis-stable.tar.gz
解压缩: tar -zxvf redis-3.0.3.tar.gz
cd redis-3.0.3
$ make
$make install
$cp redis.conf /etc/
Q:默认目录:/usr/local/bin 我不想装这儿啊。怎么办?
A:make完 不要install了 手工做
make完了之后,cd utils,里面有个install_server.sh,执行的时候会问你要安装到哪个路径
./install_server.sh
参数介绍:
make install命令执行完成后,会在/usr/local/bin目录下生成本个可执行文件,分别是redis-server、redis-cli、redis-benchmark、redis-check-aof 、redis-check-dump
redis-server:Redis服务器的daemon启动程序
redis-cli:Redis命令行操作工具。也可以用telnet根据其纯文本协议来操作
redis-benchmark:Redis性能测试工具,测试Redis在当前系统下的读写性能
redis-check-aof:数据修复
redis-check-dump:检查导出工具
修改系统配置文件,执行命令
a) echo vm.overcommit_memory=1 >> /etc/sysctl.conf
b) sysctl vm.overcommit_memory=1 或执行echo vm.overcommit_memory=1 >>/proc/sys/vm/overcommit_memory
修改redis配置文件
a) $ cd /etc
b) vi redis.conf
c) 修改daemonize yes---目的使进程在后台运行
启动redis
a) $ cd /usr/local/bin
b) ./redis-server /etc/redis.conf
/usr/local/bin/redis-server /etc/redis.conf
/usr/local/redis/bin/redis-server /usr/local/redis/etc/redis.conf
7. 检查是否启动成功
a) $ ps -ef | grep redis
果然是单进程且是epoll:
[root@localhost bin]# ps -ef | grep redis
root 317 1 0 17:20 ? 00:00:00 ./redis-server *:6379
[root@localhost bin]# strace -f -p 317
Process 317 attached with 3 threads - interrupt to quit
[pid 325] futex(0x6e6c54, FUTEX_WAIT_PRIVATE, 1, NULL <unfinished ...>
[pid 324] futex(0x6e6c24, FUTEX_WAIT_PRIVATE, 1, NULL <unfinished ...>
[pid 317] epoll_wait(3, {}, 10128, 3) = 0
[pid 317] open("/proc/317/stat", O_RDONLY) = 6
[pid 317] read(6, "317 (redis-server) R 1 317 317 0"..., 4096) = 233
[pid 317] close(6) = 0
[pid 317] epoll_wait(3, {}, 10128, 100) = 0
[pid 317] open("/proc/317/stat", O_RDONLY) = 6
[pid 317] read(6, "317 (redis-server) R 1 317 317 0"..., 4096) = 233
[pid 317] close(6) = 0
[pid 317] epoll_wait(3, {}, 10128, 100) = 0
[pid 317] open("/proc/317/stat", O_RDONLY) = 6
[pid 317] read(6, "317 (redis-server) R 1 317 317 0"..., 4096) = 233
[pid 317] close(6) = 0
[pid 317] epoll_wait(3, <unfinished ...>
PHP是最好的语言,那安装个PHP扩展,选个自己的PHP版本的扩展:
https://github.com/nicolasff/phpredis/downloads
[root@localhost software]# php -v
PHP 5.3.10 (cli) (built: Jun 4 2013 11:51:25)
Win:https://github.com/downloads/phpredis/phpredis/php_redis-5.3-vc9-ts-73d99c3e.zip
*nix: https://github.com/phpredis/phpredis
https://github.com/phpredis/phpredis/releases
Linux代码 收藏代码
$ tar zxvf nicolasff-phpredis-2.2.2-48-g7dfac44.tar.gz
$ cd nicolasff-phpredis-7dfac44/
$ /usr/local/webserver/php/bin/phpize
$ ./configure --with-php-config=/usr/local/webserver/php/bin/php-config
$ make
$ make install
3 修改php.ini
查看/usr/local/webserver/php/lib/php/extensions/no-debug-non-zts-20060613是否有redis.so
[root@localhost phpredis-2.2.7]# make install
Installing shared extensions: /usr/local/php/lib/php/extensions/no-debug-non-zts-20090626/
在php.ini中 添加
extension=redis.so
/usr/local/php/lib/php/extensions/no-debug-non-zts-20090626/redis.so
[root@localhost phpredis-2.2.7]# php -i|grep php.ini
Configuration File (php.ini) Path => /usr/local/php/etc
Loaded Configuration File => /usr/local/php/etc/php.ini
[root@localhost phpredis-2.2.7]# vi /usr/local/php/etc/php.ini
4 重启php进程
$ /usr/local/webserver/php/sbin/php-fpm restart
5 查看phpinfo() 是否有redis扩展
6 用PHP文件测试
Php代码
输出:Hello World
成功!
参考来自:http://alfred-long.iteye.com/blog/1685092
redis server安装部分摘自并参考:http://www.cnblogs.com/zhuhongbao/archive/2013/06/04/3117997.html
开机启动,没试过,需要实践:
把上述代码存为redis,放到/etc/init.d/下面
[html] view plaincopy在CODE上查看代码片派生到我的代码片
chmod +x /etc/init.d/redis
设定开机启动服务
sudo chkconfig redis on
启动,停止redis
service redis start #或者 /etc/init.d/redis start
service redis stop #或者 /etc/init.d/redis stop
测试redis
redis-cli
redis 127.0.0.1:6379> set foo 123
OK
redis 127.0.0.1:6379> get foo
"123"
redis 127.0.0.1:6379> exit
作者:jackxiang@向东博客 专注WEB应用 构架之美 --- 构架之美,在于尽态极妍 | 应用之美,在于药到病除
地址:https://jackxiang.com/post/8231/
版权所有。转载时必须以链接形式注明作者和原始出处及本声明!
最后编辑: jackxiang 编辑于2016-3-9 22:55
评论列表