缓存数据库Memcached——安装及管理数据库操作

一、Memcached 简介:

(1)介绍:

Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载。它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提高动态、数据库驱动网站的速度。Memcached基于一个存储键/值对的hashmap。其守护进程(daemon )是用C写的,但是客户端可以用任何语言来编写,并通过memcached协议与守护进程通信。

(2)特点:

memcached作为高速运行的分布式缓存服务器,具有以下的特点:

1、协议简单;
2、基于libevent的事件处理;
3、内置内存存储方式;
4、memcached不互相通信的分布式。

(3)存储方式:

为了提高性能,memcached中保存的数据都存储在memcached内置的内存存储空间中。由于数据仅存在于内存中,因此重启memcached、重启操作系统会导致全部数据消失。另外,内容容量达到指定值之后,就基于LRU(Least Recently Used)算法自动删除不使用的缓存。memcached本身是为缓存而设计的服务器,因此并没有过多考虑数据的永久性问题。

二、memcached数据库配置:

1、准备安装所需源码包

[[email protected] ~]# mkdir /mnt/tools
[[email protected] ~]# mount.cifs //192.168.100.100/tools /mnt/tools/   ##挂载
Password for [email protected]//192.168.100.100/tools:
[[email protected] ~]# cd /mnt/tools/memcached/
[[email protected] 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
[[email protected] memcached]#
[[email protected] memcached]# tar xf libevent-2.1.8-stable.tar.gz -C /opt/   ##解压
[[email protected] memcached]# tar xf memcached-1.5.6.tar.gz -C /opt/   ##解压
[[email protected] memcached]# cd /opt/
[[email protected] opt]# ls
libevent-2.1.8-stable  memcached-1.5.6  rh
[[email protected] opt]#

2、编译安装memcached

[[email protected] opt]# yum install gcc gcc-c++ make -y   ##安装依赖环境包
.............//省略过程
[[email protected] libevent-2.1.8-stable]# ./configure --prefix=/usr/local/libevent   ##配置libevent
.............//省略过程
[[email protected] libevent-2.1.8-stable]# make && make install   ##编译安装libevent
.............//省略过程
[[email protected] libevent-2.1.8-stable]# cd ../memcached-1.5.6/
[[email protected] memcached-1.5.6]# ./configure \   ##配置memcached
> --prefix=/usr/local/memcached > --with-libevent=/usr/local/libevent/
.............//省略过程
[[email protected] memcached-1.5.6]# make && make install   ##编译安装memcached
.............//省略过程
[[email protected] memcached-1.5.6]# ln -s /usr/local/memcached/bin/* /usr/local/bin   ##便于系统识别
[[email protected] memcached-1.5.6]# 

3、开启memcached

[[email protected] memcached-1.5.6]# memcached -d -m 32m -p 11211 -u root
##开启数据库服务,-d守护进程 ;-m缓存大小32M ;-p端口11211
[[email protected] memcached-1.5.6]#
[[email protected] memcached-1.5.6]# netstat -ntap | grep memcached   ##查看端口
tcp        0      0 0.0.0.0:11211           0.0.0.0:*               LISTEN      22448/memcached
tcp6       0      0 :::11211                :::*                    LISTEN      22448/memcached
[[email protected] memcached-1.5.6]#
[[email protected] memcached-1.5.6]# systemctl stop firewalld.service    ##关闭防火墙
[[email protected] memcached-1.5.6]# setenforce 0   ##关闭增强型安全功能
[[email protected] memcached-1.5.6]#

4、memcached数据库基础操作

[[email protected] memcached-1.5.6]# yum install -y telnet   ##安装Telnet服务
.............//省略过程
[[email protected] memcached-1.5.6]# telnet 127.0.0.1 11211   ##连接memcached数据库
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is ‘^]‘.
add username 0 0 7
添加数据(两个0表示:不进行压缩和序列化标识,数据过期时间为永不过期;标识号是7就需要输入7位数。)
1234567   ##输入7位的值
STORED  ##添加成功
add users 0 0 7
123   ##输入错误的值

ERROR   ##添加失败
get username
VALUE username 0 7
1234567
END
gets username     ##查询数据
VALUE username 0 7 1
1234567
END
set username 0 0 8     ##更新信息,若键名不存在,则自行添加
12345678
STORED
gets username        ##查询数据
VALUE username 0 8 3
12345678
END
replace school 0 0 2     ##更新信息,若键名不存在,则报错
un
NOT_STORED
get shcool        ##查询数据
END
replace username 0 0 9     ##更新信息,若键名不存在,则报错
123456789
STORED
gets username        ##查询数据
VALUE username 0 9 4
123456789
END
set school 0 0 4     ##更新信息,若键名不存在,则自行添加
1234
STORED
gets school        ##查询数据
VALUE school 0 4 5
1234
END
cas school 0 0 7 5    ##修改键的存储位数
logging
STORED
gets school
VALUE school 0 7 6
logging
END
cas school 0 0 8 2
loggings
EXISTS
append school 0 0 4   ##键值后追加数据
book
STORED
gets school
VALUE school 0 11 7
loggingbook
END
prepend school 0 0 2    ##键值前追加数据
un
STORED
gets school
VALUE school 0 13 8
unloggingbook
END
delete school    ##清除指定的键值数据,清除所有缓存数据为flush_all
DELETED
get school
END
quit     ##退出
Connection closed by foreign host.
[[email protected] memcached-1.5.6]# 

##查看命令:
stats                                ##显示状态信息
stats items                      ##返回所有键值对的统计信息
stats cachedump 1 0      ##返回指定存储空间的键值对
stats slabs                      ##显示各个slab的信息
stats sizes                      ##输出所有item的大小和个数
stats reset                      ##清空统计数据

三、客户端配置(部署LAMP架构)

LAMP架构之前的博客写过,想看详细解释的可以,看一下。下面就不做解释了。
LNMP架构

1、部署LAMP架构

[[email protected] ~]# mkdir /mnt/tools
[[email protected] ~]# mount.cifs //192.168.100.100/tools /mnt/tools/
Password for [email protected]//192.168.100.100/tools:
[[email protected] ~]# cd /mnt/tools/memcached/
[[email protected] 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
[[email protected] 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
[[email protected] LAMP-php5.6]# 

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

[[email protected] httpd-2.4.29]# ./configure > --prefix=/usr/local/httpd > --enable-so > --enable-rewrite > --enable-charset-lite > --enable-cgi

[[email protected] httpd-2.4.29]# make && make install

[[email protected] httpd-2.4.29]# cp /usr/local/httpd/bin/apachectl /etc/init.d/httpd
[[email protected] httpd-2.4.29]# vim /etc/init.d/httpd
# chkconfig: 35 85 21  //35级别自动运行  第85个启动 第21个关闭
# description: Apache is a World Wide Web server
[[email protected] httpd-2.4.29]# chkconfig --add httpd
[[email protected] httpd-2.4.29]#
[[email protected] httpd-2.4.29]# vim /usr/local/httpd/conf/httpd.conf
ServerName www.yun.com:80
Listen 192.168.52.132:80
#Listen 80
[[email protected] httpd-2.4.29]# ln -s /usr/local/httpd/conf/httpd.conf /etc/
[[email protected] httpd-2.4.29]# ln -s /usr/local/httpd/bin/* /usr/local/bin/
[[email protected] httpd-2.4.29]# apachectl -t
Syntax OK
[[email protected] httpd-2.4.29]#
[[email protected] httpd-2.4.29]# service httpd start
[[email protected] httpd-2.4.29]# netstat -ntap | grep 80
tcp        0      0 192.168.52.132:80       0.0.0.0:*               LISTEN      88064/httpd
[[email protected] httpd-2.4.29]#
[[email protected] httpd-2.4.29]# cd /mnt/tools/memcached/LAMP-php5.6/
[[email protected] 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
[[email protected] LAMP-php5.6]# tar xf mysql-5.6.26.tar.gz -C /opt/
[[email protected] LAMP-php5.6]# cd /opt/
[[email protected] opt]# ls
httpd-2.4.29  mysql-5.6.26  rh
[[email protected] opt]# cd mysql-5.6.26/
[[email protected] mysql-5.6.26]# yum install -y ncurses-devel autoconf cmake
[[email protected] 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

[[email protected] mysql-5.6.26]# make && make install

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

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

[[email protected] mysql-5.6.26]# service mysqld start
Starting MySQL. SUCCESS!
[[email protected] mysql-5.6.26]# netstat -ntap | grep 3306
tcp6       0      0 :::3306                 :::*                    LISTEN      103429/mysqld
[[email protected] mysql-5.6.26]#
[[email protected] mysql-5.6.26]# mysqladmin -u root -p password "abc123"
Enter password:
Warning: Using a password on the command line interface can be insecure.
[[email protected] 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
[[email protected] mysql-5.6.26]# 

[[email protected] mysql-5.6.26]# cd /mnt/tools/memcached/LAMP-php5.6/
[[email protected] 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
[[email protected] LAMP-php5.6]# tar xf php-5.6.11.tar.bz2 -C /opt/
[[email protected] LAMP-php5.6]# cd /opt/
[[email protected] opt]# ls
httpd-2.4.29  mysql-5.6.26  php-5.6.11  rh
[[email protected] opt]# cd php-5.6.11/
[[email protected] php-5.6.11]# yum -y install > gd > libpng > libpng-devel > pcre > pcre-devel > libxml2-devel > libjpeg-devel
[[email protected] 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
[[email protected] php-5.6.11]# make && make install

[[email protected] php-5.6.11]# cp php.ini-development /usr/local/php5/php.ini
[[email protected] php-5.6.11]# ln -s /usr/local/php5/bin/* /usr/local/bin/
[[email protected] php-5.6.11]# ln -s /usr/local/php5/sbin/* /usr/local/sbin/
[[email protected] php-5.6.11]#
[[email protected] 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>

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

2、测试访问,LAMP架构搭建成功

3、修改index.php文件

[[email protected] 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、测试数据库工作是否正常

[[email protected] php-5.6.11]# mysql -u root -pabc123   ##进入数据库
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;    ##创建一个数据库为 sky
Query OK, 1 row affected (0.00 sec)

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

mysql> flush privileges;    ##刷新权限
Query OK, 0 rows affected (0.01 sec)

mysql> 

5、再次访问网页,显示success表示成功

6、安装 Memcache 客户端

[[email protected] php-5.6.11]# cd /mnt/tools/memcached/
[[email protected] 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
[[email protected] memcached]# tar xf memcache-2.2.7.tgz -C /opt/   ##解压
[[email protected] memcached]# cd /opt/
[[email protected] opt]# ls
httpd-2.4.29  memcache-2.2.7  mysql-5.6.26  package.xml  php-5.6.11  rh
[[email protected] opt]#
[[email protected] opt]# cd memcache-2.2.7/
[[email protected] 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
[[email protected] memcache-2.2.7]# 

[[email protected] memcache-2.2.7]# ./configure \   ##配置
> --enable-memcache > --with-php-config=/usr/local/php5/bin/php-config 

[[email protected] memcache-2.2.7]# make && make install   ##编译安装

7、配置 PHP,添加 Memcached 组件

[[email protected] 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、修改index.php文件,再重启httpd服务

[[email protected] 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;
?>
[[email protected] memcache-2.2.7]# service httpd stop
[[email protected] memcache-2.2.7]# service httpd start
[[email protected] memcache-2.2.7]# 

9、客户端检测服务端是否可以正常连接

原文地址:https://blog.51cto.com/14449541/2461114

时间: 2024-08-11 04:36:06

缓存数据库Memcached——安装及管理数据库操作的相关文章

Memcached 安装及管理数据库操作

一.Memcached 简介: (1)介绍:Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载.它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提高动态.数据库驱动网站的速度.Memcached基于一个存储键/值对的hashmap.其守护进程(daemon )是用C写的,但是客户端可以用任何语言来编写,并通过memcached协议与守护进程通信.(2)特点:memcached作为高速运行的分布式缓存服务器,具有以下的特点:1.协议简单:2.基于l

如何利用运维面板进行数据库一键安装和管理?

数据库的管理维护工具非常多,除了系统自带的命令行管理工具之外,还有许多其他的管理工具,MySQL是一个非常流行的小型关系型数据库管理系统,2008年1月16号被Sun公司收购.目前MySQL被广泛地应用在Internet上的中小型 网站中.由于其体积小.速度快.总体拥有成本低,尤其是开放源码这一特点,许多中小型网站为了降低网站总体拥有成本而选择了MySQL作为网站数据库. 接下来我为大家详细介绍如何通过运维面板云帮手来一键安装数据库以及管理数据库,包括数据库创建.修改.权限设置.备份和恢复. 一

超实用的数据库一键安装和管理教程来了

数据库的管理维护工具非常多,除了系统自带的命令行管理工具之外,还有许多其他的管理工具,MySQL 是一个非常流行的小型关系型数据库管理系统,2008年1月16号被 Sun 公司收购.目前 MySQL 被广泛地应用在 Internet 上的中小型 网站中.由于其体积小.速度快.总体拥有成本低,尤其是开放源码这一特点,许多中小型网站为了降低网站总体拥有成本而选择了 MySQL 作为网站数据库. 接下来我为大家详细介绍如何通过运维面板云帮手来一键安装数据库以及管理数据库,包括数据库创建.修改.权限设置

ubuntu 16.04 数据库mysql安装与管理

1.安装mysql的客户端与服务器端 $>sudo apt-get install mysql-server mysql-client 2.管理服务 1.启动 $>sudo service mysql start 2.停止 $>sudo service mysql stop 3.重启 $>sudo service mysql restart 3.配置允许远程连接 1.找到mysql配置文件并修改 $>sudo vim /etc/mysql/mysql.conf.d/mysql

SQL Server 【附】创建&quot;商品管理数据库&quot;、&quot;学生选课数据库&quot;的SQL语句

附:(创建“商品管理数据库”的SQL语句) --建立"商品管理数据库"数据库-- create database 商品管理数据库 on(name='商品管理数据库_m', filename='D:\商品管理系统\商品管理数据库_m.mdf', size=6mb,filegrowth=1mb,maxsize=unlimited) log on(name='商品管理数据库_l', filename='D:\商品管理系统\商品管理数据库_l.ldf', size=1,filegrowth=1

数据库(SQL Server)管理数据库表~新奇之处

说到“数据库”,我总有一种莫名的感觉,在刚刚接触到的数据库中就让我似懂非懂渡过着,于是思考着.于是在冷静的时空中让我回想到了很多的知识,不知你们是怎样过来的,真心希望我的这篇数据库总结能够让我们都有一个更进步的空间. 就像标题中所说的:“数据库之创建与管理数据库表”,或许说不上新颖之作,但我相信应该有能帮大家解决在学习中运到的一些问题,当然谁都不会是完美,都会有不一样的感触,所以也想借此来和大家多多的交流并分享学习数据库时的郁闷与愉快. 那好,就让我们进入我总结”数据库“时空之境吧!!! 如果说

关于数据库管理系统DBMS--关系型数据库(MySQL/MariaDB)

数据库管理系统--DBMS:用来管理数据库 数据库的结构(3种):层次,网状,关系型(用的最多): DBMS的三层模型: 视图层:面向最终用户: 逻辑层:面向程序员或DBA: 物理层:面向系统管理员: 关系型数据库管理系统--RDBMS: 主要的组成部分是表:表是由行(实例,实体,记录)和列(字段,域)组成: 关系型数据库管理系统的实现: 商业方案:Oracle,Sybase{为微软提供了思路出现SQL-server},Infomix{IBM收购},DB2{IBM} 开源方案:PostgreSQ

Oracle数据库之创建和删除数据库

创建数据库 1 使用Database Configuration  Assistant工具创建Oracle数据库 步骤一 操作窗口 有4种选择  A 创建数据库 B 配置数据库选件 C 删除数据库 D 管理模板 步骤二 数据库模板 窗口 有3种选择  A 一般用途或事务处理 B 定制数据库 C 数据仓库 步骤三 数据库标识 在这一步中,需要输入 全局数据库名 和 Oracle 系统标识符(SID) 全局数据名是Oracle 数据库的唯一标识,所以不能与已有的数据库重名 打开oracle数据库时,

高性能内存对象缓存Memcached安装及数据库操作与管理

认识Memcached Memcached是一套开源的高性能分布式内存对象缓存系统,它将所有的数据都存储在内存中,因为在内存中会统一维护一张巨大的Hash表,所以支持任意存储类型的数据.很多网站通过使用Memcached提高网站的访问速度,尤其是对于大型的需要频繁访问数据的网站.Memcached是典型的C/S架构,因此需要安装Memcached服务端与MemcachedAPI客户端.Memcached服务端是用C语言编写的,而Memcached API客户端可以用任何语言来编写,如PHP.Py