记录php5.4与php-redis冲突的解决
最近由于update了ubuntu的源,导致原来新源与本地系统库一些lib冲突,导致系统桌面显示异常,后来只有重装,新安装了php5.4.9,还安装了redis,php-redis扩展,php-redis扩展2,10。但把原来的项目一放上去,就报nginx 502错误(nginx 502错误真是个很头疼的问题)。打开nginx日志查看
[error] 29229#0: *1403 recv() failed (104: Connection reset by peer) while reading response header from upstream, client: 127.0.0.1, server:
不知道所云。后来打开php-fpm错误日志,得到的log日志
[08-Oct-2013 21:22:23] WARNING: [pool www] child 12026 exited on signal 11 (SIGSEGV - core dumped) after 5.997917 seconds from start
[08-Oct-2013 21:22:23] NOTICE: [pool www] child 12034 started
可以确定的一点,是php-fpm在一个时间点后重启了一个进程。还是毫无头绪。在网上搜了一大遍,有个有用的跟踪方法。
1、设置php-fpm,只起一个work进程
2、重启php-fpm, 执行ps aux grep php-fpm 得到work进程号pid
3、strace -p pid
下面是我得到的信息
getcwd("/home/www/wms2.xxxx.com", 4096) = 26
lstat("/home/www/wms2.xiaomi.com/./RWebUser.php", 0x7ffff4e30a10) = -1 ENOENT (No such file or directory)
lstat("/home/www/wms2.xiaomi.com/protected/vendor/wms/api/RWebUser.php", 0x7ffff4e30a10) = -1 ENOENT (No such file or directory)
lstat("/home/www/wms2.xiaomi.com/protected/vendor/wms/RWebUser.php", 0x7ffff4e30a10) = -1 ENOENT (No such file or directory)
lstat("/home/www/wms2.xiaomi.com/protected/extensions/log/RWebUser.php", 0x7ffff4e30a10) = -1 ENOENT (No such file or directory)
lstat("/home/www/wms2.xiaomi.com/protected/modules/rights/components/RWebUser.php", {st_mode=S_IFREG0777, st_size=2945, ...}) = 0
lstat("/home/www/wms2.xiaomi.com/protected/modules/rights/components", {st_mode=S_IFDIR0777, st_size=4096, ...}) = 0
lstat("/home/www/wms2.xiaomi.com/protected/modules/rights", {st_mode=S_IFDIR0777, st_size=4096, ...}) = 0
lstat("/home/www/wms2.xiaomi.com/protected/modules", {st_mode=S_IFDIR0777, st_size=4096, ...}) = 0
open("/home/www/wms2.xiaomi.com/protected/modules/rights/components/RWebUser.php", O_RDONLY) = 5
fstat(5, {st_mode=S_IFREG0777, st_size=2945, ...}) = 0
fstat(5, {st_mode=S_IFREG0777, st_size=2945, ...}) = 0
fstat(5, {st_mode=S_IFREG0777, st_size=2945, ...}) = 0
fstat(5, {st_mode=S_IFREG0777, st_size=2945, ...}) = 0
mmap(NULL, 2945, PROT_READ, MAP_SHARED, 5, 0) = 0x7fd9b4804000
mmap(NULL, 266240, PROT_READPROT_WRITE, MAP_PRIVATEMAP_ANONYMOUS, -1, 0) = 0x7fd9b442f000
munmap(0x7fd9b4804000, 2945) = 0
close(5) = 0
getcwd("/home/www/wms2.xxx.com", 4096) = 26
lstat("/home/www/wms2.xxx.com/./XMCCacheHttpSession.php", 0x7ffff4e31060) = -1 ENOENT (No such file or directory)
lstat("/home/www/wms2.xxx.com/protected/vendor/wms/api/XMCCacheHttpSession.php", 0x7ffff4e31060) = -1 ENOENT (No such file or directory)
lstat("/home/www/wms2.xxx.com/protected/vendor/wms/XMCCacheHttpSession.php", 0x7ffff4e31060) = -1 ENOENT (No such file or directory)
lstat("/home/www/wms2.xxx.com/protected/extensions/log/XMCCacheHttpSession.php", 0x7ffff4e31060) = -1 ENOENT (No such file or directory)
lstat("/home/www/wms2.xxx.com/protected/modules/rights/components/XMCCacheHttpSession.php", 0x7ffff4e31060) = -1 ENOENT (No such file or directory)
lstat("/home/www/wms2.xxx.com/protected/modules/rights/XMCCacheHttpSession.php", 0x7ffff4e31060) = -1 ENOENT (No such file or directory)
lstat("/home/www/wms2.xxx.com/protected/components/XMCCacheHttpSession.php", {st_mode=S_IFREG0777, st_size=2612, ...}) = 0
open("/home/www/wms2.xxx.com/protected/components/XMCCacheHttpSession.php", O_RDONLY) = 5
fstat(5, {st_mode=S_IFREG0777, st_size=2612, ...}) = 0
fstat(5, {st_mode=S_IFREG0777, st_size=2612, ...}) = 0
fstat(5, {st_mode=S_IFREG0777, st_size=2612, ...}) = 0
fstat(5, {st_mode=S_IFREG0777, st_size=2612, ...}) = 0
mmap(NULL, 2612, PROT_READ, MAP_SHARED, 5, 0) = 0x7fd9b4804000
munmap(0x7fd9b4804000, 2612) = 0
close(5) = 0
sendto(4, "get 6cacd9674ad21aa083b5032b1a29"..., 39, MSG_DONTWAITMSG_NOSIGNAL, NULL, 0) = 39
recvfrom(4, 0x2806d88, 8196, 64, 0, 0) = -1 EAGAIN (Resource temporarily unavailable)
poll([{fd=4, events=POLLIN}], 1, 5000) = 1 ([{fd=4, revents=POLLIN}])
recvfrom(4, "VALUE 6cacd9674ad21aa083b5032b1a"..., 8196, MSG_DONTWAIT, NULL, NULL) = 128
lstat("/home/www/wms2.xxx.com/yii/web/CHttpCookie.php", {st_mode=S_IFREG0777, st_size=2772, ...}) = 0
lstat("/home/www/wms2.xxx.com/yii/web", {st_mode=S_IFDIR0777, st_size=4096, ...}) = 0
open("/home/www/wms2.xxx.com/yii/web/CHttpCookie.php", O_RDONLY) = 5
fstat(5, {st_mode=S_IFREG0777, st_size=2772, ...}) = 0
fstat(5, {st_mode=S_IFREG0777, st_size=2772, ...}) = 0
fstat(5, {st_mode=S_IFREG0777, st_size=2772, ...}) = 0
fstat(5, {st_mode=S_IFREG0777, st_size=2772, ...}) = 0
mmap(NULL, 2772, PROT_READ, MAP_SHARED, 5, 0) = 0x7fd9b4804000
munmap(0x7fd9b4804000, 2772) = 0
close(5) = 0
stat("/home/www/wms2.xxx.com/protected/extensions/redis/XMRedis.php", {st_mode=S_IFREG0777, st_size=2672, ...}) = 0
lstat("/home/www/wms2.xxx.com/protected/extensions/redis/XMRedis.php", {st_mode=S_IFREG0777, st_size=2672, ...}) = 0
lstat("/home/www/wms2.xxx.com/protected/extensions/redis", {st_mode=S_IFDIR0777, st_size=4096, ...}) = 0
lstat("/home/www/wms2.xxx.com/protected/extensions", {st_mode=S_IFDIR0777, st_size=4096, ...}) = 0
open("/home/www/wms2.xxx.com/protected/extensions/redis/XMRedis.php", O_RDONLY) = 5
fstat(5, {st_mode=S_IFREG0777, st_size=2672, ...}) = 0
fstat(5, {st_mode=S_IFREG0777, st_size=2672, ...}) = 0
fstat(5, {st_mode=S_IFREG0777, st_size=2672, ...}) = 0
fstat(5, {st_mode=S_IFREG0777, st_size=2672, ...}) = 0
mmap(NULL, 2672, PROT_READ, MAP_SHARED, 5, 0) = 0x7fd9b4804000
munmap(0x7fd9b4804000, 2672) = 0
close(5) = 0
socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP) = 5
close(5) = 0
socket(PF_INET, SOCK_STREAM, IPPROTO_IP) = 5
fcntl(5, F_GETFL) = 0x2 (flags O_RDWR)
fcntl(5, F_SETFL, O_RDWRO_NONBLOCK) = 0
connect(5, {sa_family=AF_INET, sin_port=htons(22122), sin_addr=inet_addr("10.237.36.231")}, 16) = -1 EINPROGRESS (Operation now in progress)
poll([{fd=5, events=POLLINPOLLOUTPOLLERRPOLLHUP}], 1, 3000) = 1 ([{fd=5, revents=POLLOUT}])
getsockopt(5, SOL_SOCKET, SO_ERROR, [0], [4]) = 0
fcntl(5, F_SETFL, O_RDWR) = 0
setsockopt(5, SOL_TCP, TCP_NODELAY, [1], 4) = 0
poll([{fd=5, events=POLLINPOLLPRIPOLLERRPOLLHUP}], 1, 0) = 0 (Timeout)
sendto(5, "*2\r\n$4\r\nAUTH\r\n$9\r\nxm_wms_rs\r\n", 29, MSG_DONTWAIT, NULL, 0) = 29
poll([{fd=5, events=POLLINPOLLPRIPOLLERRPOLLHUP}], 1, 0) = 0 (Timeout)
poll([{fd=5, events=POLLINPOLLERRPOLLHUP}], 1, 3000) = 1 ([{fd=5, revents=POLLIN}])
recvfrom(5, "+OK\r\n", 8192, MSG_DONTWAIT, NULL, NULL) = 5
--- SIGSEGV (Segmentation fault) @ 0 (0) ---
Process 6011 detached
可以看得出,在执行XMRedis后进程6011 就死掉了。基本可以定位是php-redis出了问题。
继续追查最终发现了有段代码问题
public function conn() {
try {
$this->_redis->pconnect($this->host, $this->port, $this->timeout);
// Set client option. must AFTER connected
//var_dump(Redis::OPT_SERIALIZER,Redis::SERIALIZER_PHP);exit();
$this->_redis->setOption(Redis::OPT_SERIALIZER,Redis::SERIALIZER_PHP);
//$this->_redis->setOption(0, 0);
$this->_redis->auth("xm_wms_rs");
$this->connected = true;
} catch (RedisException $e) {
throw new CHttpException(500, "Redis occurs an error:" . $e->getMessage());
}
}
用红色的代码那段,就会报502错误,而用绿色代码这段就没问题。
setOption 这个是个神马东西呢?查了下php-redis api
$redis->setOption(Redis::OPT_SERIALIZER, Redis::SERIALIZER_NONE); // don't serialize data 不序列化数据
$redis->setOption(Redis::OPT_SERIALIZER, Redis::SERIALIZER_PHP); // use built-in serialize/unserialize 用php内置的序列化
$redis->setOption(Redis::OPT_SERIALIZER, Redis::SERIALIZER_IGBINARY); // use igBinary serialize/unserialize 用扩展IGBINARY序列化
$redis->setOption(Redis::OPT_PREFIX, 'myAppName:'); // use custom prefix on all keys redis key前缀
$redis->setOption(Redis::OPT_SERIALIZER, Redis::SERIALIZER_PHP); 用此就报502
为什么用php内置的序列化就会出错呢?到底发生了什么?
后来又安装了IGBINARY 扩展,还是不行。最后把php-redis安装了最新的版本2.24,再运行,就完全没问题了。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



The future of PHP will be achieved by adapting to new technology trends and introducing innovative features: 1) Adapting to cloud computing, containerization and microservice architectures, supporting Docker and Kubernetes; 2) introducing JIT compilers and enumeration types to improve performance and data processing efficiency; 3) Continuously optimize performance and promote best practices.

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

Redis cluster mode deploys Redis instances to multiple servers through sharding, improving scalability and availability. The construction steps are as follows: Create odd Redis instances with different ports; Create 3 sentinel instances, monitor Redis instances and failover; configure sentinel configuration files, add monitoring Redis instance information and failover settings; configure Redis instance configuration files, enable cluster mode and specify the cluster information file path; create nodes.conf file, containing information of each Redis instance; start the cluster, execute the create command to create a cluster and specify the number of replicas; log in to the cluster to execute the CLUSTER INFO command to verify the cluster status; make

PHP is not dying, but constantly adapting and evolving. 1) PHP has undergone multiple version iterations since 1994 to adapt to new technology trends. 2) It is currently widely used in e-commerce, content management systems and other fields. 3) PHP8 introduces JIT compiler and other functions to improve performance and modernization. 4) Use OPcache and follow PSR-12 standards to optimize performance and code quality.

Using the Redis directive requires the following steps: Open the Redis client. Enter the command (verb key value). Provides the required parameters (varies from instruction to instruction). Press Enter to execute the command. Redis returns a response indicating the result of the operation (usually OK or -ERR).

The steps to start a Redis server include: Install Redis according to the operating system. Start the Redis service via redis-server (Linux/macOS) or redis-server.exe (Windows). Use the redis-cli ping (Linux/macOS) or redis-cli.exe ping (Windows) command to check the service status. Use a Redis client, such as redis-cli, Python, or Node.js, to access the server.

How to clear Redis data: Use the FLUSHALL command to clear all key values. Use the FLUSHDB command to clear the key value of the currently selected database. Use SELECT to switch databases, and then use FLUSHDB to clear multiple databases. Use the DEL command to delete a specific key. Use the redis-cli tool to clear the data.

The best way to understand Redis source code is to go step by step: get familiar with the basics of Redis. Select a specific module or function as the starting point. Start with the entry point of the module or function and view the code line by line. View the code through the function call chain. Be familiar with the underlying data structures used by Redis. Identify the algorithm used by Redis.
