Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Memcached main memory database-deployment and operation

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/03 Report--

Overview

  Memcached is a high-performance distributed in-memory object caching system for dynamic Web applications to reduce database load. It reduces the number of times to read the database by caching data and objects in memory, thus improving the speed of dynamic, database-driven websites. Memcached is based on a hashmap that stores key / value pairs. Its daemon (daemon) is written in C, but the client can write in any language and communicate with the daemon through the memcached protocol.

Features 1, simple protocol; 2, event handling based on libevent; 3, built-in memory storage mode; 4. Distributed memcached that does not communicate with each other. Storage mode

To improve performance, the data saved in memcached is stored in memcached's built-in memory storage space. Because the data exists only in memory, restarting memcached and restarting the operating system will cause all data to disappear. In addition, when the content capacity reaches the specified value, the unused cache is automatically deleted based on the LRU (Least Recently Used) algorithm. Memcached itself is a server designed for caching, so it doesn't think too much about the persistence of the data.

working principle

  Memcached deals with each (key,value) pair (hereinafter referred to as kv pair), and key is converted into hash-key through a hash algorithm, making it easy to find, compare, and hash as much as possible. At the same time, memcached uses a secondary hash, which is maintained by a large hash table.

  Memcached consists of two core components: server (server) and client (client). In a memcached query, client first determines the server location of the kv pair by calculating the hash value of the kv. When the server is determined, the client sends a query request to the corresponding server to find the exact data. Because there is no interaction and multicast protocol, the impact of memcached interaction on the network is minimized.

Lab environment memcached server 192.168.13.128 (memcached-1.5.6.tar.gz, libevent-2.1.8-stable.tar.gz) memcache client 192.168.13.129 (memcache-2.2.7.tgz, LAMP) 1 Deploy memcached server [root@server ~] # yum install-y gcc gcc-c++ make # # installation environment components [root@server ~] # mount.cifs / / 192.168.100.3/LNMP-C7 / mnt/Password for root@//192.168.100.3/LNMP-C7: [root@server ~] # cd / mnt/memcached/ [root@server memcached] # tar zxvf libevent-2.1.8-stable.tar.gz-C / opt/ # # Event Notification Library [root@server memcached] # tar zxvf memcached-1.5.6.tar.gz-C / opt/ # # decompress event Library server [root@server memcached] # cd / opt/libevent-2.1.8-stable/ [root@server libevent-2.1.8-stable] #. / configure\ # configuration >-- prefix=/usr/local/libevent # # installation path [root@server libevent-2.1.8-stable] # Make & & make install # # compile and install [root@server libevent-2.1.8-stable] # cd / opt/memcached-1.5.6/ [root@server memcached-1.5.6] #. / configure\ >-- prefix=/usr/local/memcached\ >-- with-libevent=/usr/local/libevent # # Associate libevent event Notification Library [root@server memcached-1.5.6] # make & & make install [root@server memcached-1.5.6 ] # ln-s / usr/local/memcached/bin/* / usr/local/bin/ # # establish a soft connection [root@server memcached-1.5.6] # memcached- d-m 32m-p11211-uroot # # start the service # #-d daemon -m cache size 32m -p port 11211 [root@server memcached-1.5.6] # netstat-ntap | grep memcached # # View port [root@server memcached-1.5.6] # systemctl stop firewalld.service [root@server memcached-1.5.6] # setenforce 0 [root@server memcached-1.5.6] # yum install telnet-y # # install telnet software [root@server memcached-1.5.6] # telnet 127.0.0.1 11211 # # Local test login Record add username 0 07 # # 0: do not set sequence number 0: do not set expiration event 7: byte length 1234567STOREDadd users 0 0 7123ERRORget username # # View VALUE username 0 71234567ENDgets username # # View VALUE username 0 71 # # 1: update times 1234567END2 Install lamp structure in memcache client # # lamp structure see previous blog for specific operations # # Test whether the data is working properly [root@client php-5.6.11] # mysql-u root-pabc123 / / enter the database CREATE DATABASE sky / / create a database as skyGRANT all ON sky.* TO 'skyuser'@'%' IDENTIFIED BY' admin123'; / / flush privileges / / Refresh database # # modify PHP homepage [root@client php-5.6.11] # vim / usr/local/httpd/htdocs/index.php 3 Install memcache client [root@client ~] # yum install autoconf-y [root@client ~] # cd / mnt/memcached/ [root@client memcached] # tar zvxf memcache-2.2.7.tgz-C / opt/ [root@client memcached] # cd / opt/memcache-2.2.7/ [root@client memcache-2.2.7] # / usr/local/php5/bin/phpize # # add PHP module generation script [root@client memcache-2 .2.7] #. / configure\ >-- enable-memcache\ # enable memcache >-- with-php-config=/usr/local/php5/bin/php-config # # Associate PHP configuration file [root@client memcache-2.2.7] # make & & make install/usr/local/php5/lib/php/extensions/no-debug-zts-20131226/ # # shared file location Later, you need to use [root@client memcache-2.2.7] # vim / usr/local/php5/php.ini # # to modify the php configuration file extension_dir = "/ usr/local/php5/lib/php/extensions/no-debug-zts-20131226/" # shared file location extension = memcache.so # # point to memcache module 4 Use the client to check whether the server is connected properly [root@client memcache-2.2.7] # vim / usr/local/httpd/htdocs/index.php [root@client memcache-2.2.7] # service httpd stop # # restart [root@client memcache-2.2.7] # service httpd start## browser access

5 basic operation of add username memcache database: new data: add username 007 / / add data (two zeros mean: without compression and serialization identification, the data expiration time will never expire; if the identification number is 7, you need to enter 7 digits. ) allways / / enter a 7-digit query data: get username / / query data gets username update data: set username 010 / / update information, if the key name does not exist, then add everythingreplace username 008 / / update information, if the key name does not exist Then report error 12345678 detect / view updated data: gets username / / detect update VALUE username 0 8 412345678 append data: append username 0 07 / / add data after key value exampleprepend username 0 02 / / add data before key value un clear data: delete username / / clear specified key data flush_all / / clear all cached data OK view server statistics: stats / / display status information stats items / / return statistics of all key-value pairs stats cachedump 1 / / return key values of specified storage space stats slabs / / display information of each slab stats sizes / / output the size and number of all item stats reset / / clear statistics thank you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report