Redis基础

来源:互联网 发布:创维电视软件 编辑:程序博客网 时间:2024/05/18 02:51
步骤一: 下载Redis
下载安装包:wget http://redis.googlecode.com/files/redis-2.2.12.tar.gz

[root@localhost 4setup]# wget http://redis.googlecode.com/files/redis-2.2.12.tar.gz --19:06:56-- http://redis.googlecode.com/files/redis-2.2.12.tar.gz 正在解析主机 redis.googlecode.com... 74.125.71.82 Connecting to redis.googlecode.com|74.125.71.82|:80... 已连接。 已发出 HTTP 请求,正在等待回应... 200 OK 长度:455240 (445K) [application/x-gzip] Saving to: `redis-2.2.12.tar.gz' 100%[==========================================>] 455,240 34.8K/s in 13s 19:07:16 (34.8 KB/s) - `redis-2.2.12.tar.gz' saved [455240/455240] [root@localhost 4setup]#


步骤二: 编译源程序

[root@localhost 4setup]# ll 总计 29168 -rw-r--r-- 1 root root 455240 2011-07-22 redis-2.2.12.tar.gz [root@localhost 4setup]# tar xzf redis-2.2.12.tar.gz [root@localhost 4setup]# cd redis-2.2.12 [root@localhost redis-2.2.12]# make cd src && make all make[1]: Entering directory `/root/4setup/redis-2.2.12/src'


步骤三: 启动Redis服务
src/redis-server

[root@localhost redis-2.2.12]# src/redis-server [6246] 05 Aug 19:17:22 # Warning: no config file specified, using the default config. In order to specify a config file use 'redis-server /path/to/redis.conf' [6246] 05 Aug 19:17:22 * Server started, Redis version 2.2.12 [6246] 05 Aug 19:17:22 # 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. [6246] 05 Aug 19:17:22 * The server is now ready to accept connections on port 6379 [6246] 05 Aug 19:17:22 - 0 clients connected (0 slaves), 539544 bytes in use

Redis 服务端的默认连接端口是 6379

步骤四: 将Redis作为 Linux 服务随机启动
vi /etc/rc.local, 使用vi编辑器打开随机启动配置文件,并在其中加入下面一行代码

/root/4setup/redis-2.2.12/src/redis-server



步骤五: 客户端连接验证
新打开一个Session输入:src/redis-cli,如果出现下面提示,那么您就可以开始Redis之旅了

[root@localhost redis-2.2.12]# src/redis-cli redis 127.0.0.1:6379>


步骤六: 查看Redis日志
查看服务器端session,即可对Redis的运行状况进行查看或分析了

[6246] 05 Aug 19:24:33 - 0 clients connected (0 slaves), 539544 bytes in use [6246] 05 Aug 19:24:37 - Accepted 127.0.0.1:51381 [6246] 05 Aug 19:24:38 - 1 clients connected (0 slaves), 547372 bytes in use

以上的几个步骤就OK了!!这样一个简单的Redis数据库就可以畅通无阻地运行起来了。

步骤七: 停止Redis实例
最简单的方法是在启动实例的session中,直接使用Control-C来将实例停止。 我们还可以用客户端来停止服务,如可以用shutdown来停止Redis实例, 具体如下:

[root@localhost redis-2.2.12]# src/redis-cli shutdown


配置Redis
在redis-2.2.12目录下有一个redis.conf文件,这个文件即是Redis的配置文件,用配置文件来启动Redis的方法如下:

root@localhost redis-2.2.12]# src/redis-server redis.conf [6353] 05 Aug 19:36:45 * Server started, Redis version 2.2.12 [6353] 05 Aug 19:36:45 # 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. [6353] 05 Aug 19:36:45 * The server is now ready to accept connections on port 6379 [6353] 05 Aug 19:36:45 - 0 clients connected (0 slaves), 539540 bytes in use


参数

 daemonize:
默认情况下,redis不是在后台运行的,如果需要在后台运行,把该项的值更改为yes
 pidfile
当Redis在后台运行的时候,Redis默认会把pid文件放在/var/run/redis.pid,你可以配置到其他地址。当运行多个redis服务时,需要指定不同的pid文件和端口
 bind
指定Redis只接收来自于该IP地址的请求,如果不进行设置,那么将处理所有请求,在生产环境中最好设置该项
 port
监听端口,默认为6379
 timeout
设置客户端连接时的超时时间,单位为秒。当客户端在这段时间内没有发出任何指令,那么关闭该连接
 loglevel
log等级分为4级,debug, verbose, notice, 和warning。生产环境下一般开启notice
 logfile
配置log文件地址,默认使用标准输出,即打印在命令行终端的窗口上
 databases
设置数据库的个数,可以使用SELECT <dbid>命令来切换数据库。默认使用的数据库是0
 save
设置Redis进行数据库镜像的频率。
 rdbcompression
在进行镜像备份时,是否进行压缩
 dbfilename
镜像备份文件的文件名
 dir
数据库镜像备份的文件放置的路径。
 slaveof
设置该数据库为其他数据库的从数据库
 masterauth
当主数据库连接需要密码验证时,在这里指定
 requirepass
设置客户端连接后进行任何其他指定前需要使用的密码。
maxclients
限制同时连接的客户数量。
 maxmemory
设置redis能够使用的最大内存。



操作数据库
插入数据(设置一个key-value对)

redis 127.0.0.1:6379> set name wwl OK


查询数据(取出key所对应的value)

redis 127.0.0.1:6379> get name "wwl"


删除键值(删除这个key及对应的value)

redis 127.0.0.1:6379> del name


验证键是否存在(其中0,代表此key不存在;1代表存在)

redis 127.0.0.1:6379> exists name (integer) 0




0 0
原创粉丝点击