MySQL uses triggers to automatically synchronize data to memcached

Compile and install

First make sure that the mysql you installed is a version above 5.1, and you need to install mysql-devel to
install memcached

First you have to install libevent, this will not be introduced.

First download memcached-1.4.5-2.el5.remi.x86_64.rpm and memcached-devel-1.4.5-2.el5.remi.x86_64.rpm (this is needed to install libmemcached)

installation execution

rpm -Uvh memcached-1.4.5-2.el5.remi.x86_64.rpm
memcached-devel-1.4.5-2.el5.remi.x86_64.rpm

If you compile it yourself, remember to set the installation directory so that subsequent installations of libraries that depend on it can be found.
Compile and install libmemcached

This is more tangled, because you have to choose a good version, and you may have to go to XX. Download address: https://launchpad.net/libmemcached/+download

Select version 0.34, otherwise it will report " servers.c:263:28: error: 'memcached_st' has no member named 'hosts' " or " ERROR 1126 (HY000) at line 38: Can't open shared library 'libmemcached_functions_mysql.so ' (errno: 0 /usr/local/mysql/lib/plugin/libmemcached_functions_mysql.so: undefined symbol: memcached_string_append) ", the latter error is reported when mysql executes sql.

Unzip and compile, the process is as follows

./configure –prefix=/usr/local

make && make install

Because the installation path is under /usr/local, you need to modify /etc/ld.so.conf, add /usr/local/lib, and execute /sbin/ldconfig

And you need to set pkg-confg (if you don't need to install it), otherwise it will report "No package 'libmemcached' found" error when compiling memcached_functions_mysql

implement

whereis pkgconfig

export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig:$
PKG_CONFIG_PATH
编译安装 memcached_functions_mysql

This also needs to choose the appropriate version. I chose version 1.0 here. I just used version 1.1 at the beginning, and there will be some problems with compilation. This version is officially available, other versions may have problems.

The compilation process is as follows

./configure –prefix=/usr/local
–with-mysql=/usr/bin/mysql_config

make && make install

Next, copy the series files of /usr/local/lib/libmemcached_functions_mysql* to /usr/lib64/mysql/plugin, and cp it directly, the soft connection relationship may be lost, and you need to go to the /usr/lib64/mysql/plugin directory Next, establish a soft connection relationship.

Finally, enter the source directory of memcached_functions_mysql, first enter the mysql terminal "mysql -u root -p"

Then execute "source sql/install_functions.sql;"

After successfully querying the table mysql.func, you can see many functions prefixed with memc.
Startup configuration memcached startup

启动命令和参数如下

memcached -d -m 256 -p 11211

-p 指定端口号(默认11211)

-U UDP监听端口 (默认: 11211, 0 时关闭)

-s 用于监听的UNIX套接字路径(禁用网络支持)

-a UNIX套接字访问掩码,八进制数字(默认:0700)

-m 指定最大使用内存大小(默认64MB)

-t 线程数(默认4)

-l 绑定地址
(默认:所有都允许,无论内外网或者本机更换IP,有安全隐患,若设置为127.0.0.1就只能本机访问)

-d start 启动memcached服务

-d restart 重起memcached服务

-d stop|shutdown 关闭正在运行的memcached服务

-u 绑定使用指定用于运行进程
(只有root用户可以使用这个参数)

-P 将PID写入文件,这样可以使得后边进行快速进程终止, 需要与 -d
一起使用

-m 最大内存使用,单位MB。默认64MB

-M 内存耗尽时返回错误,而不是删除项

-c 最大同时连接数,默认是1024

-f 块大小增长因子,默认是1.25

-n 最小分配空间,key+value+flags默认是48

-k锁定所有内存页。注意你可以锁定的内存上限。
mysql配置

mysql去自动同步memcached,主要是采用触发器来实现的。

首先在每次mysql启动后要进行配置(以下都是在mysql终端里执行)。

select memc_servers_set(’127.0.0.1:11211′);

select
memc_servers_behavior_set(‘MEMCACHED_BEHAVIOR_NO_BLOCK’,’1′);

select
memc_servers_behavior_set(‘MEMCACHED_BEHAVIOR_TCP_NODELAY’,’1′);

设置MEMCACHED_BEHAVIOR_NO_BLOCK为打开状态,这样在memcached出现问题时(不能连接时)数据继续插入到mysql中,报错提示,如果不设置此值,如果memcached失败,mysql需要等到timeout才可以插入到表中。

开始创建一个例子

use test;

create table xxd (id int, value varchar(100));

create trigger xxdmmci after insert on xxd for each row set @tmp
= memc_set(NEW.id, NEW.value);

create trigger xxdmmcu after update on xxd for each row set @tmp
= memc_set(NEW.id, NEW.value);

create trigger xxdmmcd before delete on xxd for each row set
@tmp = memc_delete(OLD.id);

至此,全部配置ok了。

然后可以用telnet连接上memcached去看下,执行“get xx”看是否生效。

这就完了吗,可能没有哦。

当你按照这样去操作时,你可能会发现,其实根本没有写入到memcached中。

怎么办呢,网上也没有响应的解决方法,笔者通过写简单的socket通讯测试,发现在mysql UDF中写socket会连接时会报“Permission denied”。所以你根本连接不上任何的服务器,当然memcached也在内。

问题找到了,就好办了,一劳永逸的解决办法

修改/etc/sysconfig/selinux,加入SELINUX=disabled,然后执行“setenforce 0”

当然,出现这样的问题的原因是,你的库是一个用户生成的,而你的mysqld的执行用户是另外一个。如果用root来启动mysql可能就不会有这样的问题了。

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326760077&siteId=291194637