环境:
mysqlmaster:192.168.1.94
mysqlslave:192.168.1.95
mysql-proxy:192.168.1.91
拓扑图如下:
mysql 读写分离笔记_第1张图片
所需软件:
yum -y install gcc* gcc-c++* autoconf* automake* zlib* libxml* ncurses-devel* libmcrypt* libtool* flex* pkgconfig*    libevent* glib*
wget http://www.lua.org/ftp/lua-5.1.4.tar.gz

wget http://mysql.cdpa.nsysu.edu.tw/Downloads/MySQL-Proxy/mysql-proxy-0.8.1-linux-rhel5-x86-32bit.tar.gz    ##此包为编译好的二进制包
wget http://mysql.cdpa.nsysu.edu.tw/Downloads/MySQL-Proxy/mysql-proxy-0.8.1.tar.gz
 
 

在建立读写分离之前需要配置mysql的主从复制。
步骤:
1.mysqlmaster端,编辑my.cnf 加入以下几行
 
server-id=1         #这里的ID一定要唯一
log-bin=mysql-bin    #开启binlog
#binlog-do-db=data     #这行我注释了,如果要是去掉注释,则指定同步那一个库
binlog-ignore-db=mysql #mysql库不需要同步
log-slave-updates
 
进入mysql执行授权:
mysql> GRANT REPLICATION SLAVE ON *.* to rep1@'%' identified by 'www';
mysql> show master status;
+------------------+----------+--------------+------------------+
| File                         | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000005 |            514 |                            | mysql                        |
+------------------+----------+--------------+------------------+
 
记录File,Position
2.mysqlslave端,编辑my.cnf加入以下几行:
server-id=2
log-bin=mysql-bin
master-host=192.168.1.94
master-user=rep1
master-password=www
master-port=3306
master-connect-retry=60
replicate-ignore-db=mysql
#replicate-do-db=data
log-slave-update
 
启动slave
mysql> change master to master_host='192.168.1.94' ,master_user='rep1',master_password='www',master_log_file='mysql-bin.000005',master_log_pos=514;
mysql> start slave;
mysql> show slave status \G
*************************** 1. row ***************************
                         Slave_IO_State: Waiting for master to send event
                                Master_Host: 192.168.1.94
                                Master_User: rep1
                                Master_Port: 3306
                            Connect_Retry: 60
                        Master_Log_File: mysql-bin.000005
                Read_Master_Log_Pos: 514
                         Relay_Log_File: mysqld-relay-bin.000002
                            Relay_Log_Pos: 235
            Relay_Master_Log_File: mysql-bin.000005
                     Slave_IO_Running: Yes
                    Slave_SQL_Running: Yes
                        Replicate_Do_DB:
                Replicate_Ignore_DB: mysql

3.验证效果在mysqlmaster建立一个库,看mysqlslave是否同步过去。

mysql读写分离配置:
1.安装lua
tar zvfx lua-5.1.4.tar.gz
cd lua-5.1.4

vim src/Makefile

将CFLAGS= -O2 -Wall $(MYCFLAGS)
改为
CFLAGS= -O2 -Wall -fPIC $(MYCFLAGS)
cd src
make linux
cd ..
make install
cp etc/lua.pc    /usr/lib/pkgconfig/
vim    /etc/profile
在最后加入
export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/lib/pkgconfig
执行
source /etc/profile
 

2.安装mysql-proxy
tar xzvf mysql-proxy-0.8.1-linux-rhel5-x86-32bit.tar.gz
mv mysql-proxy-0.8.1-linux-rhel5-x86-32bit /usr/local/mysql-proxy/
 
建立mysql-proxy启动脚本
vim /etc/init.d/mysql-proxy
#!/bin/sh
#
# mysql-proxy This script starts and stops the mysql-proxy daemon
#
# chkconfig: - 78 30
# processname: mysql-proxy
# description: mysql-proxy is a proxy daemon to mysql

# Source function library.
. /etc/rc.d/init.d/functions

PROXY_PATH=/usr/local/mysql-proxy/bin


prog="mysql-proxy"

# Source networking configuration.
. /etc/sysconfig/network

# Check that networking is up.
[ ${NETWORKING} = "no" ] && exit 0

# Set default mysql-proxy configuration.
#PROXY_OPTIONS="--daemon"
PROXY_OPTIONS="--admin-username=root --admin-password=www --proxy-read-only-backend-addresses=192.168.1.95:3306 --proxy-backend-addresses=192.168.1.94:3306    --admin-lua-script=/usr/local/mysql-proxy/lib/mysql-proxy/lua/admin.lua    --proxy-lua-script=/usr/local/mysql-proxy/scripts/rw-splitting.lua"
PROXY_PID=/usr/local/mysql-proxy/run/mysql-proxy.pid

# Source mysql-proxy configuration.
if [ -f /etc/sysconfig/mysql-proxy ]; then
                . /etc/sysconfig/mysql-proxy
fi

PATH=$PATH:/usr/bin:/usr/local/bin:$PROXY_PATH

# By default it's all good
RETVAL=0

# See how we were called.
case "$1" in
    start)
                # Start daemon.
                echo -n $"Starting $prog: "
                $NICELEVEL $PROXY_PATH/mysql-proxy $PROXY_OPTIONS --daemon --pid-file=$PROXY_PID --user=mysql --log-level=warning --log-file=/usr/local/mysql-proxy/log/mysql-proxy.log
                RETVAL=$?
                echo
                if [ $RETVAL = 0 ]; then
                                touch /var/lock/subsys/mysql-proxy
                fi
             ;;
    stop)
                # Stop daemons.
                echo -n $"Stopping $prog: "
                killproc $prog
                RETVAL=$?
                echo
                if [ $RETVAL = 0 ]; then
                                rm -f /var/lock/subsys/mysql-proxy
                                rm -f $PROXY_PID
                fi
             ;;
    restart)
                $0 stop
                sleep 3
                $0 start
             ;;
    condrestart)
             [ -e /var/lock/subsys/mysql-proxy ] && $0 restart
            ;;
    status)
                status mysql-proxy
                RETVAL=$?
             ;;
    *)
                echo "Usage: $0 {start|stop|restart|status|condrestart}"
                RETVAL=1
             ;;
esac

exit $RETVAL

 
注释:
PROXY_PATH=/usr/local/mysql-proxy/bin    //定义mysql-proxy服务二进制文件路径

PROXY_OPTIONS="--admin-username=root \ //定义内部管理服务器账号
--admin-password=password \ //定义内部管理服务器密码
--proxy-read-only-backend-addresses=192.168.1.95:3306 \ //定义后端只读从服务器地址    
--proxy-backend-addresses=192.168.1.94:3306 \ //定义后端主服务器地址
admin-lua-script=/usr/local/mysql-proxy/lib/mysql-proxy/lua/admin.lua    \ //定义lua管理脚本路径
--proxy-lua-script= /usr/local/mysql-proxy/scripts/rw-splitting.lua" \ //定义lua读写分离脚本路径

PROXY_PID=/usr/local/mysql-proxy/run/mysql-proxy.pid //定义mysql-proxy PID文件路径

$NICELEVEL $PROXY_PATH/mysql-proxy $PROXY_OPTIONS \
--daemon \ //定义以守护进程模式启动
--keepalive \ //使进程在异常关闭后能够自动恢复
--pid-file=$PROXY_PID \ //定义mysql-proxy PID文件路径
--user=mysql \ //以mysql用户身份启动服务
--log-level=warning \ //定义log日志级别,由高到低分别有(error|warning|info|message|debug)
--log-file=/usr/local/mysql-proxy/log/mysql-proxy.log //定义log日志文件路径
 

 
mkdir    /usr/local/mysql-proxy/run    
mkdir /usr/local/mysql-proxy/log
chmod +x /etc/init.d/mysql-proxy
tar xzvf mysql-proxy-0.8.1.tar.gz
cd mysql-proxy-0.8.1
cp lib/rw-splitting.lua /usr/local/mysql-proxy/scripts
vim /usr/local/mysql-proxy/script/rw-splitting.lua
改为:
min_idle_connections = 1, //默认为4
max_idle_connections = 1, //默认为8
 
然后启动:
service mysql-proxy start
然后
netstat -lntp
会看到:
tcp        0      0 0.0.0.0:4040                0.0.0.0:*                   LISTEN      22961/mysql-proxy
tcp        0      0 0.0.0.0:4041                0.0.0.0:*                   LISTEN      22961/mysql-proxy

3.测试效果:
mysql -uroot -ppassword -h 192.168.1.91 -P 4041
mysql> SELECT * FROM backends;
+-------------+-------------------+-------+------+------+-------------------+
| backend_ndx | address                     | state | type | uuid | connected_clients |
+-------------+-------------------+-------+------+------+-------------------+
|                     1 | 192.168.1.94:3306 | up        | rw     | NULL |                                 0 |
|                     2 | 192.168.1.95:3306 | up        | ro     | NULL |                                 0 |
+-------------+-------------------+-------+------+------+-------------------+
 
进入mysqlslave,关闭同步进程
mysql>stop slave;
登陆mysql-proxy
mysql -uroot -ppassword -h 192.168.1.91 -P 4040
mysql> use datatest
Database changed
mysql> select * from proxy;
+------+-------+
| id     | name    |
+------+-------+
|        3 | first |
|        4 | first |
|        6 | first |
|        7 | first |
+------+-------+
4 rows in set (0.00 sec)

mysql>    insert into proxy values(88,'second');
mysql> select * from proxy;
+------+-------+
| id     | name    |
+------+-------+
|        3 | first |
|        4 | first |
|        6 | first |
|        7 | first |
+------+-------+
 
进入slave,查看
mysql> use datatest
mysql> select * from proxy;
+------+-------+
| id     | name    |
+------+-------+
|        3 | first |
|        4 | first |
|        6 | first |
|        7 | first |
+------+-------+
由此看来无数据,因为如果是读操作,会读取slave服务器的数据
mysql> select * from proxy;
+------+--------+
| id     | name     |
+------+--------+
|        3 | first    |
|        4 | first    |
|        6 | first    |
|        7 | first    |
|     12 | first    |
|     88 | second |
+------+--------+
由此看来写是正常的。
因此配置成功