Article Directory
LAMP architecture adds Memcached cache
1. Install and deploy memcached, php7, httpd, mariadb
1. Install memcached to deploy and install telnet for testing
#安装memcached
[root@memcached ~]# yum -y install memcached
[root@memcached ~]# systemctl start memcached
[root@memcached ~]# ss -ntal
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:11211 *:*
LISTEN 0 128 *:22 *:*
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 128 :::11211 :::*
[root@memcached ~]# cat /etc/sysconfig/memcached
PORT="11211" # 监听端口
USER="memcached" # 用户
MAXCONN="1024" # 最大并发数
CACHESIZE="64" # 分配内存大小
OPTIONS="" # 监听网络地址,空就是默认监听所有网段
#然后把ip地址发给开发人员,开发的会使用api接口连接memcached.
#测试:
#安装telnet用于熟悉memcached的插入和查询数据方法
[root@memcached ~]# yum -y install telnet
[root@memcached ~]# telnet 192.168.138.131 11211
Trying 192.168.138.131...
Connected to 192.168.138.131.
Escape character is '^]'.
set name 0 60 9 #设置名称为name的key
sunlizhen #给name的值
STORED #出现stored表示已经存储成功。
get name #查询key值
VALUE name 0 9
sunlizhen
END
#set key key的id 缓存过期时间 字符串最大长度
#value
#输入的value等于设置的字符串的最大长度,没反应就是没存进去,出现stored就是存储成功了
#get key
#切记的value要和设定的字符串的最大正度一样
#参数解释:
#name:key的名字 自己定义
#0:key的id号,需要和其他的key不一样
#60:缓存过期时间,单位为秒,0为永远
#7:字符串最大长度
#END
#^]
#@ctrl ] 退出memcached
#ctrl d 退出telnet
#telnet> Connection closed.
2. Install php to support memcached expansion module, install mysql to import data, install httpd to configure support php
#1、安装php7.0
[root@memcached ~]# rpm -Uvh https://mirror.webtatic.com/yum/el7/epel-release.rpm
[root@memcached ~]# rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
[root@memcached ~]# yum install -y php70w.x86_64 php70w-cli.x86_64 php70w-common.x86_64 php70w-gd.x86_64 php70w-ldap.x86_64 php70w-mbstring.x86_64 php70w-mcrypt.x86_64 php70w-mysql.x86_64 php70w-pdo.x86_64 php70w-devel zlib-devel
[root@memcached ~]# yum -y install php70w-fpm
[root@memcached ~]# yum install -y make gcc zlib-devel libmemcached-devel git
#下载PHP Memcache 扩展包
[root@memcached ~]# git clone https://github.com/websupport-sk/pecl-memcache
[root@memcached ~]# cd pecl-memcache/
[root@memcached pecl-memcache]# /usr/bin/phpize
\Configuring for:
PHP Api Version: 20151012
Zend Module Api No: 20151012
Zend Extension Api No: 320151012
[root@memcached pecl-memcache]# ./configure --with-php-config=/usr/bin/php-config
[root@memcached pecl-memcache]# make && cp modules/memcache.so /usr/lib64/php/modules/
[root@memcached pecl-memcache]# vim /etc/php.ini
最后添加一行:extension=memcache.so
[root@memcached pecl-memcache]# systemctl restart php-fpm
#2、安装mariadb并且导入数据
[root@memcached pecl-memcache]# cd
[root@memcached ~]# yum -y install mariadb mariadb-server
[root@memcached ~]# systemctl start mariadb
#数据库进行初始化,并且导入数据
#mysql -uroot -p'QianFeng@123' -D jspgou < jspgou.sql
#3、安装httpd,修改配置,让他支持php
[root@memcached ~]# yum install -y httpd
#修改下列配置,支持php
DirectoryIndex index.html index.php
[root@memcached ~]# systemctl start httpd
Two, cache data test
[root@memcached html]# vim /var/www/html/test.php
<?php
$memcachehost = '127.0.0.1'; # 配置memcached
$memcacheport = 11211;
$memcachelife = 60;
$memcache = new Memcache;
$memcache->connect($memcachehost,$memcacheport) or die ("Could not connect");
$query="select Id,name from jc_address;";
$key=md5($query);
if(!$memcache->get($key))
{
$conn=mysqli_connect("localhost","root","QianFeng@123","jspgou"); # 配置mysql
$result=mysqli_query($conn,$query);
while ($row=mysqli_fetch_assoc($result))
{
$arr[]=$row;
}
$f = 'mysql';
$memcache->add($key,serialize($arr),0,30);
$data = $arr ;
}
else{
$f = 'memcache';
$data_mem=$memcache->get($key);
$data = unserialize($data_mem);
}
echo $f;
echo "<br>";
echo "$key";
echo "<br>";
foreach($data as $a)
{
echo "key is <b><font color=#FF0000>$a[Id]</font></b>";
echo "<br>";
echo "value is <b><font color=#FF0000>$a[name]</font></b>";
echo "<br>";
}
?>
Three, browser access test
The first visit is to read data from mysql and cache a copy in memcached. The
browser refreshes and displays the data read from memcached.
Supplement: Solve the problem of Chinese garbled characters
[root@memcached html]# vim /etc/my.cnf
[mysqld]
character-set-client-handshake = FALSE
character-set-server = utf8mb4
collation-server = utf8mb4_unicode_ci
init_connect=’SET NAMES utf8mb4’
[root@memcached html]# systemctl restart mysqld