memcached cache application in lamp architecture

Source: Internet
Author: User
Tags curl memcached php server

What is memcached?

Memcached is a set of distributed memory object caching systems designed to reduce database load in dynamic systems and thereby improve system performance. Memcache Most of the situation is used as the front-end cache of the database, because it is much less expensive than database SQL parsing, disk operations, and uses memory to manage the data, so it can provide better performance than directly reading the database.

Caches are typically used to hold some frequently accessed objects or data (for example, browsers cache frequently accessed pages), and accessing objects or data through caching is much faster than disk. Memcache is a memory cache that puts frequently accessed objects or data in memory, in-memory cached data is accessed through the API, and the data is like a large hash table, in the form of Key-value.

The process logic for Memcache and database collaboration is as follows:
  1. Check that the data requested by the client is present in the memcache, and if it returns the requested data directly, it will no longer perform any operation on the data;
  2. If the requested data is not in Memcache, the database is queried, the data obtained from the database is returned to the client, and the data is cached to the memcache;
  3. Update the data in the Memcache every time the database is updated, such as updating and deleting data from the database, ensuring that the data in the memcache is consistent with the data in the database.
  4. When the allocated memcache memory space is exhausted, the LRU (least recently used) policy is added with the expiration invalidation policy, the invalidated data is replaced first, and then the unused data is replaced.
Characteristics of the memcached:
    • Simple protocol (based on text line protocol, can access data directly on the server via Telnet)
    • Libevent-based event management
    • Built-in memory management method
    • Distribution characteristics between non-communicating memcached
Site architecture Diagram

System environment:
Host name Operating System IP Address Service Name
Memcached centos7.4 192.168.96.22 Memcached-1.5.9.tar.gz, libevent-2.0.21-stable.tar.gz
Web1 centos7.4 192.168.96.23 LAMP, memcache-2.2.7.tgz
Client Windows 10 192.168.96.2 Web browser

Shutting down firewalls and SELinux

systemctl stop firewalldsetenforce 0

Related packages Baidu Cloud disk password: j2v3

Start deploying one, Memcache server 1. Installing the Libevent Software
#解压tar -zxvf libevent-2.0.21-stable.tar.gz -C /opt#进入目录cd /opt/libevent-2.0.21-stable/#配置./configure#编译及安装make && make install
2. Installing the Memcached Software
#解压tar zxvf memcached-1.5.9.tar.gz -C /opt#进入目录cd /opt/memcached-1.5.9/#配置./configure#编译及安装make && make install
3. Start the memcached service
memcached -m 32m -p 11211 -d -u root -P /var/run/memcached.pid -c256

The above options are described below:
-P: TCP port used, default is 11211
-M: Maximum memory size, default is 64M
-VV: Start in very vrebose mode, output debug information and errors to the console
-D: Run as daemon background
-C: The maximum number of concurrent connections to run, default is 1024, generally according to the server load amount to set
-P: Set the PID file to save memcached
-L: The server IP address of the listener, if there are multiple addresses
-u: User running memcached, default cannot start with root, if you need to use-U to specify the root user

4. View Memcached Service Information
netstat -tunlp | grep memcached
5. Connect to Login
telnet 192.168.96.22 11211
Five basic operations commands
    1. set    2. add    3. replace    4. get    5. delete
The syntax for key-value pairs is as follows:

Command <key> <flags> <expiration time> <bytes>
<value>

Memcached Modifying command parameters
Parameters usage
Key Key to find cached values
Flags You can include integer parameters for key-value pairs that the client uses to store additional information about key-value pairs
Expiration time The length of time (in seconds, 0 for forever) that a key-value pair is saved in the cache
bytes The byte points stored in the cache
Value Stored values (always on the second row)
memcached Basic Operations Command
1.setset命令用于向缓存添加新的键值对,如果已经存在,则之前的值将被替换,响应STORED2.add当缓存中不存在键时,add命令才会向缓存中添加一个键值对,如果缓存中已经存在该键,则之前的值将仍将保持不变,并返回响应NOT_STORED3.append username 0 0 4    //键值后追加4个字节4.prepend username 0 0 2   //键值前追加2个字节5.replace仅当键已经存在时,replace命令才会替换缓存中的键,如果缓存中不存在该键,则返回响应NOT_STORED6.get用于检索与之前添加的键值对相关的值7.delete用于删除memcached中的任何现有值,将使用一个键调用delete,如果该键存在于缓存中,则删除该值。如果不存在,则返回一条NOT_FOUND消息。8.stats转储所连接的 memcached 实例的当前统计数据。9.flush_all仅用于清理缓存中的所有名称/值对。如果需要将缓存重置到干净的状态,则 flush_all 能提供很大的用处。10.quit //退出
Second, the application memcache API (PHP server) Here choose Yum to quickly build the lamp server 1. Install Apache
yum install httpd httpd-devel -y
2. Setting up the HTTPD service boot
systemctl enable httpd
3. Start the HTTPD service
systemctl start httpd
4. Check the port monitoring situation
netstat -tunlp | grep httpd

5. Client Access Testing

6. Install MySQL Database
yum install mariadb mariadb-server mariadb-libs mariadb-devel -y
7. View Package Status
rpm -qa | grep mariadb

8. Set up boot from boot
systemctl enable mariadb
9. Start the MySQL service
systemctl start mariadb
10. Check the port monitoring situation
netstat -tunlp | grep mysql

11. Database Security Settings

Mysql_secure_installation

Note:running all PARTS of this SCRIPT are RECOMMENDED for all MariaDB SERVERS in PRODUCTION use! Please READ each STEP carefully!  In order to log into MariaDB to secure it, we'll need the CurrentPassword for the root user. If you ' ve just installed MariaDB, Andyou Haven ' t set the root password yet, the password would be a blank,so you should just Press ENTER here. Enter current password to root (enter for none): #直接 "carriage return" Setting the root password ensures that nobody can log I Nto the Mariadbroot user without the proper authorisation. You already has a root password set so that you can safely answer ' n '. Change the root password? [y/n] y #输入 "y" new Password:re-enter new Password:password updated successfully! Reloading privilege tables. ... success!  By default, a MariaDB installation had an anonymous user, allowing Anyoneto log into MariaDB without had to had a user  Account created Forthem.  This was intended only for testing, and the Installationgo a bit smoother.You should remove them before moving into aproduction environment. Remove anonymous users? [y/n] y #输入 "y" ... success!  Normally, Root should only is allowed to connect from ' localhost '. Thisensures that someone cannot guess at the root of password from the network. Disallow Root login remotely? [y/n] y #输入 "y" ... success!  By default, the MariaDB comes with a database named ' Test ' anyone canaccess. This was also intended only for testing, and should was removedbefore moving into a production environment. Remove test database and access to it? [y/n] y #输入 "y"-dropping test database ... success! -Removing privileges on test database ... success! Reloading the privilege tables would ensure that all changes made so farwill take effect immediately. Reload privilege tables now? [y/n] y #输入 "y" ... success! Cleaning up ...  All done! If you've completed all of the above steps, your mariadbinstallation should now is secure. Thanks for using mariadb!
12. Log in to MySQL database test
mysql -u root -p

13. Install PHP
yum -y install php php-devel
14. View installed PHP-related packages
rpm -ql php

15. Associating PHP with MySQL
yum install php-mysql
16. View
rpm -ql php-mysql

17. Install Common PHP modules

Yum install-y php-gd php-ldap php-odbc php-pear php-xml php-xmlrpc php-mbstring php-snmp php-soap Curl Curl-devel php-bcm Ath

18. Create a PHP test page

Cd/var/www/html
Vim info.php

<?php    phpinfo();?>
19. Restart the HTTPD service
systemctl restart httpd
20. Client Test PHP

Client Access http://192.168.96.22/info.php

21. Client-side installation of memcache PHP extension features
#安装autoconf软件包yum install autoconf -y#解压tar xf memcache-2.2.7.tgz -C /opt/#进入目录cd /opt/memcache-2.2.7#增加为PHP的模块后再对memcache进行配置编译/usr/bin/phpize![](http://i2.51cto.com/images/blog/201807/25/7f0ba593f9d05d86f8e0f75065739d88.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)#配置./configure --enable-memcache --with-php-config=/usr/bin/php-config#编译及安装make && make install

22. Edit PHP.ini

Vim/etc/php.ini

#732行,新增以下命令extension_dir = "/usr/lib64/php/modules/"#864行,新增以下命令extension = memcache.so
23. Write the test page to test whether the memcached is working properly

vim/var/www/html/index.php

<?php$memcache = new Memcache();$memcache->connect(‘192.168.96.22‘,11211);$memcache->set(‘key‘,‘Memcache test Successfull!‘,0,60);$result = $memcache->get(‘key‘);unset($memcache);echo $result;?>
24. Restart the HTTPD service

Service httpd Restart

25. The client's access test was successful, http://192.168.96.22/index.php

memcached cache application in lamp architecture

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.