1..1开始安装redis在redis的官方网站(http://www.redis.io)下载最新的稳定版本redis,在此选的是redis-3.0.7版本。
操作命令如下:
1、获取redis安装包,并解压
2、进入目录,并查看说明文件README
1
2
| cd redis-3.0.7
less README
|
3、配置redis安装包
1
2
3
| cd src/
make MANIFESTO=jemalloc && make PREFIX=/usr/local/redis-3.0.7/src install
make test
|
4、创建软连接
1
| ln -s /usr/local/src/redis-3.0.7 /usr/local/redis
|
5、配置环境语言
6、查看redis命令的目录
1
2
3
4
5
6
7
8
9
| tree /usr/local/redis-3.0.7/src/bin/
执行结果如下:
/usr/local/redis/bin/
├── redis-benchmark
├── redis-check-aof
├── redis-check-dump
├── redis-cli
├── redis-sentinel -> redis-server
└── redis-server
|
命令执行完成后,会在/usr/local/redis/bin/目录下生成5个可执行文件,分别是:
redis-benchmark redis-check-aof redis-check-dump redis-cli redis-sentinel redis-server 1.2 配置并启动redis服务a. 配置环境变量,命令如下
1
2
| echo ' PATH=/usr/local/redis/bin/:$PATH' >> /etc/profile
source /etc/profile
|
查看执行结果如下:
提示: PATH=/usr/local/redis-3.0.7/src/bin/:$PATH
查看是否被导入到全局路径下
执行结果如下
/usr/local/redis/bin/redis-server b. 查看命令帮助
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
| [iyunv@mysql01 ~]# redis-server --help
Usage: ./redis-server [/path/to/redis.conf] [options]
./redis-server - (read config from stdin)
./redis-server -v or --version
./redis-server -h or --help
./redis-server --test-memory <megabytes>
Examples:
./redis-server (run the server with default conf)
./redis-server /etc/redis/6379.conf
./redis-server --port 7777
./redis-server --port 7777 --slaveof 127.0.0.1 8888
./redis-server /etc/myredis.conf --loglevel verbose
Sentinel mode:
./redis-server /etc/sentinel.conf --sentinel
|
c. 启动redis服务
操作命令:
1
2
3
4
5
| mkdir /usr/local/redis/conf -p
cp /usr/local/src/redis-3.0.7/redis.conf /usr/local/redis/conf/
#安装包内含有redis的默认配置文件
sysctl vm.overcommit_memory=1
redis-server /usr/local/redis/conf/redis.conf &
|
提示:启动后会出现夯住的命令行,但是其实只要回车就好,因为此处回车后台执行
提示:查看端口是否存在确定redis是否启动成功
1
2
3
| [iyunv@mysql01 ~]# netstat -lntup|grep -w 6379
tcp 0 0 0.0.0.0:6379 0.0.0.0:* LISTEN 5896/redis-server *
tcp 0 0 :::6379 :::* LISTEN 5896/redis-server *
|
d. 关闭redis
1
2
3
4
| 方式一
redis-cli shutdown
方式二
killall redis-server
|
1.3 通过客户端测试redis服务redis-cli客户端帮助
1
2
3
4
5
6
7
8
| [iyunv@mysql01 ~]# redis-cli --help
redis-cli 3.0.7
Usage: redis-cli [OPTIONS] [cmd [arg [arg ...]]]
-h <hostname> Server hostname (default: 127.0.0.1).
-p <port> Server port (default: 6379).
-s <socket> Server socket (overrides hostname and port).
-a <password> Password to use when connecting to the server.
|
执行redis-cli连接到数据库
1
2
| [iyunv@mysql01 ~]# redis-cli
127.0.0.1:6379>
|
运用help查看帮助
1
2
3
4
5
6
7
8
| 127.0.0.1:6379> help get
GET key
summary: Get the value of a key
since: 1.0.0
group: string
127.0.0.1:6379>
|
help查看set
1
2
3
4
5
6
| 127.0.0.1:6379> help set
SET key value [EX seconds] [PX milliseconds] [NX|XX]
summary: Set the string value of a key
since: 1.0.0
group: string
|
1.4 测试1.4.1 写入数据
1
2
3
4
| 127.0.0.1:6379> set 1 mobartsgame
OK
127.0.0.1:6379> get 1
"mobartsgame"
|
1.4.2 redis支持多种数据类型
1
2
3
4
5
| 127.0.0.1:6379> set mykey "hello world"
OK
127.0.0.1:6379> get mykey
"hello world"
127.0.0.1:6379>
|
1.5 多实例配置
1.5.1 配置多实例
多实例配置其实很简单只需要将redis.conf拷贝到一个目录然后更改其端口和pid,启动时指定此文件或者直接 redis-server --port 6381 即可
1
2
3
4
5
6
7
8
9
10
11
12
13
14
| mv redis.conf redis_6379.conf
redis-server /usr/local/redis/conf/redis_6380.conf &
[iyunv@puppet01 conf]# diff redis_6379.conf redis_6380.conf
46c46
< pidfile /var/run/redis.pid
---
> pidfile /var/run/redis_6380.pid
50c50
< port 6379
---
> port 6380
[iyunv@puppet01 conf]# netstat -lntup|grep 63
tcp 0 0 0.0.0.0:6379 0.0.0.0:* LISTEN 5896/redis-server *
tcp 0 0 :::6379 :::* LISTEN 5896/redis-server *
|
或者
1
2
3
4
5
6
7
8
| [iyunv@puppet01 conf]# redis-server --port 6381 --daemonize yes
[iyunv@puppet01 conf]# redis-cli -p 6381
1.5.2 测试多实例
[iyunv@puppet01 conf]# redis-cli -p 6380
127.0.0.1:6380> set 1 mobartsgame
OK
127.0.0.1:6380> get 1
"mobartsgame"
|
1.6 redis 主从配置1.6.1 配置主从复制
配置一个从服务器非常简单,在配置文件中增加以下一行即可:
1
| slaveof 192.168.1.1 6379
|
当然,你需要将代码中的 192.168.1.1 和 6379 替换成你的主服务器的 IP 和端口号。
另外一种方法是调用 SLAVEOF 命令,输入主服务器的 IP 和端口,然后同步就会开始:
1
2
| 127.0.0.1:6379> SLAVEOF 192.168.1.1 6379
OK
|
1.6.2 测试主从
主库写入数据从库查询
1
2
3
4
5
6
7
8
9
| [iyunv@puppet01 ~]# redis-cli -p 6379
127.0.0.1:6379> set mykey mobartsgame
OK
127.0.0.1:6379> get mykey
"mobartsgame"
127.0.0.1:6379>
[iyunv@puppet01 conf]# redis-cli -p 6380
127.0.0.1:6380> get mykey
"mobartsgame"
|
以上表示主从复制完成,由于redis是自动实现主写从读的,那么我们从库测试是否能够写入数据
1
2
| 127.0.0.1:6380> set mykey2 mobartsgame
(error) READONLY You can't write against a read only slave.
|
表示主写从读正常
到此redis 搭建及主从完成,集群请点击 redis集群Twemproxy
|