Install Memcached

Thảo luận trong 'Góc Linux' bắt đầu bởi hautp, Thg 12 19, 2012.

  1. hautp

    hautp Well-Known Member

    GIT – RHEL / CentOS 6 Linux Install Memcached High Performance Distributed Memory ObjectCache Server.
    How do I install memcached server under Red Hat Enterprise Linux 6.x server running on IBM hardware to cache query for my php based apps?
    memcached is a high-performance, distributed memory object caching server, generic in nature, but intended for use in speeding up dynamic web applications by alleviating database load. You need to install the following packages:
    • memcached : High Performance, Distributed Memory Object Cache server package.
    • perl-Cache-Memcached : Perl client (library) to work with memcached server.
    • python-memcached : Perl client (library) to work with memcached server.
    • php-pecl-memcache : PHP extension to work with the Memcached server.
    Memcached is used by many high traffic sites including YouTube, Reddit, Facebook, Twitter, and many others.Google App Engine, AppScale and Amazon Web Services also offer a memcached service through an API. Memcached is also supported by popular open source projects and CMSs such as Drupal, Joomla, WordPress, and many more.
    [​IMG]WARNING! These examples only works with RHEL / CentOS Linux version 6.x or above. For RHEL 5.x and older version use memcached source code installation method.
    Task: Install memcached
    Open a terminal or login to remote server using ssh client. Type the following yum command to install the same:
    # yum install memcached.x86_64 php-pecl-memcache.x86_64
    Sample outputs:
    Loaded plugins: product-id, rhnplugin, subscription-manager Updating certificate-based repositories. Setting up Install Process Resolving Dependencies --> Running transaction check ---> Package memcached.x86_64 0:1.4.4-3.el6 will be installed --> Processing Dependency: libevent-1.4.so.2()(64bit) for package: memcached-1.4.4-3.el6.x86_64 ---> Package php-pecl-memcache.x86_64 0:3.0.5-3.el6 will be installed --> Processing Dependency: /usr/bin/pecl for package: php-pecl-memcache-3.0.5-3.el6.x86_64 --> Processing Dependency: /usr/bin/pecl for package: php-pecl-memcache-3.0.5-3.el6.x86_64 --> Running transaction check ---> Package libevent.x86_64 0:1.4.13-1.el6 will be installed ---> Package php-pear.noarch 1:1.9.4-4.el6 will be installed --> Finished Dependency Resolution Dependencies Resolved ================================================================================ Package Arch Version Repository Size ================================================================================ Installing: memcached x86_64 1.4.4-3.el6 rhel-x86_64-server-6 68 k php-pecl-memcache x86_64 3.0.5-3.el6 rhel-x86_64-server-6 60 k Installing for dependencies: libevent x86_64 1.4.13-1.el6 rhel-x86_64-server-6 66 k php-pear noarch 1:1.9.4-4.el6 rhel-x86_64-server-6 393 k Transaction Summary ================================================================================ Install 4 Package(s) Total download size: 587 k Installed size: 0 Is this ok [y/N]: y Downloading Packages: (1/4): libevent-1.4.13-1.el6.x86_64.rpm | 66 kB 00:00 (2/4): memcached-1.4.4-3.el6.x86_64.rpm | 68 kB 00:00 (3/4): php-pear-1.9.4-4.el6.noarch.rpm | 393 kB 00:00 (4/4): php-pecl-memcache-3.0.5-3.el6.x86_64.rpm | 60 kB 00:00 -------------------------------------------------------------------------------- Total 2.4 MB/s | 587 kB 00:00 Running rpm_check_debug Running Transaction Test Transaction Test Succeeded Running Transaction Warning: RPMDB altered outside of yum. Installing : 1:php-pear-1.9.4-4.el6.noarch 1/4 Installing : libevent-1.4.13-1.el6.x86_64 2/4 Installing : memcached-1.4.4-3.el6.x86_64 3/4 Installing : php-pecl-memcache-3.0.5-3.el6.x86_64 4/4 Installed products updated. Installed: memcached.x86_64 0:1.4.4-3.el6 php-pecl-memcache.x86_64 0:3.0.5-3.el6 Dependency Installed: libevent.x86_64 0:1.4.13-1.el6 php-pear.noarch 1:1.9.4-4.el6 Complete!
    How Do I Configure Memcached Server?
    Edit /etc/sysconfig/memcached file, enter:
    # vi /etc/sysconfig/memcached
    Sample outputs:
    PORT="11211" USER="memcached" MAXCONN="1024" CACHESIZE="64" OPTIONS="
    For busy servers you need to increase the values as follows:
    1. MAXCONN: Use 1024 max simultaneous connections; the default is 1024. For busy server increase this number as per requirements.
    2. CACHESIZE: Use 64 MB memory max to use for object storage; the default is 64 megabytes. For busy server you can set it to 512MB or 1GB (1024).
    3. OPTIONS: You can set server IP address (listen on address) here so that apache/php/nginx based serer can connect to the server. By default it is set to INADDR_ANY. This is an important option to consider as there is no other way to secure the installation. Binding to an internal or firewalled network interface is suggested.
    Here is my sample update file:
    PORT="11211" USER="memcached" MAXCONN="4096" CACHESIZE="512" OPTIONS="-l 192.168.1.15"
    How Do I Start / Stop / Restart Memcached Server?
    Type the following commands:
    # /etc/init.d/memcached start
    # /etc/init.d/memcached stop
    # /etc/init.d/memcached restart
    # /etc/init.d/memcached status
    OR run a System V init script using service command
    # /sbin/service memcached start
    # /sbin/service memcached stop
    # /sbin/service memcached restart
    # /sbin/service memcached status
    How Do I Verify That Memcached is Running and Working?
    Type the following command to see if it is running or not:
    # pgrep memcached
    # netstat -tulpn | grep :11211
    Sample outputs:
    tcp 0 0 192.168.1.15:11211 0.0.0.0:* LISTEN 24964/memcached udp 0 0 192.168.1.15:11211 0.0.0.0:* 24964/memcached
    Use the memcached-tool to get general stats:
    # memcached-tool 192.168.1.15:11211 stats
    Sample outputs:
    #192.168.1.15:11211 Field Value accepting_conns 1 auth_cmds 0 auth_errors 0 bytes 5481902 bytes_read 8242409 bytes_written 26023492 cas_badval 0 cas_hits 0 cas_misses 0 cmd_flush 0 cmd_get 9042 cmd_set 4469 conn_yields 0 connection_structures 48 curr_connections 47 curr_items 3197 decr_hits 0 decr_misses 0 delete_hits 6 delete_misses 112 evictions 0 get_hits 7490 get_misses 1552 incr_hits 0 incr_misses 0 limit_maxbytes 536870912 listen_disabled_num 0 pid 24964 pointer_size 64 rusage_system 0.149977 rusage_user 0.043993 threads 4 time 1327395688 total_connections 171 total_items 3226 uptime 198 version 1.4.4
    How Do I Configure Firewall To Protect My Memcached Server?
    Make sure iptables only allows access to your own servers. Edit /etc/sysconfig/iptables and append the following rules or add it as follows to your shell scripts:
    ## add to your shell script ## ## assumption default INPUT policy is set to DROP ## ## only accept connection to tcp/udp port 11211 if ip is between
    ## 192.168.1.10 and 192.168.1.15 ##
    iptables -A INPUT -p tcp --destination-port 11211 -m state --state NEW
    -m iprange --src-range 192.168.1.10-192.168.1.15 -j ACCEPT
    iptables -A INPUT -p udp --destination-port 11211 -m state --state NEW
    -m iprange --src-range 192.168.1.10-192.168.1.15 -j ACCEPT
    How Do I Cache MySQL Queries Using Memcached?
    You need to modify your php code using APIs. You can find examples and code samples here. Alternately, you can install memcached extensions such as WordPress extension/plugins if you are using worpdress as CMS.
    References:


    memcached: Failed To Set rlimit For Open Files Error and Solution
    I’m using RHEL 6 / CentOS 6.x and install the memcached server. However, whey I try to start the server using service memcached start command, I get the following error:
    Starting memcached: failed to set rlimit for open files. Try running as root or requesting smaller maxconns value. [FAILED]
    How do I fix this problem?
    Linux comes with per-process file and system-wide file system descriptor limits. Each user has per-process file descriptor limits. The default is set to 1024 including the hard limit, which is also set to 1024. Only root user can increase the hard limit. In my experience you need to to increase this when starting the memcached server.
    The memcached server is run as memcached user. You can verify this by visiting the /etc/passwd file, enter:
    $ less /etc/passwd
    $ grep -i memcached /etc/passwd
    The default config file is located at /etc/sysconfig/memcached:
    $ cat /etc/sysconfig/memcached
    Sample outputs:
    PORT="11211" USER="memcached" MAXCONN="4096" CACHESIZE="256" OPTIONS="-l 192.168.1.100"
    Set Per-process File Descriptor Limits For Memcached
    Edit /etc/security/limits.conf file, enter:
    # vi /etc/security/limits.conf
    Set max number of open files for memcached user as follows:
    #"soft" for enforcing the soft limits #"hard" for enforcing hard limits # "nofile" max open file # ********************************************************************* # * Note soft limit must be >= MAXCONN value (defined in /etc/sysconfig/memcached * # ********************************************************************* # Username type item value memcached soft nofile 5000 memcached hard nofile 6144
    Save and close the file. You need to logout and login again. Now type the following command to start the memcached server:
    # /sbin/service memcached start
    Please note that you can set global limit for all process by replacing the memcached with * as follows:
    # Username type item value * soft nofile 5000 * hard nofile 6144
    Nguồn : cyberciti
     

Chia sẻ trang này