==----------------------------------------------------------------------------------------------------------------------
-----== Linux环境下redis的安装及php对redis的连接操作
1、下载资源
redis-4.0.9.tar.gz
2、解压文件
cd /mine/software/redis-4.0.9
tar -zxvf redis-4.0.9.tar.gz
3、进入解压后的目录,编译
cd redis-4.0.9
make
编译完成后查看:
[root@iZm5eizpokikoertia0x31Z redis-4.0.9]# ll src/redis*
-rw-rw-r-- 1 root root 2417 Mar 27 2018 src/redisassert.h
-rwxr-xr-x 1 root root 2452432 Aug 6 10:27 src/redis-benchmark
-rw-rw-r-- 1 root root 29605 Mar 27 2018 src/redis-benchmark.c
-rw-r--r-- 1 root root 109104 Aug 6 10:27 src/redis-benchmark.o
-rwxr-xr-x 1 root root 5769584 Aug 6 10:27 src/redis-check-aof
-rw-rw-r-- 1 root root 7143 Mar 27 2018 src/redis-check-aof.c
-rw-r--r-- 1 root root 28656 Aug 6 10:27 src/redis-check-aof.o
-rwxr-xr-x 1 root root 5769584 Aug 6 10:27 src/redis-check-rdb
-rw-rw-r-- 1 root root 13898 Mar 27 2018 src/redis-check-rdb.c
-rw-r--r-- 1 root root 62776 Aug 6 10:27 src/redis-check-rdb.o
-rwxr-xr-x 1 root root 2617512 Aug 6 10:27 src/redis-cli
-rw-rw-r-- 1 root root 99693 Mar 27 2018 src/redis-cli.c
-rw-r--r-- 1 root root 393280 Aug 6 10:27 src/redis-cli.o
-rw-rw-r-- 1 root root 21758 Mar 27 2018 src/redismodule.h
-rwxr-xr-x 1 root root 5769584 Aug 6 10:27 src/redis-sentinel
-rwxr-xr-x 1 root root 5769584 Aug 6 10:27 src/redis-server
-rwxrwxr-x 1 root root 65991 Mar 27 2018 src/redis-trib.rb
4、编译测试
make test
Killing still running Redis server 10560
Killing still running Redis server 10559
Killing still running Redis server 10558
Killing still running Redis server 10565
Killing still running Redis server 10568
Killing still running Redis server 10912
Killing still running Redis server 10993
Killing still running Redis server 11071
Killing still running Redis server 11628
Killing still running Redis server 11629
Killing still running Redis server 11664
Killing still running Redis server 11669
Killing still running Redis server 11701
Killing still running Redis server 11739
Killing still running Redis server 11849
Killing still running Redis server 11906
Killing still running Redis server 11976
Killing still running Redis server 12220
Killing still running Redis server 12231
Killing still running Redis server 12263
Killing still running Redis server 12316
Killing still running Redis server 12322
Killing still running Redis server 12364
Killing still running Redis server 12392
Killing still running Redis server 12407
Killing still running Redis server 12444
Killing still running Redis server 12479
Killing still running Redis server 12525
Killing still running Redis server 12573
Killing still running Redis server 12574
Killing still running Redis server 12630
Killing still running Redis server 12631
make[1]: *** [test] Error 1
make[1]: Leaving directory `/mine/software/redis-4.0.9/redis-4.0.9/src'
make: *** [test] Error 2
5、安装到指定目录
make PREFIX=/mine/serve/redis install
[root@iZm5eizpokikoertia0x31Z redis-4.0.9]# make PREFIX=/mine/serve/redis install
cd src && make install
make[1]: Entering directory `/mine/software/redis-4.0.9/redis-4.0.9/src'
Hint: It's a good idea to run 'make test' ;)
INSTALL install
INSTALL install
INSTALL install
INSTALL install
INSTALL install
make[1]: Leaving directory `/mine/software/redis-4.0.9/redis-4.0.9/src'
6、这时,redis就安装到了/mine/serve/redis,我们还需拷贝一个redis.conf
cp /mine/software/redis-4.0.9/redis-4.0.9/redis.conf /mine/serve/redis/conf/redis.conf
7、启动redis-server服务
sudo /mine/serve/redis/bin/redis-server /mine/serve/redis/conf/redis.conf
查看一下redis-server:ps aux | grep redis-server
[root@iZm5eizpokikoertia0x31Z bin]# ps aux | grep redis-server
root 14288 0.0 0.4 145312 2180 ? Ssl 11:16 0:00 /mine/serve/redis/bin/redis-server 127.0.0.1:7200
root 14322 0.0 0.2 112708 984 pts/0 R+ 11:17 0:00 grep --color=auto redis-server
8、使用redis-cli
[root@iZm5eizpokikoertia0x31Z bin]# redis-cli
Could not connect to Redis at 127.0.0.1:6379: Connection refused
Could not connect to Redis at 127.0.0.1:6379: Connection refused
not connected>
我们发现连不上,原来redis-cli是连接的是本地的6379端口。
查看redis-cli 帮助命令 : redis-cli --help
[root@iZm5eizpokikoertia0x31Z bin]# redis-cli --help
redis-cli 4.0.9
Usage: redis-cli [OPTIONS] [cmd [arg [arg ...]]]
-h <hostname> Server hostname (default: 127.0.0.1).
-p <port> Server port (default: 6379).
-s <socket> Server socket (overrides hostname and port).
-a <password> Password to use when connecting to the server.
-u <uri> Server URI.
-r <repeat> Execute specified command N times.
-i <interval> When -r is used, waits <interval> seconds per command.
It is possible to specify sub-second times like -i 0.1.
-n <db> Database number.
-x Read last argument from STDIN.
-d <delimiter> Multi-bulk delimiter in for raw formatting (default: \n).
-c Enable cluster mode (follow -ASK and -MOVED redirections).
--raw Use raw formatting for replies (default when STDOUT is
not a tty).
--no-raw Force formatted output even when STDOUT is not a tty.
--csv Output in CSV format.
--stat Print rolling stats about server: mem, clients, ...
--latency Enter a special mode continuously sampling latency.
If you use this mode in an interactive session it runs
forever displaying real-time stats. Otherwise if --raw or
--csv is specified, or if you redirect the output to a non
TTY, it samples the latency for 1 second (you can use
-i to change the interval), then produces a single output
and exits.
--latency-history Like --latency but tracking latency changes over time.
Default time interval is 15 sec. Change it using -i.
--latency-dist Shows latency as a spectrum, requires xterm 256 colors.
Default time interval is 1 sec. Change it using -i.
--lru-test <keys> Simulate a cache workload with an 80-20 distribution.
--slave Simulate a slave showing commands received from the master.
--rdb <filename> Transfer an RDB dump from remote server to local file.
--pipe Transfer raw Redis protocol from stdin to server.
--pipe-timeout <n> In --pipe mode, abort with error if after sending all data.
no reply is received within <n> seconds.
Default timeout: 30. Use 0 to wait forever.
--bigkeys Sample Redis keys looking for big keys.
--hotkeys Sample Redis keys looking for hot keys.
only works when maxmemory-policy is *lfu.
--scan List all keys using the SCAN command.
--pattern <pat> Useful with --scan to specify a SCAN pattern.
--intrinsic-latency <sec> Run a test to measure intrinsic system latency.
The test will run for the specified amount of seconds.
--eval <file> Send an EVAL command using the Lua script at <file>.
--ldb Used with --eval enable the Redis Lua debugger.
--ldb-sync-mode Like --ldb but uses the synchronous Lua debugger, in
this mode the server is blocked and script changes are
are not rolled back from the server memory.
--help Output this help and exit.
--version Output version and exit.
Examples:
cat /etc/passwd | redis-cli -x set mypasswd
redis-cli get mypasswd
redis-cli -r 100 lpush mylist x
redis-cli -r 100 -i 1 info | grep used_memory_human:
redis-cli --eval myscript.lua key1 key2 , arg1 arg2 arg3
redis-cli --scan --pattern '*:12345*'
(Note: when using --eval the comma separates KEYS[] from ARGV[] items)
When no command is given, redis-cli starts in interactive mode.
Type "help" in interactive mode for information on available commands
and settings.
指定端口进行连接:redis-cli -h localhost -p 7200
[root@iZm5eizpokikoertia0x31Z bin]# redis-cli -h localhost -p 7200
localhost:7200>
查看redis相关信息
[root@iZm5eizpokikoertia0x31Z bin]# redis-cli -h localhost -p 7200
localhost:7200> info
# Server
redis_version:4.0.9
redis_git_sha1:00000000
redis_git_dirty:0
redis_build_id:de84f73060a79796
redis_mode:standalone
os:Linux 3.10.0-957.21.2.el7.x86_64 x86_64
arch_bits:64
multiplexing_api:epoll
atomicvar_api:atomic-builtin
gcc_version:4.8.5
process_id:14288
run_id:951fd48cf9054e694a4a3bdf340134d2bef3e88b
tcp_port:7200
uptime_in_seconds:674
uptime_in_days:0
hz:10
lru_clock:4780974
executable:/mine/serve/redis/bin/redis-server 【服务】
config_file:/mine/serve/redis/conf/redis.conf 【配置文件】
# Clients
connected_clients:1
client_longest_output_list:0
client_biggest_input_buf:0
blocked_clients:0
# Memory
used_memory:849464
used_memory_human:829.55K
used_memory_rss:2232320
used_memory_rss_human:2.13M
used_memory_peak:849464
used_memory_peak_human:829.55K
used_memory_peak_perc:100.12%
used_memory_overhead:836230
used_memory_startup:786600
used_memory_dataset:13234
used_memory_dataset_perc:21.05%
total_system_memory:502390784
total_system_memory_human:479.12M
used_memory_lua:37888
used_memory_lua_human:37.00K
maxmemory:0
maxmemory_human:0B
maxmemory_policy:noeviction
mem_fragmentation_ratio:2.63
mem_allocator:jemalloc-4.0.3
active_defrag_running:0
lazyfree_pending_objects:0
# Persistence
loading:0
rdb_changes_since_last_save:0
rdb_bgsave_in_progress:0
rdb_last_save_time:1565061388
rdb_last_bgsave_status:ok
rdb_last_bgsave_time_sec:-1
rdb_current_bgsave_time_sec:-1
rdb_last_cow_size:0
aof_enabled:0
aof_rewrite_in_progress:0
aof_rewrite_scheduled:0
aof_last_rewrite_time_sec:-1
aof_current_rewrite_time_sec:-1
aof_last_bgrewrite_status:ok
aof_last_write_status:ok
aof_last_cow_size:0
# Stats
total_connections_received:1
total_commands_processed:1
instantaneous_ops_per_sec:0
total_net_input_bytes:31
total_net_output_bytes:10163
instantaneous_input_kbps:0.00
instantaneous_output_kbps:0.00
rejected_connections:0
sync_full:0
sync_partial_ok:0
sync_partial_err:0
expired_keys:0
expired_stale_perc:0.00
expired_time_cap_reached_count:0
evicted_keys:0
keyspace_hits:0
keyspace_misses:0
pubsub_channels:0
pubsub_patterns:0
latest_fork_usec:0
migrate_cached_sockets:0
slave_expires_tracked_keys:0
active_defrag_hits:0
active_defrag_misses:0
active_defrag_key_hits:0
active_defrag_key_misses:0
# Replication
role:master
connected_slaves:0
master_replid:0937076b303293b6965eeea70c038b717cd7f679
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:0
second_repl_offset:-1
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0
# CPU
used_cpu_sys:0.34
used_cpu_user:0.18
used_cpu_sys_children:0.00
used_cpu_user_children:0.00
# Cluster
cluster_enabled:0
# Keyspace
localhost:7200>
9、至此,redis-server服务已启动,redis-cli也已开始运行!
10、使用PHP运行redis,首先,查看PHP已安装的扩展
php -m
[root@iZm5eizpokikoertia0x31Z bin]# php -m
-bash: php: command not found
解决:
[root@iZm5eizpokikoertia0x31Z bin]# export PATH=$PATH:/mine/serve/php/bin
[root@iZm5eizpokikoertia0x31Z bin]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/mine/serve/mysql/bin:/root/bin:/mine/serve/php/bin
[root@iZm5eizpokikoertia0x31Z serve]# php -m
[PHP Modules]
bcmath
Core
ctype
curl
date
dom
filter
gd
gettext
hash
iconv
json
libxml
mbstring
mysqli
mysqlnd
openssl
pcntl
pcre
PDO
pdo_mysql
pdo_sqlite
Phar
posix
Reflection
session
shmop
SimpleXML
soap
sockets
SPL
sqlite3
standard
sysvsem
tokenizer
xml
xmlreader
xmlrpc
xmlwriter
xsl
zip
zlib
[Zend Modules]
[root@iZm5eizpokikoertia0x31Z serve]#
我们发现没有redis扩展,所以,得安装redis扩展!
11、PHP安装redis扩展,首先得确保是否有phpize和php-config这两个文件
cd /mine/serve/php/bin
ls
[root@iZm5eizpokikoertia0x31Z bin]# ls
pear peardev pecl phar phar.phar php php-cgi php-config phpdbg phpize
有这两个文件!若没有这两个工具,那么我们就需要通过yum命令来进行安装了,命令如下:
yum install php-devel
12、查看这两个工具的目录位置
[root@iZm5eizpokikoertia0x31Z /]# which phpize
/mine/serve/php/bin/phpize
[root@iZm5eizpokikoertia0x31Z /]# which php-config
/mine/serve/php/bin/php-config
13、接下来就是下载php的redis扩展
资源地址:https://github.com/phpredis/phpredis/archive/develop.zip
wget https://github.com/phpredis/phpredis/archive/develop.zip
[root@iZm5eizpokikoertia0x31Z phpredis]# wget https://github.com/phpredis/phpredis/archive/develop.zip
--2019-08-06 11:50:38-- https://github.com/phpredis/phpredis/archive/develop.zip
Resolving github.com (github.com)... 52.74.223.119
Connecting to github.com (github.com)|52.74.223.119|:443... connected.
HTTP request sent, awaiting response... 302 Found
Location: https://codeload.github.com/phpredis/phpredis/zip/develop [following]
--2019-08-06 11:50:38-- https://codeload.github.com/phpredis/phpredis/zip/develop
Resolving codeload.github.com (codeload.github.com)... 13.229.189.0
Connecting to codeload.github.com (codeload.github.com)|13.229.189.0|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: unspecified [application/zip]
Saving to: ‘develop.zip’
[ <=> ] 273,688 267KB/s in 1.0s
2019-08-06 11:50:41 (267 KB/s) - ‘develop.zip’ saved [273688]
[root@iZm5eizpokikoertia0x31Z phpredis]# ls
develop.zip
14、解压资源
unzip develop.zip
[root@iZm5eizpokikoertia0x31Z phpredis]# unzip develop.zip
-bash: unzip: command not found
处理方法: yum install -y unzip zip
Installed:
unzip.x86_64 0:6.0-19.el7
Complete!
再次解压,成功!!
15、生成configure文件
[root@iZm5eizpokikoertia0x31Z phpredis]# cd phpredis-develop/
[root@iZm5eizpokikoertia0x31Z phpredis-develop]# ls
arrays.markdown cluster_library.h config.m4 crc16.h debian.control liblzf mkdeb-apache2.sh php_redis.h redis_array.h redis.c redis_commands.c redis_session.h tests
Changelog.md cluster.markdown config.w32 CREDITS INSTALL.markdown library.c mkdeb.sh README.markdown redis_array_impl.c redis_cluster.c redis_commands.h rpm
cluster_library.c common.h COPYING debian ISSUE_TEMPLATE.md library.h package.xml redis_array.c redis_array_impl.h redis_cluster.h redis_session.c serialize.list
[root@iZm5eizpokikoertia0x31Z phpredis-develop]# phpize
Configuring for:
PHP Api Version: 20170718
Zend Module Api No: 20170718
Zend Extension Api No: 320170718
查看,文件已存在:
[root@iZm5eizpokikoertia0x31Z phpredis-develop]# ls
acinclude.m4 build cluster.markdown config.m4 config.w32 debian ISSUE_TEMPLATE.md ltmain.sh mkdeb.sh README.markdown redis_array_impl.h redis_commands.c rpm
aclocal.m4 Changelog.md common.h config.sub COPYING debian.control liblzf Makefile.global mkinstalldirs redis_array.c redis.c redis_commands.h run-tests.php
arrays.markdown cluster_library.c config.guess configure crc16.h INSTALL.markdown library.c missing package.xml redis_array.h redis_cluster.c redis_session.c serialize.list
autom4te.cache cluster_library.h config.h.in configure.ac CREDITS install-sh library.h mkdeb-apache2.sh php_redis.h redis_array_impl.c redis_cluster.h redis_session.h tests
[root@iZm5eizpokikoertia0x31Z phpredis-develop]#
16、php-redis进行配置、编译、安装
./configure -with-php-config=/mine/serve/php/bin/php-config
creating libtool
appending configuration tag "CXX" to libtool
configure: creating ./config.status
config.status: creating config.h
make
----------------------------------------------------------------------
Libraries have been installed in:
/mine/software/phpredis/phpredis-develop/modules
If you ever happen to want to link against installed libraries
in a given directory, LIBDIR, you must either use libtool, and
specify the full pathname of the library, or use the `-LLIBDIR'
flag during linking and do at least one of the following:
- add LIBDIR to the `LD_LIBRARY_PATH' environment variable
during execution
- add LIBDIR to the `LD_RUN_PATH' environment variable
during linking
- use the `-Wl,--rpath -Wl,LIBDIR' linker flag
- have your system administrator add LIBDIR to `/etc/ld.so.conf'
See any operating system documentation about shared libraries for
more information, such as the ld(1) and ld.so(8) manual pages.
----------------------------------------------------------------------
Build complete.
Don't forget to run 'make test'.
make install
[root@iZm5eizpokikoertia0x31Z phpredis-develop]# make install
Installing shared extensions: /mine/serve/php/lib/php/extensions/no-debug-non-zts-20170718/
17、PHP配置文件,加入你要添加的扩展
查看php.ini在哪里
php --ini
[root@iZm5eizpokikoertia0x31Z phpredis-develop]# php --ini
Configuration File (php.ini) Path: /mine/serve/php/etc
Loaded Configuration File: /mine/serve/php/etc/php.ini
Scan for additional .ini files in: (none)
Additional .ini files parsed: (none)
在文件的末尾加入:extension=redis.so
:wq 保存退出
再次查看:
[root@iZm5eizpokikoertia0x31Z phpredis-develop]# php -m
[PHP Modules]
bcmath
Core
ctype
curl
date
dom
filter
gd
gettext
hash
iconv
json
libxml
mbstring
mysqli
mysqlnd
openssl
pcntl
pcre
PDO
pdo_mysql
pdo_sqlite
Phar
posix
redis
Reflection
session
shmop
SimpleXML
soap
sockets
SPL
sqlite3
standard
sysvsem
tokenizer
xml
xmlreader
xmlrpc
xmlwriter
xsl
zip
zlib
[Zend Modules]
18、至此,我们就已经安装完成phpredis扩展了,可以使用PHP操作redis了。
19、PHP操作redis
文档地址:https://github.com/phpredis/phpredis
phpredis的类的命名空间是根目录
phpredis的命令和参数,基本和redis.so中的实际命令对应
在代码目录,创建目录:/mine/project/mine
vim redis-connect.php
编辑:
<?php
$redis = new \Redis();
$redis->connect('127.0.0.1',7200);
?>
执行以下:
php redis-connect.php
[root@iZm5eizpokikoertia0x31Z mine]# php redis-connect.php
没报错!
20、至此,就告一段落,剩下的,参考文档进行操作,即可!【结束!!!】
----------------------------------------------------------------------------------------------------------------------==