Memcache fought side by side with MySQL

Memcache fought side by side with MySQL

1. Basic
   memcache is a high-performance, distributed memory object caching system, by a huge hash table to maintain unity in memory, it can be used to store Session, it is read from memory when the system needs, thus greatly improving reading take speed. All systems 4.1 after OSSIM, the use of this high-performance Memcached memory object caching system, used for dynamic Web applications, provide caching for PHP, where the main database for each read and write OSSIM cached to reduce the load on MySQL database, configuration file reader is referred to /usr/share/ossim/include/ossim_db.inc,/usr/share/ossim/include/php-ids.ini. In this way it is one of the OSSIM caching scheme. Readers may be wondering, MySQL itself both table_cache, query_cache mechanism, we see in the database with the following command:

mysql> show variables like '%query_cache%';
+------------------------------+----------+
| Variable_name                | Value    |
+------------------------------+----------+
| have_query_cache             | YES      |
| query_cache_limit            | 1048576  |
| query_cache_min_res_unit     | 4096     |
| query_cache_size             | 16777216 |
| query_cache_strip_comments   | OFF      |
| query_cache_type             | ON       |
| query_cache_wlock_invalidate | OFF      |
+------------------------------+----------+
7 rows in set (0.00 sec)


   In the query constantly changing, constantly changing content table scene (OSSIM storage, query is one such state) this kind of a simple caching system can not achieve the purpose of the database effectively alleviate the pressure. This is like provided in Apache mod_disk_cache and mod_mem_cache can not really provide caching for the Web Server, then the Sensor will not contain memcache services, such as Squid caching behind also need tools.
   memcache to reduce the number of cache data read from the database by the object and in memory, so as to provide greater memory OSSIM system, helps memcache role, thereby improving overall performance. Furthermore, in order to observe the visual effect of the cache, you can use phpMemcachedAdmin tool to view, FIG. 1, FIG. 2, FIG. 3 is a monitor performance.

 

wKiom1YF2quRaTcYAAU5W_54kss539.jpgfigure 1

wKioL1YF2xaDun4rAAwpz-Nm5vE232.jpgfigure 2

wKiom1YF24my9RjSAAPjAJaMTYw427.jpgimage 3

2. Configure memcached:
/ etc / default / memcached / * open control * /
/etc/memcached.conf / * memcached configuration parameters * /
OSSIM call memcache, port 11211 configuration file / usr / share / ossim / wwwIncludes / base_db.inc.php
/usr/share/php/adodb/adodb.inc.php
/usr/share/php/adodb/adodb-memcache.lib.inc.php


About memcached.conf important parameters explanation:

Memcached what role critical parameters and these parameters need to be set at startup. 

1) -p Memcached the TCP listening port, the default configuration to 11211; 

2) position of the log file output logfile /var/log/memcached.log

3)-s Memcached监听的UNIX套接字路径; 

4)-l 监听的服务器IP地址,OSSIM Server中为127.0.0.1,代表只能本机访问,也就是只给OSSIM Server 中的Mysql做缓存。

5)-d 为Memcached服务器启动守护进程; OSSIM下启动命令为“/usr/bin/memcached -m 128 -p 11211 -u nobody -l 127.0.0.1”

6)-r 最大core文件大小; 

7)-u 运行Memcached的用户,OSSIM中为nobody; 除了核心和重要进程以外,不易用root用户启动进程,因为不同用户启动进程可以起到隔离作用,

比如:

apache服务采用www-data用户启动,ossec-logcollector进程采用ossec用户启动,mysql进程以mysql用户启动,redis服务以redis用户启动,rabbitmq进程以rabbitmq用户启动。

每个用户分配的权限我们可以在/etc/passwd中查看UID

8)-m 分配给Memcached使用的最大内存数量,单位是MB,OSSIM 中默认为 128,可以修改之512; 

9)-c 最大并发连数,缺省配置为1024; 

10)-v �vv �vvv 设定服务器端打印的消息的详细程度,其中-v仅打印错误和警告信息,-vv在-v的基础上还会打印客户端的命令和相应,-vvv在-vv的基础上还会打印内存状态转换信息; 

11)-n 最小的chunk大小,缺省配置为48个字节; 

12)-t Memcached服务器使用的线程数,缺省配置为4个; 

13)-L 尝试使用大内存页; 

14)-R 每个事件的最大请求数,缺省配置为20个; 


3. 查看memcache状态
下面我们采用telnet命令测试memcache工作状态,首先输入以下命令:
#telnet  127.0.0.1  11211

wKioL1ZswwGQgfqSAAHOGJekWFA486.jpg

看看有什么发现?

Here you can then enter the following command:

stats items

stats detail

version

If you want to quit, ^ C, not, ^ Z is not OK.

Try quit it.


 Note , Ossim Server default telnet no need to install their own, behind the "11211" represents memcache through selected port.
Then, direct input "stats" command can get the current memcache state. As shown in Table 1.

Table 1 memcache status information

wKiom1YF2FCiGudzAAL8IRn25k4674.jpgIf you need to reset the cache to a clean state, enter "flush_all" command.


4. Save Session memcache


    PHP Session gave Memcached Apache server, so no matter which Web server connected to the IP will not be given to the problem, the configuration is simple, just add a statement in the PHP configuration file on it, but the premise needs installed memcache memcache module stored in the session write speed faster than many files, but would be more convenient when the plurality of servers need to share session, these servers are configured to use the same set of memcached server can reduce the extra work the amount. The disadvantage is that session data is stored in memory, the persistence has been lacking, but the session data is not a big problem.


  Note that , in using memcache OSSIM system aims to improve access speed, reducing the pressure MySQL database. By modifying the / etc / default / memcached profile ENABLE_MEMCACHED = yes / no control its switching.





This article comes from " Li Chenguang original technology blog " blog, declined reproduced!

Reproduced in: https: //my.oschina.net/chenguang/blog/613906

Guess you like

Origin blog.csdn.net/weixin_34232617/article/details/92045181