Cached Database Memcached - Install and Manage Database Operations

Keywords: Database MySQL PHP vim

1. Introduction to Memcached:

(1) Introduction:

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

(2) Features:

As a fast-running distributed cache server, memcached has the following characteristics:

1. Simple protocol;
2. Event handling based on libevent;
3. Built-in memory storage;
4. Distributed where memcached does not communicate with each other.

(3) Storage method:

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

2. Configuration of memcached database:

1. Preparing to install the required source packages

[root@localhost ~]# mkdir /mnt/tools
[root@localhost ~]# mount.cifs //192.168.100.100/tools/mnt/tools/ ##mount
Password for root@//192.168.100.100/tools:  
[root@localhost ~]# cd /mnt/tools/memcached/
[root@localhost memcached]# ls
LAMP-php5.6                   magent-0.5.tar.gz   memcached-1.5.6.tar.gz
libevent-2.1.8-stable.tar.gz  memcache-2.2.7.tgz
[root@localhost memcached]# 
[root@localhost memcached]# tar xf libevent-2.1.8-stable.tar.gz -C /opt/   ##decompression
[root@localhost memcached]# tar xf memcached-1.5.6.tar.gz -C /opt/   ##decompression
[root@localhost memcached]# cd /opt/
[root@localhost opt]# ls
libevent-2.1.8-stable  memcached-1.5.6  rh
[root@localhost opt]#

2. Compile and install memcached

[root@localhost opt]# yum install gcc gcc-c++ make -y   ##Install Dependent Environment Package
.............//Omitting process
[root@localhost libevent-2.1.8-stable]# ./configure --prefix=/usr/local/libevent   ##Configure libevent
.............//Omitting process
[root@localhost libevent-2.1.8-stable]# make && make install   ##Compile and install libevent
.............//Omitting process
[root@localhost libevent-2.1.8-stable]# cd ../memcached-1.5.6/
[root@localhost memcached-1.5.6]# ./configure \   ##Configure memcached
> --prefix=/usr/local/memcached \
> --with-libevent=/usr/local/libevent/
.............//Omitting process
[root@localhost memcached-1.5.6]# make && make install   ##Compile and install memcached
.............//Omitting process
[root@localhost memcached-1.5.6]# ln -s /usr/local/memcached/bin/* /usr/local/bin   ##Easy system identification
[root@localhost memcached-1.5.6]# 

3. Open memcached

[root@localhost memcached-1.5.6]# memcached -d -m 32m -p 11211 -u root
##Open database service, -d daemon; -m cache size 32M; -p port 11211
[root@localhost memcached-1.5.6]#
[root@localhost memcached-1.5.6]# netstat -ntap | grep memcached   ##View Port
tcp        0      0 0.0.0.0:11211           0.0.0.0:*               LISTEN      22448/memcached     
tcp6       0      0 :::11211                :::*                    LISTEN      22448/memcached     
[root@localhost memcached-1.5.6]# 
[root@localhost memcached-1.5.6]# systemctl stop firewalld.service    ##Close Firewall
[root@localhost memcached-1.5.6]# setenforce 0   ##Turn off enhanced security features
[root@localhost memcached-1.5.6]#

4. Basic operation of memcached database

[root@localhost memcached-1.5.6]# yum install -y telnet   ##Install Telnet Service
.............//Omitting process
[root@localhost memcached-1.5.6]# telnet 127.0.0.1 11211   ##Connect to memcached database
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
add username 0 0 7
//Add data (two zeros indicate that the data will never expire without compression and serialization of the identification; an identification number of 7 requires a seven-digit input.)
1234567   ##Enter a 7-bit value
STORED  ##Added Successfully
add users 0 0 7
123   ##Input error value

ERROR   ##Failed to add
get username      
VALUE username 0 7
1234567
END
gets username     ##Query Data
VALUE username 0 7 1
1234567
END
set username 0 0 8     ##Update information, add it if the key name does not exist
12345678
STORED
gets username        ##Query Data
VALUE username 0 8 3
12345678
END
replace school 0 0 2     ##Update information, error if key name does not exist
un 
NOT_STORED
get shcool        ##Query Data
END
replace username 0 0 9     ##Update information, error if key name does not exist
123456789
STORED
gets username        ##Query Data
VALUE username 0 9 4
123456789
END
set school 0 0 4     ##Update information, add it if the key name does not exist
1234
STORED
gets school        ##Query Data
VALUE school 0 4 5
1234
END
cas school 0 0 7 5    ##Modify key storage number
logging
STORED
gets school   
VALUE school 0 7 6
logging
END
cas school 0 0 8 2
loggings
EXISTS
append school 0 0 4   ##Append data after key value
book
STORED
gets school
VALUE school 0 11 7
loggingbook
END
prepend school 0 0 2    ##Append data before key value
un
STORED
gets school
VALUE school 0 13 8
unloggingbook
END
delete school    ##Clear specified key value data, clear all cached data as flush_all
DELETED
get school
END
quit     ##Sign out
Connection closed by foreign host.
[root@localhost memcached-1.5.6]# 

##View commands:
stats                                ##show status information
stats items                      ##Returns statistics for all key-value pairs
stats cachedump 1 0      ##Returns the key-value pairs for the specified storage space 
stats slabs                      ##Display information about individual slab s
stats sizes                      ##Output the size and number of all item s
stats reset                      ##Clear statistics

3. Client Configuration (Deploying LAMP Architecture)

LAMP architecture has been blogged before, to see a detailed explanation can, take a look.There is no explanation below.
LNMP Architecture

1. Deploy LAMP architecture

[root@localhost ~]# mkdir /mnt/tools
[root@localhost ~]# mount.cifs //192.168.100.100/tools /mnt/tools/
Password for root@//192.168.100.100/tools:  
[root@localhost ~]# cd /mnt/tools/memcached/
[root@localhost memcached]# ls
LAMP-php5.6  libevent-2.1.8-stable.tar.gz  magent-0.5.tar.gz  memcache-2.2.7.tgz  memcached-1.5.6.tar.gz
[root@localhost LAMP-php5.6]# ls
apr-1.6.2.tar.gz       httpd-2.4.29.tar.bz2  mysql-5.6.26.tar.gz
apr-util-1.6.0.tar.gz  LAMP-php5.6.txt       php-5.6.11.tar.bz2
[root@localhost LAMP-php5.6]# 

[root@localhost LAMP-php5.6]# tar xf apr-1.6.2.tar.gz -C /opt/
[root@localhost LAMP-php5.6]# tar xf apr-util-1.6.0.tar.gz -C /opt/
[root@localhost LAMP-php5.6]# tar xf httpd-2.4.29.tar.bz2 -C /opt/
[root@localhost LAMP-php5.6]# 
[root@localhost LAMP-php5.6]# cd /opt/
[root@localhost opt]# ls
apr-1.6.2  apr-util-1.6.0  httpd-2.4.29  rh
[root@localhost opt]# mv apr-1.6.2/ httpd-2.4.29/srclib/apr
[root@localhost opt]# mv apr-util-1.6.0/ httpd-2.4.29/srclib/apr-util
[root@localhost opt]# ls
httpd-2.4.29  rh
[root@localhost opt]# 
[root@localhost opt]# cd httpd-2.4.29/
[root@localhost httpd-2.4.29]# yum -y install \
> gcc \
> gcc-c++ \
> make \
> pcre-devel \
> expat-devel \
> perl \
> zlib-devel

[root@localhost httpd-2.4.29]# ./configure \
> --prefix=/usr/local/httpd \
> --enable-so \
> --enable-rewrite \
> --enable-charset-lite \
> --enable-cgi

[root@localhost httpd-2.4.29]# make && make install

[root@localhost httpd-2.4.29]# cp /usr/local/httpd/bin/apachectl /etc/init.d/httpd
[root@localhost httpd-2.4.29]# vim /etc/init.d/httpd 
# Chkconfig: 35 85 21 //35 level autorun 85th boot 21 shutdown
# description: Apache is a World Wide Web server
[root@localhost httpd-2.4.29]# chkconfig --add httpd
[root@localhost httpd-2.4.29]# 
[root@localhost httpd-2.4.29]# vim /usr/local/httpd/conf/httpd.conf 
ServerName www.yun.com:80
Listen 192.168.52.132:80
#Listen 80
[root@localhost httpd-2.4.29]# ln -s /usr/local/httpd/conf/httpd.conf /etc/
[root@localhost httpd-2.4.29]# ln -s /usr/local/httpd/bin/* /usr/local/bin/
[root@localhost httpd-2.4.29]# apachectl -t
Syntax OK
[root@localhost httpd-2.4.29]# 
[root@localhost httpd-2.4.29]# service httpd start 
[root@localhost httpd-2.4.29]# netstat -ntap | grep 80
tcp        0      0 192.168.52.132:80       0.0.0.0:*               LISTEN      88064/httpd         
[root@localhost httpd-2.4.29]# 
[root@localhost httpd-2.4.29]# cd /mnt/tools/memcached/LAMP-php5.6/
[root@localhost LAMP-php5.6]# ls
apr-1.6.2.tar.gz       httpd-2.4.29.tar.bz2  mysql-5.6.26.tar.gz
apr-util-1.6.0.tar.gz  LAMP-php5.6.txt       php-5.6.11.tar.bz2
[root@localhost LAMP-php5.6]# tar xf mysql-5.6.26.tar.gz -C /opt/
[root@localhost LAMP-php5.6]# cd /opt/
[root@localhost opt]# ls
httpd-2.4.29  mysql-5.6.26  rh
[root@localhost opt]# cd mysql-5.6.26/
[root@localhost mysql-5.6.26]# yum install -y ncurses-devel autoconf cmake
[root@localhost 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

[root@localhost mysql-5.6.26]# make && make install

[root@localhost mysql-5.6.26]# cp support-files/my-default.cnf /etc/my.cnf
cp: Whether to Overwrite"/etc/my.cnf"? yes
[root@localhost mysql-5.6.26]# cp support-files/mysql.server /etc/init.d/mysqld
[root@localhost mysql-5.6.26]# chmod 755 /etc/init.d/mysqld       
[root@localhost mysql-5.6.26]# chkconfig --add /etc/init.d/mysqld
[root@localhost mysql-5.6.26]# chkconfig  mysqld --level 235 on
[root@localhost mysql-5.6.26]# echo "PATH=$PATH:/usr/local/mysql/bin" >> /etc/profile
[root@localhost mysql-5.6.26]# source /etc/profile
[root@localhost mysql-5.6.26]# useradd -s /sbin/nologin mysql
[root@localhost mysql-5.6.26]# chown -R mysql:mysql /usr/local/mysql/
[root@localhost mysql-5.6.26]# 
[root@localhost mysql-5.6.26]# /usr/local/mysql/scripts/mysql_install_db \
> --user=mysql \
> --ldata=/var/lib/mysql \
> --basedir=/usr/local/mysql \
> --datadir=/home/mysql

[root@localhost mysql-5.6.26]# ln -s /var/lib/mysql/mysql.sock  /home/mysql/mysql.sock
[root@localhost mysql-5.6.26]# vim /etc/init.d/mysqld 
basedir=/usr/local/mysql
datadir=/home/mysql
[root@localhost mysql-5.6.26]# service mysqld start 
Starting MySQL. SUCCESS! 
[root@localhost mysql-5.6.26]# netstat -ntap | grep 3306
tcp6       0      0 :::3306                 :::*                    LISTEN      103429/mysqld       
[root@localhost mysql-5.6.26]# 

[root@localhost mysql-5.6.26]# service mysqld start 
Starting MySQL. SUCCESS! 
[root@localhost mysql-5.6.26]# netstat -ntap | grep 3306
tcp6       0      0 :::3306                 :::*                    LISTEN      103429/mysqld       
[root@localhost mysql-5.6.26]# 
[root@localhost mysql-5.6.26]# mysqladmin -u root -p password "abc123"
Enter password: 
Warning: Using a password on the command line interface can be insecure.
[root@localhost mysql-5.6.26]# mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.6.26 Source distribution

Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> \q
Bye
[root@localhost mysql-5.6.26]# 

[root@localhost mysql-5.6.26]# cd /mnt/tools/memcached/LAMP-php5.6/
[root@localhost LAMP-php5.6]# ls
apr-1.6.2.tar.gz       httpd-2.4.29.tar.bz2  mysql-5.6.26.tar.gz
apr-util-1.6.0.tar.gz  LAMP-php5.6.txt       php-5.6.11.tar.bz2
[root@localhost LAMP-php5.6]# tar xf php-5.6.11.tar.bz2 -C /opt/
[root@localhost LAMP-php5.6]# cd /opt/
[root@localhost opt]# ls
httpd-2.4.29  mysql-5.6.26  php-5.6.11  rh
[root@localhost opt]# cd php-5.6.11/
[root@localhost php-5.6.11]# yum -y install \
> gd \
> libpng \
> libpng-devel \
> pcre \
> pcre-devel \
> libxml2-devel \
> libjpeg-devel
[root@localhost 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-mbstring 
[root@localhost php-5.6.11]# make && make install

[root@localhost php-5.6.11]# cp php.ini-development /usr/local/php5/php.ini
[root@localhost php-5.6.11]# ln -s /usr/local/php5/bin/* /usr/local/bin/
[root@localhost php-5.6.11]# ln -s /usr/local/php5/sbin/* /usr/local/sbin/
[root@localhost php-5.6.11]# 
[root@localhost php-5.6.11]# vim /etc/httpd.conf 
    AddType application/x-httpd-php .php
    AddType application/x-httpd-php-source .phps
<IfModule dir_module>
    DirectoryIndex index.html index.php
</IfModule>

[root@localhost php-5.6.11]# vim /usr/local/httpd/htdocs/index.php
<?php
phpinfo();
?>
[root@localhost php-5.6.11]# service httpd stop 
[root@localhost php-5.6.11]# service httpd start 
[root@localhost php-5.6.11]# systemctl stop firewalld.service 
[root@localhost php-5.6.11]# setenforce 0
[root@localhost php-5.6.11]# 

2. Test access, LAMP architecture built successfully

3. Modify index.php file

[root@localhost php-5.6.11]# vim /usr/local/httpd/htdocs/index.php
<?php
$link=mysql_connect('192.168.52.132','skyuser','admin123');
if($link) echo "<h1>Success!!</h1>";
else echo "Fail!!";
mysql_close();
?>

4. Test whether the database is working properly

[root@localhost php-5.6.11]# mysql -u root -pabc123   ##Enter database
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 6
Server version: 5.6.26 Source distribution

Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> create database sky;    ##Create a database as sky
Query OK, 1 row affected (0.00 sec)

mysql> grant all on sky.* to 'skyuser'@'%' identified by 'admin123';     ##Title
Query OK, 0 rows affected (0.01 sec)

mysql> flush privileges;    ##Refresh Permissions
Query OK, 0 rows affected (0.01 sec)

mysql> 

5. Visit the web page again and show success to indicate success

6. Install Memcache Client

[root@localhost php-5.6.11]# cd /mnt/tools/memcached/
[root@localhost memcached]# ls
LAMP-php5.6  libevent-2.1.8-stable.tar.gz  magent-0.5.tar.gz  memcache-2.2.7.tgz  memcached-1.5.6.tar.gz
[root@localhost memcached]# tar xf memcache-2.2.7.tgz -C /opt/   ##decompression
[root@localhost memcached]# cd /opt/
[root@localhost opt]# ls
httpd-2.4.29  memcache-2.2.7  mysql-5.6.26  package.xml  php-5.6.11  rh
[root@localhost opt]# 
[root@localhost opt]# cd memcache-2.2.7/
[root@localhost memcache-2.2.7]# /usr/local/php5/bin/phpize 
Configuring for:
PHP Api Version:         20131106
Zend Module Api No:      20131226
Zend Extension Api No:   220131226
[root@localhost memcache-2.2.7]# 

[root@localhost memcache-2.2.7]# ./configure \   ##To configure
> --enable-memcache \
> --with-php-config=/usr/local/php5/bin/php-config 

[root@localhost memcache-2.2.7]# make && make install   ##Compile Installation

7. Configure PHP and add Memcached components

[root@localhost memcache-2.2.7]# vim /usr/local/php5/php.ini
extension_dir = "/usr/local/php5/lib/php/extensions/no-debug-zts-20131226/"
extension=memcache.so

8. Modify the index.php file and restart the httpd service

[root@localhost memcache-2.2.7]# vim /usr/local/httpd/htdocs/index.php 
<?php
$memcache = new Memcache();
$memcache->connect('192.168.52.149',11211);
$memcache->set('key','Memcache test Successfull!',0,60);
$result = $memcache->get('key');
unset($memcache);
echo $result;
?>
[root@localhost memcache-2.2.7]# service httpd stop
[root@localhost memcache-2.2.7]# service httpd start 
[root@localhost memcache-2.2.7]# 

9. Client detects whether the server can connect properly

Posted by alvinchua on Mon, 23 Dec 2019 12:56:56 -0800