Linux安装redis

Linux安装redis===================

1,安装Redis

先创建好安装路径:/data1/apps/redis2.6.14


1.1,下载及安装

wget http://redis.googlecode.com/files/redis-2.6.14.tar.gz
tar -zxvf redis-2.6.14.tar.gz
cd redis-2.6.14
make PREFIX=/data1/apps/redis2.6.14 install
mkdir /data1/apps/redis2.6.14/etc

1.5,可能会出现的错误提示
>>提示1:
make[3]: gcc:命令未找到
>>解决
yum -y install gcc-c++

>>提示2:
在包含自 adlist.c:34 的文件中:
zmalloc.h:50:31: 错误:jemalloc/jemalloc.h:没有那个文件或目录
zmalloc.h:55:2: 错误:#error "Newer version of jemalloc required"
>>解决
make的时候加上 MALLOC=libc 参数
make PREFIX=/data/apps/redis2.6.14 MALLOC=libc install


cp redis.conf /etc/
这个文件是redis启动的配置文件

由于redis安装在/data1/apps/redis2.6.14/下,所以
cd /data1/apps/redis2.6.14/ 进入这个目下,执行
cp redis-benchmark redis-cli redis-server /usr/bin/
这样就不用再执行时加上./了,而且可以在任何地方执行


echo 1 > /proc/sys/vm/overcommit_memory

设置内存分配策略(可选,根据服务器的实际情况进行设置) /proc/sys/vm/overcommit_memory
可选值:0、1、2。
0,表示内核将检查是否有足够的可用内存供应用进程使用;如果有足够的可用内存,内存申请允许;否则,内存申请失败,并把错误返回给应用进程。

1,表示内核允许分配所有的物理内存,而不管当前的内存状态如何。
2,表示内核允许分配超过所有物理内存和交换空间总和的内存
值得注意的一点是,redis在dump数据的时候,会fork出一个子进程,理论上child进程所占用的内存和parent是一样的,比如parent 占用的内存为8G,这个时候也要同样分配8G的内存给child,如果内存无法负担,往往会造成redis服务器的down机或者IO负载过高,效率下降。所以这里比较优化的内存分配策略应该设置为 1(表示内核允许分配所有的物理内存,而不管当前的内存状态如何)

开启redis端口,修改防火墙配置文件
vi /etc/sysconfig/iptables
加入端口配置

-A INPUT -m state --state NEW -m tcp -p tcp --dport 6379 -j ACCEPT

重新加载规则
service iptables restart

启动redis service
redis-server /etc/redis.conf

[3862] 19 Feb 23:10:56.339 * Max number of open files set to 10032
[3862] 19 Feb 23:10:56.347 # Warning: 32 bit instance detected but no memory limit set. Setting 3 GB maxmemory limit with 'noeviction' policy now.
_._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 2.6.14 (00000000/0) 32 bit
.-`` .-```. ```\/ _.,_ ''-._
( ' , .-` | `, ) Running in stand alone mode
|`-._`-...-` __...-.``-._|'` _.-'| Port: 6379
| `-._ `._ / _.-' | PID: 3862
`-._ `-._ `-./ _.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' | http://redis.io
`-._ `-._`-.__.-'_.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-'

[3862] 19 Feb 23:10:56.347 # Server started, Redis version 2.6.14
[3862] 19 Feb 23:10:56.347 * The server is now ready to accept connections on port 6379

查看进程,确定redis启动成功:
ps -ef | grep redis
[root@mtycentos ~]# ps -ef | grep redis
root 3862 3666 0 23:10 pts/1 00:00:00 redis-server /etc/redis.conf
root 3886 3866 0 23:11 pts/0 00:00:00 grep redis

如果启动失败,大部分情况下是因为redis.conf的配置有问题。
设置redis为后台守护进程:
vi /etc/redis.conf
# 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 no
daemonize yes
把no 改为 yes

修改完成后关闭redis : redis-cli shutdown
查看是否关闭:ps -ef | grep redis

再次启动为守护进程:redis-server /etc/redis.conf
[root@mtycentos ~]# ps -ef | grep redis
root 3913 1 0 23:16 ? 00:00:00 redis-server /etc/redis.conf
root 3917 3866 0 23:16 pts/0 00:00:00 grep redis

OK,启动成功

测试一把:
进入redis客户端命令行:
redis-cli 回车
redis 127.0.0.1:6379>
redis 127.0.0.1:6379> set name hanlu
OK
redis 127.0.0.1:6379> get name
"hanlu"

说明成功了。

退出当前命令行:quit


关闭redis:
redis-cli shutdown
redis关闭后,缓存数据会自动保存到硬盘上,硬盘地址为redis.conf中的配置项dgfilename dump.rdb


强制备份数据到磁盘:
redis-cli save or redis-cli -p 6380 save (要制定端口进行备份)

猜你喜欢

转载自www.cnblogs.com/javaboy2018/p/8882605.html
今日推荐