目录
7.7 在数据库节点**(192.168.150.16)上创建测试数据库**
1.什么是缓存
缓存是指可以进行高速数据交换的存储器,它先于内存与CPU交换数据,因此速率很快。
从性能分析: CPU缓存>内存>磁盘>数据库
从性能看:内存是介于CPU和磁盘,在实际中内存是CPU和磁盘的桥梁。buffer和cache是内存的不同的体现。
2.buffer和cache
2.1buffer
buffer就是写入磁盘,buffer是为了提高内存和硬盘(或其他I/O设备)之间的数据交换的速度而设计的,解决速度慢和快的交接问题。速度快的需要通过缓冲区将数据一点一点传给速度慢的区域。例如:从内存中将数据往硬盘中写入,并不是直接写入,而是缓冲到一定大小之后刷入硬盘中。
2.2 cache
cache就是从磁盘读取数据然后存起来方便以后使用。cache实现数据的重复使用,速度慢的设备
2.3 buffer和cache的特点
- buffer是要写入数据;cache是已读取数据。
- buffer数据丢失会影响数据完整性,源数据不受影响;cache数据丢失不会影响数据完整性,但会影响性能
2.4 linux如何查看内存的缓存
2.4.1 cat /proc/meminfo
[root@mysql ~]# cat /proc/meminfo
MemTotal: 2027868 kB
MemFree: 1314988 kB
MemAvailable: 1354984 kB
Buffers: 2108 kB
Cached: 170820 kB
2.4.2 free
[root@mysql ~]# free -h
total used free shared buff/cache available
Mem: 1.9G 510M 1.3G 9.5M 185M 1.3G
Swap: 2.0G 0B 2.0G
2.4.3 top
[root@mysql ~]# top
top - 08:34:43 up 2 min, 1 user, load average: 0.03, 0.05, 0.03
Tasks: 97 total, 2 running, 95 sleeping, 0 stopped, 0 zombie
%Cpu(s): 0.0 us, 0.0 sy, 0.0 ni,100.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
KiB Mem : 2027868 total, 1314316 free, 523144 used, 190408 buff/cache
KiB Swap: 2097148 total, 2097148 free, 0 used. 1354696 avail Mem
Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载。它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提高动态、数据库驱动网站的速度。Memcached基于一个存储键/值对的hashmap。其守护进程(daemon )是用C写的,但是客户端可以用任何语言来编写,并通过memcached协议与守护进程通信。
3.1 memcache和memcached
3.2 为什么使用memcached
3.2.1 对数据库的高并发读写
3.2.2 对海量数据的处理
4. memcached的主要原理及其架构
5. memcached安装
yum install gcc gcc-c++ libevent-devel
yum install memcached -y
systemctl start memcached
6. memcached的特性
[root@slave ~]# telnet localhost 11211
Trying ::1...
Connected to localhost.
Escape character is '^]'.
set foo 0 0 3
bar
STORED
get foo
VALUE foo 0 3
bar
END
6.1 memcached 的set 命令
set key flags exptime bytes [noreply]value
6.2 memcached 的get命令
Memcached get 命令获取存储在 key(键) 中的 value(数据值) ,如果 key 不存在,则返回空。
基本语法:
get key
get key1 key2 key3 ....
7. 应用示例配置
7.1 应用示例配置
7.2 基础环境配置
关闭防火墙
systemctl stop firewalld
systemctl disable firewalld
sed -i 's/enforcing/disabled/' /etc/selinux/config
setenforce 0
yum -y install ntp ntpdate
ntpdate cn.pool.ntp.org
hwclock --systohc
ip地址 | 环境 |
192.168.150.16 | mysql |
192.168.150.17 | web |
192.168.150.18 | memcached |
7.3 web服务器配置
在web服务器192.168.150.17上安装软件,启动服务
卸载系统默认的mysql版本
rpm -e mariadb-libs postfix
安装数据库软件
安装ln\amp环境
yum install httpd php php-gb php-mysql php-memcache -y
systemctl restart httpd
systemctl enable httpd
7.4 mysql数据库服务器配置
在mysql服务器192.168.150.16创建用户
create user 'memcache'@'%' identified by 'Mem@1234'
alter user 'memcache'@'%' identified with mysql_native_password by 'Mem@1234'
flush privileges
7.5 web服务器测试

[root@web html]# cat index.php
<?php
phpinfo();
?>
7.6 mysql服务器测试
[root@web html]# cat mysql.php
<?php
$link=mysql_connect('192.168.150.16','memcache','Zcf@201104');
if($link)
echo "<h1>Success!!<h1>";
else
echo "Fail!!";
mysql_close();
?>
通过测试,可见MySQL数据库是可以正常链接的。
7.6 memcached 服务器配置及测试
[root@slave bin]# ./memcached -d -l 0.0.0.0 -p 11211 -u root -m 64 -c 1024 -P /var/run/memcached.pid
[root@slave bin]# ps -ef | grep memcache
memcach+ 1193 1 0 08:55 ? 00:00:00 /usr/bin/memcached -u memcached -p 11211 -m 64 -c 1024
root 1564 1504 0 10:11 pts/1 00:00:00 grep --color=auto memcache
[root@web html]# cat memcache.php
<?php
$memcache = new Memcache;
$memcache->connect('192.168.150.18', 11211) or die ("Could not connect");
$version = $memcache->getVersion();
echo "Server's version: ".$version."<br/>";
$tmp_object = new stdClass;
$tmp_object->str_attr = 'test';
$tmp_object->int_attr = 123;
$memcache->set('key', $tmp_object, false, 10) or die ("Failed to save data at the server");
echo "Store data in the cache (data will expire in 10 seconds)<br/>";
$get_result = $memcache->get('key');
echo "Data from the cache:<br/>";
var_dump($get_result);
?>

配置session(web端192.168.42.30):
session.save_handler = memcachesession.save_path ="tcp://192.168.42.29:11211?persistent=1&weight=1&timeout=1&retry_interval=15"
7.7 在数据库节点**(192.168.150.16)上创建测试数据库**
create database testab;
use testab;
create table test(id int not null auto_increment,name varchar(20) default null,primary key(id))engine=innodb auto_increment=1 default charset=utf8;
insert into test(name) values ('tom1'),('tom2'),('tom3'),('tom4'),('tom5');
mysql> select * from test;
+----+------+
| id | name |
+----+------+
| 1 | tom1 |
| 2 | tom2 |
| 3 | tom3 |
| 4 | tom4 |
| 5 | tom5 |
+----+------+
5 rows in set (0.00 sec)
grant select on testab.* to memcache@'%' ;
查看结果如下
mysql> show grants for memcache@'%';
+----------------------------------------------+
| Grants for memcache@% |
+----------------------------------------------+
| GRANT USAGE ON *.* TO `memcache`@`%` |
| GRANT SELECT ON `testab`.* TO `memcache`@`%` |
+----------------------------------------------+
2 rows in set (0.00 sec)
7.8 在web服务器上配置一下文件
[root@web html]# cat memcache1.php
<?php
$memcachehost = '192.168.150.18';
$memcacheport = 11211;
$memcachelife = 60;
$memcache = new Memcache;
$memcache->connect($memcachehost,$memcacheport) or die ("Could not connect");
$query="select * from test limit 10";
$key=md5($query);
if(!$memcache->get($key))
{
$conn=mysql_connect("192.168.150.16","memcache","Zcf@201104");
mysql_select_db(testab);
$result=mysql_query($query);
while ($row=mysql_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/>";
//print_r($data);
foreach($data as $a)
{
echo "number is $a[id]";
echo "<br/>";
echo "name is $a[name]";
echo "<br/>";
}
?>

