centos7安装redis
关闭防火墙:
systemctl stop firewalld.service #停止firewall
systemctl disable firewalld.service #禁止firewall开机启动
firewall-cmd --state #查看默认防火墙状态(关闭后显示notrunning,开启后显示running)
配置编译环境:
sudo yum install gcc-c++
下载源码:
wget http://download.redis.io/releases/redis-3.2.8.tar.gz
解压源码:
tar -zxvf redis-3.2.8.tar.gz
进入到解压目录:
cd redis-3.2.8
执行make编译Redis:
make MALLOC=libc
注意:make命令执行完成编译后,会在src目录下生成6个可执行文件,分别是redis-server、redis-cli、redis-benchmark、redis-check-aof、redis-check-rdb、redis-sentinel。
安装Redis:
make install
配置Redis能随系统启动:
./utils/install_server.sh
显示结果信息如下:
Welcome to the redis service installer
This script will help you easily set up a running redis server
Please select the redis port for this instance: [6379]
Selecting default: 6379
Please select the redis config file name [/etc/redis/6379.conf]
Selected default - /etc/redis/6379.conf
Please select the redis log file name [/var/log/redis_6379.log]
Selected default - /var/log/redis_6379.log
Please select the data directory for this instance [/var/lib/redis/6379]
Selected default - /var/lib/redis/6379
Please select the redis executable path [/usr/local/bin/redis-server]
Selected config:
Port : 6379
Config file : /etc/redis/6379.conf
Log file : /var/log/redis_6379.log
Data dir : /var/lib/redis/6379
Executable : /usr/local/bin/redis-server
Cli Executable : /usr/local/bin/redis-cli
Is this ok? Then press ENTER to go on or Ctrl-C to abort.
Copied /tmp/6379.conf => /etc/init.d/redis_6379
Installing service...
Successfully added to chkconfig!
Successfully added to runlevels 345!
Starting Redis server...
Installation successful!
Redis服务查看、开启、关闭:
a.通过ps -ef|grep redis命令查看Redis进程
b.开启Redis服务操作通过/etc/init.d/redis_6379 start命令,也可通过(service redis_6379 start)
c.关闭Redis服务操作通过/etc/init.d/redis_6379 stop命令,也可通过(service redis_6379 stop)
redis.conf 的配置信息
1、daemonize 如果需要在后台运行,把该项改为yes
2、pidfile 配置多个pid的地址 默认在/var/run/redis.pid
3、bind 绑定ip,设置后只接受来自该ip的请求
4、port 监听端口,默认是6379
5、loglevel 分为4个等级:debug verbose notice warning
6、logfile 用于配置log文件地址
7、databases 设置数据库个数,默认使用的数据库为0
8、save 设置redis进行数据库镜像的频率。
9、rdbcompression 在进行镜像备份时,是否进行压缩
10、dbfilename 镜像备份文件的文件名
11、Dir 数据库镜像备份的文件放置路径
12、Slaveof 设置数据库为其他数据库的从数据库
13、Masterauth 主数据库连接需要的密码验证
14、Requriepass 设置 登陆时需要使用密码
15、Maxclients 限制同时使用的客户数量
16、Maxmemory 设置redis能够使用的最大内存
17、Appendonly 开启append only模式
18、Appendfsync 设置对appendonly.aof文件同步的频率(对数据进行备份的第二种方式)
19、vm-enabled 是否开启虚拟内存支持 (vm开头的参数都是配置虚拟内存的)
20、vm-swap-file 设置虚拟内存的交换文件路径
21、vm-max-memory 设置redis使用的最大物理内存大小
22、vm-page-size 设置虚拟内存的页大小
23、vm-pages 设置交换文件的总的page数量
24、vm-max-threads 设置VM IO同时使用的线程数量
25、Glueoutputbuf 把小的输出缓存存放在一起
26、hash-max-zipmap-entries 设置hash的临界值
27、Activerehashing 重新hash
177 # dbid is a number between 0 and 'databases'-1 178 databases 16
在redis数据库配置文件中发现redis有16个库,查看进程的方法ps -ef|grep 加apache或者redis
ps -ef|grep redis root 11646 1 0 00:48 ? 00:00:00 /usr/local/bin/redis-server 127.0.0.1:6379 root 12019 10530 0 01:03 pts/0 00:00:00 grep --color=auto redis
安装完我的redis目录在:user/local/bin
按着这个教程安装成功,尊重作者:http://www.cnblogs.com/web424/p/6796993.html
这里只是安装redis服务,还需要PHP开启redis扩展,让PHP连接上redis,请看这里 http://www.cnblogs.com/hanshuai0921/p/7092081.html
php7安装redis拓展
phpredis下载地址https://github.com/phpredis/phpredis
解压并进入源码包
wget https://github.com/phpredis/phpredis/archive/develop.zip
mv develop.zip phpredis-develop.zip
unzip phpredis-develop.zip
cd phpredis-develop
生成configure配置文件:/usr/local/php/bin/phpize
编译安装:
./configure --with-php-config=/usr/local/php/bin/php-config
make && make install
配置php.ini
在extension后添加
extension=redis.so
检查:
[root@test etc]# /usr/local/php-7.1/bin/php -m|grep redis
redis
重启php后在浏览器中可看到phpinfo();中也有redis扩展模块了
设置redis访问密码
在服务器上,这里以linux服务器为例,为redis配置密码。
1.第一种方式 (当前这种linux配置redis密码的方法是一种临时的,如果redis重启之后密码就会失效,)
(1)首先进入redis,如果没有开启redis则需要先开启:
[root@iZ94jzcra1hZ bin]# redis-cli -p 6379
127.0.0.1:6379>
(2)查看当前redis有没有设置密码:
127.0.0.1:6379> config get requirepass
1) "requirepass"
2) ""
(3)为以上显示说明没有密码,那么现在来设置密码:
127.0.0.1:6379> config set requirepass abcdefg
OK
127.0.0.1:6379>
(4)再次查看当前redis就提示需要密码:
127.0.0.1:6379> config get requirepass
(error) NOAUTH Authentication required.
127.0.0.1:6379>
2.第二种方式 (永久方式)
需要永久配置密码的话就去redis.conf的配置文件中找到requirepass这个参数,如下配置:
修改redis.conf配置文件
# requirepass foobared
requirepass 123 指定密码123
保存后重启redis就可以了
连接redis
1.redis-cli连接redis
[root@iZ2ze3zda3caeyx6pn7c5zZ bin]# redis-cli
127.0.0.1:6379> keys *
(error) NOAUTH Authentication required.
127.0.0.1:6379> auth 123 //指定密码
OK
127.0.0.1:6379> keys *
1) "a"
2) "cit"
3) "clist"
4) "1"
127.0.0.1:6379>
远程连接redis
redis-cli -h 这里填写ip地址 -p 1234
设置了密码的redis停止方法有所不一样,如果使用service redis_6379 stop 去停止,会报需要认证的错误
Stopping ...
(error) NOAUTH Authentication required.
Waiting for Redis to shutdown ...
Waiting for Redis to shutdown ...
Waiting for Redis to shutdown ...
可以使用下面的方法停止redis,用redis-cli 密码登陆 然后shutdown 然后exit 就OK了。
redis-cli
127.0.0.1:6379> auth 你的密码
OK
127.0.0.1:6379> shutdown
not connected> exit
再用ps -ef|grep redis 可以看到redis进程已经退出。
搜索指定REDIS前缀的键并删除它们,如果是连本机则不需要-h参数,如果没有密码则不需要-a参数
redis-cli -a 'yourPassword' -h 192.168.1.88 keys "REDIS_USER_*" | xargs redis-cli -a 'yourPassword' -h 192.168.1.88 del