本文共 9019 字,大约阅读时间需要 30 分钟。
参考:
http://www.cnblogs.com/xxcn/p/4385412.html
http://blog.jobbole.com/94606/
http://www.open-open.com/lib/view/open1413274853450.html
原理:略(下次再写)图片来自:http://www.open-open.com/lib/view/open1413274853450.html
架构:
master: 192.168.2.127
slave: 192.168.2.201 192.168.2.202
mysql proxy: 192.168.2.138
haproxy:192.168.2.138
事先准备:
主从必须安装好mysql、关闭selinux,配置好防火墙(很重要,因为从服务器被挡导致读写分离不成功,我排查了4个小时..),主从服务器之间的数据库主从复制前要事先同步或完全一致,本文都以尽可能简单的配置来。
主从复制搭建:
master:
修改my.cnf为:
1 2 3 4 5 6 7 8 9 10 11 | [mysqld] datadir= /var/lib/mysql socket= /var/lib/mysql/mysql .sock user=mysql # Disabling symbolic-links is recommended to prevent assorted security risks symbolic-links=0 server- id =127 #id必须唯一,用来识别主机身份,可任意取 log-bin=mysql-bin #二进制日志必须开,从服务器会拷贝这些文件到自身变为中继日志,然后根据日志内容更新从服务器自身数据库 [mysqld_safe] log-error= /var/log/mysqld .log pid- file = /var/run/mysqld/mysqld .pid |
slave:
修改my.cnf为:
1 2 3 4 5 6 7 8 9 10 | [mysqld] datadir= /var/lib/mysql socket= /var/lib/mysql/mysql .sock user=mysql # Disabling symbolic-links is recommended to prevent assorted security risks symbolic-links=0 server- id =202 #从服务器不需要开二进制日志,因为它从主服务器拷贝,当然我们这里假设我们是对所有库进行读写分离,如果仅对某些库,还是有必要看情况开的 [mysqld_safe] log-error= /var/log/mysqld .log pid- file = /var/run/mysqld/mysqld .pid |
master:
在主服务器上授权一个用户远程访问,给予replication和slave权限
grant replication slave on *.* to slave@'%' identified by '123456';
查看master 状态并记录日志名(file)和位移值(position)
mysql> show master status ;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000011 | 106 | | |
+------------------+----------+--------------+------------------+
slave:
配置从服务器并开启
change master to \
master_host='192.168.2.127\
master_user='slave'\
master_password='123456'\
master_log_file='mysql-bin.000011'#对应master的binlog file
master_log_pos=106;#对应master的position
start slave
show slave status \G 如果以下两个参数为yes就表明没有问题
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
测试:
过程就不写了,在主服务上创建删除数据看看从会不会同步即可
例子:
创建库:
create database mydb;
创建表:
use mydb;
create table users ( id INT NULL AUTO_INCREMENT PRIMARY KEY, username VARCHAR(30) NOT NULL, );
插入数据:
insert into mydb.users(username) values("xizixiaoxiao");
删除数据:
delete from mydb.users where username='xizixiaoxiao';
读写分离:
wget http://101.96.10.43/downloads.mysql.com/archives/get/file/mysql-proxy-0.8.5-linux-el6-x86-64bit.tar.gz
tar xf mysql-proxy-0.8.5-linux-el6-x86-64bit.tar.gz
mv mysql-proxy-0.8.5-linux-el6-x86-64bit /usr/local/mysql-proxy
cd /usr/local/mysql-proxy
mkdir lua
cp share/doc/mysql-proxy/rw-splitting.lua lua
cp share/doc/mysql-proxy/admin-sql.lua lua
vi /etc/mysql-proxy.conf
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | [mysql-proxy] user=root #运行用户 admin-username=admin #proxy管理用户 admin-password=123456 #proxy管理用户密码 admin-address=192.168.2.127:4001 #proxy 管理服务监听套接字,这个是用来管理proxy服务本身的,而proxy管理mysql的读写分离 proxy-address=192.168.2.127:4000 #proxy 自身服务套接字,默认4040端口 proxy- read -only-backend-addresses=192.168.2.201:3306,192.168.2.202:3306 #只读从 proxy-backend-addresses=192.168.2.127:3306 #读写主 proxy-lua-script= /usr/local/mysql-proxy/lua/rw-splitting .lua #读写分离lua脚本 admin-lua-script= /usr/local/mysql-proxy/lua/admin-sql .lua #管理脚本 log- file = /var/log/mysql-proxy .log log-level=debug daemon= true keepalive= true #进程意外挂掉后尝试重启 |
vi /usr/local/mysql-proxy/lua/rw-splitting.lua
1 2 3 4 5 6 7 8 | if not proxy.global.config.rwsplit then proxy.global.config.rwsplit = { min_idle_connections = 1, #最小链接数,只有连接数大于这个数才会进行读写分离,默认4 max_idle_connections = 1, #最大连接数,默认8,改为1 is_debug = false } end |
/usr/local/mysql-proxy/bin/mysql-proxy --defaults-file=/etc/mysql-proxy.cnf --plugins=proxy --plugins=admin#启动proxy
测试:
mysql -uproxy -p12346 -h192.168.2.138 -P 4000 -e 'select * from mydb.users';
然后可以关闭从服务器(在从服务器上: stop slave),在主服务器上更新数据库内容,会发现上面显示内容不变,此时如果再开启主从同步,上面的命令的显示就会更新,此时读写分离就成功。
需要说明的是,如果从服务器全挂掉,那么会在主服务器上查询,而如果主服务挂掉,理论上要能查询不能写,但是这里会报ERROR 1105 (HY000): (proxy) all backends are down错误,从而全部不能写,这应该是proxy的问题。
haproxy安装(在192.168.2.138上):
wget http://down1.chinaunix.net/distfiles/haproxy-1.4.21.tar.gz #官网最近下不了,随便拿个凑合了
tar xf haproxy-1.4.21.tar.gz
cd haproxy-1.4.21
make TARGET=linux26#haproxy不支持make直接编译,TARGET可以在README中找到相应信息
make install
编辑haproxy的配置文件
cd /usr/local/haproxy/#这个是因为我在编译时,修改Makefile里面的安装目录为这个,默认/usr/local/haproxy/
mkdir etc;cd etc
vi haproxy.conf
内容如下:(具体参数意义参考:http://www.cnblogs.com/dkblog/archive/2012/03/13/2393321.html或官网文档)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | defaults log global mode http retries 3 option redispatch maxconn 4096 timeout connect 50000 timeout client 50000 timeout server 50000 listen mysql_proxy 0.0.0.0:3308 #前端的监听端口 mode tcp balance roundrobin # option tcpka option httpchk server slave1 192.168.2.201:3306 weight 1 #后端服务器,weight表示权重,可以根据balance选项替换算法 server slave2 192.168.2.202:3306 weight 1 listen status 192.168.2.138:8080 #定义web,可以通过http://192.168.2.138:8080/stats查看haproxy状态,你没看错是stats而不是status stats enable stats uri /stats stats auth admin:123456 stats realm (Haproxy\ statistic) |
自定义haproxy的启动脚本:
vi /etc/init.d/haproxy
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 | #!/bin/bash # # haproxy # # chkconfig: 35 85 15 # description: HAProxy is a free, very fast and reliable solution \ # offering high availability, load balancing, and \ # proxying for TCP and HTTP-based applications # processname: haproxy # config: /etc/haproxy.cfg # pidfile: /var/run/haproxy.pid # Source function library. . /etc/rc .d /init .d /functions # Source networking configuration. . /etc/sysconfig/network # Check that networking is up. [ "$NETWORKING" = "no" ] && exit 0 config= "/usr/local/haproxy/etc/haproxy.conf" #配置文件位置 exec = "/usr/local/haproxy/sbin/haproxy" #二进制可执行文件位置 prog=$( basename $ exec ) [ -e /etc/sysconfig/ $prog ] && . /etc/sysconfig/ $prog lockfile= /var/lock/subsys/haproxy check() { $ exec -c -V -f $config } start() { $ exec -c -q -f $config if [ $? - ne 0 ]; then echo "Errors in configuration file, check with $prog check." return 1 fi echo -n $ "Starting $prog: " # start it up here, usually something like "daemon $exec" daemon $ exec -D -f $config -p /var/run/ $prog.pid retval=$? echo [ $retval - eq 0 ] && touch $lockfile return $retval } stop() { echo -n $ "Stopping $prog: " # stop it here, often "killproc $prog" killproc $prog retval=$? echo [ $retval - eq 0 ] && rm -f $lockfile return $retval } restart() { $ exec -c -q -f $config if [ $? - ne 0 ]; then echo "Errors in configuration file, check with $prog check." return 1 fi stop start } reload() { $ exec -c -q -f $config if [ $? - ne 0 ]; then echo "Errors in configuration file, check with $prog check." return 1 fi echo -n $ "Reloading $prog: " $ exec -D -f $config -p /var/run/ $prog.pid -sf $( cat /var/run/ $prog.pid) retval=$? echo return $retval } force_reload() { restart } fdr_status() { status $prog } case "$1" in start|stop|restart|reload) $1 ;; force-reload) force_reload ;; checkconfig) check ;; status) fdr_status ;; condrestart|try-restart) [ ! -f $lockfile ] || restart ;; *) echo $ "Usage: $0 {start|stop|status|checkconfig|restart|try-restart|reload|force-reload}" exit 2 esac |
启动:/etc/init.d/haproxy start
netstat -tnlp|grep haproxy 会看到两个服务:
[root@cqhdtest2 etc]# netstat -tnlp|grep haproxy
tcp 0 0 0.0.0.0:3308 0.0.0.0:* LISTEN 1914/haproxy
tcp 0 0 192.168.2.138:8080 0.0.0.0:* LISTEN 1914/haproxy
修改mysql_proxy的配置,把proxy-read-only-backend-addresses改为192.168.2.138:3308
killall mysql-proxy #因为我们设置了keepalive,所以’kill -9 进程id‘的方式它不会终止,而是自己又生成另外一个进程
内容如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | [mysql-proxy] user=root admin-username=admin admin-password=123456 admin-address=192.168.2.138:4001 proxy-address=192.168.2.138:4000 proxy- read -only-backend-addresses=192.168.2.138:3308 proxy-backend-addresses=192.168.2.127:3306 proxy-lua-script= /usr/local/mysql-proxy/lua/rw-splitting .lua admin-lua-script= /usr/local/mysql-proxy/lua/admin-sql .lua log- file = /var/log/mysql-proxy .log log-level=debug daemon= true keepalive= true |
启动mysql_proxy:/usr/local/mysql-proxy/bin/mysql-proxy --defaults-file=/etc/mysql-proxy.cnf --plugins=proxy --plugins=admin
测试:
可以把slave1 关掉主从,slave2开启主从,主服务修改一个数据,然后通过mysql -uproxy -p -h 192.168.2.138 -P4000 不断地 select * from mydb.users ; 会发现每次值都不一样,如此便是成功的
具体测试,略
memcache安装配置:(参考:http://www.cnblogs.com/xxcn/p/4385412.html,这里我使用的是官网提供的安装方法)
wget http://memcached.org/latest
tar xf latest#你没有看错,它的安装包就是lastest
cd memcached-1.4.33/ #我解压后是这个版本
./configure --prefix=/usr/local/memcache#编译报错的话查看libevent是否没装
make
make test#测试是否有问题
make install
启动:/usr/local/memcache/bin/memcached -d -m 20 -uroot -l 192.168.2.138 -p 2211
-d:以daemon方式运行
-m:指定内存大小
-u:以什么用户身份运行
-l:listen 监听的地址
-p:监听的端口
-h:帮助
-P:保存pid的位置