redis的搭建过程

27次阅读
没有评论

共计 4214 个字符,预计需要花费 11 分钟才能阅读完成。

本篇内容介绍了“redis 的搭建过程”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让丸趣 TV 小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

redis 官网:http://download.redis.io/releases

redis 安装包    redis-3.2.13.tar.gz

解压:tar -xvf redis-3.2.13.tar.gz

把解压的 redis 放在你准备的目录下面,我放在根 / 下面。

cd redis-3.2.13
make

编译好了,进入 src 文件下面;

[root@localhost src]# make install
Hint: It s a good idea to run make test )
   INSTALL install
   INSTALL install
   INSTALL install
   INSTALL install
   INSTALL install
[root@localhost src]#

返回上一层,

[root@localhost redis-3.2.13]# pwd

/redis-3.2.13

[root@localhost redis-3.2.13]# vim redis.conf

bind 0.0.0.0   如果别人要访问修改成 0.0.0.0,默认是本机连接

daemonize yes   默认不是后台启动,我们修改成 yes

requirepass 123   去掉 #号,修改密码,重启生效,

timeout 10

现在启动 redis,我们设置了后台启动的,所以不会显示什么:

[root@localhost redis-3.2.13]# ./src/redis-server /redis-3.2.13/redis.conf

查看进程:

[root@localhost redis-3.2.13]# ps -ef|grep redis
root      4434     1  0 10:54 ?        00:00:00 ./src/redis-server 127.0.0.1:6379
root      4506   818  0 10:55 pts/2    00:00:00 grep –color=auto redis

进入 redis:

[root@localhost redis-3.2.13]# redis-cli -a 123
127.0.0.1:6379
127.0.0.1:6379 config get dir
1) dir
2) /redis-3.2.13
127.0.0.1:6379

下面可做可不做,方便后面维护而已:

进入 redis 安装目录:

[root@localhost ~]# cd /redis-3.2.13/utils/
[root@localhost utils]# ll
total 52
-rw-rw-r–. 1 root root  593 Mar 19 00:25 build-static-symbols.tcl
-rw-rw-r–. 1 root root 1303 Mar 19 00:25 cluster_fail_time.tcl
-rw-rw-r–. 1 root root 1070 Mar 19 00:25 corrupt_rdb.c
drwxrwxr-x. 2 root root   60 Mar 19 00:25 create-cluster
-rwxrwxr-x. 1 root root 2137 Mar 19 00:25 generate-command-help.rb
drwxrwxr-x. 2 root root   39 Mar 19 00:25 hashtable
drwxrwxr-x. 2 root root   70 Mar 19 00:25 hyperloglog
-rwxrwxr-x. 1 root root 8529 Mar 19 00:25 install_server.sh
drwxrwxr-x. 2 root root   39 Mar 19 00:25 lru
-rw-rw-r–. 1 root root 1277 Mar 19 00:25 redis-copy.rb
-rwxrwxr-x. 1 root root 1098 Mar 19 00:25 redis_init_script
-rwxrwxr-x. 1 root root 1047 Mar 19 00:25 redis_init_script.tpl
-rw-rw-r–. 1 root root 1762 Mar 19 00:25 redis-sha1.rb
drwxrwxr-x. 2 root root  114 Mar 19 00:25 releasetools
-rwxrwxr-x. 1 root root 3787 Mar 19 00:25 speed-regression.tcl
-rwxrwxr-x. 1 root root  693 Mar 19 00:25 whatisdoing.sh
[root@localhost utils]# vim redis_init_script
REDISPORT=6379
EXEC=/usr/local/bin/redis-server    – 找到 redis-server,我的在 /redis-3.2.13/src
CLIEXEC=/usr/local/bin/redis-cli    – 找到 redis-server,我的在 /redis-3.2.13/src
PIDFILE=/var/run/redis_${REDISPORT}.pid
CONF= /etc/redis/${REDISPORT}.conf     — 配置文件在安装目录下面 /redis-3.2.13,注意 redis.confg 名字
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
       ;;
   stop)
       if [! -f $PIDFILE]
       then
               echo $PIDFILE does not exist, process is not running
       else
               PID=$(cat $PIDFILE)
               echo Stopping …
               $CLIEXEC -p $REDISPORT shutdown    —- 注意密码问题 -a password
               while [-x /proc/${PID} ]
               do
                   echo Waiting for Redis to shutdown …
                   sleep 1
               done
               echo Redis stopped
       fi
       ;;
   *)
       echo Please use start or stop as first argument
       ;;
esac

修改好了,就把这个文件 /etc/init.d 下面,改名 redis

[root@localhost utils]# mv redis_init_script /etc/init.d/redis
[root@localhost utils]# chmod +x /etc/init.d/redis

修改好了,停一下报错,发现还没有配置好:

[root@localhost utils]# service redis stop
Stopping …
(error) NOAUTH Authentication required.
Waiting for Redis to shutdown …
Waiting for Redis to shutdown …
Waiting for Redis to shutdown …
Waiting for Redis to shutdown …
Waiting for Redis to shutdown …
Waiting for Redis to shutdown …
Waiting for Redis to shutdown …
Waiting for Redis to shutdown …
Waiting for Redis to shutdown …
Waiting for Redis to shutdown …

找到问题了,原来是 /etc/init.d/redis 下面的脚本缺少 -a 密码认证

[root@localhost init.d]# service redis stop
Stopping …
Redis stopped

配置下面的内核参数,否则 Redis 脚本在重启或停止 redis 时,将会报错,并且不能自动在停止服务前同步数据到磁盘上 /etc/sysctl.conf 加上  

#vim /etc/sysctl.conf

vm.overcommit_memory = 1 

#sysctl -p 

vi /etc/profile

export PATH= $PATH:/redis-3.2.13/src     这个位置就是 redis-server 和 redis-cli 的位置

redis 状态监测:

[root@localhost init.d]# redis-cli -a 123 –stat

[root@localhost init.d]# redis-cli -a 123 –bigkeys -i 0.01

扫描大 key

主从同步:

1,先修改从库的配置文件

slaveof 主库 ip 端口

2,重启从库

3,登录从库,slaveof 主库 ip 端口   —- 开启主从同步

遇到的问题。

1,从库配置文件添加了 slaveof,但是从主不同步,查看 info,

repl_backlog_active:0   显示的为 0

解决办法:重启从库,执行 slaveof no one,再执行 slaveof 主 ip+ 端口,

redis 使用命令:

127.0.0.1:6379 config get dir
1) dir
2) /apps/redis/data/master
127.0.0.1:6379

info 查看 redis 的基本信息

1、protected-mode

#protected-mode yes
#是否开启保护模式,默认开启。要是配置里没有指定 bind 和密码。开启该参数后,redis 只会
本地进行访问,拒绝外部访问。要是开启了密码 和 bind,可以开启。否 则最好关闭,设置为 no。

“redis 的搭建过程”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注丸趣 TV 网站,丸趣 TV 小编将为大家输出更多高质量的实用文章!

正文完
 
丸趣
版权声明:本站原创文章,由 丸趣 2023-07-28发表,共计4214字。
转载说明:除特殊说明外本站除技术相关以外文章皆由网络搜集发布,转载请注明出处。
评论(没有评论)