This article mainly and everyone introduced the PHP use Redis storage session of a warning location, with a certain reference value, interested in small partners can refer to, hope to help everyone.
1. Problem phenomena
When the system page refreshes, occasionally it will error the following warnning, but not often appear:
Warning:Unknown:Failed to write session data (Redis). Please verify this current setting of Session.save_path is correct (tcp://x.x.x.x:6379?auth=yyy) in Unknown on line 0
Look at the network is said to be a redis version of the problem, but no specific conclusions, then the spirit of learning attitude, try to see if you can catch this bug.
Positioning issues:
See if the PHP file has a place to set the session and find no
Continue checking the PHP configuration file and find the session stored in Redis
[Session]session.save_handler = Redissession.save_path = "tcp://x.x.x.x:6379?auth=yyyy" session.use_cookies = 1
1, continue to view the PHP session extension source code, positioning the error prompt in the function php_session_save_current_state
Check the session extension file for an error prompt:
static void Php_session_save_current_state (Tsrmls_d)/* {{{*/{int ret = FAILURE; Whether the SESSION array if_session_vars () {if (PS (mod_data) | | | PS (mod_user_implemented)) {char *val; int Vallen; Variable Code val = Php_session_encode (&vallen tsrmls_cc); if (val) {//save session data RET = PS (mod)->s_write (&ps (Mod_data), PS (ID), Val, Vallen tsrmls_cc); Efree (Val); } else {//empty session RET = PS (mod)->s_write (&ps (Mod_data), PS (ID), "", 0 tsrmls_cc); }}//See the error prompt here if (ret = = FAILURE) {php_error_docref (NULL tsrmls_cc, e_warning, "Failed to write session D ATA (%s). "" "Verify that the current setting of Session.save_path" is correct (%s) ", PS (MoD)-> ; S_name, PS (Save_path)); }} if (PS (mod_data) | | | PS (mod_user_implemented)) {PS (mod)->s_close (&ps (mod_data) tsrmls_cc); }}static void Php_session_flush (tsrmls_d) {if (PS (session_status) = = Php_session_active) Session_status = None call here ... php_session_save_current_state}
The following two function calls: Php_session_flush ()
Static Php_function (session_write_close) { php_session_flush (tsrmls_c);} Static Php_function (Session_register_shutdown) {}
As can be seen above, Php_session_save_current_state is called in the Php_session_flush, that is, when the session is saved, emptied, and so on when the session is refreshed.
by PHP seesion Source did not find any problem, suddenly think of whether it is Redis itself has no problem, resulting in write error. So continue to look for questions from Redis:
Redis Problem Locator
1. First look at the Redis log file:
See below every 5 minutes the following log is refreshed:
[16723] 15:15:01.987 # Server started, Redis version 2.8.9[16723] Jul 15:15:01.987 # WARNING Overcommit_memory i s set to 0! Background save may fail under low memory condition. To fix this issue add ' vm.overcommit_memory = 1 ' to/etc/sysctl.conf and then reboot or run the command ' Sysctl vm.overcom Mit_memory=1 ' for the take effect. [16723] 15:15:01.996 * DB loaded from disk:0.008 seconds[16723] in Jul 15:15:01.996 * The server is now ready to a ccept Connections on Port 6379
Suspect that this is the cause of the problem of PHP write log failure. So why is Redis restarting in 5 minutes? Keep Tracking!
2. Is the system memory not enough, Redis core?
To view system and Redis memory usage status:
System memory Status:
$ free-m Total used free shared buffers cachedmem: 3516 3171 345 0 684 1680-/+ buffers/cache: 806 2709Swap: 2055 724 1330
Memory state occupied by Redis server:
Used_memory:2841648used_memory_human:2.71mused_memory_rss:3710976used_memory_peak:2877576used_memory_peak_ human:2.74mused_memory_lua:33792mem_fragmentation_ratio:1.31mem_allocator:jemalloc-3.2.0
You can see that Redis actually consumes very little memory. The reason for the Redis restart is unclear.
3. Timed Scripts
Because restarting the log is a 5-minute, very regular occurrence, it is doubtful that a redis restart, such as a timed script, is caused by another cause.
Because I do not have root corntab authority, find the boss to mention power, see impressively there is a crontab,5 minute monitoring redis.
As follows:
/5 * * */data/scripts/check_redis.sh >/dev/null 2>&1 # #check_redis. tag.1
Viewing the contents of a script
port= ' 6379 ' eth1_addr= '/sbin/ifconfig eth1 | Awk-f ': '/inet addr/{print ' | Sed ' s/[a-za-z]//g ' retval= ' Nmap--system-dns-st-p ${port} ${eth1_addr} | grep open ' If ["${retval}x" = "X"]; Then /sbin/service redis Restart >/dev/null 2>&1fi
This is a script that checks if the Redis port is serving, and if the check is unsuccessful, pull up redis.
You can see that this script does not have any problems.
However: by executing this command manually, you find:
This machine does not have the command to install Nmap,
So this script always fails at the end of the execution! Then Redis restarts.
Because the root cause of the problem found, find operations and maintenance related commands, problem solving.