NOSQL系列-Redis精简版安装与Ruby测试

前端之家收集整理的这篇文章主要介绍了NOSQL系列-Redis精简版安装与Ruby测试前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

简介:Redis是一个NOsql数据库,它是一款key-values存储型数据库,也叫“memcached改进版”,它不同与memcached最大特点是它由内存+硬盘来存储数据的,它支持存储的values类型很多,包括string字符串、list链表、set集合、zset有序集合、散列表等。这些数据类型都支持push/popadd/remove及取交集并集和差集及更丰富的操作,而且这些操作都是原子性的。Redis支持不同方式的排序,与memcached一样,数据都是缓存在内存中的,但会定期把更新的数据写入硬盘,在此基础上实现了master-slave主从同步。Redis还提供了广泛的开发语言客户端,rubyperljavacc++PHPscalaclojurenode.jsobjective-C

一、实验环境

1.系统类型

[leo@h4 etc]$ cat issue

CentOS Linux release 6.0 (Final)

Kernel \r on an \m

2.系统位数

[leo@h4 桌面]$ getconf LONG_

32

Linux ISOCentOS-6.0-i386-bin-DVD.iso 32

JDK version"1.6.0_25-ea"

虚拟机:Product VMware® Workstation Version 7.0.0 build-203739

3.CentOS需要能连入网络,这个比较简单,可以参考《NOSQL系列-memcached安装管理与repcached高可用性》,这篇文章在一开始就介绍了虚拟机如何连接上网

http://f.dataguru.cn/thread-46905-1-1.html

4.Redis版本:redis.i386 0:2.4.10-1.el5

5.EPLE版本:http://dl.Fedoraproject.org/pub/epel/5/i386/epel-release-5-4.noarch.rpm

6.Ruby 版本:ruby-1.9.2-p180


二、Redis 安装与配置

1.我们使用yum方式安装redis数据库

[root@h4 ~]# ping 202.99.96.68 测试一下,现在我们可以连接到外网

PING 202.99.96.68 (202.99.96.68) 56(84) bytes of data.

64 bytes from 202.99.96.68: icmp_seq=1 ttl=245 time=4.14 ms

64 bytes from 202.99.96.68: icmp_seq=2 ttl=245 time=4.44 ms

64 bytes from 202.99.96.68: icmp_seq=3 ttl=245 time=4.12 ms

64 bytes from 202.99.96.68: icmp_seq=4 ttl=245 time=5.60 ms

注:如果是新启动的VMware系统,有可能会连不上网,没关系,我们重启下network服务即可恢复

[root@h4 ~]# service network restart

[root@h4 ~]# yum install redis

Loaded plugins: fastestmirror,refresh-packagekit

Loading mirror speeds from cached hostfile

* base: mirrors.neusoft.edu.cn

* extras: mirrors.neusoft.edu.cn

* updates: mirrors.neusoft.edu.cn

base | 3.7 kB 00:00

extras | 3.5 kB 00:00

updates | 3.5 kB 00:00

updates/primary_db | 4.3 MB 00:26

Setting up Install Process

No package redis available. 没有可用的redis

Error: Nothing to do

CentOSRedHat企业版(收费)编译过来的社区版(免费),去掉了所有关于版权问题的东西。如果我们想使用收费版yum源(软件包齐全),我们就需要安装EPEL(Extra Packages for Enterprise Linux )即企业版Linux的扩展包,EPLE提供了很多RedhatCentos共用的组件,安装完这个以后基本常用的rpm包都可以找到了^_^

32位系统选择:(我是32位)

[root@h4 ~]# rpm -ivh http://dl.Fedoraproject.org/pub/ ... ease-5-4.noarch.rpm

Retrieving http://dl.Fedoraproject.org/pub/ ... ease-5-4.noarch.rpm

warning: /var/tmp/rpm-tmp.UX54fR: Header V3 DSA/SHA1 Signature,key ID 217521f6: NOKEY

Preparing... ########################################### [100%]

1:epel-release ########################################### [100%]

64位系统选择:

rpm -ivh http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm

近水楼台先得月可参考http://f.dataguru.cn/thread-47927-1-1.html感谢zhaoyg 童鞋,它是64

下面我们再次安装redis -> yum install redis

[root@h4 ~]# yum install redis

Loaded plugins: fastestmirror,refresh-packagekit

Loading mirror speeds from cached hostfile

* base: mirrors.neusoft.edu.cn

* epel: mirrors.neusoft.edu.cn

* extras: mirrors.neusoft.edu.cn

* updates: mirrors.neusoft.edu.cn

epel | 3.7 kB 00:00

epel/primary_db | 3.1 MB 00:15

Setting up Install Process

Resolving Dependencies

--> Running transaction check

---> Package redis.i386 0:2.4.10-1.el5 set to be updated

--> Finished Dependency Resolution

Dependencies Resolved

=======================================================================================

Package Arch Version Repository Size

=======================================================================================

Installing:

redis i386 2.4.10-1.el5 epel 299 k

Transaction Summary

=======================================================================================

Install 1 Package(s)

Upgrade 0 Package(s)

Total download size: 299 k

Installed size: 646 k

Is this ok [y/N]:y 是否安装redis yes

Downloading Packages:

redis-2.4.10-1.el5.i386.rpm warning: rpmts_HdrFromFdno: Header V4 DSA/SHA1 Signature,key ID 217521f6: NOKEY | 299 kB 00:01

epel/gpgkey Importing GPG key 0x217521F6 "Fedora EPEL <epel@fedoraproject.org>" from /etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL | 3.3 kB 00:00 ... 是否导入密钥

Is this ok [y/N]: y

Running rpm_check_debug

Running Transaction Test

Transaction Test Succeeded

Running Transaction

Warning: RPMDB altered outside of yum. 警告已经修改yum

Installing:redis-2.4.10-1.el5.i386 1/1

Installed:

redis.i386 0:2.4.10-1.el5

Complete! 到此redis软件安装完毕,下面我们开始配置

配置redis

2.找一找redis安装到哪些目录中了

[root@h4 ~]# find / -name "redis*"

/var/lib/redis

/var/run/redis

/var/log/redis

/usr/bin/redis-cli

/usr/bin/redis-check-dump

/usr/bin/redis-check-aof

/usr/bin/redis-benchmark

/usr/sbin/redis-server redis服务器主程序

/usr/share/gimp/2.0/patterns/redishstuff.pat

/usr/share/doc/redis-2.4.10

/etc/logrotate.d/redis

/etc/redis.conf redis配置文件,启动参数都在这里配置

/etc/rc.d/init.d/redis redis开机启动服务

3.设置redis参数文件/etc/redis.conf

[root@h4 ~]# vim /etc/redis.conf

daemonize yes 启动redis后要不要后台运行

port 6379 redis服务默认端口号,如果启动多个服务端口号要设置不同

bind 127.0.0.1 绑定ip地址

timeout 0 telnet连接服务过期时间,0 代表关闭这项功能

loglevel notice 日志级别

SNAPSHOTTING 快照设置

4.启动redis服务

[root@h4 ~]# redis-server /etc/redis.conf 按照此配置文件的参数设置启动服务

5.验证启动是否成功

[root@h4 ~]# pstree 索引树方式查看

├─redis-server───2*[{redis-server}] 启动一个redis服务,包含2个线程

[root@h4 ~]# ps aux | grep redis 进程列表方式查看

root 187870.00.2312801080 ? Ssl15:42 0:00 redis-server /etc/redis.conf

这还是启动命令

6.使用telnet简单测试

我们知道redismemcached的改进版,那么命令语法也是大同小异

[root@h4 ~]# telnet localhost 6379

Trying 127.0.0.1...

Connected to localhost.

Escape character is '^]'.

set leonarding 8 输入key-value key=leonarding value=8

+OK

get leonarding 输出key-value

$1 $1: 字符串类型,1个字符长度

8 显示value

set liu sheng 输入key-value key=liu value=sheng

+OK

get liu 输出key-value

$5 $5: 字符串类型,5个字符长度

Sheng 显示value

测试链表

lpush 11 22 定义第一个链表lpush表头添加结点(左边添加 key=11value=22

:1

lpush 33 44 定义第二个链表 key=33value=44

:1

rpush 55 66 定义第三个链表rpush表尾添加结点(右边添加key=55value=66

:1

lrange 11 0 -1 显示第一个链表,0-1 表示从头到尾显示链表

*1

$2

22

lrange 33 0 -1 显示第二个链表,*1 表示链表中有1个结点

*1

$2 $2字符串类型,2个字符长度

44

lrange 55 0 -1 显示第三个链表,说明链表是基于key为单位组成的

*1

$2

66

del 33 删除链表33

:1

del 55 删除链表55

:1

del 11 删除链表11

:1

quit 退出redis

+OK

Connection closed by foreign host.


三、Ruby连接Redis数据库测试

1.下载ruby程序包

[root@h4 ~]# rpm -qa | grep ruby

由于我没有安装过ruby语言程序,所以什么都没有显示

[root@h4 ~]# mkdir ruby 先建立一个ruby目录用来存放ruby安装程序包

[root@h4 ~]# cd ruby 进入目录

首先要安装ruby软件包

Ruby版本:http://ftp.ruby-lang.org/pub/ruby/1.9/ruby-1.9.2-p180.tar.gz

99% [========================================> ] 11,154,81526.9K/s eta(英国中部时

100%[===============================================>] 11,158,93527.5K/s

当进度条走到100%ruby程序完成下载

[root@h4 ruby]# chmod 777 ruby-1.9.2-p180.tar.gz 授予777权限

[root@h4 ruby]# ll

rwxrwxrwx. 1 root root 111589352182011 ruby-1.9.2-p180.tar.gz

2.解压ruby程序包

[root@h4 ruby]# tar -zxvf ruby-1.9.2-p180.tar.gz

[root@h4 ruby]# cd ruby-1.9.2-p180 进入ruby程序包目录

3.运行config文件

[root@h4 ruby-1.9.2-p180]# ./configure 运行ruby配置文件

checking build system type... i686-pc-linux-gnu

checking host system type... i686-pc-linux-gnu 。。。。。。检查系统类型

config.status: creating Makefile 创建Makefile

4.编译ruby并把编译好的结果进行安装

[root@h4 ruby-1.9.2-p180]# make 编译源文件,需要等待10分钟

gcc -O3 -ggdb -Wextra -Wno-unused-parameter -Wno-parentheses -Wpointer-arith -Wwrite-strings -Wno-missing-field-initializers -Wno-long-long -I. -I.ext/include/i686-linux -I./include -I. -DRUBY_EXPORT -o dmyversion.o -c dmyversion

[root@h4 ruby-1.9.2-p180]# make install 把编译好的结果进行安装

./miniruby -I./lib -I.ext/common -I./- -r./ext/purelib.rb./tool/generic_erb.rb -c -o encdb.h ./template/encdb.h.tmpl ./enc enc

encdb.h unchanged

make -f enc.mk RUBY="./miniruby -I./lib -I.ext/common -I./- -r./ext/purelib.rb " MINIRUBY="./miniruby -I./lib -I.ext/common -I./- -r./ext/purelib.rb "srcs

make[1]: Entering directory `/root/ruby/ruby-1.9.2-p180'

5.找一找ruby安装到哪些目录下

[root@h4 ruby-1.9.2-p180]# find / -name ruby

/root/ruby

/root/ruby/ruby-1.9.2-p180/ruby

/root/ruby/ruby-1.9.2-p180/test/ruby

/root/ruby/ruby-1.9.2-p180/include/ruby

/root/ruby/ruby-1.9.2-p180/.ext/include/ruby

/root/ruby/ruby-1.9.2-p180/.ext/include/i686-linux/ruby

/home/grid/hbase-0.90.5/lib/ruby

/home/grid/hbase-0.90.5/src/test/ruby

/home/grid/hbase-0.90.5/src/main/ruby

/usr/local/include/ruby-1.9.1/ruby

/usr/local/include/ruby-1.9.1/i686-linux/ruby

/usr/local/lib/ruby

/usr/local/bin/ruby

/usr/local/share/doc/ruby

/usr/share/swig/1.3.40/ruby

6.设置/etc/profile环境变量,对全局有效

export PATH=/usr/java/jdk1.6.0_25/bin:/usr/local/bin:/sbin:$PATH 添加红色字符串

[root@h4 bin]$ ruby 到此ruby程序已经安装到系统中可以运行ruby脚本

ruby: Interrupt

-:1: Syntax error,unexpected ':',expecting $end

ruby: Interrupt

^

7.安装rubygems组件(通过这个组件安装redis-rb程序库)

[root@h4 ruby-1.9.2-p180]# yum install rubygems

Loaded plugins: fastestmirror,refresh-packagekit

Loading mirror speeds from cached hostfile

* base: mirrors.grandcloud.cn

* epel: mirrors.ustc.edu.cn

* extras: mirrors.grandcloud.cn

* updates: mirrors.grandcloud.cn

epel | 3.7 kB 00:00

Setting up Install Process

Resolving Dependencies

--> Running transaction check

---> Package rubygems.noarch 0:1.3.7-1.el6 set to be updated

--> Processing Dependency: ruby(abi) = 1.8 for package: rubygems-1.3.7-1.el6.noarch

--> Processing Dependency: ruby-rdoc for package: rubygems-1.3.7-1.el6.noarch

--> Processing Dependency: /usr/bin/ruby for package: rubygems-1.3.7-1.el6.noarch

--> Running transaction check

---> Package ruby.i686 0:1.8.7.352-7.el6_2 set to be updated

---> Package ruby-libs.i686 0:1.8.7.352-7.el6_2 set to be updated

--> Processing Dependency: libreadline.so.5 for package: ruby-libs-1.8.7.352-7.el6_2.i686

---> Package ruby-rdoc.i686 0:1.8.7.352-7.el6_2 set to be updated

--> Processing Dependency: ruby-irb = 1.8.7.352-7.el6_2 for package: ruby-rdoc-1.8.7.352-7.el6_2.i686

--> Running transaction check

---> Package compat-readline5.i686 0:5.2-17.1.el6 set to be updated

---> Package ruby-irb.i686 0:1.8.7.352-7.el6_2 set to be updated

--> Finished Dependency Resolution

Is this ok [y/N]: y

Total 211 kB/s | 3.2 MB 00:15

Running rpm_check_debug

Running Transaction Test

Transaction Test Succeeded

Running Transaction

Installing : compat-readline5-5.2-17.1.el6.i686 1/6

Installing : ruby-libs-1.8.7.352-7.el6_2.i686 2/6

Installing : ruby-1.8.7.352-7.el6_2.i686 3/6

Installing : ruby-irb-1.8.7.352-7.el6_2.i686 4/6

Installing : ruby-rdoc-1.8.7.352-7.el6_2.i686 5/6

Installing : rubygems-1.3.7-1.el6.noarch 6/6

Installed:

rubygems.noarch 0:1.3.7-1.el6

Dependency Installed:

compat-readline5.i686 0:5.2-17.1.el6 ruby.i686 0:1.8.7.352-7.el6_2 ruby-irb.i686 0:1.8.7.352-7.el6_2 ruby-libs.i686 0:1.8.7.352-7.el6_2

ruby-rdoc.i686 0:1.8.7.352-7.el6_2

Complete!

如果遇上如下报错请参考下面解决方

[root@h4 ruby-1.9.2-p180]# gem install redis

ERROR:Loading command: install (LoadError)

no such file to load – zlib 原因是gem运行缺少zlib支持

ERROR:While executing gem ... (NameError)

uninitialized constant Gem::Commands::InstallCommand

解决方

[root@h4 ruby-1.9.2-p180]# yum -y install gcc

Loaded plugins: fastestmirror,refresh-packagekit

Loading mirror speeds from cached hostfile

由于安装系统的时候没有装c编译器 导致很多源码安装的程序无法编译 所以需要在linux上装c编译器,必须按顺序安装否则安装不会成功

[root@h4 ruby-1.9.2-p180]# yum install zlib-devel 安装zlib-devel

Loaded plugins: fastestmirror,refresh-packagekit

Loading mirror speeds from cached hostfile

[root@h4 ruby-1.9.2-p180]# cd ext/zlib/ 进入这个目录

[root@h4 zlib]# ruby extconf.rb 执行这条命令

checking for deflateReset() in -lz... yes

checking for zlib.h... yes

checking for kind of operating system... Unix

checking for crc32_combine() in zlib.h... yes

checking for adler32_combine() in zlib.h... yes

creating Makefile

[root@h4 zlib]# make && make install 编译并安装

gcc -I. -I/usr/local/include/ruby-1.9.1/i686-linux -I/usr/local/include/ruby-1.9.1/ruby/backward -I/usr/local/include/ruby-1.9.1 -I. -DHAVE_ZLIB_H-DOS_CODE=OS_UNIX -DHAVE_CRC32_COMBINE -DHAVE_ADLER32_COMBINE-D_FILE_OFFSET_BITS=64-fPIC -O3 -ggdb -Wextra -Wno-unused-parameter -Wno-parentheses -Wpointer-arith -Wwrite-strings -Wno-missing-field-initializers -Wno-long-long-o zlib.o -c zlib.c

gcc -shared -o zlib.so zlib.o -L. -L/usr/local/lib -Wl,-R/usr/local/lib -L.-rdynamic -Wl,-export-dynamic -lz-lpthread -lrt -ldl -lcrypt -lm -lc

/usr/bin/install -c -m 0755 zlib.so /usr/local/lib/ruby/site_ruby/1.9.1/i686-linux

[root@h4 zlib]# gem list

*** LOCAL GEMS ***

minitest (1.6.0)

rake (0.8.7)

rdoc (2.5.8)

到此我们已经解决了“gem运行缺少zlib库”问题,现在我们可以正常gem install redis

8.安装redis-rb程序库,因为只有安装了此程序库ruby才能正常访问redis数据库

[root@h4 zlib]# gem install redis

Successfully installed redis-3.0.1

1 gem installed

Installing ri documentation for redis-3.0.1...

Installing RDoc documentation for redis-3.0.1...

[root@h4 zlib]#

感谢天感谢地,我们终于安装上了redis-rb,经历了多少个日夜,经历了多少个困苦,查阅了多少资料,当我们迎来成功之时,一切都是值得的,台上10分钟 台下十年功

9.编写ruby脚本访问redis数据库

[root@h4 ruby]# vim leonarding_redisrb1.rb 编写ruby脚本,创建链表

require 'rubygems' 引用程序库

require 'redis'

redis=Redis.new 开启两个终端进程访问redis数据库

redis2=Redis.new

hoge = "leonarding"; 设置keyhoge leonarding

redis.lpush(hoge,3) 创建链表,在表头添加三个结点

redis.lpush(hoge,2) value 3 2 1 4

redis.lpush(hoge,1)

redis.rpush(hoge,4) 在表尾添加一个结点

p redis.lrange(hoge,-1) 从头到尾显示链表

[root@h4 ruby]# ruby leonarding_redisrb1.rb 执行ruby脚本

/usr/local/lib/ruby/gems/1.9.1/gems/redis-3.0.1/lib/redis/client.rb:79:in `call': ERR Operation against a key holding the wrong kind of value (Redis::CommandError)

from /usr/local/lib/ruby/gems/1.9.1/gems/redis-3.0.1/lib/redis.rb:859:in `block in lpush'

from /usr/local/lib/ruby/gems/1.9.1/gems/redis-3.0.1/lib/redis.rb:36:in `block in synchronize'

from /usr/local/lib/ruby/1.9.1/monitor.rb:201:in `mon_synchronize'

from /usr/local/lib/ruby/gems/1.9.1/gems/redis-3.0.1/lib/redis.rb:36:in `synchronize'

from /usr/local/lib/ruby/gems/1.9.1/gems/redis-3.0.1/lib/redis.rb:858:in `lpush'

from leonarding_redisrb1.rb:7:in `<main>'

报错这是为什么呢,想起来了,不同的ruby版本语法不同,我们来修改一下

Ruby 版本:ruby-1.9.2-p180

#############################################################################

新版leonarding_redisrb1.rb

[root@h4 ruby]# vim leonarding_redisrb1.rb

require 'rubygems'

require 'redis'

redis=Redis.new

redis2=Redis.new

hoge = "leonarding";

redis.lpush:hoge,3

redis.lpush:hoge,2

redis.lpush:hoge,1

redis.rpush:hoge,4

p redis.lrange:hoge,-1

执行ruby脚本

[root@h4 ruby]# ruby leonarding_redisrb1.rb

["1","2","3","4"]

[root@h4 ruby]# ruby leonarding_redisrb1.rb

["1","1","4","4"]

value=4始终从表尾添加,符合题意

#################################################################################

我们在编写一个ruby脚本,测试不同终端是否可以访问同一个数据库

[root@h4 ruby]# vim leonarding_redisrb2.rb

require 'rubygems'

require 'redis'

redis=Redis.new

redis2=Redis.new 开启2个终端进程访问redis数据库

hoge = "leonarding";

redis.lpush:hoge,4

redis2.rpush:hoge,5

p redis.lrange:hoge,-1

使用redis2进程从表尾添加结点value=5,看看是不是能够操作同一个链表,那就证明我们连接的是同一个redis数据库,并且操作是具有原子性和一致性的

执行ruby脚本

[root@h4 ruby]# ruby leonarding_redisrb2.rb

["1","5"]

小结:Redis精简版安装与Ruby测试到此总结完毕,通过上面的实验我们应该掌握如下内容

(1)redis简要概念

(2)redis安装与配置

(3)ruby程序和组件包安装与故障诊断

(4) ruby连接Redis数据库

心得:一步一脚印,不懂咱就问,思考与耐心,苦尽奕甘来


redisrubygemrubygemsredis-rb





Leonarding
2013.1.13
天津&winter
分享技术~成就梦想
Blogwww.leonarding.com

原文链接:https://www.f2er.com/nosql/204418.html

猜你在找的NoSQL相关文章