`

CentOS7安装redis

 
阅读更多

CentOS7安装redis和mongodb

本文介绍在CentOS7上安装redis数据库。

系统配置信息如下:

 

[linus@localhost ~]$ uname -r
3.10.0-229.20.1.el7.x86_64
[linus@localhost ~]$ more /etc/redhat-release 
CentOS Linux release 7.1.1503 (Core) 
[linus@localhost ~]$ 

 

 

Redis

1、下载Redis,当前最新版本是3.0.5,然后安装。

 

[linus@localhost redis]$ wget http://download.redis.io/releases/redis-3.0.5.tar.gz
--2015-11-24 13:33:32--  http://download.redis.io/releases/redis-3.0.5.tar.gz
Resolving download.redis.io (download.redis.io)... 109.74.203.151
Connecting to download.redis.io (download.redis.io)|109.74.203.151|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 1366160 (1.3M) [application/x-gzip]
Saving to: ‘redis-3.0.5.tar.gz’

100%[==============================================>] 1,366,160   40.0KB/s   in 25s    

2015-11-24 13:33:58 (54.1 KB/s) - ‘redis-3.0.5.tar.gz’ saved [1366160/1366160]

[linus@localhost redis]$ 
[linus@localhost redis]$ ls
redis-3.0.5.tar.gz
[linus@localhost redis]$ tar -zxf redis-3.0.5.tar.gz 
[linus@localhost redis]$ cd redis-3.0.5/
[linus@localhost redis-3.0.5]$ make
cd src && make all
make[1]: Entering directory `/home/linus/redis/redis-3.0.5/src'
rm -rf redis-server redis-sentinel redis-cli redis-benchmark redis-check-dump redis-check-aof *.o *.gcda *.gcno *.gcov redis.info lcov-html
(cd ../deps && make distclean)
make[2]: Entering directory `/home/linus/redis/redis-3.0.5/deps'
(cd hiredis && make clean) > /dev/null || true
(cd linenoise && make clean) > /dev/null || true
(cd lua && make clean) > /dev/null || true
(cd jemalloc && [ -f Makefile ] && make distclean) > /dev/null || true
(rm -f .make-*)
make[2]: Leaving directory `/home/linus/redis/redis-3.0.5/deps'
(rm -f .make-*)
echo STD=-std=c99 -pedantic >> .make-settings
echo WARN=-Wall -W >> .make-settings
echo OPT=-O2 >> .make-settings
echo MALLOC=jemalloc >> .make-settings
echo CFLAGS= >> .make-settings
echo LDFLAGS= >> .make-settings
echo REDIS_CFLAGS= >> .make-settings
echo REDIS_LDFLAGS= >> .make-settings
echo PREV_FINAL_CFLAGS=-std=c99 -pedantic -Wall -W -O2 -g -ggdb   -I../deps/hiredis -I../deps/linenoise -I../deps/lua/src -DUSE_JEMALLOC -I../deps/jemalloc/include >> .make-settings
echo PREV_FINAL_LDFLAGS=  -g -ggdb -rdynamic >> .make-settings
。。。
。。。
    LINK redis-server
    INSTALL redis-sentinel
    CC redis-cli.o
    LINK redis-cli
    CC redis-benchmark.o
    LINK redis-benchmark
    CC redis-check-dump.o
    LINK redis-check-dump
    CC redis-check-aof.o
    LINK redis-check-aof

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

make[1]: Leaving directory `/home/linus/redis/redis-3.0.5/src'

 然后执行make test进行验证,报如下错误,安装tcl之后就可以正常执行了:

 

 

[linus@localhost redis-3.0.5]$ make test
cd src && make test
make[1]: Entering directory `/home/linus/redis/redis-3.0.5/src'
You need tcl 8.5 or newer in order to run the Redis test
make[1]: *** [test] Error 1
make[1]: Leaving directory `/home/linus/redis/redis-3.0.5/src'
make: *** [test] Error 2
[linus@localhost redis-3.0.5]$ make test
...
...
                   The End

Execution time of different units:
  1 seconds - unit/printver
  2 seconds - unit/scan
  2 seconds - unit/quit
  3 seconds - unit/auth
  3 seconds - unit/multi
  5 seconds - unit/protocol
  11 seconds - unit/expire
  21 seconds - unit/type/list
  35 seconds - unit/other
  40 seconds - unit/type/list-2
  19 seconds - integration/aof
  41 seconds - unit/type/hash
  42 seconds - unit/dump
  3 seconds - integration/logging
  2 seconds - unit/pubsub
  8 seconds - integration/rdb
  3 seconds - unit/slowlog
  2 seconds - unit/introspection
  6 seconds - integration/convert-zipmap-hash-on-load
  2 seconds - unit/limits
  12 seconds - unit/scripting
  19 seconds - unit/bitops
  67 seconds - unit/type/set
  72 seconds - integration/replication-2
  30 seconds - unit/memefficiency
  77 seconds - unit/type/zset
  79 seconds - unit/sort
  79 seconds - unit/aofrw
  84 seconds - unit/basic
  42 seconds - unit/maxmemory
  91 seconds - unit/type/list-3
  117 seconds - integration/replication-4
  67 seconds - unit/hyperloglog
  122 seconds - integration/replication-3
  137 seconds - integration/replication
  134 seconds - integration/replication-psync
  114 seconds - unit/obuf-limits

\o/ All tests passed without errors!

Cleanup: may take some time... OK
make[1]: Leaving directory `/home/linus/redis/redis-3.0.5/src'
[linus@localhost redis-3.0.5]$ 

 安装完成后,可以切换src目录下,执行redis-server命令启动redis服务器:

 

 

[linus@localhost src]$ ./redis-server &
[1] 8921
[linus@localhost src]$ 8921:C 24 Nov 13:44:17.995 # Warning: no config file specified, using the default config. In order to specify a config file use ./redis-server /path/to/redis.conf
8921:M 24 Nov 13:44:17.997 # You requested maxclients of 10000 requiring at least 10032 max file descriptors.
8921:M 24 Nov 13:44:17.997 # Redis can't set maximum open files to 10032 because of OS error: Operation not permitted.
8921:M 24 Nov 13:44:17.997 # Current maximum open files is 4096. maxclients has been reduced to 4064 to compensate for low ulimit. If you need higher maxclients increase 'ulimit -n'.
                _._                                                  
           _.-``__ ''-._                                             
      _.-``    `.  `_.  ''-._           Redis 3.0.5 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._                                   
 (    '      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |     PID: 8921
  `-._    `-._  `-./  _.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |           http://redis.io        
  `-._    `-._`-.__.-'_.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |                                  
  `-._    `-._`-.__.-'_.-'    _.-'                                   
      `-._    `-.__.-'    _.-'                                       
          `-._        _.-'                                           
              `-.__.-'                                               

8921:M 24 Nov 13:44:17.998 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
8921:M 24 Nov 13:44:17.999 # Server started, Redis version 3.0.5
8921:M 24 Nov 13:44:17.999 # 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.
8921:M 24 Nov 13:44:17.999 # 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.
8921:M 24 Nov 13:44:17.999 * DB loaded from disk: 0.000 seconds
8921:M 24 Nov 13:44:17.999 * The server is now ready to accept connections on port 6379

 

 

此时可以通过redis-cli命令连接该redis服务器,并且可以通过SHUTDOWN命令关闭服务器:

 

[linus@localhost src]$ ./redis-cli 
127.0.0.1:6379> INFO
# Server
redis_version:3.0.5
redis_git_sha1:00000000
redis_git_dirty:0
redis_build_id:cdf25f7608cc4cbe
redis_mode:standalone
os:Linux 3.10.0-229.20.1.el7.x86_64 x86_64
arch_bits:64
multiplexing_api:epoll
gcc_version:4.8.3
process_id:8921
run_id:c4bf78bec7e54a517634bb62430406946c91733d
tcp_port:6379
uptime_in_seconds:104
uptime_in_days:0
hz:10
lru_clock:5503385
config_file:

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

# Memory
used_memory:508712
used_memory_human:496.79K
used_memory_rss:8024064
used_memory_peak:508712
used_memory_peak_human:496.79K
used_memory_lua:36864
mem_fragmentation_ratio:15.77
mem_allocator:jemalloc-3.6.0

# Persistence
loading:0
rdb_changes_since_last_save:0
rdb_bgsave_in_progress:0
rdb_last_save_time:1448343857
rdb_last_bgsave_status:ok
rdb_last_bgsave_time_sec:-1
rdb_current_bgsave_time_sec:-1
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

# Stats
total_connections_received:1
total_commands_processed:0
instantaneous_ops_per_sec:0
total_net_input_bytes:14
total_net_output_bytes:0
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
evicted_keys:0
keyspace_hits:0
keyspace_misses:0
pubsub_channels:0
pubsub_patterns:0
latest_fork_usec:0
migrate_cached_sockets:0

# Replication
role:master
connected_slaves:0
master_repl_offset:0
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0

# CPU
used_cpu_sys:0.10
used_cpu_user:0.04
used_cpu_sys_children:0.00
used_cpu_user_children:0.00

# Cluster
cluster_enabled:0

# Keyspace
127.0.0.1:6379> 
127.0.0.1:6379> SHUTDOWN
not connected> 

服务器端会打印:
8921:M 24 Nov 13:46:52.838 # User requested shutdown...
8921:M 24 Nov 13:46:52.838 * Saving the final RDB snapshot before exiting.
8921:M 24 Nov 13:46:52.844 * DB saved on disk
8921:M 24 Nov 13:46:52.844 # Redis is now ready to exit, bye bye...

 服务器端配置:

 

最关键的配置以下选项:

 

# By default Redis does not run as a daemon. Use 'yes' if you need it.
# Note that Redis will write a pid file in /var/run/redis.pid when daemonized.
daemonize yes
# Specify the server verbosity level.
# This can be one of:
# debug (a lot of information, useful for development/testing)
# verbose (many rarely useful info, but not a mess like the debug level)
# notice (moderately verbose, what you want in production probably)
# warning (only very important / critical messages are logged)
loglevel notice
# Specify the log file name. Also the empty string can be used to force
# Redis to log on the standard output. Note that if you use standard
# output for logging but daemonize, logs will be sent to /dev/null
logfile /var/log/redis.log
# The working directory.
#
# The DB will be written inside this directory, with the filename specified
# above using the 'dbfilename' configuration directive.
#
# The Append Only File will also be created inside this directory.
#
# Note that you must specify a directory here, not a file name.
dir ./

 

添加启动脚本/etc/init.d/redis:

 

#!/bin/sh
#
# redis        Startup script for Redis Server
#
# chkconfig: - 90 10
# description: Redis is an open source, advanced key-value store. 
#
# processname: redis-server
# config: /etc/redis.conf
# pidfile: /var/run/redis.pid
 
REDISPORT=6379
# 下面的目录要看编译的目录,这两个文件会在编译目录的src目录下。
EXEC=/usr/local/bin/redis-server
REDIS_CLI=/usr/local/bin/redis-cli
 
PIDFILE=/var/run/redis.pid
# 下面的目录要看编译的目录,这个文件会在编译目录下。
CONF="/usr/src/redis-2.8.13/redis.conf"
 
case "$1" in
    start)
        if [ -f $PIDFILE ]
        then
                echo "$PIDFILE exists, process is already running or crashed"
        else
                echo "Starting Redis server..."
                $EXEC $CONF
        fi
        if [ "$?"="0" ] 
        then 
              echo "Redis is running..."
        fi 
        ;;
    stop)
        if [ ! -f $PIDFILE ]
        then
                echo "$PIDFILE does not exist, process is not running"
        else
                PID=$(cat $PIDFILE)
                echo "Stopping ..."
                $REDIS_CLI -p $REDISPORT SHUTDOWN
                while [ -x ${PIDFILE} ]
               do
                    echo "Waiting for Redis to shutdown ..."
                    sleep 1
                done
                echo "Redis stopped"
        fi
        ;;
   restart|force-reload)
        ${0} stop
        ${0} start
        ;;
  *)
    echo "Usage: /etc/init.d/redis {start|stop|restart|force-reload}" >&2
        exit 1
esac

 

添加redis为系统的服务:

[root@localhost redis]# ll /etc/init.d/redis 
-rw-r--r--. 1 root root 1394 Nov 24 14:06 /etc/init.d/redis
[root@localhost redis]# chmod +x /etc/init.d/redis 
[root@localhost redis]# ll /etc/init.d/redis 
-rwxr-xr-x. 1 root root 1394 Nov 24 14:06 /etc/init.d/redis
[root@localhost redis]# 
[root@localhost redis]# chkconfig --add redis
[root@localhost redis]# chkconfig redis on
[root@localhost redis]# chkconfig 

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]'.

netconsole     	0:off	1:off	2:off	3:off	4:off	5:off	6:off
network        	0:off	1:off	2:on	3:on	4:on	5:on	6:off
redis          	0:off	1:off	2:on	3:on	4:on	5:on	6:off
[root@localhost redis]# 

 ok,现在就安装好了。启动redis使用service redis start 或者/etc/init.d/redis start ,停止redis的命令service redis stop 或者/etc/init.d/redis stop。

 

 MongoDB

待续

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics