mysql-proxy

本文系统Centos6.0

MySQL代理是一个介于MySQL客户端和MySQL服务器之间的简单程序,可用来监视、分析或者传输他们之间的通讯。他的灵活性允许你最大限度的使用它,这些包括负载平衡、容错 、分析查询和修正等等。

因此,一般来说都是通过主从复制(Master-Slave)的方式来同步数据,在通过读写分离(MySQL-Proxy)来提升数据的并发能力这样的方案来进行实施与 部

署的。

master :172.16.1.88:13267

slave :172.16.1.88:13268

注意我在一台上面搭建了两个数据,

master登录mysql的方式mysql -u root -p123 -S /data/mysql_13267/mysql.sock

slave登录mysql的方式mysql -u root -S /data/mysql_13268/mysql.sock

一、mysql主从复制

1.1主数据库服务器上面进行操作

 

授权给从数据库服务器172.16.1.88

 

mysql> grant replication slave on *.* to cluster@'172.16.1.88' identified by 'cluster';

Query OK, 0 rows affected (0.00 sec)

查看主数据状态

 

mysql> show master status;

+------------------+----------+--------------+------------------+

| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |

+------------------+----------+--------------+------------------+

| mysql-bin.000001 | 107 | | |

+------------------+----------+--------------+------------------+

1 row in set (0.00 sec)

记录下FILE及Position的值,在后面进行从数据库操作的时候需要用到。

1.2配置从服务器

修改从服务器的配置文件/etc/13268/my.cnf

service-id=10

log-bin=mysql-bin

启动mysql

执行同步SQL语句

mysql> change master to master_host='172.16.1.88', master_user='cluster', master_password='cluster', master_log_file='mysql-bin.000001', master_log_pos=107,master_port=1327;

 

正确执行后启动Slave同步进程

 

mysql> start slave;

Query OK, 0 rows affected (2.68 sec)

主从同步检查

 

mysql> show slave status\G

*************************** 1. row ***************************

Slave_IO_State: Waiting for master to send event

Master_Host: 172.16.1.88

Master_User: cluster

Master_Port: 13267

Connect_Retry: 60

Master_Log_File: mysql-bin.000001

Read_Master_Log_Pos: 262

Relay_Log_File: asdasda-relay-bin.000003

Relay_Log_Pos: 408

Relay_Master_Log_File: mysql-bin.000001

Slave_IO_Running: Yes

Slave_SQL_Running: Yes

Replicate_Do_DB:

Replicate_Ignore_DB:

Replicate_Do_Table:

Replicate_Ignore_Table:

Replicate_Wild_Do_Table:

Replicate_Wild_Ignore_Table:

Last_Errno: 0

Last_Error:

Skip_Counter: 0

Exec_Master_Log_Pos: 262

Relay_Log_Space: 566

Until_Condition: None

Until_Log_File:

Until_Log_Pos: 0

Master_SSL_Allowed: No

Master_SSL_CA_File:

Master_SSL_CA_Path:

Master_SSL_Cert:

Master_SSL_Cipher:

Master_SSL_Key:

Seconds_Behind_Master: 0

Master_SSL_Verify_Server_Cert: No

Last_IO_Errno: 0

Last_IO_Error:

Last_SQL_Errno: 0

Last_SQL_Error:

Replicate_Ignore_Server_Ids:

Master_Server_Id: 2

1 row in set (0.00 sec)

其中Slave_IO_Running与Slave_SQL_Running的值必须都为YES,才表明状态正常.

1)如果主服务器已经存在应用数据,则在进行主从复制时,需要做以下处理:

 

mysql> flush tables with read lock;

Query OK, 0 rows affected (0.00 sec)

2)查看主数据库状态

mysql> show master status;

3)记录FILE及Position的值.

将主服务器的数据文件(数据存放的目录)复制到从服务器,建议通过tar归档压缩在传到从服务器解压.

4)取消主数据锁定

 

mysql> unlock tables;

Query OK, 0 rows affected (0.00 sec)

1.3验证主从复制效果

主服务器上的操作

在主服务器上创建数据库first_db

 

mysql> create database first_db;

Query OK, 1 row affected (0.00 sec)

在主服务器上创建表first_tb

 

mysql> use first_db;

Database changed

mysql> create table first_tb(id int(3),name char(10));

Query OK, 0 rows affected (2.95 sec)

在主服务器上的表first_tb中插入记录

 

mysql> insert into first_tb values (001,'myself');

Query OK, 1 row affected (0.00 sec)

 

 

 

在从服务器查看

 

mysql> show databases;

+--------------------+

| Database |

+--------------------+

| information_schema |

| fightprototype |

| first_db |

| mysql |

| performance_schema |

| test |

| zhangfang |

+--------------------+

7 rows in set (0.00 sec)

 

mysql> use first_db;

Database changed

mysql> show tables;

+--------------------+

| Tables_in_first_db |

+--------------------+

| first_tb |

+--------------------+

1 row in set (0.00 sec)

由此,整个mysql主从复制过程就完成了,接下来,我们进行MySQL读写分离的安装与配置。

二,MySQL读写分离

 

master :172.16.1.88:13267

slave :172.16.1.88:13268

mysql_proxy:172.16.1.89

以下操作均在172.16.1.89即mysql_proxy调度服务器上进行的。

2.1Mysql的安装,自行安装

2.2检查系统需要软件包

gcc* gcc-c++* autoconf* automake* zlib* libxml* ncurses-devel* libmcrypt* libtool* flex* pkgconfig*
libevent* glib*

若确实相关软件包,可以通过yum -install 方式在线安装。

2.3编译安装lua

mysql-proxy的读写分离主要是通过rw-splitting.lua脚本实现的,因此需要安装lua

[root@mysql_proxy ~]# wget http://www.lua.org/ftp/lua-5.1.4.tar.gz

[root@mysql_proxy ~]# tar zxf lua-5.1.4.tar.gz

 

[root@mysql_proxy ~]# cd lua-5.1.4

[root@mysql_proxy lua-5.1.4]# vim src/Makefile

在CFLAGS=-02 -Wall $(MYCFLAGS)这一行记录里面加上-fPIC,更改为CFLAGS=-02 -Wall $(MYCFLAGS)来避免编译过程中出现错误.

 

[root@mysql_proxy lua-5.1.4]# make linux

[root@mysql_proxy lua-5.1.4]# make install

[root@mysql_proxy lua-5.1.4]# cp etc/lua.pc /usr/lib64/pkgconfig/

[root@mysql_proxy lua-5.1.4]# export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/lib64/pkgconfig/

三、安装配置mysql-proxy

测试平台centos64,选择64位包,可以根据自己的环境来选择相应的版本,我在这里用的是08.3

wget http://mysql.cdpa.nsysu.edu.tw/Downloads/MySQL-Proxy

[root@mysql_proxy ~]# tar mysql-proxy-0.8.3-linux-sles10-x86-64bit.tar.gz

[root@mysql_proxy ~]# mv mysql-proxy-0.8.3-linux-sles10-x86-64bit /opt/mysql-proxy/

创建mysql-proxy服务器管理脚本

[root@mysql_proxy ~]# mkdir /opt/mysql-proxy/init.d/

[root@mysql_proxy ~]# cat /opt/mysql-proxy/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/bin

PROXY_PATH=/opt/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=" --proxy-read-only-backend-addresses=172.16.1.88:13268 --proxy-backend-addresses=172.16.1.88:13267 --proxy-lua-script=/opt/mysql-proxy/scripts/rw-splitting.lua"

PROXY_PID=/opt/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=debug --log-file=/opt/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

//--admin-username=root指定管理员用户
//--admin-password=password指定管理员密码
//--proxy-read-only-backend-address=172.16.1.88:13268只读数据库
//--proxy-backend-address=172.16.1.88:13267写的数据库
//--admin-lua-script=/opt/mysql-prox/lib/mysql-proyx/lua/adminlua lua管理脚本路径
//--proxy-lua-script=/opt/mysql-proxy/scripts/rw-splitting.lua 指定lua脚本,使用的是rw-splitting.lua,用于读写分离.
//--daemon 采用daemon方式启动
//--pid-file=/opt/mysql-proxy/run/mysql-proxy.pid
//--user=mysql
//--log-level=debug定义log日志级别,由高到低(error|warining|info|message|debug)
//--log-file=/opt/mysql-proxy/log/mysql-proxy.log定义log日志文件的路径
(critical) mysql-proxy-cli.c:503: Unknown option --admin-username=root (use --help to show all options)
2013-09-09 15:35:52: (message) Initiating shutdown, requested from mysql-proxy-cli.c:513
2013-09-09 15:35:52: (message) shutting down normally, exit code is: 1
不知道是不是版本的问题?
注意,如果加入红线部分的选项,启动时会报
[root@mysql_proxy ~]# chmod +x /opt/mysql-proxy/init.d/mysql_proxy
[root@mysql_proxy ~]# mkdir /opt/mysql-proxy/run/
[root@mysql_proxy ~]# mkdir /opt/mysql-proxy/log/
[root@mysql_proxy ~]# mkdir /opt/mysql-proxy/scripts/
四配置并使用rw-splitting读写分离脚本。
最新的脚本我们可以从最新的mysql-proxy源码包获取
[root@mysql_proxy ~]# tar zxf mysql-proxy-0.8.3.tar.gz
[root@mysql_proxy ~]# cd mysql-proxy-0.8.3
[root@mysql_proxy mysql-proxy-0.8.3]# cp lib/rw-splitting.lua /opt/mysql-proxy/scripts/
修改读写分离脚本
修改默认连接,快速测试,不修改的话达到连接数为4时才启动读写分离。
[root@mysql_proxy mysql-proxy-0.8.3]# vim /opt/mysql-proxy/scripts/rw-splitting.lua
-- connection pool
if not proxy.global.config.rwsplit then
proxy.global.config.rwsplit = {
min_idle_connections = 1, //默认为4
max_idle_connections = 1, //默认为8
is_debug = false
}
end
修改完成后,启动mysql-proxy
/opt/mysql-proxy/init.d/mysql-proxy start
可以查看日志是否启动
[root@mysql_proxy ~]# tail -f /opt/mysql-proxy/log/mysql-proxy.log
2013-09-09 12:41:57: (message) added read-only backend: 172.16.1.88:13268
2013-09-09 12:41:57: (debug) now running as user: mysql (501/501)
2013-09-09 12:48:05: (message) Initiating shutdown, requested from signal handler
2013-09-09 12:48:05: (message) shutting down normally, exit code is: 0
2013-09-09 14:17:23: (critical) plugin proxy 0.8.3 started
2013-09-09 14:17:23: (debug) max open file-descriptors = 1024
2013-09-09 14:17:23: (message) proxy listening on port :4040
2013-09-09 14:17:23: (message) added read/write backend: 172.16.1.88:13267
2013-09-09 14:17:23: (message) added read-only backend: 172.16.1.88:13268
2013-09-09 14:17:23: (debug) now running as user: mysql (501/501)
3.1测试读写分离效果
创建用于读写分离的数据库连接用户
登录主数据库服务器172.16.1.88
mysql> GRANT ALL ON *.* TO 'proxy1'@'192.168.10.132' IDENTIFIED BY 'password';
由于我们配置了主从复制功能,因此从数据库服务器172.16.1.88:13268上已经同步此操作。
为了清晰的看到读写分离的效果,需要暂时关闭MySQL主从复制功能
登录从数据库服务器172.16.1.88:13268
关闭Slave同步进程
mysql> stop slave;
Query OK, 0 rows affected (1.79 sec)
连接mysql-proxy
[root@mysql_proxy mysql-proxy-0.8.3]# mysql -uproxy1 -p'password' -P4040 -h172.16.1.89
登录成功后,在first_db数据库的first_tb表中插入两条记录
mysql> use first_db;
Database changed
mysql> insert into first_tb values (007,'first');
Query OK, 1 row affected (0.00 sec)
 
mysql> insert into first_tb values (110,'second');
Query OK, 1 row affected (0.00 sec)
查询记录
退出来一下,重新登录,否则执行查询的时候还会出现刚才插入的数据。
mysql> select * from first_tb;
+------+--------+
| id | name |
+------+--------+
| 1 | myself |
+------+--------+
1 row in set (0.00 sec)
通过读操作并没有看到新的记录
退出mysql-proxy
下面,分别登录到主从数据库服务器,对比记录信息
首先,检查主数据库服务器
mysql> select * from first_tb;
+------+--------+
| id | name |
+------+--------+
| 1 | myself |
| 7 | first |
| 110 | second |
+------+--------+
3 rows in set (0.00 sec)
两条新记录已经存在,说明写的操作在主上面。
然后检查从数据库服务器
mysql> select * from first_tb;
+------+--------+
| id | name |
+------+--------+
| 1 | myself |
+------+--------+
1 row in set (0.00 sec)
没有记录存在,
由此验证,我们已经实现了MySQL读写分离,目前所有的写操作全部在master主服务器上,用来避免数据不同步,另外,所有的读操作都分摊给了其它slave服务器,用来分担数据库压力
posted @ 2017-07-03 14:31  ノGHJ  阅读(212)  评论(0编辑  收藏  举报