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

LAMP+Memcached High performance caching Service

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

Share

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

Introduction to Memcached

Memcached is a high-performance distributed memory cache server. By maintaining a unified huge hash table in memory, it can be used to store data in various formats, including images, videos, files and database retrieval results, etc., developed by the development team of foreign community website LiveJournal. The purpose of general use is to reduce the number of database visits by caching database query results, so as to improve the speed and scalability of dynamic Web applications.

Official website: http://www.danga.com/memcached/

Typical architectures are as follows:

When the Web client sends a request to the application of the web server, the application will call the Memcached API client library interface to connect to the Memcached server and query the data.

1. If the data requested by the web client has been cached in the Memcached server, the Memcached server will return the data to the Web client

2. If the data does not exist, the Web client request will be sent to the MySQL database, and the database will return the requested data to the Memcached and Web clients. At the same time, the Memcached server will also save the data to facilitate users to use it next time.

Memcached characteristic Protocol simple event handling based on libevent built-in memory Storage memcached distributed without communicating with each other

Libevent: http://www.monkey.org/~provos/libevent/ protocol is simple

Memcached's server-client communication does not use complex formats such as XML, but uses a simple text-line-based protocol. Therefore, data can also be saved and obtained on memcached through telnet. Here is an example.

$telnet localhost 11211

Trying 127.0.0.1...

Connected to localhost.localdomain (127.0.0.1).

Escape character is'^]'.

Set foo 0 03 (save command)

Bar (data)

STORED (result)

Get foo (get order)

VALUE foo 0 3 (data)

Event handling based on libevent

Libevent is a library that encapsulates event handling functions such as epoll of Linux and kqueue of BSD operating systems into a unified interface. O (1) performance can be achieved even if the number of connections to the server increases. Memcached uses this libevent library, so it can perform its high performance on Linux, BSD, Solaris, and other operating systems.

Built-in memory storage

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.

Data storage method: Slab Allocation

Slab Allocation allocates memory by group, allocating one slab at a time, which is equivalent to a page of 1MB size, and then dividing the chunk of the same size according to the data in the space of 1MB. This method can effectively solve the problem of memory fragmentation, but it may waste space.

Memcached distributed without communicating with each other

Although memcached is a "distributed" cache server, there is no distributed function on the server side. Memcached does not communicate with each other to share information.

Memcached caching mechanism and distributed

Caching mechanism:

Memcached is a very good caching software. When a program writes a request for cached data, the API interface of Memcached will input Key.

The algorithm module is routed to a server in the cluster, and then the API interface communicates with the server to complete a distributed cache write.

Distributed system

Memcached distributed deployment mainly depends on the client of Memcached, and multiple Memcached servers are independent. How distributed data is stored is determined by routing algorithms. When the data reaches the client library, the client algorithm determines the saved Memcached server according to the routing algorithm. When reading the data, the client selects the same server according to the routing algorithm when saving the data and stores the data to read the data.

Experimental environment

Two CentOS7 are used in this lab:

Host IP address role main software package Memcached192.168.37.128Memcached ​ server libevent-2.1.8-stable.tar.gzmemcached-1.5.6.tar.gzMemcached ​ API192.168.37.130Memcached ​ API client http-2.4.29.tar.gz;mysql-5.6.26.tar.gz;php-5.6.11.tar.bz2;memcache-2.2.7.tar.gz experimental deployment I. memcached server

1. Install the compilation environment

Yum install gcc gcc-c++ make

2. Extract the installation package

Tar zxvf memcached-1.5.6.tar.gz-C / opt

Tar zxvf libevent-2.1.8-stable.tar.gz-C / opt # libevent: event Notification Library

3. Compile and install libevent and memcached manually

Cd / opt/libevent-2.1.8-stable/./configure-prefix=/usr/local/libeventmake & & make installcd / opt/memcached-1.5.6/./configure\-prefix=/usr/local/memcached\-with-libevent=/usr/local/libevent/make & & make install

4. Establish a soft connection

Ln-s / usr/local/memcached/bin/* / usr/local/bin

5. Enable memcached

Systemctl stop firewalld.service

Setenforce 0

Memcached-d-m 32m-p 11211-u root

#-d: specify daemon-m: cache size-p: Port-u: specify administrative user

Netstat-ntap | grep memc

2. LAMP building

(1) Apache building

1. Install the environment package

Yum install gcc gcc-c++ make pcre-devel expat-devel perl-y

2. Extract the installation package

Tar xzvf http-2.4.29.tar.gz-C / opt

Tar xzvf apr-1.4.6.tar.gz-C / opt # supports Apache upper applications across platforms and provides underlying interface libraries

Tar xzvf apr-util-1.4.1.tar.gz-C / opt

Mv apr-1.6.2/ httpd-2.4.29/srclib/apr

Mv apr-util-1.6.0/ httpd-2.4.29/srclib/apr-util

3. Compile and install by hand

Cd / opt/httpd-2.4.29./configure\-prefix=/usr/local/httpd\-enable-so\-enable-rewrite\-enable-charset-lite\-enable-cgimake & & make install

4. Edit the startup script

Grep-v "#" / usr/local/httpd/bin/apachectl > / etc/init.d/httpd # filter comment information in the configuration file Copy to the server directory vi / etc/init.d/httpd # insert the following line #! / bin/sh # chkconfig:2345 85 21 # description:Apache is a World Wide Web server.chmod + x / etc/init.d/httpdchkconfig-- add httpd * # add httpd to the system service * * at the front of the file

5. Optimize the service to make the system recognize the command.

Ln-s / usr/local/httpd/conf/httpd.conf / etc/httpd.conf

Ln-s / usr/local/httpd/bin/* / usr/local/bin

6. Edit the httpd main configuration file

Vim / usr/local/httpd/conf/httpd.conf ServerName www.yun.com:80 Listen 192.168.37.130:80

7. Enable httpd service

Systemctl stop firewalld.service

Setenforce 0

Apachectl-t # check syntax

Service httpd start

(2) Mysql building

1. Install the environment package

Yum-y install ncurses-devel autoconf cmake

2. Compile and install mysql manually

Tar zxvf mysql-5.6.26.tar.gz-C / opt # decompression package

Cd / opt/mysql-5.6.26

Cmake\

-DCMAKE_INSTALL_PREFIX=/usr/local/mysql\

-DDEFAULT_CHARSET=utf8\

-DDEFAULT_COLLATION=utf8_general_ci\

-DEXTRA_CHARSETS=all\

-DSYSCONFIDIR=/etc\

-DMYSQL_DATADIR=/home/mysql/\

-DMYSQL_UNIX_ADDR=/home/mysql/mysql.sock\

Make & & make install

3. Add system services

Cp support-files/mysql.server / etc/init.d/mysqld # add system services

Chmod 755 / etc/init.d/mysqld # modify execution permissions

Chkconfig-- add / etc/init.d/mysqld # add mysqld as a system service

Chkconfig mysqld-level 235 on #

Echo "PATH=$PATH:/usr/local/mysql/bin" > > / etc/profile

Source / etc/profile # reload environment variables

Useradd-s / sbin/nologin mysql # create process user mysql

Chown-R mysql:mysql / usr/local/mysql/ # modifies the owner and group of mysql

4. Initialize the database

/ usr/local/mysql/scripts/mysql_install_db\

-- user=mysql\

-- ldata=/var/lib/mysql\

-- basedir=/usr/local/mysql\

-- datadir=/home/mysql

5. Establish a soft connection

Ln-s / var/lib/mysql/mysql.sock / home/mysql/mysql.sock

6. Edit the configuration file

Vi / etc/init.d/mysqld basedir=/usr/local/mysql datadir=/home/mysql

7. Start the mysql service

Service mysqld start

Netstat-ntap | grep 3306

(3) php building

1. Install the environment package

Yum-y install gd libpng pcre libxml2-devel libjpeg-devel libpng-devel fontconfig-devel openssl-devel bzip2-devel

2. Compile and install php manually

Tar xjvf php-5.6.11.tar.bz2-C / optcd / opt/php-5.6.11./configure\-- prefix=/usr/local/php5\-- with-gd\-- with-zlib\-- with-apxs2=/usr/local/httpd/bin/apxs\-- with-mysql=/usr/local/mysql\-- with-config-file-path=/usr/local/php5\-enable-mbstringmake & & make install

3. Create a php configuration file

Cp php.ini-development / usr/local/php5/php.ini

4. Establish a soft connection

Ln-s / usr/local/php5/bin/ / usr/local/bin/

Ln-s / usr/local/php5/sbin/ / usr/local/sbin

5. Modify apache configuration file

Vim / etc/httpd.conf

AddType application/x-httpd-php .php

AddType application/x-httpd-php-source .phps # / etc/httpd.conf to see if there is a php5

DirectoryIndex index.php index.html

6. Modify the apache home page to test php

The settings in mv index.html index.php # and httpd configuration files should be the same.

6. Restart the httpd service and test

Visit: http://192.168.37.130/index.php

(4) Test database

1. Create a database test

Create database a

2. Test database authorizes auser users

Grant all on a.* to 'auser'@'%' identified by' 123123 authorization

Flush privileges; # Refresh permissions

3. Modify the apache home page to access the database again

Vim / usr/local/httpd/htdocs/index.php

4. Restart the apache service and test the database connection

(5) install the client

1. Install the environment package

Yum install autoconf-y

2. Compile and install memcached manually

Tar zxvf memcache-2.2.7.tar.gz-C / optcd / opt/memcache-2.2.7/ # Note: no configure/usr/local/php5/bin/phpize # is added to PHP template before configuring memcache. / configure\-- enable-memcache\-- with-php-config=/usr/local/php5/bin/php-configmake & & make install/usr/local/php5/lib/php/extensions/no-debug-zts-20131226/ # copy this line after installation

3. Add memcached components to PHP

Vim / usr/local/php5/php.ini extension_dir = "ext" # add extension_dir = "/ usr/local/php5/lib/php/extensions/no-debug-zts-20131226/" # copy here extension = memcache.so

4. Modify the home page of apache

Vim / usr/local/httpd/htdocs/index.php

5. Restart the apache service and test

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