溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

緩存數(shù)據(jù)庫Memcached——安裝及管理數(shù)據(jù)庫操作

發(fā)布時(shí)間:2020-02-20 20:48:05 來源:網(wǎng)絡(luò) 閱讀:322 作者:wx5d2c2d660c282 欄目:關(guān)系型數(shù)據(jù)庫

一、Memcached 簡介:

(1)介紹:

Memcached 是一個(gè)高性能的分布式內(nèi)存對象緩存系統(tǒng),用于動(dòng)態(tài)Web應(yīng)用以減輕數(shù)據(jù)庫負(fù)載。它通過在內(nèi)存中緩存數(shù)據(jù)和對象來減少讀取數(shù)據(jù)庫的次數(shù),從而提高動(dòng)態(tài)、數(shù)據(jù)庫驅(qū)動(dòng)網(wǎng)站的速度。Memcached基于一個(gè)存儲(chǔ)鍵/值對的hashmap。其守護(hù)進(jìn)程(daemon )是用C寫的,但是客戶端可以用任何語言來編寫,并通過memcached協(xié)議與守護(hù)進(jìn)程通信。

(2)特點(diǎn):

memcached作為高速運(yùn)行的分布式緩存服務(wù)器,具有以下的特點(diǎn):

1、協(xié)議簡單;
2、基于libevent的事件處理;
3、內(nèi)置內(nèi)存存儲(chǔ)方式;
4、memcached不互相通信的分布式。

(3)存儲(chǔ)方式:

為了提高性能,memcached中保存的數(shù)據(jù)都存儲(chǔ)在memcached內(nèi)置的內(nèi)存存儲(chǔ)空間中。由于數(shù)據(jù)僅存在于內(nèi)存中,因此重啟memcached、重啟操作系統(tǒng)會(huì)導(dǎo)致全部數(shù)據(jù)消失。另外,內(nèi)容容量達(dá)到指定值之后,就基于LRU(Least Recently Used)算法自動(dòng)刪除不使用的緩存。memcached本身是為緩存而設(shè)計(jì)的服務(wù)器,因此并沒有過多考慮數(shù)據(jù)的永久性問題。

二、memcached數(shù)據(jù)庫配置:

1、準(zhǔn)備安裝所需源碼包

[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                   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/   ##解壓
[root@localhost memcached]# tar xf memcached-1.5.6.tar.gz -C /opt/   ##解壓
[root@localhost memcached]# cd /opt/
[root@localhost opt]# ls
libevent-2.1.8-stable  memcached-1.5.6  rh
[root@localhost opt]#

2、編譯安裝memcached

[root@localhost opt]# yum install gcc gcc-c++ make -y   ##安裝依賴環(huán)境包
.............//省略過程
[root@localhost libevent-2.1.8-stable]# ./configure --prefix=/usr/local/libevent   ##配置libevent
.............//省略過程
[root@localhost libevent-2.1.8-stable]# make && make install   ##編譯安裝libevent
.............//省略過程
[root@localhost libevent-2.1.8-stable]# cd ../memcached-1.5.6/
[root@localhost memcached-1.5.6]# ./configure \   ##配置memcached
> --prefix=/usr/local/memcached \
> --with-libevent=/usr/local/libevent/
.............//省略過程
[root@localhost memcached-1.5.6]# make && make install   ##編譯安裝memcached
.............//省略過程
[root@localhost memcached-1.5.6]# ln -s /usr/local/memcached/bin/* /usr/local/bin   ##便于系統(tǒng)識別
[root@localhost memcached-1.5.6]# 

3、開啟memcached

[root@localhost memcached-1.5.6]# memcached -d -m 32m -p 11211 -u root
##開啟數(shù)據(jù)庫服務(wù),-d守護(hù)進(jìn)程 ;-m緩存大小32M ;-p端口11211
[root@localhost memcached-1.5.6]#
[root@localhost 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     
[root@localhost memcached-1.5.6]# 
[root@localhost memcached-1.5.6]# systemctl stop firewalld.service    ##關(guān)閉防火墻
[root@localhost memcached-1.5.6]# setenforce 0   ##關(guān)閉增強(qiáng)型安全功能
[root@localhost memcached-1.5.6]#

4、memcached數(shù)據(jù)庫基礎(chǔ)操作

[root@localhost memcached-1.5.6]# yum install -y telnet   ##安裝Telnet服務(wù)
.............//省略過程
[root@localhost memcached-1.5.6]# telnet 127.0.0.1 11211   ##連接memcached數(shù)據(jù)庫
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
add username 0 0 7
添加數(shù)據(jù)(兩個(gè)0表示:不進(jìn)行壓縮和序列化標(biāo)識,數(shù)據(jù)過期時(shí)間為永不過期;標(biāo)識號是7就需要輸入7位數(shù)。)
1234567   ##輸入7位的值
STORED  ##添加成功
add users 0 0 7
123   ##輸入錯(cuò)誤的值

ERROR   ##添加失敗
get username      
VALUE username 0 7
1234567
END
gets username     ##查詢數(shù)據(jù)
VALUE username 0 7 1
1234567
END
set username 0 0 8     ##更新信息,若鍵名不存在,則自行添加
12345678
STORED
gets username        ##查詢數(shù)據(jù)
VALUE username 0 8 3
12345678
END
replace school 0 0 2     ##更新信息,若鍵名不存在,則報(bào)錯(cuò)
un 
NOT_STORED
get shcool        ##查詢數(shù)據(jù)
END
replace username 0 0 9     ##更新信息,若鍵名不存在,則報(bào)錯(cuò)
123456789
STORED
gets username        ##查詢數(shù)據(jù)
VALUE username 0 9 4
123456789
END
set school 0 0 4     ##更新信息,若鍵名不存在,則自行添加
1234
STORED
gets school        ##查詢數(shù)據(jù)
VALUE school 0 4 5
1234
END
cas school 0 0 7 5    ##修改鍵的存儲(chǔ)位數(shù)
logging
STORED
gets school   
VALUE school 0 7 6
logging
END
cas school 0 0 8 2
loggings
EXISTS
append school 0 0 4   ##鍵值后追加數(shù)據(jù)
book
STORED
gets school
VALUE school 0 11 7
loggingbook
END
prepend school 0 0 2    ##鍵值前追加數(shù)據(jù)
un
STORED
gets school
VALUE school 0 13 8
unloggingbook
END
delete school    ##清除指定的鍵值數(shù)據(jù),清除所有緩存數(shù)據(jù)為flush_all
DELETED
get school
END
quit     ##退出
Connection closed by foreign host.
[root@localhost memcached-1.5.6]# 

##查看命令:
stats                                ##顯示狀態(tài)信息
stats items                      ##返回所有鍵值對的統(tǒng)計(jì)信息
stats cachedump 1 0      ##返回指定存儲(chǔ)空間的鍵值對 
stats slabs                      ##顯示各個(gè)slab的信息
stats sizes                      ##輸出所有item的大小和個(gè)數(shù)
stats reset                      ##清空統(tǒng)計(jì)數(shù)據(jù)

三、客戶端配置(部署LAMP架構(gòu))

LAMP架構(gòu)之前的博客寫過,想看詳細(xì)解釋的可以,看一下。下面就不做解釋了。
LNMP架構(gòu)

1、部署LAMP架構(gòu)

[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級別自動(dòng)運(yùn)行  第85個(gè)啟動(dòng) 第21個(gè)關(guān)閉
# 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:是否覆蓋"/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、測試訪問,LAMP架構(gòu)搭建成功

緩存數(shù)據(jù)庫Memcached——安裝及管理數(shù)據(jù)庫操作

3、修改index.php文件

[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 "<h2>Success!!</h2>";
else echo "Fail!!";
mysql_close();
?>

4、測試數(shù)據(jù)庫工作是否正常

[root@localhost php-5.6.11]# mysql -u root -pabc123   ##進(jìn)入數(shù)據(jù)庫
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;    ##創(chuàng)建一個(gè)數(shù)據(jù)庫為 sky
Query OK, 1 row affected (0.00 sec)

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

mysql> flush privileges;    ##刷新權(quán)限
Query OK, 0 rows affected (0.01 sec)

mysql> 

5、再次訪問網(wǎng)頁,顯示success表示成功

緩存數(shù)據(jù)庫Memcached——安裝及管理數(shù)據(jù)庫操作

6、安裝 Memcache 客戶端

[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/   ##解壓
[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 \   ##配置
> --enable-memcache \
> --with-php-config=/usr/local/php5/bin/php-config 

[root@localhost memcache-2.2.7]# make && make install   ##編譯安裝

7、配置 PHP,添加 Memcached 組件

[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、修改index.php文件,再重啟httpd服務(wù)

[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、客戶端檢測服務(wù)端是否可以正常連接

緩存數(shù)據(jù)庫Memcached——安裝及管理數(shù)據(jù)庫操作

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI