NoSQL(Not Only SQL)
准备源码包redis-4.0.8.tar.gz
[root@client ~]# rpm -q gcc
[root@client ~]# yum -y install gcc
[root@client ~]# tar -zxf redis-4.0.8.tar.gz
[root@client ~]# cd redis-4.0.8/
[root@client redis-4.0.8]# make && make install
[root@client redis-4.0.8]# ./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!
[root@client redis-4.0.8]# netstat -utnlp |grep 6379
tcp 0 0 127.0.0.1:6379 0.0.0.0:* LISTEN 5140/redis-server 1
[root@client ~]# /etc/init.d/redis_6379 stop
Stopping ...
Redis stopped
[root@client ~]# /etc/init.d/redis_6379 start
Starting Redis server...
[root@client ~]# ps -C redis-server
PID TTY TIME CMD
5157 ? 00:00:00 redis-server
[root@client ~]# netstat -utnlp |grep 6379
tcp 0 0 127.0.0.1:6379 0.0.0.0:* LISTEN 5157/redis-server 1
[root@client redis-4.0.8]# redis-cli
127.0.0.1:6379> ping
PONG
127.0.0.1:6379> set name abc //存数据
OK
127.0.0.1:6379> get name //取数据
"abc"
127.0.0.1:6379> exit
[root@client redis-4.0.8]#
127.0.0.1:6379> set name abc //存数据
OK
127.0.0.1:6379> get name //取数据
"abc"
127.0.0.1:6379> MSET age 19 sex boy
OK
127.0.0.1:6379> get name
"abc"
127.0.0.1:6379> MGET age sex
1) "19"
2) "boy"
127.0.0.1:6379> keys *
1) "sex"
2) "name"
3) "age"
127.0.0.1:6379> keys ??? //显示三个字符的
1) "sex"
2) "age"
127.0.0.1:6379> keys ???? //显示四个字符的
1) "name"
127.0.0.1:6379> keys a* //显示以a开头的
1) "age"
127.0.0.1:6379> type age //使用set 命令存储的都是字符类型
string
127.0.0.1:6379> del age
(integer) 1
127.0.0.1:6379> EXISTS age //变量不存在返回值0
(integer) 0
127.0.0.1:6379> EXISTS sex //变量存在返回值1
(integer) 1
127.0.0.1:6379> TTL sex //返回值 -1,表示变量永不过期
(integer) -1
127.0.0.1:6379> EXPIRE sex 20 //设置变量过期时间为20秒
(integer) 1
127.0.0.1:6379> TTL sex //还剩14秒过期
(integer) 14
127.0.0.1:6379> ttl sex //返回值 -2 表示已经过期
(integer) -2
127.0.0.1:6379> EXISTS sex //变量已经不存在
(integer) 0
127.0.0.1:6379> MOVE name 1 //把变量name移动到1号库
(integer) 1
127.0.0.1:6379> select 1 //切换到1号库
OK
127.0.0.1:6379[1]> keys * //查看所有
1) "name"
127.0.0.1:6379[1]> FLUSHDB
OK
127.0.0.1:6379[1]> keys *
(empty list or set)
127.0.0.1:6379[1]> FLUSHALL
OK
127.0.0.1:6379[1]> SAVE
OK
127.0.0.1:6379[1]> SHUTDOWN
not connected> exit
[root@client redis-4.0.8]# netstat -utnlp | grep redis-server //没有进程信息
[root@client redis-4.0.8]#
[root@client ~]# /etc/init.d/redis_6379 start
Starting Redis server...
[root@client ~]# netstat -utnlp | grep redis-server
tcp 0 0 127.0.0.1:6379 0.0.0.0:* LISTEN 1570/redis-server 1
名称 | 说明 |
---|---|
NETWORK | 网络 |
GENERAL | 常规 |
SNAPSHOTTING | 快照 |
REPLICATION | 复制 |
SECURITY | 安全 |
CLIENTS | 客户端 |
MEMORY MANAGEMENT | 内存管理 |
[root@client ~]# sed -n '70p;93p;501p' /etc/redis/6379.conf //修改前
bind 127.0.0.1
port 6379
# requirepass foobared
[root@client ~]# vim /etc/redis/6379.conf
bind 192.168.4.50
port 6350
requirepass 123456
重启服务
[root@client ~]# /etc/init.d/redis_6379 stop
Stopping ...
Redis stopped
[root@client ~]# /etc/init.d/redis_6379 start
Starting Redis server...
[root@client ~]# netstat -utnlp | grep redis
tcp 0 0 192.168.4.50:6350 0.0.0.0:* LISTEN 1702/redis-server 1
连接服务时需要指定IP地址和端口
[root@client ~]# redis-cli -h 192.168.4.50 -p 6350
192.168.4.50:6350> ping
(error) NOAUTH Authentication required.
192.168.4.50:6350> AUTH 123456 //输入密码
OK
192.168.4.50:6350> ping
PONG
[root@client ~]# redis-cli -h 192.168.4.50 -p 6350 -a 123456
192.168.4.50:6350> ping
PONG
两台虚拟机
[root@lnmp ~]# yum -y install gcc pcre-devel zlib-devel
[root@lnmp ~]# tar -zxf nginx-1.12.2.tar.gz
[root@lnmp ~]# ls
[root@lnmp ~]# cd nginx-1.12.2/
[root@lnmp nginx-1.12.2]#
[root@lnmp nginx-1.12.2]# ./configure
[root@lnmp nginx-1.12.2]# make && make install
[root@lnmp nginx-1.12.2]# yum -y install php-fpm
[root@lnmp ~]# vim +65 /usr/local/nginx/conf/nginx.conf
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi.conf;
}
[root@lnmp ~]# /usr/local/nginx/sbin/nginx -t //测试修改
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@lnmp ~]# /usr/local/nginx/sbin/nginx
[root@lnmp ~]# netstat -utnlp | grep 80
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 4022/nginx: master
[root@lnmp ~]# systemctl start php-fpm.service
[root@lnmp ~]# netstat -utnlp | grep 9000
tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 4090/php-fpm: maste
[root@lnmp ~]# vim /usr/local/nginx/html/test.php
<?php
echo "l love china" ;
?>
[root@lnmp ~]# curl http://127.0.0.1/test.php
l love china
[root@lnmp ~]# tar -zxf redis-4.0.8.tar.gz
[root@lnmp ~]# cd redis-4.0.8/
[root@lnmp redis-4.0.8]# make && make install
[root@lnmp redis-4.0.8]# ./utils/install_server.sh
[root@lnmp ~]# yum -y install php php-devel automake autocon
[root@lnmp ~]# tar -zxf php-redis-2.2.4.tar.gz
[root@lnmp ~]# ls
[root@lnmp ~]# cd phpredis-2.2.4/
[root@lnmp phpredis-2.2.4]# phpize //生成配置文件php-config及configure命令
Configuring for:
PHP Api Version: 20100412
Zend Module Api No: 20100525
Zend Extension Api No: 220100525
[root@lnmp phpredis-2.2.4]# ./configure --with-php-config=/usr/bin/php-config
[root@lnmp phpredis-2.2.4]# make
[root@lnmp phpredis-2.2.4]# make install
Installing shared extensions: /usr/lib64/php/modules/
[root@lnmp phpredis-2.2.4]# ls /usr/lib64/php/modules/
curl.so fileinfo.so json.so phar.so redis.so zip.so
[root@lnmp ~]# vim +728 /etc/php.ini
extension_dir = "/usr/lib64/php/modules/"
; On windows:
extension = "redis.so"
[root@lnmp ~]# systemctl restart php-fpm.service
[root@lnmp ~]# php -m | grep -i redis
redis
[root@lnmp ~]# vim /usr/local/nginx/html/x.php
<?php
$redis = new redis();
$redis->connect("192.168.4.50","6350");
$redis->auth("123456");
$redis->set("school","abc");
echo $redis->get("school");
?>
[root@lnmp ~]# curl http://127.0.0.1/x.php
192.168.4.50:6350> keys *
1) "school"
192.168.4.50:6350> get school
"abc"