zoukankan      html  css  js  c++  java
  • 通过repcached实现memcached主从复制

    一、环境

    服务器A:ubuntu server 12.04(192.168.1.111)

    服务器B:ubuntu server 12.04 (47.50.13.111)

    二、memcached安装

    sudo apt-get install memcached

    安装完成后可以启动一下,看是否安装好了:

    # sudo memcached -d -m 128 -p 11211 -u root

    # telnet 127.0.0.1 11211
    Trying 127.0.0.1...
    Connected to 127.0.0.1.
    Escape character is '^]'.
    set name 0 0 5
    hello
    STORED
    get name
    hello
    

    可以存取数据即代表安装完成。

    三、repcached安装

    https://sourceforge.net/projects/repcached/files/repcached/ 下载repcached安装包(memcached-1.2.8-repcached-2.2.1.tar.gz),然后解压安装:

    # tar zxvf memcached-1.2.8-repcached-2.2.tar.gz

    # cd memcached-1.2.8-repcached-2.2/

    # ./configure --enable-replication

    # make

    # make install
    正常情况下按上面的安装就可以了,但也有可能会遇到以下问题:

    1、machine `i686-pc-linux' not recognized 错误

    解决办法是在./configure时加上参数:--build=i686-pc-linux-gnu

    2、configure: error: no acceptable C compiler found in $PATH See `config.log' for more details.

    这是因为没有安装c编译器,安装gcc即可:

    # apt-get install gcc

    3、memcached.c:696:30: error: ‘IOV_MAX’ undeclared (first use in this function)

    把memcached.c中55行~60行代码:

      55 /* FreeBSD 4.x doesn't have IOV_MAX exposed. */
      56 #ifndef IOV_MAX
      57 #if defined(__FreeBSD__)  defined(__APPLE__)
      58 # define IOV_MAX 1024
      59 #endif
      60 #endif
    

    换为:

      55 /* FreeBSD 4.x doesn't have IOV_MAX exposed. */
      56 #ifndef IOV_MAX
      57 /*#if defined(__FreeBSD__)  defined(__APPLE__)*/
      58 # define IOV_MAX 1024
      59 #endif
      60 /*#endif*/
    

    即把 57 行和60行注释掉。

    四、主从配置

    我们要把服务器B作为Master,把服务器A作为Slave,首先在服务器A(Master)上配置如下:

    # /usr/local/bin/memcached -p 11211 -v -d -u root
    
    replication: listen
    

    在服务器B(Slave)上配置如下:

    # /usr/local/bin/repcached -p 11212 -x 47.50.13.111 -v -d -u root
    
    replication: connect (peer=47.50.13.111:11212) 
    
    replication: marugoto copying
    
    replication: accept
    

    这里看到出现了一个11212,Memcached默认服务端口是11211,默认同步监听端口是11212。

    这一步要确保在Slave上可以telnet上Master服务器。

    如果telnet失败,一般会是iptables把11211/11212端口阻止了,打开即可。

    可以通过netstat命令查看:

    # netstat -tupln  grep memcached
    
    tcp        0      0 0.0.0.0:11211           0.0.0.0:*               LISTEN      2044/memcached  
    tcp        0      0 0.0.0.0:11212           0.0.0.0:*               LISTEN      2044/memcached  
    tcp6       0      0 :::11211                :::*                    LISTEN      2044/memcached  
    udp        0      0 0.0.0.0:11211           0.0.0.0:*                           2044/memcached  
    udp6       0      0 :::11211                :::*                                2044/memcached
    

    五、验证

    在Master上创建数据:

    # telnet 127.0.0.1 11211
    Trying 127.0.0.1...
    Connected to 127.0.0.1.
    Escape character is '^]'.
    set name 0 0 5
    atwal
    STORED
    

    在Slave上查看数据:

    $ telnet 127.0.0.1 11211
    Trying 127.0.0.1...
    Connected to 127.0.0.1.
    Escape character is '^]'.
    get name
    VALUE name 0 5
    atwal
    END
    

    可以看到数据已经同步了。

  • 相关阅读:
    python 执行sql得到字典格式数据
    python爬虫 url链接编码成gbk2312格式
    windows环境下elasticsearch安装教程(单节点)
    python SQLServer 存储图片
    爬虫的本质是和分布式爬虫的关系
    requests form data 请求 爬虫
    mysql 删除 binlog 日志文件
    查看mysql数据表的大小
    xshell 连接报错 Disconnected from remote host
    centos 7.3 安装 mysqldb 报错 EnvironmentError: mysql_config not found ERROR: Command errored out with exit status 1: python setup.py egg_info Check the logs for full command output.
  • 原文地址:https://www.cnblogs.com/lurenjiashuo/p/memcache-repcached.html
Copyright © 2011-2022 走看看