Get the App
SLTechnology News&Howtos  ›  Servers  › 

Nginx+PHP+memcached+MySQL+ip-hash to do memcached Cluster

Shulou Source: shulou.com Published: 2022-06-03 06:36:36 09月21日 Update

1. Integration of nginx and memcached

# install libevent, a transaction library supported by memcached

Wget https://github.com/libevent/libevent/releases/download/release-2.0.22-stable/libevent-2.0.22-stable.tar.gztar zxf libevent-2.0.22-stable.tar.gz cd libevent-2.0.22-stable./configure-- prefix=/usr/local/libeventmake & & make installecho $? cd..

# next install memcached:

Wget http://www.memcached.org/files/memcached-1.4.35.tar.gztar zxf memcached-1.4.35.tar.gzcd memcached-1.4.35./configure-prefix=/usr/local/memcached-with-libevent=/usr/local/libeventmake & & make installecho $? cd.

# run memcached

[root@nginx] # / usr/local/memcached/bin/memcached-d-u nobody-vv

# configure the nginx configuration file nginx.conf, and locate the uri of the user to be cached by memcached

# add location location:

Location ~ * user {set $memcached_key "$uri"; # set the key of memcached to uri memcached_pass 192.168.146.132 set 11211; # Link memcached error_page 404 / callback.php; # error location}

# load nginx configuration

Nginx-s reload

# write a piece of URI data in memcached to test whether the integration is successful

[root@nginx ~] # telnet 192.168.146.132 11211Trying 192.168.146.132...Connected to 192.168.146.132.Escape character is'^] .add / user1.html 0 7iamlisi STOREDquit

# if you can see iamlisi when visiting http://192.168.146.132/user1.html, then the integration of nginx and memcache is successful!

2. Integrate PHP and memcahced

# install PHP expansion module memcache

Wget http://pecl.php.net/get/memcache-2.2.7.tgztar zxvf memcache-2.2.7.tgzcd memcache-2.2.7/usr/local/php/bin/phpize. / configure-enable-memcache-with-php-config=/usr/local/php/bin/php-config-with-zlib-dirmake & & make installecho $? cd.

Pkill php-fpm # Kills the php-fpm process

Php-fpm # launch php-fpm

If http://192.168.146.132/test.php can see the memcache module, it will be successful.

# Test PHP and memcached

Vim / usr/local/nginx/html/callback.phpphp

# if the uri does not exist in memcached when accessing http://1982.168.146.132/user2.html, it will be called back to callback.php to process the request. The result is as follows:

Array ([USER] = > nobody [HOME] = > / [FCGI_ROLE] = > RESPONDER [SCRIPT_FILENAME] = > / usr/local/nginx/html/callback.php [QUERY_STRING] = > [REQUEST_METHOD] = > GET [CONTENT_TYPE] = > [CONTENT_LENGTH] = > [SCRIPT_NAME] = > / callback.php [REQUEST_URI] = > / user2.html [DOCUMENT_URI] = > / callback.php [DOCUMENT_ROOT ] = > / usr/local/nginx/html [SERVER_PROTOCOL] = > HTTP/1.1 [REQUEST_SCHEME] = > http [GATEWAY_INTERFACE] = > CGI/1.1 [SERVER_SOFTWARE] = > nginx/1.12.1 [REMOTE_ADDR] = > 192.168.146.1 [REMOTE_PORT] = > 14187 [SERVER_ADDR] = > 192.168.146.132 [SERVER_PORT] = > 80 [SERVER_NAME] = > localhost [ REDIRECT_STATUS] = > 200 [HTTP_HOST] = > 192.168.146.132 [HTTP_CONNECTION] = > keep-alive [HTTP_UPGRADE_INSECURE_REQUESTS] = > 1 [HTTP_USER_AGENT] = > Mozilla/5.0 (Windows NT 10.0 Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113 Safari/537.36 [HTTP_ACCEPT] = > text/html,application/xhtml+xml,application/xml;q=0.9,p_w_picpath/webp,p_w_picpath/apng,*/*;q=0.8 [HTTP_ACCEPT_ENCODING] = > gzip, deflate [HTTP_ACCEPT_LANGUAGE] = > zh-CN,zh;q=0.8 [PHP_SELF] = > / callback.php [REQUEST_TIME] = > 1503552110)

3. Use PHP to make memcached link MySQL to query key and value and store them in memcached.

# next, we write a PHP program, link the database, and let the uri that memcached cannot find call back to PHP, and then PHP links the database, finds the data and gives it to memcached:

Cat html/callback.php

Tags: Data link configuration database query consistency module hash cluster consistency file reference integration success garbled document server algorithm processing location Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology macOS MySQL Redmi Xiaomi