Mysql高性能之Memcached(1)

本文将介绍Memcached的安装与使用

What is Memcached?

Free & open source, high-performance, distributed memory object caching system, generic in nature, but intended for use in speeding up dynamic web applications by alleviating database load.

Memcached is an in-memory key-value store for small chunks of arbitrary data (strings, objects) from results of database calls, API calls, or page rendering.

Memcached is simple yet powerful. Its simple design promotes quick deployment, ease of development, and solves many problems facing large data caches. Its API is available for most popular languages.

Why Use Memcached?

Benefits of using memcached include:

? Because all information is stored in RAM, the access speed is faster than loading the information

each time from disk.

? Because the “value” portion of the key-value pair does not have any data type restrictions, you can

cache data such as complex structures, documents, images, or a mixture of such things.

? If you use the in-memory cache to hold transient information, or as a read-only cache for information

also stored in a database, the failure of any memcached server is not critical. For persistent data, you

can fall back to an alternative lookup method using database queries, and reload the data into RAM

on a different server.

The typical usage environment is to modify your application so that information is read from the cache

provided by memcached. If the information is not in memcached, then the data is loaded from the

MySQL database and written into the cache so that future requests for the same object benefit from the

cached data.

Case:

Fotolog, as they themselves point out, is probably the largest site nobody has ever heard of, pulling in more page views than even Flickr. Fotolog has 51 instances of memcached on 21 servers with 175G in use and 254G available. As a large successful photo-blogging
site they have very demanding performance and scaling requirements. To meet those requirements they‘ve developed a sophisticated approach to using memcached that others can learn from and emulate.

Memcached的下载:

http://www.memcached.org/files/memcached-1.4.21.tar.gz

在RedHat中,系统自带Memcached,可以使用yum进行安装:

yum install memcached

也可以下载包进行安装,这里就详细说明了。

基本使用:

[[email protected] bin]# memcached -h
memcached 1.4.4
-p <num>      TCP port number to listen on (default: 11211)
-U <num>      UDP port number to listen on (default: 11211, 0 is off)
-s <file>     UNIX socket path to listen on (disables network support)
-a <mask>     access mask for UNIX socket, in octal (default: 0700)
-l <ip_addr>  interface to listen on (default: INADDR_ANY, all addresses)
-d            run as a daemon
-r            maximize core file limit
-u <username> assume identity of <username> (only when run as root)
-m <num>      max memory to use for items in megabytes (default: 64 MB)
-M            return error on memory exhausted (rather than removing items)
-c <num>      max simultaneous connections (default: 1024)
-k            lock down all paged memory.  Note that there is a
              limit on how much memory you may lock.  Trying to
              allocate more than that would fail, so be sure you
              set the limit correctly for the user you started
              the daemon with (not for -u <username> user;
              under sh this is done with 'ulimit -S -l NUM_KB').
-v            verbose (print errors/warnings while in event loop)
-vv           very verbose (also print client commands/reponses)
-vvv          extremely verbose (also print internal state transitions)
-h            print this help and exit
-i            print memcached and libevent license
-P <file>     save PID in <file>, only used with -d option
-f <factor>   chunk size growth factor (default: 1.25)
-n <bytes>    minimum space allocated for key+value+flags (default: 48)
-L            Try to use large memory pages (if available). Increasing
              the memory page size could reduce the number of TLB misses
              and improve the performance. In order to get large pages
              from the OS, memcached will allocate the total item-cache
              in one large chunk.
-D <char>     Use <char> as the delimiter between key prefixes and IDs.
              This is used for per-prefix stats reporting. The default is
              ":" (colon). If this option is specified, stats collection
              is turned on automatically; if not, then it may be turned on
              by sending the "stats detail on" command to the server.
-t <num>      number of threads to use (default: 4)
-R            Maximum number of requests per event, limits the number of
              requests process for a given connection to prevent
              starvation (default: 20)
-C            Disable use of CAS
-b            Set the backlog queue limit (default: 1024)
-B            Binding protocol - one of ascii, binary, or auto (default)
-I            Override the size of each slab page. Adjusts max item size
              (default: 1mb, min: 1k, max: 128m)

初始化Memcached:

memcached -u root -d -m 512 -p 11211 -l 192.168.56.12

[[email protected] bin]# ps -ef |grep mem
root      4382     1  0 02:01 ?        00:00:00 memcached -u root -d -m 512 -p 11211 -l 192.168.56.12

查看当前Memcached的状态:

[[email protected] bin]# telnet 192.168.56.12 11211
Trying 192.168.56.12...
Connected to 192.168.56.12.
Escape character is '^]'.
stats
STAT pid 4382
STAT uptime 7288
STAT time 1418893354
STAT version 1.4.4
STAT pointer_size 64
STAT rusage_user 0.353946
STAT rusage_system 0.379942
STAT curr_connections 5
STAT total_connections 8
STAT connection_structures 6
STAT cmd_get 0
STAT cmd_set 0
STAT cmd_flush 0
STAT get_hits 0
STAT get_misses 0
STAT delete_misses 0
STAT delete_hits 0
STAT incr_misses 0
STAT incr_hits 0
STAT decr_misses 0
STAT decr_hits 0
STAT cas_misses 0
STAT cas_hits 0
STAT cas_badval 0
STAT auth_cmds 0
STAT auth_errors 0
STAT bytes_read 144
STAT bytes_written 1732
STAT limit_maxbytes 536870912
STAT accepting_conns 1
STAT listen_disabled_num 0
STAT threads 4
STAT conn_yields 0
STAT bytes 0
STAT curr_items 0
STAT total_items 0
STAT evictions 0
END

对应参数解释:
pid  memcache服务器的进程ID
uptime  服务器已经运行的秒数
time  服务器当前的unix时间戳
version  memcache版本
pointer_size  当前操作系统的指针大小(32位系统一般是32bit)
rusage_user  进程的累计用户时间
rusage_system  进程的累计系统时间
curr_items  服务器当前存储的items数量
total_items  从服务器启动以后存储的items总数量
bytes  当前服务器存储items占用的字节数
curr_connections  当前打开着的连接数
total_connections  从服务器启动以后曾经打开过的连接数
connection_structures  服务器分配的连接构造数
cmd_get  get命令(获取)总请求次数
cmd_set  set命令(保存)总请求次数
get_hits  总命中次数
get_misses  总未命中次数
evictions  为获取空闲内存而删除的items数(分配给memcache的空间用满后需要删除旧的items来得到空间分配给新的items)
bytes_read  总读取字节数(请求字节数)
bytes_written  总发送字节数(结果字节数)
limit_maxbytes  分配给memcache的内存大小(字节)
threads  当前线程数
时间: 2024-08-13 15:26:29

Mysql高性能之Memcached(1)的相关文章

Mysql高性能之Memcached(2)

本文将介绍在部署Memcached中需要注意的问题以及Memcached的分布式算法 无论你是新上线的系统还是已经上线很久的系统.我们都可以很简单的对Memcached进行配置,但是配置之前我们需要注意如下问题: 1.memcached is only a caching mechanism. It shouldn't be used to store information that you cannot otherwise afford to lose and then load from

基于CentOS 5.4搭建nginx+php+spawn-fcgi+mysql高性能php平台

一.安装准备 1.1平台环境: CentOS 5.4 x86_64 GNU/Linux nginx-0.8.21 php-5.2.9 spawn-fcgi-1.6.3 mysql-5.1.34 .2系统安装及分区:1.2.1操作系统安装:         安装过程中选择最少的包,采用文本模式安装,不安装图形.1.2.3系统分区:         /boot  100M    (大约100左右)          SWAP  4G      物理内存的2倍(如果你的物理内存大于4G,分配4G即可)

nginx+PHP+memcached+MySQL+ip-hash做memcached集群

1.nginx与memcached整合 #安装memcached支持的事务库libevent wget https://github.com/libevent/libevent/releases/download/release-2.0.22-stable/libevent-2.0.22-stable.tar.gz tar zxf libevent-2.0.22-stable.tar.gz  cd libevent-2.0.22-stable ./configure --prefix=/usr/

Mysql高性能优化规范建议

原文:Mysql高性能优化规范建议 数据库命令规范 所有数据库对象名称必须使用小写字母并用下划线分割 所有数据库对象名称禁止使用mysql保留关键字(如果表名中包含关键字查询时,需要将其用单引号括起来) 数据库对象的命名要能做到见名识意,并且最后不要超过32个字符 临时库表必须以tmp_为前缀并以日期为后缀,备份表必须以bak_为前缀并以日期(时间戳)为后缀 所有存储相同数据的列名和列类型必须一致(一般作为关联列,如果查询时关联列类型不一致会自动进行数据类型隐式转换,会造成列上的索引失效,导致查

Nginx + MySQL + PHP + Xcache + Memcached

传统上基于进程或线程模型架构的web服务通过每进程或每线程处理并发连接请求,这势必会在网络和I/O操作时产生阻塞,其另一个必然结果则是对内存或CPU的利用率低下.生成一个新的进程/线程需要事先备好其运行时环境,这包括为其分配堆内存和栈内存,以及为其创建新的执行上下文等.这些操作都需要占用CPU,而且过多的进程/线程还会带来线程抖动或频繁的上下文切换,系统性能也会由此进一步下降. 在设计的最初阶段,nginx的主要着眼点就是其高性能以及对物理计算资源的高密度利用,因此其采用了不同的架构模型.受启发

MySQL 高性能表设计规范

良好的逻辑设计和物理设计是高性能的基石, 应该根据系统将要执行的查询语句来设计schema, 这往往需要权衡各种因素. 一.选择优化的数据类型 MySQL支持的数据类型非常多, 选择正确的数据类型对于获得高性能至关重要. 更小的通常更好 更小的数据类型通常更快, 因为它们占用更少的磁盘. 内存和CPU缓存, 并且处理时需要的CPU周期也更少. 简单就好 简单数据类型的操作通常需要更少的CPU周期. 例如, 整型比字符操作代价更低, 因为字符集和校对规则(排序规则 )使字符比较比整型比较更复杂.

MySQL高性能以及高安全测试

1.  参数描述 sync_binlog Command-Line Format --sync-binlog=# Option-File Format sync_binlog System Variable Name sync_binlog Variable Scope Global Dynamic Variable Yes Permitted Values Platform Bit Size 32 Type numeric Default 0 Range 0 .. 4294967295 Per

mysql高性能索引(Ⅰ)

在开发中,我们知道大多数应用的瓶颈在于sql语句的执行时耗,在这里并不讨论sql语句的安全,仅仅讨论高性能sql语句,而与高性能sql语句紧密相连的就是传说中的----索引. 索引--一种工作在存储引擎端的用于快速找到记录的一种数据结构. mysql使用索引的方式是:先找到索引的值,再根据索引的值找到数据行. 索引之B-Tree索引 B-Tree索引通常意味着所有的值都是按顺序存储的,每个叶子节点到根的距离相同.图示: B-Tree索引能够快速的访问数据,因为存储引擎不需要进行全表扫描来获得数据

史上最全的MySQL高性能优化实战总结!

1.1 前言 MySQL对于很多Linux从业者而言,是一个非常棘手的问题,多数情况都是因为对数据库出现问题的情况和处理思路不清晰.在进行MySQL的优化之前必须要了解的就是MySQL的查询过程,很多的查询优化工作实际上就是遵循一些原则让MySQL的优化器能够按照预想的合理方式运行而已. 今天给大家体验MySQL的优化实战,助你高薪之路顺畅.图 - MySQL查询过程 1.2 优化的哲学 优化有风险,涉足需谨慎 1.2.1 优化可能带来的问题 1.2.2 优化的需求1.2.3 优化由谁参与 在进