dong

Linux 环境下编译安装 redis
1.确认服务器环境[root@example ~]# cat /etc/redhat-release CentOS...
扫描右侧二维码阅读全文
30
2018/07

Linux 环境下编译安装 redis

1.确认服务器环境

[root@example ~]# cat /etc/redhat-release
CentOS Linux release 7.4.1708 (Core)

2.下载 Redis 源码

官网下载地址:

http://download.redis.io/releases/redis-4.0.10.tar.gz

进入源码存放目录:

[root@example ~]# cd /usr/local/src

下载对应源码包:

[root@example ~]# wget http://download.redis.io/releases/redis-4.0.10.tar.gz

3.确认是否安装 Redis 所需要的软件( gcc , tcl )

gcc 是 C 语言的编译器,Redis 是基于 C 语言编写的,所以必须要安装 gcctcl 是一门语言,Redis 的一些测试组件是使用 tcl 来进行编写的所以我们也需要安装 tcl。在 CentOS 中我们可以使用 yum 来安装 gcctcl

4.服务端安装使用

Redis 分为两个部分,一个是 redis-server,一个是 redis-cli。它们都是可以通过 Redis 的源码编译出来的。下面演示如何安装 redis-server

解压缩刚才下载的 Redis 的源码:

[root@example ~]# tar -zxvf redis-4.0.10.tar.gz

进入解压后的源码目录:

[root@example ~]# cd redis-4.0.10

执行 make 操作:

[root@example ~]# make

这个步骤可能会执行比较长的时间,具体的执行时间由机器的性能决定。

编译好之后,查看一下刚刚生成的二进制文件:

[root@example ~]# ll src/redis*
-rw-rw-r-- 1 root root    2417 Jun 13 19:02 src/redisassert.h
-rwxr-xr-x 1 root root 2451720 Jul 30 16:02 src/redis-benchmark
-rw-rw-r-- 1 root root   29605 Jun 13 19:02 src/redis-benchmark.c
-rw-r--r-- 1 root root  109160 Jul 30 16:02 src/redis-benchmark.o
-rwxr-xr-x 1 root root 5774400 Jul 30 16:02 src/redis-check-aof
-rw-rw-r-- 1 root root    7143 Jun 13 19:02 src/redis-check-aof.c
-rw-r--r-- 1 root root   28680 Jul 30 16:02 src/redis-check-aof.o
-rwxr-xr-x 1 root root 5774400 Jul 30 16:02 src/redis-check-rdb
-rw-rw-r-- 1 root root   13898 Jun 13 19:02 src/redis-check-rdb.c
-rw-r--r-- 1 root root   62808 Jul 30 16:02 src/redis-check-rdb.o
-rwxr-xr-x 1 root root 2617808 Jul 30 16:02 src/redis-cli
-rw-rw-r-- 1 root root  100621 Jun 13 19:02 src/redis-cli.c
-rw-r--r-- 1 root root  396864 Jul 30 16:02 src/redis-cli.o
-rw-rw-r-- 1 root root   21758 Jun 13 19:02 src/redismodule.h
-rwxr-xr-x 1 root root 5774400 Jul 30 16:02 src/redis-sentinel
-rwxr-xr-x 1 root root 5774400 Jul 30 16:02 src/redis-server
-rwxrwxr-x 1 root root   65991 Jun 13 19:02 src/redis-trib.rb

里面包含了我们所需要的 redis-cliredis-server

执行安装。make install 是把生成的二进制文件放到 /usr/local/bin/ 目录下,需要 root 权限,如果没有则使用 sudo make install :

[root@example redis-4.0.10]# make install
cd src && make install
make[1]: Entering directory `/usr/local/src/redis-4.0.10/src'
    CC Makefile.dep
make[1]: Leaving directory `/usr/local/src/redis-4.0.10/src'
make[1]: Entering directory `/usr/local/src/redis-4.0.10/src'

Hint: It's a good idea to run 'make test' ;)

    INSTALL install
    INSTALL install
    INSTALL install
    INSTALL install
    INSTALL install
make[1]: Leaving directory `/usr/local/src/redis-4.0.10/src'

至此安装完毕。

使用 which redis-server 查看安装位置:

[root@example redis-4.0.10]# which redis-server
/usr/local/bin/redis-server

查看一下 Redis 的相关命令:

[root@example redis-4.0.10]# 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

这里我们着重看一下第一个 ./redis-server [/path/to/redis.conf] [options] 。一般我们启动 Redis 都会通过 redis-server 加上一个配置文件的方式来启动。这个配置文件可以在源码中找到一个基本的原型。

[root@example redis-4.0.10]#ll
total 308
-rw-rw-r--  1 root root 162174 Jun 13 19:02 00-RELEASENOTES
-rw-rw-r--  1 root root     53 Jun 13 19:02 BUGS
-rw-rw-r--  1 root root   1815 Jun 13 19:02 CONTRIBUTING
-rw-rw-r--  1 root root   1487 Jun 13 19:02 COPYING
drwxrwxr-x  6 root root   4096 Jul 30 16:01 deps
-rw-rw-r--  1 root root     11 Jun 13 19:02 INSTALL
-rw-rw-r--  1 root root    151 Jun 13 19:02 Makefile
-rw-rw-r--  1 root root   4223 Jun 13 19:02 MANIFESTO
-rw-rw-r--  1 root root  20543 Jun 13 19:02 README.md
-rw-rw-r--  1 root root  58766 Jun 13 19:02 redis.conf
-rwxrwxr-x  1 root root    271 Jun 13 19:02 runtest
-rwxrwxr-x  1 root root    280 Jun 13 19:02 runtest-cluster
-rwxrwxr-x  1 root root    281 Jun 13 19:02 runtest-sentinel
-rw-rw-r--  1 root root   7606 Jun 13 19:02 sentinel.conf
drwxrwxr-x  3 root root   4096 Jul 30 16:25 src
drwxrwxr-x 10 root root   4096 Jun 13 19:02 tests
drwxrwxr-x  8 root root   4096 Jun 13 19:02 utils

源码目录下的 redis.conf 就是配置文件的原型,我们把它拷贝到我们事先要存储的地方:

[root@example redis-4.0.10]# cp redis.conf /root/config/redis/redis.conf

我们需要修改一下这个文件里面的一些配置项:

[root@example redis-4.0.10]# vim /root/config/redis/redis.conf
134 # By default Redis does not run as a daemon. Use 'yes' if you need it.
135 # Note that Redis will write a pid file in /var/run/redis.pid when daemonized.
136 daemonize no

找到配置文件中的 daemonize no 这一行,这个决定 Redis 是前台启动还是后台启动,一般我们都选择选择后台启动,所以我们把这一行的 daemonize no 改成 daemonize yes

下面我们修改一下 port

90 # Accept connections on the specified port, default is 6379 (IANA #815344).
91 # If port 0 is specified Redis will not listen on a TCP socket.
92 port 6379

这个代表的是 Redis 启动在哪个端口,默认是 6379。但是由于多实例或安全性的问题,我们把这个端口改成 7200,其他地方就不需要修改了。

然后启动 redis-server

[root@example redis-4.0.10]# /usr/local/bin/redis-server /root/config/redis/redis.conf
2366:C 30 Jul 17:00:41.157 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
2366:C 30 Jul 17:00:41.157 # Redis version=4.0.10, bits=64, commit=00000000, modified=0, pid=2366, just started
2366:C 30 Jul 17:00:41.157 # Configuration loaded

redis-server 已经启动,我们查看一下进程:

[root@example redis-4.0.10]# ps aux | grep redis-server
root      2367  0.0  0.4 145264  2164 ?        Ssl  17:00   0:00 /usr/local/bin/redis-server 127.0.0.1:7200
root      2384  0.0  0.1 112660   980 pts/0    S+   17:01   0:00 grep --color=auto redis-server

5.客户端使用

客户端 redis-cli 已经在上面的步骤中安装完成了:

[root@example redis-4.0.10]# which redis-cli
/usr/local/bin/redis-cli

我们现在使用 redis-cli 的命令来登录 Redis

[root@example redis-4.0.10]# redis-cli
Could not connect to Redis at 127.0.0.1:6379: Connection refused
Could not connect to Redis at 127.0.0.1:6379: Connection refused
not connected>
[root@example redis-4.0.10]#

发现登录不了,这里显示 Connection refused,因为 redis-cli 默认登录的是本机的 6379 端口,所以我们现在看一下 redis-clihelp 命令:

[root@example redis-4.0.10]# redis-cli --help
redis-cli 4.0.10

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.
  -u <uri>           Server URI.
  -r <repeat>        Execute specified command N times.
  -i <interval>      When -r is used, waits <interval> seconds per command.
                     It is possible to specify sub-second times like -i 0.1.
  -n <db>            Database number.
  -x                 Read last argument from STDIN.
  -d <delimiter>     Multi-bulk delimiter in for raw formatting (default: \n).
  -c                 Enable cluster mode (follow -ASK and -MOVED redirections).
  --raw              Use raw formatting for replies (default when STDOUT is
                     not a tty).
  --no-raw           Force formatted output even when STDOUT is not a tty.
  --csv              Output in CSV format.
  --stat             Print rolling stats about server: mem, clients, ...
  --latency          Enter a special mode continuously sampling latency.
                     If you use this mode in an interactive session it runs
                     forever displaying real-time stats. Otherwise if --raw or
                     --csv is specified, or if you redirect the output to a non
                     TTY, it samples the latency for 1 second (you can use
                     -i to change the interval), then produces a single output
                     and exits.
  --latency-history  Like --latency but tracking latency changes over time.
                     Default time interval is 15 sec. Change it using -i.
  --latency-dist     Shows latency as a spectrum, requires xterm 256 colors.
                     Default time interval is 1 sec. Change it using -i.
  --lru-test <keys>  Simulate a cache workload with an 80-20 distribution.
  --slave            Simulate a slave showing commands received from the master.
  --rdb <filename>   Transfer an RDB dump from remote server to local file.
  --pipe             Transfer raw Redis protocol from stdin to server.
  --pipe-timeout <n> In --pipe mode, abort with error if after sending all data.
                     no reply is received within <n> seconds.
                     Default timeout: 30. Use 0 to wait forever.
  --bigkeys          Sample Redis keys looking for big keys.
  --hotkeys          Sample Redis keys looking for hot keys.
                     only works when maxmemory-policy is *lfu.
  --scan             List all keys using the SCAN command.
  --pattern <pat>    Useful with --scan to specify a SCAN pattern.
  --intrinsic-latency <sec> Run a test to measure intrinsic system latency.
                     The test will run for the specified amount of seconds.
  --eval <file>      Send an EVAL command using the Lua script at <file>.
  --ldb              Used with --eval enable the Redis Lua debugger.
  --ldb-sync-mode    Like --ldb but uses the synchronous Lua debugger, in
                     this mode the server is blocked and script changes are
                     are not rolled back from the server memory.
  --help             Output this help and exit.
  --version          Output version and exit.

Examples:
  cat /etc/passwd | redis-cli -x set mypasswd
  redis-cli get mypasswd
  redis-cli -r 100 lpush mylist x
  redis-cli -r 100 -i 1 info | grep used_memory_human:
  redis-cli --eval myscript.lua key1 key2 , arg1 arg2 arg3
  redis-cli --scan --pattern '*:12345*'

  (Note: when using --eval the comma separates KEYS[] from ARGV[] items)

When no command is given, redis-cli starts in interactive mode.
Type "help" in interactive mode for information on available commands
and settings.

[root@example redis-4.0.10]#

我们如果要登录远端的机子,那么我们就要使用 -h 参数,-p 指定端口:

[root@example redis-4.0.10]# redis-cli -h 127.0.0.1 -p 7200
127.0.0.1:7200>

登录成功。我们可以使用 info 命令来查看 redis-server 的当前状态:

127.0.0.1:7200> info
# Server
redis_version:4.0.10
redis_git_sha1:00000000
redis_git_dirty:0
redis_build_id:d5e3b782fe3eb412
redis_mode:standalone
os:Linux 3.10.0-693.2.2.el7.x86_64 x86_64
arch_bits:64
multiplexing_api:epoll
atomicvar_api:atomic-builtin
gcc_version:4.8.5
process_id:2367
run_id:365d412a8f0edaed5a914fffe4540bc9fa473a3b
tcp_port:7200
uptime_in_seconds:1214
uptime_in_days:0
hz:10
lru_clock:6215799
executable:/usr/local/bin/redis-server
config_file:/root/config/redis/redis.conf

# Clients
connected_clients:1
client_longest_output_list:0
client_biggest_input_buf:0
blocked_clients:0

# Memory
used_memory:849440
used_memory_human:829.53K
used_memory_rss:2215936
used_memory_rss_human:2.11M
used_memory_peak:849440
used_memory_peak_human:829.53K
used_memory_peak_perc:100.12%
used_memory_overhead:836206
used_memory_startup:786576
used_memory_dataset:13234
used_memory_dataset_perc:21.05%
total_system_memory:512086016
total_system_memory_human:488.36M
used_memory_lua:37888
used_memory_lua_human:37.00K
maxmemory:0
maxmemory_human:0B
maxmemory_policy:noeviction
mem_fragmentation_ratio:2.61
mem_allocator:jemalloc-4.0.3
active_defrag_running:0
lazyfree_pending_objects:0

# Persistence
loading:0
rdb_changes_since_last_save:0
rdb_bgsave_in_progress:0
rdb_last_save_time:1532941241
rdb_last_bgsave_status:ok
rdb_last_bgsave_time_sec:-1
rdb_current_bgsave_time_sec:-1
rdb_last_cow_size:0
aof_enabled:0
aof_rewrite_in_progress:0
aof_rewrite_scheduled:0
aof_last_rewrite_time_sec:-1
aof_current_rewrite_time_sec:-1
aof_last_bgrewrite_status:ok
aof_last_write_status:ok
aof_last_cow_size:0

# Stats
total_connections_received:1
total_commands_processed:1
instantaneous_ops_per_sec:0
total_net_input_bytes:31
total_net_output_bytes:10163
instantaneous_input_kbps:0.00
instantaneous_output_kbps:0.00
rejected_connections:0
sync_full:0
sync_partial_ok:0
sync_partial_err:0
expired_keys:0
expired_stale_perc:0.00
expired_time_cap_reached_count:0
evicted_keys:0
keyspace_hits:0
keyspace_misses:0
pubsub_channels:0
pubsub_patterns:0
latest_fork_usec:0
migrate_cached_sockets:0
slave_expires_tracked_keys:0
active_defrag_hits:0
active_defrag_misses:0
active_defrag_key_hits:0
active_defrag_key_misses:0

# Replication
role:master
connected_slaves:0
master_replid:d7ca8255315d3a719aee5ca68bd67d9e00cf8460
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:0
second_repl_offset:-1
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0

# CPU
used_cpu_sys:0.63
used_cpu_user:0.43
used_cpu_sys_children:0.00
used_cpu_user_children:0.00

# Cluster
cluster_enabled:0

# Keyspace
127.0.0.1:7200>

至此,redis-serverredis-cli 已经安装并运行起来了。

本文内容摘录自慕课网《 redis的入门与应用 》

Last modification:March 14th, 2019 at 01:22 pm
If you think my article is useful to you, please feel free to appreciate

Leave a Comment