系统城装机大师 - 固镇县祥瑞电脑科技销售部宣传站!

当前位置:首页 > 数据库 > Redis > 详细页面

编译安装redisd的方法示例详解

时间:2020-02-15来源:系统城作者:电脑系统城

安装方法:

yum安装

查看yum仓库redis版本


 
  1. [root@centos ~]# yum list redis
  2. Loaded plugins: fastestmirror, langpacks
  3. Loading mirror speeds from cached hostfile
  4. Available Packages
  5. redis.x86_64 3.2.12-2.el7 myepel

yum安装

[root@centos ~]# yum install redis -y

启动服务并设为开机启动


 
  1. [root@centos ~]# systemctl enable --now redis
  2. Created symlink from /etc/systemd/system/multi-user.target.wants/redis.service to /usr/lib/systemd/system/redis.service.

查看redis端口


 
  1. [root@centos ~]# ss -ntl
  2. State Recv-Q Send-Q Local Address:Port Peer Address:Port
  3. LISTEN 0 128 127.0.0.1:631 *:*
  4. LISTEN 0 100 127.0.0.1:25 *:*
  5. LISTEN 0 128 127.0.0.1:6010 *:*
  6. LISTEN 0 128 *:54909 *:*
  7. LISTEN 0 128 127.0.0.1:6379 *:* # 这个是redis端口
  8. LISTEN 0 128 *:111 *:*
  9. LISTEN 0 5 192.168.122.1:53 *:*

测试登录redis


 
  1. [root@centos ~]# redis-cli
  2. 127.0.0.1:6379> info
  3. # Server
  4. redis_version:3.2.12
  5. redis_git_sha1:00000000
  6. redis_git_dirty:0
  7. redis_build_id:7897e7d0e13773f
  8. redis_mode:standalone
  9. os:Linux 3.10.0-1062.el7.x86_64 x86_64
  10. arch_bits:64
  11. multiplexing_api:epoll
  12. gcc_version:4.8.5
  13. process_id:2914
  14. run_id:c75137717c54caa78bb05757d05c91471ef5817f
  15. tcp_port:6379
  16. uptime_in_seconds:175
  17. uptime_in_days:0
  18. hz:10
  19. lru_clock:4329484
  20. executable:/usr/bin/redis-server
  21. config_file:/etc/redis.conf
  22.  
  23. # Clients
  24. connected_clients:1
  25. client_longest_output_list:0
  26. client_biggest_input_buf:0
  27. blocked_clients:0

测试使用


 
  1. 127.0.0.1:6379> set key1 value1
  2. OK
  3. 127.0.0.1:6379> get key1
  4. "value1"

编译安装

下载当前最新release版本redis源码包:http://download.redis.io/releases/

编译安装命令

编译安装redisd的方法示例详解

官方安装命令:https://redis.io/download


 
  1. # 源码包存放目录
  2. [root@centos ~]# cd /usr/local/src/
  3. # 下载源码包
  4. [root@centos src]# wget http://download.redis.io/releases/redis-5.0.7.tar.gz
  5. --2020-02-11 10:37:54-- http://download.redis.io/releases/redis-5.0.7.tar.gz
  6. Resolving download.redis.io (download.redis.io)... 109.74.203.151
  7. Connecting to download.redis.io (download.redis.io)|109.74.203.151|:80... connected.
  8. HTTP request sent, awaiting response... 200 OK
  9. Length: 1984203 (1.9M) [application/x-gzip]
  10. Saving to: ‘redis-5.0.7.tar.gz'
  11. 100%[===============================================>] 1,984,203 6.75KB/s in 3m 35s
  12. 2020-02-11 10:41:39 (9.02 KB/s) - ‘redis-5.0.7.tar.gz' saved [1984203/1984203]
  13. # 查看下载好的源码包
  14. [root@centos src]# ll
  15. total 1940
  16. -rw-r--r-- 1 root root 1984203 Nov 20 01:06 redis-5.0.7.tar.gz

解压源码包


 
  1. [root@centos src]# tar xf redis-5.0.7.tar.gz
  2. [root@centos src]# ll
  3. total 1940
  4. drwxrwxr-x 6 root root 334 Nov 20 01:05 redis-5.0.7
  5. -rw-r--r-- 1 root root 1984203 Nov 20 01:06 redis-5.0.7.tar.gz

创建配置文件、日志、数据等目录


 
  1. [root@centos ~]# mkdir /apps/redis/{etc,logs,data,run} -p
  2. # 查看目录结构
  3. [root@centos ~]# tree /apps/
  4. /apps/
  5. └── redis
  6. ├── data
  7. ├── etc
  8. ├── logs
  9. └── run

进入redis目录编译安装


 
  1. [root@centos ~]# cd /usr/local/src/redis-5.0.7/
  2. [root@centos redis-5.0.7]# make PREFIX=/apps/redis install
  3. cd src && make install
  4. make[1]: Entering directory `/usr/local/src/redis-5.0.7/src'
  5. CC Makefile.dep
  6. make[1]: Leaving directory `/usr/local/src/redis-5.0.7/src'
  7. make[1]: Entering directory `/usr/local/src/redis-5.0.7/src'
  8. CC adlist.o
  9. /bin/sh: cc: command not found
  10. make[1]: *** [adlist.o] Error 127
  11. make[1]: Leaving directory `/usr/local/src/redis-5.0.7/src'
  12. make: *** [install] Error 2
  13.  
  14. # 出现以上报错是没有gcc编译器导致的
  15. # 下载gcc编译器
  16. [root@centos redis-5.0.7]# yum install gcc
  17. # 记得这里要把之前的redis目录删除重新解压
  18. [root@centos src]# rm -rf redis-5.0.7
  19. [root@centos src]# tar xf redis-5.0.7.tar.gz
  20.  
  21. # 重新进入目录进行编译
  22. [root@centos src]# cd redis-5.0.7/
  23. [root@centos redis-5.0.7]# make PREFIX=/apps/redis install # 指定安装目录
  24. cd src && make install
  25. make[1]: Entering directory `/usr/local/src/redis-5.0.7/src'
  26. CC Makefile.dep
  27. make[1]: Leaving directory `/usr/local/src/redis-5.0.7/src'
  28. make[1]: Entering directory `/usr/local/src/redis-5.0.7/src'
  29. rm -rf redis-server redis-sentinel redis-cli redis-benchmark redis-check-rdb redis-check-aof *.o *.gcda *.gcno *.gcov redis.info lcov-html Makefile.dep dict-benchmark
  30. (cd ../deps && make distclean)
  31. make[2]: Entering directory `/usr/local/src/redis-5.0.7/deps'
  32. (cd hiredis && make clean) > /dev/null || true
  33. (cd linenoise && make clean) > /dev/null || true
  34. (cd lua && make clean) > /dev/null || true
  35. (cd jemalloc && [ -f Makefile ] && make distclean) > /dev/null || true
  36. (rm -f .make-*)
  37. make[2]: Leaving directory `/usr/local/src/redis-5.0.7/deps'
  38. (rm -f .make-*)
  39. ....(省略)
  40. Hint: It's a good idea to run 'make test' ;)
  41.  
  42. INSTALL install
  43. INSTALL install
  44. INSTALL install
  45. INSTALL install
  46. INSTALL install
  47. make[1]: Leaving directory `/usr/local/src/redis-5.0.7/src'
  48. # 最后报这个就是编译完成

拷贝配置文件


 
  1. [root@centos redis-5.0.7]# ll
  2. total 276
  3. -rw-rw-r-- 1 root root 115100 Nov 20 01:05 00-RELEASENOTES
  4. -rw-rw-r-- 1 root root 53 Nov 20 01:05 BUGS
  5. -rw-rw-r-- 1 root root 2381 Nov 20 01:05 CONTRIBUTING
  6. -rw-rw-r-- 1 root root 1487 Nov 20 01:05 COPYING
  7. drwxrwxr-x 6 root root 192 Feb 11 11:32 deps
  8. -rw-rw-r-- 1 root root 11 Nov 20 01:05 INSTALL
  9. -rw-rw-r-- 1 root root 151 Nov 20 01:05 Makefile
  10. -rw-rw-r-- 1 root root 6888 Nov 20 01:05 MANIFESTO
  11. -rw-rw-r-- 1 root root 20555 Nov 20 01:05 README.md
  12. -rw-rw-r-- 1 root root 61797 Nov 20 01:05 redis.conf
  13. -rwxrwxr-x 1 root root 275 Nov 20 01:05 runtest
  14. -rwxrwxr-x 1 root root 280 Nov 20 01:05 runtest-cluster
  15. -rwxrwxr-x 1 root root 373 Nov 20 01:05 runtest-moduleapi
  16. -rwxrwxr-x 1 root root 281 Nov 20 01:05 runtest-sentinel
  17. -rw-rw-r-- 1 root root 9710 Nov 20 01:05 sentinel.conf
  18. drwxrwxr-x 3 root root 8192 Feb 11 11:33 src
  19. drwxrwxr-x 11 root root 182 Nov 20 01:05 tests
  20. drwxrwxr-x 8 root root 4096 Nov 20 01:05 utils
  21. [root@centos redis-5.0.7]# cp redis.conf /apps/redis/etc/

启动redis(这个启动方式)


 
  1. [root@centos redis-5.0.7]# /apps/redis/bin/redis-server /apps/redis/etc/redis.conf
  2. 8315:C 11 Feb 2020 11:40:12.016 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
  3. 8315:C 11 Feb 2020 11:40:12.016 # Redis version=5.0.7, bits=64, commit=00000000, modified=0, pid=8315, just started
  4. 8315:C 11 Feb 2020 11:40:12.016 # Configuration loaded
  5. 8315:M 11 Feb 2020 11:40:12.017 * Increased maximum number of open files to 10032 (it was originally set to 1024).
  6. _._
  7. _.-``__ ''-._
  8. _.-`` `. `_. ''-._ Redis 5.0.7 (00000000/0) 64 bit
  9. .-`` .-```. ```\/ _.,_ ''-._
  10. ( ' , .-` | `, ) Running in standalone mode
  11. |`-._`-...-` __...-.``-._|'` _.-'| Port: 6379
  12. | `-._ `._ / _.-' | PID: 8315
  13. `-._ `-._ `-./ _.-' _.-'
  14. |`-._`-._ `-.__.-' _.-'_.-'|
  15. | `-._`-._ _.-'_.-' | http://redis.io
  16. `-._ `-._`-.__.-'_.-' _.-'
  17. |`-._`-._ `-.__.-' _.-'_.-'|
  18. | `-._`-._ _.-'_.-' |
  19. `-._ `-._`-.__.-'_.-' _.-'
  20. `-._ `-.__.-' _.-'
  21. `-._ _.-'
  22. `-.__.-'
  23.  
  24. 8315:M 11 Feb 2020 11:40:12.017 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
  25. 8315:M 11 Feb 2020 11:40:12.017 # Server initialized
  26. 8315:M 11 Feb 2020 11:40:12.017 # 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.
  27. 8315:M 11 Feb 2020 11:40:12.017 # 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.
  28. 8315:M 11 Feb 2020 11:40:12.018 * Ready to accept connections

解决当前的警告提示

tcp-backlog
The backlog argument defines the maximum length to which the queue of pending connections for sockfdmay grow. If a connection request arrives when the queue is full, the client may receive an error with an indication of ECONNREFUSED or, if the underlying protocol supports retransmission, the request may be ignored so that a later reattempt at connection succeeds.

backlog参数控制的是三次握手的时候server端收到client ack确认号之后的队列值。


 
  1. [root@centos ~]# echo 511 > /proc/sys/net/core/somaxconn
  2. vm.overcommit_memory

0、表示内核将检查是否有足够的可用内存供应用进程使用;如果有足够的可用内存,内存申请允许;否则,内存申请失败,并把错误返回给应用进程。 1、表示内核允许分配所有的物理内存,而不管当前的内存状态如何。 2、表示内核允许分配超过所有物理内存和交换空间总和的内存


 
  1. [root@centos ~]# echo "vm.overcommit_memory = 1" >/etc/sysctl.conf
  2. transparent hugepage
  3. 大页内存动态分配,需要关闭让redis 负责内存管理。
  4. [root@centos ~]# echo never > /sys/kernel/mm/transparent_hugepage/enabled

重启一下服务


 
  1. # 再次启动服务,警告信息没有了。
  2. [root@centos ~]# /apps/redis/bin/redis-server /apps/redis/etc/redis.conf
  3. 1847:C 13 Feb 2020 12:03:59.281 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
  4. 1847:C 13 Feb 2020 12:03:59.281 # Redis version=5.0.7, bits=64, commit=00000000, modified=0, pid=1847, just started
  5. 1847:C 13 Feb 2020 12:03:59.281 # Configuration loaded
  6. 1847:M 13 Feb 2020 12:03:59.282 * Increased maximum number of open files to 10032 (it was originally set to 1024).
  7. _._
  8. _.-``__ ''-._
  9. _.-`` `. `_. ''-._ Redis 5.0.7 (00000000/0) 64 bit
  10. .-`` .-```. ```\/ _.,_ ''-._
  11. ( ' , .-` | `, ) Running in standalone mode
  12. |`-._`-...-` __...-.``-._|'` _.-'| Port: 6379
  13. | `-._ `._ / _.-' | PID: 1847
  14. `-._ `-._ `-./ _.-' _.-'
  15. |`-._`-._ `-.__.-' _.-'_.-'|
  16. | `-._`-._ _.-'_.-' | http://redis.io
  17. `-._ `-._`-.__.-'_.-' _.-'
  18. |`-._`-._ `-.__.-' _.-'_.-'|
  19. | `-._`-._ _.-'_.-' |
  20. `-._ `-._`-.__.-'_.-' _.-'
  21. `-._ `-.__.-' _.-'
  22. `-._ _.-'
  23. `-.__.-'
  24.  
  25. 1847:M 13 Feb 2020 12:03:59.282 # Server initialized
  26. 1847:M 13 Feb 2020 12:03:59.282 * DB loaded from disk: 0.000 seconds
  27. 1847:M 13 Feb 2020 12:03:59.282 * Ready to accept connections

编辑redis服务启动脚本


 
  1. [root@centos ~]# cat /usr/lib/systemd/system/redis.service
  2. [Unit]
  3. Description=Redis persistent key-value database
  4. After=network.target
  5. After=network-online.target
  6. Wants=network-online.target
  7. [Service]
  8. #ExecStart=/usr/bin/redis-server /etc/redis.conf --supervised systemd
  9. ExecStart=/apps/redis/bin/redis-server /apps/redis/etc/redis.conf --supervised systemd
  10. ExecReload=/bin/kill -s HUP $MAINPID
  11. ExecStop=/bin/kill -s QUIT $MAINPID
  12. Type=notify
  13. User=redis
  14. Group=redisRuntimeDirectory=redis
  15. RuntimeDirectoryMode=0755
  16. [Install]
  17. WantedBy=multi-user.target

添加redis用户


 
  1. # 添加用户和组
  2. [root@centos ~]# groupadd -g 1001 redis && useradd -u 1001 -g 1001 redis -s /sbin/nologin
  3. # 数据目录设置所有者所属组
  4. [root@centos ~]# chown redis.redis -R /apps/redis/
  5. system启动测试
  6. # 开启redis并设为开机启动
  7. [root@centos ~]# systemctl enable --now redis
  8. Created symlink from /etc/systemd/system/multi-user.target.wants/redis.service to /usr/lib/systemd/system/redis.service.
  9. # 查看端口
  10. [root@centos ~]# ss -tnl
  11. State Recv-Q Send-Q Local Address:Port Peer Address:Port
  12. LISTEN 0 128 127.0.0.1:631 *:*
  13. LISTEN 0 100 127.0.0.1:25 *:*
  14. LISTEN 0 128 127.0.0.1:6010 *:*
  15. LISTEN 0 128 127.0.0.1:6011 *:*
  16. LISTEN 0 128 *:43108 *:*
  17. LISTEN 0 511 127.0.0.1:6379 *:* # 这个为redis端口
  18. LISTEN 0 128 *:111 *:*
  19. LISTEN 0 5 192.168.122.1:53 *:*
  20. LISTEN 0 128 *:22 *:*
  21. LISTEN 0 128 [::1]:631 [::]:*
  22. LISTEN 0 100 [::1]:25 [::]:*
  23. LISTEN 0 128 [::1]:6010 [::]:*
  24. LISTEN 0 128 [::1]:6011 [::]:*
  25. LISTEN 0 128 [::]:59279 [::]:*
  26. LISTEN 0 128 [::]:111 [::]:*
  27. LISTEN 0 128 [::]:22 [::]:*

客户端连接redis测试


 
  1. # 这里还没有把这个命令加到PATH变量里
  2. [root@centos ~]# /apps/redis/bin/redis-cli
  3. 127.0.0.1:6379> info
  4. # Server
  5. redis_version:5.0.7
  6. redis_git_sha1:00000000
  7. redis_git_dirty:0
  8. redis_build_id:b0887378c143d6e9
  9. redis_mode:standalone
  10. os:Linux 3.10.0-1062.el7.x86_64 x86_64
  11. arch_bits:64
  12. multiplexing_api:epoll
  13. atomicvar_api:atomic-builtin
  14. gcc_version:4.8.5
  15. process_id:2088
  16. run_id:e0bbd2dc1561d1610565c6c8fb61aa817e30924c
  17. tcp_port:6379

创建命令软连接(也可以把这个路径加到PATH变量里面)


 
  1. [root@centos ~]# ln -sv /apps/redis/bin/redis-* /usr/bin/
  2. ‘/usr/bin/redis-benchmark' -> ‘/apps/redis/bin/redis-benchmark'
  3. ‘/usr/bin/redis-check-aof' -> ‘/apps/redis/bin/redis-check-aof'
  4. ‘/usr/bin/redis-check-rdb' -> ‘/apps/redis/bin/redis-check-rdb'
  5. ‘/usr/bin/redis-cli' -> ‘/apps/redis/bin/redis-cli'
  6. ‘/usr/bin/redis-sentinel' -> ‘/apps/redis/bin/redis-sentinel'
  7. ‘/usr/bin/redis-server' -> ‘/apps/redis/bin/redis-server'
  8. # 命令连接测试
  9. [root@centos ~]# redis-cli
  10. 127.0.0.1:6379> info
  11. # Server
  12. redis_version:5.0.7
  13. redis_git_sha1:00000000
  14. redis_git_dirty:0
  15. redis_build_id:b0887378c143d6e9
  16. redis_mode:standalone
  17. os:Linux 3.10.0-1062.el7.x86_64 x86_64
  18. arch_bits:64
  19. multiplexing_api:epoll
  20. atomicvar_api:atomic-builtin
  21. gcc_version:4.8.5
  22. process_id:2088
  23. run_id:e0bbd2dc1561d1610565c6c8fb61aa817e30924c
  24. tcp_port:6379
  25. uptime_in_seconds:755
  26. uptime_in_days:0
  27. [root@centos ~]# ll /apps/redis/bin/
  28. total 32772
  29. -rwxr-xr-x 1 redis redis 4366824 Feb 11 11:33 redis-benchmark # redis性能测试工具
  30. -rwxr-xr-x 1 redis redis 8125216 Feb 11 11:33 redis-check-aof # AOF文件检查工具
  31. -rwxr-xr-x 1 redis redis 8125216 Feb 11 11:33 redis-check-rdb # RDB文件检查工具
  32. -rwxr-xr-x 1 redis redis 4807896 Feb 11 11:33 redis-cli # 客户端工具
  33. lrwxrwxrwx 1 redis redis 12 Feb 11 11:33 redis-sentinel -> redis-server # 哨兵软连接到server
  34. -rwxr-xr-x 1 redis redis 8125216 Feb 11 11:33 redis-server # redis 服务启动命令

**以上就是redis的安装方法和一些小问题的解决方法了!**

总结

以上所述是小编给大家介绍的编译安装redisd的方法示例详解,希望对大家有所帮助,也非常感谢大家对我们网站的支持!

分享到:

相关信息

  • redis实现session共享的方法

    引言大厂很多项目都是部署到多台服务器上,这些服务器在各个地区都存在,当我们访问服务时虽然执行的是同一个服务,但是可能是不同服务器运行的;在我学习项目时遇到这样一个登录情...

    2023-11-01

  • 简单聊一聊redis过期时间的问题

    1.多次修改一个redis的String过期键,如何保证他仍然能保留第一次设置时的删除时间 2.修改hash、set、Zset、list的值,会使过期时间重置吗?...

    2023-11-01

系统教程栏目

栏目热门教程

人气教程排行

站长推荐

热门系统下载