数据库
首页 > 数据库> > 源码安装Redis缓存服务 ;常用Redis数据库操作指令;配置Redis主从服务器

源码安装Redis缓存服务 ;常用Redis数据库操作指令;配置Redis主从服务器

作者:互联网

源码安装Redis缓存服务

1.3 步骤
步骤一:安装Redis服务器

1)源码安装Redis软件

[root@svr5 ~]# tar -xzf redis-3.0.6.tar.gz

[root@svr5 ~]# cd redis-3.0.6

[root@svr5 ~]# make

[root@svr5 ~]# make install

[root@svr5 ~]# ./utils/install_server.sh            //初始化

Welcome to the redis service installer

This script will help you easily set up a running redis server



Please select the redis port for this instance: [6379]    //设置端口号,默认即可

Selecting default: 6379

Please select the redis config file name [/etc/redis/6379.conf] //配置文件

Selected default - /etc/redis/6379.conf

Please select the redis log file name [/var/log/redis_6379.log] //日志文件

Selected default - /var/log/redis_6379.log

Please select the data directory for this instance [/var/lib/redis/6379]

//数据目录

Selected default - /var/lib/redis/6379

Please select the redis executable path [/usr/local/bin/redis-server]

//Redis服务器软件存储路径

Selected config:

Port           : 6379

Config file    : /etc/redis/6379.conf

Log file       : /var/log/redis_6379.log

Data dir       : /var/lib/redis/6379

Executable     : /usr/local/bin/redis-server

Cli Executable : /usr/local/bin/redis-cli

Is this ok? Then press ENTER to go on or Ctrl-C to abort.

//确认信息是否正确,回车确认即可

Copied /tmp/6379.conf => /etc/init.d/redis_6379

Installing service...

Successfully added to chkconfig!

Successfully added to runlevels 345!

Starting Redis server...

Installation successful!
[root@svr5 ~]# ls /etc/init.d/redis_6379         //查看启动脚本

2)启用Redis服务并查看监听端口状态

[root@svr5 ~]# /etc/init.d/redis_6379 restart

[root@srv5 ~]# netstat -nutlp |grep redis

tcp        0      0 0.0.0.0:6379    0.0.0.0:*    LISTEN      5749/redis-server *

tcp        0      0 :::6379            :::*            LISTEN      5749/redis-server *

步骤二:测试缓存数据库

1)使用redis-cli测试数据库

[

root@srv5 ~]# redis-cli

127.0.0.1:6379> ping                        //测试服务器

PONG

127.0.0.1:6379> set test 123                //设置变量

OK

127.0.0.1:6379> get test                    //查看test值

"123"

127.0.0.1:6379> INCR mycounter                //设置计数器mycounter

(integer) 1

127.0.0.1:6379> INCR mycounter                //对计数器mycounter进行自增运算

(integer) 2

2:常用Redis数据库操作指令

通过redis-cli工具,对Redis数据库各数据类型进行增删改查等操作,要求如下:

分别对Strings、Hash表、List列表三种数据类型进行增删改查等常见操作

设置数据缓存时间

清空所有数据

对数据库操作

步骤一:常用Redis数据操作指令

1)使用redis-cli测试数据库(字符串常见操作指南)

[root@srv5 ~]# redis-cli

127.0.0.1:6379> ping

PONG

127.0.0.1:6379> set string1 "hello the word"    //设置字符串变量

OK

127.0.0.1:6379> get string1                    //查看字符串变量

"hello the word"

127.0.0.1:6379> set string2 "hello" ex 5    //设置字符串变量,并设置过期时间为5秒

OK

127.0.0.1:6379> get string2                //查看字符串变量

"hello"

127.0.0.1:6379> get string2                //字符串过期后,查看该值为空

(nil)

127.0.0.1:6379> get string1

"hello the word"

127.0.0.1:6379> set string1 hello nx        //仅当string1不存在时,才执行set指令

(nil)

127.0.0.1:6379> set string1 hello xx        //仅当string1存在时,才执行set指令

OK

127.0.0.1:6379> get string1                     //查看修改后string1的值

"hello"

127.0.0.1:6379> set string1 "hello the world"    //修改string1的值

OK

127.0.0.1:6379> get string1

"hello the world"

127.0.0.1:6379> setrange string1 6 "Redis"        //从第6个字符开始替换string1的值

(integer) 15

127.0.0.1:6379> get string1

"hello Redisorld"

127.0.0.1:6379> strlen string1                    //计算string1的长度

(integer) 15

127.0.0.1:6379> append string1 xxx                //对string1进行追加操作

(integer) 18

127.0.0.1:6379> get string1

"hello Redisorldxxx"

127.0.0.1:6379> append string1 " xxx"

(integer) 22

127.0.0.1:6379> get string1

"hello Redisorldxxx xxx"





127.0.0.1:6379> setbit string2 0 1                //按位设置string2的值,0位为1

(integer) 0

127.0.0.1:6379> setbit string2 1 1                //按位设置string2的值,1位为1

(integer) 0

127.0.0.1:6379> setbit string2 2 1

(integer) 0

127.0.0.1:6379> setbit string2 3 0

(integer) 0

127.0.0.1:6379> get string2                    //不可以查看所有的值

"\xe0"

127.0.0.1:6379> bitcount string2                 //统计string2中1的个数

(integer) 3

127.0.0.1:6379> getbit string2 0                //查看string2第0位的值

(integer) 1

127.0.0.1:6379> getbit string2 1                //查看string2第1位的值

(integer) 1







127.0.0.1:6379> decr string3                    //递减运算,初始值为0

(integer) -1

127.0.0.1:6379> decr string3

(integer) -2

127.0.0.1:6379> decr string3

(integer) -3

127.0.0.1:6379> decr string3

(integer) -4

127.0.0.1:6379> decr string3

(integer) -5

127.0.0.1:6379> set string4 10                    //自定义变量初始值为10

OK

127.0.0.1:6379> decr string4                    //对自定义变量进行递减运算

(integer) 9

127.0.0.1:6379> decr string4

(integer) 8

127.0.0.1:6379> decr string4

(integer) 7

127.0.0.1:6379> decrby string4 2                //对变量进行递减2运算

(integer) 5

127.0.0.1:6379> decrby string4 2

(integer) 3

127.0.0.1:6379> get string4

"3"







127.0.0.1:6379> set string5 "hello the world"        //设置字符串变量

OK

127.0.0.1:6379> getrange string5 0 4            //查看字串的第0至第4位

"hello"

127.0.0.1:6379> getrange string5 -3 -1            //查看字串的倒数第3位至倒数第1位

"rld"

127.0.0.1:6379> incr page                        //对变量进行递增运算,初始值为0

(integer) 1

127.0.0.1:6379> incr page

(integer) 2

127.0.0.1:6379> incr page

(integer) 3

127.0.0.1:6379> incr page

(integer) 4

127.0.0.1:6379> set string6 10                    //设置字符串变量为10

OK

127.0.0.1:6379> incr string6                    //对变量进行递增运算

(integer) 11

127.0.0.1:6379> incr string6

(integer) 12

127.0.0.1:6379> incrby string6 2                //对变量进行递增2运算

(integer) 14

127.0.0.1:6379> incrby string6 2

(integer) 16

127.0.0.1:6379> incrby string6 2

(integer) 18

127.0.0.1:6379> incrby string6 2

(integer) 20

127.0.0.1:6379> set num 16.1                //设置浮点数变量

OK

127.0.0.1:6379> incrbyfloat num 1.1        //对浮点数进行递增1.1运算

"17.2"

127.0.0.1:6379> incrbyfloat num 1.1

"18.3"

127.0.0.1:6379> incrbyfloat num 1.1

"19.4"

127.0.0.1:6379> incrbyfloat num 1.1

"20.5"

2)Hash表常见操作指南

127.0.0.1:6379> hset hkey google “www.g.cn”        

//设置hash表hkey,google列的值为www.g.cn

(integer) 1

127.0.0.1:6379> hset hkey baidu “www.baidu.com”

//设置hash表hkey,baidu列的值为www.baidu.com

(integer) 1

127.0.0.1:6379> hget hkey google        //查看hash表hkey中google列的值

"www.g.cn"

127.0.0.1:6379> hget hkey baidu        //查看hash表hkey中baidu列的值

"www.baidu.com"

127.0.0.1:6379> hmset site google "www.g.cn" baidu "www.baidu.com"

OK

//一次性设置hash表site的多个列与值

127.0.0.1:6379> hmget site google baidu

1) "www.g.cn"

2) "www.baidu.com"

//一次性查看hash表site的多个列值

127.0.0.1:6379> hgetall site                //查看site表中所有的列与值

1) "google"

2) "www.g.cn"

3) "baidu"

4) "www.baidu.com"

127.0.0.1:6379> hdel site google            //删除site表中google列

(integer) 1

127.0.0.1:6379> hgetall site                //验证删除效果

1) "baidu"

2) "www.baidu.com"

127.0.0.1:6379> hmset site google "www.g.cn" baidu "www.baidu.com" sina "www.sina.com"

OK

127.0.0.1:6379> hkeys site                    //查看site表的所有列

1) "baidu"

2) "google"

3) "sina"

127.0.0.1:6379> hvals site                    //查看site表中所有列的值

1) "www.baidu.com"

2) "www.g.cn"

3) "www.sina.com"

127.0.0.1:6379> hmget site google baidu    //一次性查看site表中的多个列值

1) "www.g.cn"

2) "www.baidu.com"

3)List列表常见操作指南

127.0.0.1:6379> lpush list1 a b c            //创建列表并赋值

(integer) 3

127.0.0.1:6379> lpush list2 a                //创建列表并赋值

(integer) 1

127.0.0.1:6379> lpush list2 b                //给列表追加新值

(integer) 2

127.0.0.1:6379> lpush list2 c                //给列表追加新值

(integer) 3

127.0.0.1:6379> lrange list1 0 -1            

//查看列表list1中的所有值,从0位到最后1位

1) "c"

2) "b"

3) "a"

127.0.0.1:6379> lrange list1 1 2            //查看列表中第1和2位的值

1) "b"

2) "a"

127.0.0.1:6379> lrange list1 1 -1            //查看列表中第1至最后1位的值

1) "b"

127.0.0.1:6379> lrange list2 0 -1            //查看所有的值

1) "a"

2) "c"

3) "b"

4) "a"

127.0.0.1:6379> lrange list2 0 -1

1) "t"

2) "a"

3) "c"

4) "b"

5) "a"

127.0.0.1:6379> lpop list2                

//返回list2列表头元素数据,并将该值从列表中删除,key不存在则返回nil

"t"

127.0.0.1:6379> lrange list2 0 -1        //验证结果

1) "a"

2) "c"

3) "b"

4) "a"

127.0.0.1:6379> lpop list2                //接续删除头部元素

"a"

127.0.0.1:6379> lrange list2 0 -1        //验证结果

1) "c"

2) "b"

3) "a"

127.0.0.1:6379> lpop list2

"c"

127.0.0.1:6379> lrange list2 0 -1

1) "b"

2) "a"

127.0.0.1:6379> lrange list4 0 -1            //查看全部数据如下

1) "f"

2) "e"

3) "d"

4) "a"

5) "c"

127.0.0.1:6379> lindex list4 0                //返回list4中第0个值

"f"

127.0.0.1:6379> lindex list4 1                //返回list4中第1个值

"e"

127.0.0.1:6379> lindex list4 -1            //返回list4中最后1个值

"c"

127.0.0.1:6379> lindex list4 -2            //返回list4中倒数第2个值

"a"

127.0.0.1:6379> lrange list4 0 -1

1) "f"

2) "e"

3) "d"

4) "a"

5) "c"

127.0.0.1:6379> lset list4 0 test        //给list4的第0为插入值,值为test

OK

127.0.0.1:6379> lrange list4 0 -1        //验证结果

1) "test"

2) "d"

3) "a"

4)其他操作指南

127.0.0.1:6379> set mykey "hello"            //定义字符串变量

OK

127.0.0.1:6379> get mykey                    //查看变量

"hello"

127.0.0.1:6379> del mykey                    //删除变量

(integer) 1

127.0.0.1:6379> get mykey                    //验证结果

(nil)

127.0.0.1:6379> set mykey "hello"            //定义变量即值

OK

127.0.0.1:6379> get mykey                    //查看有值

"hello"

127.0.0.1:6379> get mykey

"hello"

127.0.0.1:6379> expire mykey 10            //定义过期时间

(integer) 1

127.0.0.1:6379> get mykey                    //10秒后查看,无值

(nil)

127.0.0.1:6379> set mykey "hello"            //设置变量

OK

127.0.0.1:6379> persist mykey                //重新定义过期时间为,永久有效

(integer) 1

127.0.0.1:6379> get mykey

"hello"

127.0.0.1:6379> get mykey

"hello"

127.0.0.1:6379> ttl mykey

(integer) -1                            //永不过期

127.0.0.1:6379> expire mykey 10        //定义过期时间

(integer) 1

127.0.0.1:6379> ttl mykey                //查看过期时间

(integer) 9

127.0.0.1:6379> ttl mykey

(integer) 8

127.0.0.1:6379> ttl mykey

(integer) 7

127.0.0.1:6379> ttl mykey

(integer) 6

127.0.0.1:6379> ttl mykey

(integer) 5

127.0.0.1:6379> ttl mykey

(integer) 4

127.0.0.1:6379> ttl mykey

(integer) 3

127.0.0.1:6379> ttl mykey

(integer) 3

127.0.0.1:6379> ttl mykey

(integer) 2

127.0.0.1:6379> ttl mykey

(integer) 1

127.0.0.1:6379> ttl mykey

(integer) -2        //已经过期

127.0.0.1:6379> get mykey        //查看mykey的值已经为空

(nil)

127.0.0.1:6379> set mykey "hello"

OK

127.0.0.1:6379> keys *        //查看数据库下所有数据

 1) "string6"

 2) "list7"

 3) "mykey"

 4) "string4"

 5) "db"

 6) "num"

 7) "result"

 8) "hkey"

 9) "string5"

10) "string1"

11) "bit1"

12) "page"

13) "bit2"

14) "site"

15) "string2"

16) "list1"

17) "string3"

18) "list6"

127.0.0.1:6379> keys li*

1) "list7"

2) "list1"

3) "list6"

127.0.0.1:6379> keys s*

1) "string6"

2) "string4"

3) "string5"

4) "string1"

5) "site"

6) "string2"

7) "string3"

127.0.0.1:6379> keys string[15]        //查看string1或string5

1) "string5"

2) "string1"

127.0.0.1:6379> keys string[0-9]        //查看string0值9的数据

1) "string6"

2) "string4"

3) "string5"

4) "string1"

5) "string2"

6) "string3"

127.0.0.1:6379> keys ?it*        //使用通配符所有数据

1) "bit1"

2) "bit2"

127.0.0.1:6379> select 1        //进入1数据库,默认数据库为0

OK

127.0.0.1:6379[1]> keys *        //在新数据库中查看数据为空

(empty list or set)

127.0.0.1:6379[1]> set test "test"        //在数据库1中创建变量

OK

127.0.0.1:6379[1]> get test            //查看变量的值

"test"

127.0.0.1:6379[1]> select 2            //进入2数据库

OK

127.0.0.1:6379[2]> keys *                //查看所有数据

(empty list or set)

127.0.0.1:6379[2]> select 1

OK

127.0.0.1:6379[1]> keys *

1) "test"

127.0.0.1:6379[1]> select 0

OK

127.0.0.1:6379> keys *

 1) "string6"

 2) "list7"

 3) "mykey"

 4) "string4"

 5) "db"

 6) "num"

 7) "result"

 8) "hkey"

 9) "string5"

10) "string1"

11) "bit1"

12) "page"

13) "bit2"

14) "site"

15) "string2"

16) "list1"

17) "string3"

18) "list6"

127.0.0.1:6379> flushall        //清空所有数据

OK

127.0.0.1:6379> keys *        //验证结果

127.0.0.1:6379[2]> select 0

OK

127.0.0.1:6379> set mykey "hello"

OK

127.0.0.1:6379> keys *

1) "mykey"

127.0.0.1:6379> select 1

OK

127.0.0.1:6379[1]> keys *

(empty list or set)

127.0.0.1:6379[1]> select 0

OK

127.0.0.1:6379> keys *

1) "mykey"

127.0.0.1:6379> move mykey 1        //将数据库0中的mykey变量移动至数据库1

(integer) 1

127.0.0.1:6379> keys *        //在数据库0中查看为空

(empty list or set)

127.0.0.1:6379> select 1        //进入数据库1

OK

127.0.0.1:6379[1]> keys *        //查看所有数据库

1) "mykey"

127.0.0.1:6379[1]> lpush cost 1 8 7 2 5        //创建列表cost

(integer) 5

127.0.0.1:6379[1]> sort cost                    //对列表值进行排序

1) "1"

2) "2"

3) "5"

4) "7"

5) "8"

3:配置Redis主从服务器
3.1 问题
本案例要求先快速搭建好两台Redis服务器,实现两台服务器之间自动数据同步,具体要求如下:

主服务器IP为192.168.2.100

从服务器IP为192.168.2.200

主服务器认证密码为redis123

测试主从数据是否正常通过

3.2 方案
通过修改Redis配置文件,实现两台服务器之间的自动主从同步功能,方案拓扑如图-1所示。



图-1

3.3 步骤
实现此案例需要按照如下步骤进行。

步骤一:配置主从服务器设置

1)主服务器安装Redis

[root@svr100 ~]# tar -xzf redis-3.0.6.tar.gz

[root@svr100 ~]# cd redis-3.0.6

[root@svr100 ~]# make

[root@svr100 ~]# make install

[root@svr100 ~]# cd utils/

[root@svr100 ~]#./install_server.sh

Welcome to the redis service installer

This script will help you easily set up a running redis server



Please select the redis port for this instance: [6379]

Selecting default: 6379

Please select the redis config file name [/etc/redis/6379.conf]

Selected default - /etc/redis/6379.conf

Please select the redis log file name [/var/log/redis_6379.log]

Selected default - /var/log/redis_6379.log

Please select the data directory for this instance [/var/lib/redis/6379]

Selected default - /var/lib/redis/6379

Please select the redis executable path [/usr/local/bin/redis-server]

Selected config:

Port           : 6379

Config file    : /etc/redis/6379.conf

Log file       : /var/log/redis_6379.log

Data dir       : /var/lib/redis/6379

Executable     : /usr/local/bin/redis-server

Cli Executable : /usr/local/bin/redis-cli

Is this ok? Then press ENTER to go on or Ctrl-C to abort.

Copied /tmp/6379.conf => /etc/init.d/redis_6379

Installing service...

Successfully added to chkconfig!

Successfully added to runlevels 345!

Starting Redis server...

Installation successful!

2)从服务器安装Redis

[root@svr200 ~]# tar -xzf redis-3.0.6.tar.gz

[root@svr200 ~]# cd redis-3.0.6

[root@svr200 ~]# make

[root@svr200 ~]# make install

[root@svr200 ~]# cd utils/

[root@svr200 ~]#./install_server.sh

Welcome to the redis service installer

This script will help you easily set up a running redis server



Please select the redis port for this instance: [6379]

Selecting default: 6379

Please select the redis config file name [/etc/redis/6379.conf]

Selected default - /etc/redis/6379.conf

Please select the redis log file name [/var/log/redis_6379.log]

Selected default - /var/log/redis_6379.log

Please select the data directory for this instance [/var/lib/redis/6379]

Selected default - /var/lib/redis/6379

Please select the redis executable path [/usr/local/bin/redis-server]

Selected config:

Port           : 6379

Config file    : /etc/redis/6379.conf

Log file       : /var/log/redis_6379.log

Data dir       : /var/lib/redis/6379

Executable     : /usr/local/bin/redis-server

Cli Executable : /usr/local/bin/redis-cli

Is this ok? Then press ENTER to go on or Ctrl-C to abort.

Copied /tmp/6379.conf => /etc/init.d/redis_6379

Installing service...

Successfully added to chkconfig!

Successfully added to runlevels 345!

Starting Redis server...

Installation successful!

步骤二:配置主从服务器设置

1)修改主服务器/etc/redis/6379.conf配置文件

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

requirepass redis123                               //设置服务器密码

[root@svr100 ~]# /etc/init.d/redis_6379 restart       //重启服务

2)修改主服务器的启动脚本,添加服务器密码

[root@svr100 ~]# vim  /etc/init.d/redis_6379

$CLIEXEC –a redis123  -p $REDISPORT shutdown

3)修改从服务器/etc/redis/6379.conf配置文件

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

slaveof 192.168.4.100 6379

masterauth redis123

[root@svr200 ~]# /etc/init.d/redis_6379  restart

步骤三:验证效果

1) 主服务器操作

[root@svr100 ~]# redis-cli –h 192.168.4.100 –a redis123      //登录主服务器设置数据

192.168.4.10:6379> set test 123456

OK

2) 从服务器操作

[root@svr200 ~]# redis-cli –h 192.168.4.200            //登录主服务器查看数据同步效果

192.168.4.20:6379> set test

“123456”

标签:缓存,0.1,redis,Redis,127.0,6379,源码,integer,mykey
来源: https://blog.csdn.net/Rio520/article/details/116563920