1. 程式人生 > 其它 >01.linux下安裝redis並做成服務

01.linux下安裝redis並做成服務

技術標籤:redisrediscentoslinux

Redis安裝

官網

Redis官網

Redis官方中文網

中文版的的弊端你懂得。要是看官網能解決的就不要看別人寫的文章,哈哈

安裝

首先獲取redis安裝包

yum install wget
wget http://download.redis.io/releases/redis-5.0.3.tar.gz

解壓壓縮包

tar xf redis-5.0.3.tar.gz

進入redis目錄中

cd redis-5.0.3

任何開源軟體都有仔細閱讀README.md後再動手,開始make,缺啥少啥就install

make

yum install gcc-c++

yum install gcc

如果第一次make失敗要清理一下

make disclean

可以直接install,可執行檔案就會在當前目錄下生產,防止混亂,install的時候用PREFIX指定目錄

make install PREFIX=/opt/redis5

加入到環境變數中,在任何位置都可以實行redis的可執行檔案了。

vi /etc/profile

在最後兩行加入

export REDIS_HOME=/opt/redis5
export PATH=$PATH:$REDIS_HOME/bin

:wq儲存後source一下

source
/etc/profile

這樣可執行檔案就單獨存放了,也將redis的命令加入到了環境變數中

[[email protected] bin]# ll
total 32672
-rwxr-xr-x. 1 root root 4366592 Jan 27 23:17 redis-benchmark
-rwxr-xr-x. 1 root root 8090064 Jan 27 23:17 redis-check-aof
-rwxr-xr-x. 1 root root 8090064 Jan 27 23:17 redis-check-rdb
-rwxr-xr-x. 1 root root 4801840 Jan 27 23:17 redis-cli
lrwxrwxrwx. 1 root root      12 Jan 27 23:17 redis-sentinel -> redis-server
-rwxr-xr-x. 1 root root 8090064 Jan 27 23:17 redis-server
[
[email protected]
bin]# pwd /opt/redis5/bin

將redis製作成service

原始碼中就提供了將redis製作成service的可執行檔案了

[[email protected] redis-5.0.3]# cd utils/
[[email protected] utils]# ll
total 52
-rw-rw-r--. 1 root root  593 Dec 12  2018 build-static-symbols.tcl
-rw-rw-r--. 1 root root 1303 Dec 12  2018 cluster_fail_time.tcl
-rw-rw-r--. 1 root root 1070 Dec 12  2018 corrupt_rdb.c
drwxrwxr-x. 2 root root   60 Dec 12  2018 create-cluster
-rwxrwxr-x. 1 root root 2149 Dec 12  2018 generate-command-help.rb
drwxrwxr-x. 3 root root   31 Dec 12  2018 graphs
drwxrwxr-x. 2 root root   39 Dec 12  2018 hashtable
drwxrwxr-x. 2 root root   70 Dec 12  2018 hyperloglog
-rwxrwxr-x. 1 root root 9567 Dec 12  2018 install_server.sh
drwxrwxr-x. 2 root root   63 Dec 12  2018 lru
-rw-rw-r--. 1 root root 1277 Dec 12  2018 redis-copy.rb
-rwxrwxr-x. 1 root root 1352 Dec 12  2018 redis_init_script
-rwxrwxr-x. 1 root root 1047 Dec 12  2018 redis_init_script.tpl
-rw-rw-r--. 1 root root 1762 Dec 12  2018 redis-sha1.rb
drwxrwxr-x. 2 root root  135 Dec 12  2018 releasetools
-rwxrwxr-x. 1 root root 3787 Dec 12  2018 speed-regression.tcl
-rwxrwxr-x. 1 root root  693 Dec 12  2018 whatisdoing.sh
[[email protected] utils]# pwd
/opt/redis-5.0.3/utils

在utils目錄下,install_server.sh

[[email protected] 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] 
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.

根據提示可以配置redis,也可以使用預設的,只要使用的埠號不一樣,配置就不會出現覆蓋的情況。

如果是初次安裝的話一路回車就可以了。

最後會將redis加入到service中並且啟動,/etc/init.d 目錄下也多了redis_6379這個可執行檔案。

通過chkconfig檢視,也是執行在2、3、4、5級別上了

[[email protected] ~]# chkconfig --list redis_6379

Note: This output shows SysV services only and does not include native
      systemd services. SysV configuration data might be overridden by native
      systemd configuration.

      If you want to list systemd services use 'systemctl list-unit-files'.
      To see services enabled on particular target use
      'systemctl list-dependencies [target]'.

redis_6379      0:off   1:off   2:on    3:on    4:on    5:on    6:off

啟動

service redis_6379 start

停止

service redis_6379 stop

            在這裡插入圖片描述