1. 程式人生 > >第二十一章 NoSQL(上)

第二十一章 NoSQL(上)

21.1 NoSQL介紹

21.2 memcached介紹

資料結構簡單(k-v),資料存放在記憶體裡。memcached 不支援持久化,資料落地。每當memcached服務重啟或者重啟伺服器,資料就會丟失。但可以定時來備份來讓資料落地

21.3 安裝memcached

使用yum下載安裝

yum -y install memcached

想下載最新版本可以去官網上下載原始碼包,由於memcached是基於libevent,因此,當你yum安裝memcached時就已經安裝libevent

啟動memcached服務

systemctl start memcached.service

啟動引數
-u 指定執行memcached服務的使用者
-p 執行監聽埠
-m 指定記憶體大小 單位M
-c 指定最大併發數
啟動引數配置檔案在/etc/sysconfig/memcached

更改啟動引數配置檔案

vim /etc/sysconfig/memcached
PORT="11211"  ##埠
USER="memcached" ##使用者
MAXCONN="1024"  ##最大併發數
CACHESIZE="64"  ##分配記憶體大小
OPTIONS=""

21.4 檢視memcached狀態

使用memcached-tool工具來檢視memcached狀態

memcached-tool 127.0.0.1:11211 stats

#127.0.0.1:11211   Field       Value
         accepting_conns           1
               auth_cmds           0
             auth_errors           0
                   bytes           0
              bytes_read          14
           bytes_written        1024
              cas_badval           0
                cas_hits           0
              cas_misses           0
               cmd_flush           0
                 cmd_get           0
                 cmd_set           0
               cmd_touch           0
             conn_yields           0
   connection_structures          11
        curr_connections          10
              curr_items           0
               decr_hits           0
             decr_misses           0
             delete_hits           0
           delete_misses           0
       evicted_unfetched           0
               evictions           0
       expired_unfetched           0
                get_hits           0
              get_misses           0
              hash_bytes      524288
       hash_is_expanding           0
        hash_power_level          16
               incr_hits           0
             incr_misses           0
                libevent 2.0.21-stable
          limit_maxbytes    67108864
     listen_disabled_num           0
                     pid        1275
            pointer_size          64
               reclaimed           0
            reserved_fds          20
           rusage_system    0.039859
             rusage_user    0.028470
                 threads           4
                    time  1534201319
       total_connections          12
             total_items           0
              touch_hits           0
            touch_misses           0
                  uptime        1050
                 version      1.4.15

在生產環境中要留意 curr_items暫存的專案 cmd_get表示總的get次數,get_hits表示get的總命中次數,命中率 = get_hits/cmd_get

使用nc命令來檢視memcached狀態

yum -y install nc

echo stats |nc 127.0.0.1 11211

STAT pid 1275
STAT uptime 2058
STAT time 1534202327
STAT version 1.4.15
STAT libevent 2.0.21-stable
STAT pointer_size 64
STAT rusage_user 0.050910
STAT rusage_system 0.070002
STAT curr_connections 10
STAT total_connections 13
STAT connection_structures 11
STAT reserved_fds 20
STAT cmd_get 0
STAT cmd_set 0
STAT cmd_flush 0
STAT cmd_touch 0
STAT get_hits 0
STAT get_misses 0
STAT delete_misses 0
STAT delete_hits 0
STAT incr_misses 0
STAT incr_hits 0
STAT decr_misses 0
STAT decr_hits 0
STAT cas_misses 0
STAT cas_hits 0
STAT cas_badval 0
STAT touch_hits 0
STAT touch_misses 0
STAT auth_cmds 0
STAT auth_errors 0
STAT bytes_read 20
STAT bytes_written 2053
STAT limit_maxbytes 67108864
STAT accepting_conns 1
STAT listen_disabled_num 0
STAT threads 4
STAT conn_yields 0
STAT hash_power_level 16
STAT hash_bytes 524288
STAT hash_is_expanding 0
STAT bytes 0
STAT curr_items 0
STAT total_items 0
STAT expired_unfetched 0
STAT evicted_unfetched 0
STAT evictions 0
STAT reclaimed 0
END

使用memstats命令來檢視memcached狀態

先安裝 libmemcached

yum -y install libmemcached

 memstat --servers=127.0.0.1:11211

Server: 127.0.0.1 (11211)
     pid: 1275
     uptime: 2277
     time: 1534202546
     version: 1.4.15
     libevent: 2.0.21-stable
     pointer_size: 64
     rusage_user: 0.056080
     rusage_system: 0.077110
     curr_connections: 10
     total_connections: 14
     connection_structures: 11
     reserved_fds: 20
     cmd_get: 0
     cmd_set: 0
     cmd_flush: 0
     cmd_touch: 0
     get_hits: 0
     get_misses: 0
     delete_misses: 0
     delete_hits: 0
     incr_misses: 0
     incr_hits: 0
     decr_misses: 0
     decr_hits: 0
     cas_misses: 0
     cas_hits: 0
     cas_badval: 0
     touch_hits: 0
     touch_misses: 0
     auth_cmds: 0
     auth_errors: 0
     bytes_read: 37
     bytes_written: 3098
     limit_maxbytes: 67108864
     accepting_conns: 1
     listen_disabled_num: 0
     threads: 4
     conn_yields: 0
     hash_power_level: 16
     hash_bytes: 524288
     hash_is_expanding: 0
     bytes: 0
     curr_items: 0
     total_items: 0
     expired_unfetched: 0
     evicted_unfetched: 0
     evictions: 0
     reclaimed: 0

總結三種檢視memcache狀態命令

memcached-tool IP:埠 stats
echo stats |nc IP 埠
memstat --servers=IP:埠

21.5 memcached命令列

使用Telnet進入memcached

telnet 127.0.0.1 11211

退出Telnet按Ctrl+]再輸入quit撤銷按Ctrl+退格鍵

新增鍵

set key1 0 100 2 ## 0為標記 100為儲存時間 2為2位數abSTORED

獲得鍵對應的值

get key1  
VALUE key1 0 2
ab
END

替換值

replace key1 0 1000 3 ##replace和set一樣也要輸入<flags> <exptime> <bytes>
123
STORED

刪除鍵

delete key1
DELETED

21.6 memcached資料匯出和匯入

由於memcached在重啟服務或者重啟機器就會把資料丟掉,因此需要把資料進行備份。

使用memcached-tool命令把資料匯出

格式:memcached-tool IP:埠 dump > 備份檔案

vim data.txt 更改時間戳

檢視之前儲存的資料

通過時間戳找出對應的時間。因為我們裝置設定資料的過期時間為0,永不過期。但是匯出資料就會把建立資料的時間為過期時間,因此要把時間改一改,不然匯入資料就會出問題

21.7 php連線memcached

進入src目錄並下載解壓memcache

cd /usr/local/src/

wget http://www.apelearn.com/bbs/data/attachment/forum/memcache-2.2.3.tgz

tar zxvf memcache-2.2.3.tgz
進入memcache目錄下並生成configure檔案
cd memcache-2.2.3
/usr/local/php-fpm/bin/phpize

這裡是缺少autoconf導致的因此安裝autoconf再來phpize

yum install -y autoconf

/usr/local/php-fpm/bin/phpize用來生成configure檔案

初始化

./configure --with-php-config=/usr/local/php-fpm/bin/php-config
make && make install

檢視模組

 vim /usr/local/php/etc/php.ini

extension=memcache.so

測試

下載一個php的測試頁面看是否支援php來操作memcache

https://coding.net/u/aminglinux/p/yuanke_centos7/git/blob/master/21NOSQL/1.php

curl www.apelearn.com/study_v2/.memcache.txt > 1.php 2>/dev/null

21.8 memcached中儲存session

vim /usr/local/php-fpm/etc/php.ini

註釋掉 session.save_handler = files

它表示session以檔案的形式存在tmp目錄下

編輯php-fpm.conf對應的pool

php_value[session.save_handler] = memcache
php_value[session.save_path] = "tcp://192.168.93.128:11211"

測試

下載一個php測試頁面看memcache是否儲存了session
程式碼內容在https://coding.net/u/aminglinux/p/yuanke_centos7/git/blob/master/21NOSQL/session.php

wget http://study.lishiming.net/.mem_se.txt
cp .mem_se.txt /var/wwwroot/aming.com/index.php