CentOS7安装PHP开发环境3-源码安装Redis、Memcache

OS:CentOS 7.3
Redis:3.2.9
Memcache:1.5.12

安装Redis服务

源码安装Redis

Redis安装后的命令

配置文件:/application/redis/redis.conf
启动命令:
redis-server & 启动redis,加上&表示使redis以后台程序方式运行
redis-server /application/redis/redis.conf 启动redis,指定配置文件
redis-cli shutdown 关闭redis
redis-cli 命令行中连接redis
127.0.0.1:6397> exit exit是退出命令,或者Ctrl+C
netstat -ntlp | grep redis 查看端口是否在使用中
ps -ef | grep redis | grep -v grep

参考:http://www.cnblogs.com/hanyinglong/p/5036558.html
下载redis源码:https://redis.io/download
或使用命令
wget http://download.redis.io/releases/redis-3.2.3.tar.gz
wget http://download.redis.io/releases/redis-5.0.5.tar.gz
如:redis-3.2.9.tar.gz,将redis安装在/application/目录下(错)

$ tar -zxvf redis-3.2.9.tar.gz -C /application #解压到/application目录下
$ cd /application/redis-3.2.9
$ make 
$ make PREFIX=/application/redis-3.2.9/ install 

PREFIX=#安装到指定目录中,注意:PREFIX省略则 二进制文件会到 /usr/local/bin/ 目录下
在这里插入图片描述

在这里插入图片描述
出现上面的语句,则说明安装成功了。

创建无版本号的软连接,并加入到PATH中。

# ln -s /application/redis-3.2.9 /application/redis
# echo 'PATH=/application/redis/bin:$PATH' >> /etc/profile
# source /etc/profile

注意:PREFIX省略则 二进制文件会到 /usr/local/bin/ 目录下

# find /usr -name redis-*
/usr/local/bin/redis-server
/usr/local/bin/redis-benchmark
/usr/local/bin/redis-cli
/usr/local/bin/redis-check-rdb
/usr/local/bin/redis-check-aof
/usr/local/bin/redis-sentinel

在这里插入图片描述
在这里插入图片描述

Redis的测试

启动Redis服务

因为上文中,安装时指定了二进制文件的安装目录,所以
# redis-server & 启动redis,加上&表示使redis以后台程序方式运行
# /application/redis/bin/redis-server & 启动redis,后台方式启动
# redis-server /application/redis/redis.conf 启动redis,指定配置文件

[root@sy-pc src]# redis-server /application/redis/redis.conf
12880:M 06 Jul 07:48:50.576 * Increased maximum number of open files to 10032 (it was originally set to
                _._                                                 
           _.-``__ ''-._                                             
      _.-``    `.  `_.  ''-._           Redis 3.2.9 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._                                   
 (    '      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |     PID: 12880
  `-._    `-._  `-./  _.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                 
 |    `-._`-._        _.-'_.-'    |           http://redis.io
  `-._    `-._`-.__.-'_.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                 
 |    `-._`-._        _.-'_.-'    |                                 
  `-._    `-._`-.__.-'_.-'    _.-'                                   
      `-._    `-.__.-'    _.-'                                       
          `-._        _.-'                                           
              `-.__.-'                                               

12880:M 06 Jul 07:48:50.578 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/
12880:M 06 Jul 07:48:50.578 # Server started, Redis version 3.2.9
12880:M 06 Jul 07:48:50.578 # WARNING overcommit_memory is set to 0! Background save may fail under lowmemory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for
12880:M 06 Jul 07:48:50.578 # WARNING you have Transparent Huge Pages (THP) support enabled in your kerth Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' tain the setting after a reboot. Redis must be restarted after THP is disabled.
12880:M 06 Jul 07:48:50.579 * The server is now ready to accept connections on port 6379

如果输入 redis-server & 后按Ctrl+C,再次输入redis-server & ,则出现下面的语句:

在这里插入图片描述

关闭redis服务

在这里插入图片描述
关闭redis服务,在另一个远程会话框输入命令 redis-cli shutdown ,那么上面的会话框则出现如下内容:
在这里插入图片描述
当redis启动完成之后,使用 netstat -ntlp | grep 6379 查看端口是否在使用中
在这里插入图片描述
命令行中连接redis并且测试,连接命令为 src/redis-cli ;退出命令是 exit
在这里插入图片描述

Redis后台启动,隐藏界面

参考:https://blog.csdn.net/ksdb0468473/article/details/52126009
# vim /application/redis3.2.9/redis.conf

daemonize no
修改为
daemonize yes

关闭redis:
# killall redis-server
启动redis:
# /application/redis3.2.9/src/redis-server /application/redis3.2.9/redis.conf

Redis远程访问:

参考:https://www.cnblogs.com/liusxg/p/5712493.html
# vim /application/redis3.2.9/redis.conf

bind 127.0.0.1
注释为
# bind 127.0.0.1
注释掉bind 127.0.0.1可以使所有的ip访问redis。若是想指定多个ip访问,但并不是全部ip访问,可以bind。

在redis3.2之后,还要继续一下修改

protected-mode yes
修改为
protected-mode no

指定配置文件,后台启动

# /application/redis/bin/redis-server /application/redis/redis.conf &

加入开机启动脚本

# echo '/application/redis/bin/redis-server /application/redis/redis.conf &' >> /etc/rc.load

yum安装Redis

1、检查是否有redis yum 源

yum install redis

2、下载fedora的epel仓库

yum install epel-release

3、安装redis数据库

yum install redis

4、安装完毕后,使用下面的命令启动redis服务
CentOS 6

service redis start # 启动redis
service redis stop # 停止redis
service redis status # 查看redis运行状态
ps -ef | grep redis # 查看redis进程

CentOS 7

systemctl start redis # 启动redis
systemctl stop redis # 停止redis
systemctl restart redis # 重启redis
systemctl status redis # 查看redis运行状态

5、设置redis为开机自动启动
CentOS 6

# chkconfig redis on

CentOS 7

# systemctl enable redis
Created symlink from /etc/systemd/system/multi-user.target.wants/redis.service to /usr/lib/systemd/system/redis.service.

6、禁止开机启动
CentOS 6

# chkconfig redis off

CentOS 7

# systemctl disable redis
Removed symlink /etc/systemd/system/multi-user.target.wants/redis.service.

注意,CentOS 7 yum 安装redis服务后,自动生成的文件:

[]# vim /usr/lib/systemd/system/redis.service 

[Unit]
Description=Redis persistent key-value database
After=network.target
After=network-online.target
Wants=network-online.target

[Service]
ExecStart=/usr/bin/redis-server /etc/redis.conf --supervised systemd
ExecStop=/usr/libexec/redis-shutdown
Type=notify
User=redis
Group=redis
RuntimeDirectory=redis
RuntimeDirectoryMode=0755

[Install]
WantedBy=multi-user.target

7、防火墙开放相应端口
CentOS 6

# 开启6379
/sbin/iptables -I INPUT -p tcp --dport 6379 -j ACCEPT
# 开启6380
/sbin/iptables -I INPUT -p tcp --dport 6380 -j ACCEPT
# 保存
/etc/rc.d/init.d/iptables save
# centos 7下执行
service iptables save

修改Redis默认端口和密码

1、打开配置文件 vim /etc/redis.conf
2、修改默认端口,查找 port 6379 修改为相应端口即可,如6179
3、修改默认密码,查找 requirepass foobaredfoobared 修改为你的密码,如111
4、使用配置文件启动 redis

redis-server /etc/redis.conf &

5、使用端口登录

# redis-cli -h 127.0.0.1 -p 6179

6、此时再输入命令则会报错

# redis-cli -h 127.0.0.1 -p 6179
127.0.0.1:6179> info
NOAUTH Authentication required.
127.0.0.1:6179>

7、输入刚才输入的密码 auth 111

# redis-cli -h 127.0.0.1 -p 6179
127.0.0.1:6179> info
NOAUTH Authentication required.
127.0.0.1:6179> auth 111
OK
127.0.0.1:6179>

++++++++++++++++++++++++++++++++++++++++++++++++++++
++++++++++++++++++++++++++++++++++++++++++++++++++++

安装Memcached服务

待续。。。。。。
在线安装参考:http://blog.csdn.net/baidu_30000217/article/details/51494007
源码安装参考:http://www.cnblogs.com/ggjucheng/p/3349233.html

源码安装

执行:

yum install libevent
yum install libevent-devel 

默认安装目录为 /usr/lib64/libevent

编译:

./configure --prefix=/usr/local/memcached-1.5.12

提示:

If it's already installed, specify its path using --with-libevent=/dir/

编译命令改为

./configure --prefix=/usr/local/memcached-1.5.12 --with-libevent=/usr/lib64/libevent

创建软连接

ln -s /usr/local/memcached-1.5.12 /usr/local/memcached

yum安装

yum install memcached*
#配置文件的位置 :
vim /etc/sysconfig/memcached
启动
# /usr/bin/memcached -d -l 127.0.0.1 -p 11211 -m 64 -u root
-d 守护进程模式(退出终端窗口之后使程序还在运行),
-l 指定IP地址127.0.0.1 ,
-p 指定端口号11211,
-m 为memcached分配多少内存(单位:M),
-u 指定使用哪个用户启动memcached
# /usr/local/bin/memcached -d -m 200 -u root -l 127.0.0.1 -p 11211 -c 1024 -P /tmp/memcached.pid
# /usr/local/memcached/bin/memcached -d -m 200 -u root -l 127.0.0.1 -p 11211 -P /tmp/memcached.pid
-d选项是启动一个守护进程,(退出终端窗口之后使程序还在运行),
-m是分配给Memcache使用的内存数量,单位是MB,这里是200MB
-u是运行Memcache的用户,如果当前为 root 的话,需要使用此参数指定用户。
-l是监听的服务器IP地址,如果有多个地址的话,我这里指定了服务器的IP地址127.0.0.1
-p是设置Memcache监听的端口,我这里设置了11211,最好是1024以上的端口
-c选项是最大运行的并发连接数,默认是1024,这里设置了256
-P是设置保存Memcache的pid文件,我这里是保存在 /tmp/memcached.pid

查看memcached是否在运行:

[root@sy-pc sy]# ps -ef|grep memcached
root      9664     1  0 22:25 ?        00:00:00 /usr/bin/memcached -d -l 127.0.0.1 -p 11211 -m 64 -u root
root      9671  9582  0 22:25 pts/3    00:00:00 grep --color=auto memcached

[root@sy-pc sy]# pstree -p|grep memcached
           |-memcached(9664)-+-{memcached}(9665)
           |                 |-{memcached}(9666)
           |                 |-{memcached}(9667)
           |                 |-{memcached}(9668)
           |                 `-{memcached}(9669)

上面的命令不准,用下面的命令:
#启动
memcached -u root -d
查看memcached的运行状态
ps -ef|grep memcached
pgrep memcached
#停止
pgrep memcached
kill -9 上面得到的进程号
kill -9 9664

kill -9  `cat /tmp/memcached.pid`

-d参数说明

-d install 安装memcached
-d uninstall 卸载memcached
-d start 启动memcached服务
-d restart 重启memcached服务
-d stop 停止memcached服务
-d shutdown 停止memcached服务

参考文献

[1] 赖荣生. CentOS7 linux下yum安装redis以及使用[DB|OL]. https://www.cnblogs.com/rslai/p/8249812.html
[2] 大大的微笑. systemctl管理Redis启动、停止、开机启动[DB|OL]. https://blog.csdn.net/chwshuang/article/details/68489968
[3] Kencery. Linux(Centos)之安装Redis及注意事项[DB|OL]. https://www.cnblogs.com/hanyinglong/p/5036558.html .

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值