Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Memcache service building

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

Memcache

The function of Memcache is well described on the Internet. To put it simply, it reduces the pressure of reading the database, and the principle is also very simple:

The requested data will be picked up in memcache first, if not, in the database, and bring a copy to memcache by the way.

Each time you update the data, update the data in memcache first, if not, update the database, and update memcache at the same time.

Therefore, it is important to note that this data is easily lost.

Mode and Port

Memcache is a structure based on Cramp S:

Server side: using Memcached software

Client: use the Memcache plug-in (this plug-in is combined with back-end languages such as php python java)

Service port: 11211 (changeable)

Software inventory:

Libevent dependency library http://www.libevent.org/

Memcache plug-in http://pecl.php.net/package/memcache/

Memcached Service http://www.memcached.org/

Lamp environment yum-y install httpd php php-mysql mysql-server

Operating system CentOS-6.5 (x86x64)

1. The relevant software packages will be uploaded and the lamp environment will be installed

Yum-y install httpd php php-mysql mysql-server / etc/init.d/httpd start echo "" > / var/www/html/index.php

Then use the browser to access and view the php information, and you can't find memcache in the information.

two。 Install the libevent plug-in

Tar xf libevent-2.0.22-stable.tar.gz cd libevent-2.0.22-stable. / configure-- prefix=/usr/local/libevent & & make & & make install

3. Install the memcached server

Tar xf memcached-1.4.36.tar.gz cd memcached-1.4.36. / configure-- prefix=/usr/local/memcached-with-libevent=/usr/local/libevent/ make & & make install

After installation, memcached will be generated in the / usr/local/memcached/bin/ directory

4. Configure environment variables

Cd / etc/profile.d/ vim mem.sh export PATH= "/ usr/local/memcached/bin:$PATH" # write profile file boot automatically import memcached-m 32-p 11211-d-c 8192-u root # m split memory size p port d mixed mode c maximum number of connections netstat-anptu | grep memcached # check whether it is started Run multiple instances to change the port to free-m # you can see that there is less and less memory. Because ps-aux is assigned | grep memcached # check how much the process pid is kill-9 1234 # disable the memcached service pkill memcached # ditto

5.memcached usage

Yum-y install nc telnet

1) use the nc command to connect to memcache

Printf "set first 0305\ r\ nmmmmm\ r\ n" | nc 127.0.0.1 11211 # Storage data (fields are key, flag, validity, length, value) printf "get first\ r\ n" | nc 127.0.0.1 11211 # fetch data

2) use the telnet command to connect to memcache

Telnet 127.0.0.1 11211 # then you can use the relevant memcached command

6. Here are the operation commands related to memcached

Add key1 0 30 3 # add data 30 for validity period (if 0 means never expire) 3 is size

Set key1 030 3 # updates the data. It will be created automatically if it does not exist.

Replace key1 0303 # updates the data, no error will be reported.

Delete key1 # Delete data

Get key1 # get data

Gets key 1 # for more information

Stats setting # View configuration information

Stats slabs # View slab

Stats items # View item

Stats size # View size

7. Install the memcache client php plug-in

Install the phpize command to add a new module to php

If you don't know what package it is, you can use yum provides * / phpize

Yum-y install php-devel tar xf memcache-2.2.7.tgz cd memcache-2.2.7 phpize # type module, generate configure and other files which php-config # View php-config path location. / configure-- enable-memcache-- with-php-config=/usr/bin/php-config make & & make install

After the installation number, the module will be installed / usr/lib64/php/modules/memcache.so

Cd / etc/php.d/ cp mysql.ini memcache.ini # vim edits to set the value of extension to memcache.so

After restarting the service, you can see that php already supports the memcache module.

8. Later, you can test the database with the php website.

Tar xf memcache_page.tar.gz-C / var/www/html/ cd! $

The test page is edited by mysql_connect.php

Therefore, you need to set up the users of mysql first.

/ etc/init.d/mysqld start mysql_secure_installation

Or be in the database by yourself.

Grant all on *. * to 'root'@'127.0.0.1' identified by' 123456 'flush privileges

Then the browser accesses mysql_connect.php

Docking is successful

Here you can read read.php and write.php to understand the reading and writing principles of memcache.

Read.php

Write.php

For simple use of php memcache, see http://www.cnblogs.com/demonxian3/p/6868361.html

You can see in the above two php that the db1 and table T1 of the database are called, so you need to create

Seq 1999 > / tmp/sum # create test data from 1 to 999

Connect to the database to import data

Create database db1; create T1 (id int) engine=innodb; load data infile'/ tmp/sum' into table T1; # Import test data

Popular science: use history to view history command, enter! + number to execute the numbered command

! one hundred and eleven

Use the browser to access the test page

The test reads the data and queries the database for the value of id 5

Step back and get it again.

Test write data

Finally, I recommend a very useful memcache management tool: written by memadmin php.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report