Linux 環境下編譯安裝 Redis

dongzt發表於2018-07-30

1.確認伺服器環境

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

2.下載 Redis 原始碼

官網下載地址:

http://download.redis.io/releases/redis-4....

進入原始碼存放目錄:

[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 已經安裝並執行起來了。

本作品採用《CC 協議》,轉載必須註明作者和本文連結

相關文章