redis5.0.3安裝
參考redis 中文網 http://www.redis.cn/
下載、解壓、編譯Redis
$ wget http://download.redis.io/releases/redis-5.0.3.tar.gz
$ tar xzf redis-5.0.3.tar.gz
$ cd redis-5.0.3
$ make
簡單啓動
進入到解壓後的 src
目錄,通過如下命令啓動Redis:
$ src/redis-server
簡單交互
您可以使用內置的客戶端與Redis進行交互:
$ src/redis-cli
redis> set foo bar
OK
redis> get foo
"bar"
正常安裝
以下兩個選項任選其一進行 (推薦選擇默認安裝)
- 安裝命令 (默認安裝目錄是/usr/local/bin/)
[root@localhost redis-5.0.3]# make install #安裝
[root@localhost redis-5.0.3]# ll /usr/local/bin/redis* #查看目錄
-rwxr-xr-x. 1 root root 4367312 1月 25 02:54 /usr/local/bin/redis-benchmark
-rwxr-xr-x. 1 root root 8092014 1月 25 02:54 /usr/local/bin/redis-check-aof
-rwxr-xr-x. 1 root root 8092014 1月 25 02:54 /usr/local/bin/redis-check-rdb
-rwxr-xr-x. 1 root root 4802679 1月 25 02:54 /usr/local/bin/redis-cli
lrwxrwxrwx. 1 root root 12 1月 25 02:54 /usr/local/bin/redis-sentinel -> redis-server
-rwxr-xr-x. 1 root root 8092014 1月 25 02:54 /usr/local/bin/redis-server
- 指定redis安裝目錄
[root@localhost redis-5.0.3]# make install PREFIX=/usr/local/redis #安裝
配置文件
配置文件在redis-5.0.3目錄下,文件名是 redis.conf
根據你的安裝目錄,將配置文件拷貝至對應的目錄,比如拷貝到 /etc/
目錄下,命令如下:
[root@localhost redis-5.0.3]# cp redis.conf /etc/
正常啓動
啓動redis命令和效果如下,默認是前臺啓動,按 ctrl+c
停止
[root@localhost ~]# redis-server /etc/redis.conf
7076:C 25 Jan 2019 03:23:39.524 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
7076:C 25 Jan 2019 03:23:39.524 # Redis version=5.0.3, bits=64, commit=00000000, modified=0, pid=7076, just started
7076:C 25 Jan 2019 03:23:39.524 # Configuration loaded
7076:M 25 Jan 2019 03:23:39.525 * Increased maximum number of open files to 10032 (it was originally set to 1024).
_._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 5.0.3 (00000000/0) 64 bit
.-`` .-```. ```\/ _.,_ ''-._
( ' , .-` | `, ) Running in standalone mode
|`-._`-...-` __...-.``-._|'` _.-'| Port: 6379
| `-._ `._ / _.-' | PID: 7076
`-._ `-._ `-./ _.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' | http://redis.io
`-._ `-._`-.__.-'_.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-'
7076:M 25 Jan 2019 03:23:39.527 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
7076:M 25 Jan 2019 03:23:39.527 # Server initialized
7076:M 25 Jan 2019 03:23:39.527 # 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.
7076:M 25 Jan 2019 03:23:39.527 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
7076:M 25 Jan 2019 03:23:39.527 * Ready to accept connections
後臺啓動
後臺啓動需要修改配置文件 (大概在136行 ),將daemonize no
改爲 daemonize yes
```
135 # Note that Redis will write a pid file in /var/run/redis.pid when daemonized.
136 daemonize no
137
138 # If you run Redis from upstart or systemd, Redis can interact with your
```
重啓啓動,效果如下
[root@localhost ~]# redis-server /etc/redis.conf
7108:C 25 Jan 2019 03:36:53.028 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
7108:C 25 Jan 2019 03:36:53.028 # Redis version=5.0.3, bits=64, commit=00000000, modified=0, pid=7108, just started
7108:C 25 Jan 2019 03:36:53.028 # Configuration loaded
[root@localhost ~]#
查看redis服務進程
[root@localhost ~]# ps aux| grep redis
root 7124 0.0 0.7 153892 7708 ? Ssl 03:51 0:00 redis-server *:6379
root 7129 0.0 0.0 112724 984 pts/1 R+ 03:51 0:00 grep --color=auto redis
本地客戶端交互
[root@localhost ~]# redis-cli
127.0.0.1:6379> keys *
(empty list or set)
127.0.0.1:6379> set foo bar
OK
127.0.0.1:6379> get foo
"bar"
退出交互
[root@localhost ~]# redis-cli
127.0.0.1:6379> exit
[root@localhost ~]#
遠程客戶端連接
直接使用客戶端加參數 redis-cli -h <ip>
進行遠程連接
[root@localhost ~]# redis-cli -h 192.168.25.254
Could not connect to Redis at 192.168.25.254:6379: Connection refused
默認配置會報錯,連接被拒絕,需要修改配置文件 redis.conf
默認配置,69行默認綁定了本機地址,註釋此配置
redis-5.0.3 默認開啓保護模式,protected-mode yes
,修改爲 protected-mode no
68 # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
69 bind 127.0.0.1
...
87 # are explicitly listed using the "bind" directive.
88 protected-mode yes
修改後
68 # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
69 #bind 127.0.0.1
...
87 # are explicitly listed using the "bind" directive.
88 protected-mode no
重啓啓動
[root@localhost ~]# redis-server /etc/redis.conf
關閉redis服務
[root@localhost ~]# redis-cli shutdown