perl 操作redis

<pre name="code" class="sql"><pre name="code" class="sql">1.查看安装的redis版本
jrhrpt01:/root# redis-server --version
Redis server v=2.8.19 sha=00000000:0 malloc=jemalloc-3.6.0 bits=64 build=1138bcb7ae3eee58

源码包为redis-2.8.19.tar.gz

2.
yum -y install  gcc gcc-c++ gcc-g77 autoconf automake zlib* fiex* libxml* ncurses* libmcrypt* libtool-ltdl-devel* make cmake 


3.开始编译
test-redis:/root/redis-2.8.19# make install

Hint: It's a good idea to run 'make test' ;)

    INSTALL install
    INSTALL install
    INSTALL install
    INSTALL install
    INSTALL install
make[1]: Leaving directory `/root/redis-2.8.19/src'

4.

安装成功之后会在src文件夹内有redis-server和redis-cli两个命令
建议将其放到bin下
cp redis-server /usr/local/bin/
cp redis-cli /usr/local/bin/

test-redis:/root/redis-2.8.19# cp redis.conf /etc/


5.启动redis

redis-server /etc/redis.conf


redis 设置:

1.设置密码
# Warning: since Redis is pretty fast an outside user can try up to
# 150k passwords per second against a good box. This means that you should
# use a very strong password otherwise it will be very easy to break.
#
requirepass r,uLgt.313 

2.
# The Append Only File will also be created inside this directory.
#
# Note that you must specify a directory here, not a file name.
dir /data01/redis/



perl 访问redis:

介绍下怎么安装:
脚本用到了perl的Redis库,需要先安装这个:
test-redis:/root# tar -zxvf Redis-1.955.tar.gz 

  cd Redis-1.955
   ls

  perl Makefile.PL 
  make
  make install

test-redis:/root# perldoc   Redis
test-redis:/root# perl a1.pl 
Can't locate Try/Tiny.pm in @INC (@INC contains: /usr/local/lib64/perl5 /usr/local/share/perl5 /usr/lib64/perl5/vendor_perl /usr/share/perl5/vendor_perl /usr/lib64/perl5 /usr/share/perl5 .) at 

/usr/local/share/perl5/Redis.pm line 18.
BEGIN failed--compilation aborted at /usr/local/share/perl5/Redis.pm line 18.
Compilation failed in require at a1.pl line 1.
BEGIN failed--compilation aborted at a1.pl line 1.
我一运行 就包这个错



需要安装:
test-redis:/root# cpan  Try::Tiny

[root@redis01 ~]# perl redis.pl 
Can't locate Time/HiRes.pm in @INC (@INC contains: /usr/local/lib64/perl5 /usr/local/share/perl5 /usr/lib64/perl5/vendor_perl /usr/share/perl5/vendor_perl /usr/lib64/perl5 /usr/share/perl5 .) at /usr/local/share/perl5/Redis.pm line 405.

[root@redis01 ~]# yum install perl-Time-HiRes


6.redis 连接不上会报错:

test-redis:/root# cat a1.pl 
use Redis;
my $setRedis = Redis->new( server => "192.168.32.168:6379",reconnect => 1,every=>60);
test-redis:/root# perl a1.pl
Could not connect to Redis server at 192.168.32.168:6379: No route to host at /usr/local/share/perl5/Redis.pm line 423
	Redis::__build_sock('Redis=HASH(0x1b4a250)') called at /usr/local/share/perl5/Redis.pm line 410
	eval {...} called at /usr/local/share/perl5/Redis.pm line 410
	Redis::__connect('Redis=HASH(0x1b4a250)') called at /usr/local/share/perl5/Redis.pm line 68
	Redis::new('Redis', 'server', '192.168.32.168:6379', 'reconnect', 1, 'every', 60) called at a1.pl line 2
	...propagated at /usr/local/share/perl5/Redis.pm line 413.

192.168.32.167 上没有redis服务



密码认证:
127.0.0.1:6379> auth 'r,uLgt.313'
OK

127.0.0.1:6379> auth  'r,uLgt.313'
OK
127.0.0.1:6379> get name
(nil)
127.0.0.1:6379> set name ww2
OK
127.0.0.1:6379> get name
"ww2"
127.0.0.1:6379> save
OK


set --set(key, value):给数据库中名称为key的string赋予值valu

get ---取值

save 保存



use Redis;
my $r = Redis->new( server => "192.168.32.167:6379",reconnect => 1,every=>60);
 $r = Redis->new( password =>  'r,uLgt.313' );
  $r->set('wangpin','147258369');
  $r->save;
  $r->quit;




                                    
原文地址:https://www.cnblogs.com/hzcya1995/p/13351685.html