mycat读写分离与主从切换

RashadSchur 9年前

来自: http://blog.csdn.net/mchdba/article/details/50616534


1, 分库分表的优缺点、以及为什么分表方式无法成为主流?
分表:在台服务器上,优点是易维护,类似表分区,缺点是在一台db服务器上,无法分担IO、负载集中。
分库:在多台服务器上,优点是分担IO、负载均衡,缺点是较不易维护、数据统计以及jion操作有些难度。

数据库切分的目的是为了分担IO、负载均衡,分表无法达到最佳的要求,所以无法成为主流。
2, 准备主库
tar -xvf mysql-5.6.12.tar.gz
cd mysql-5.6.12

time cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql56m1 -DMYSQL_DATADIR=/home/data/mysql56m1/data -DWITH_INNOBASE_STORAGE_ENGINE=1 -DMYSQL_UNIX_ADDR==/usr/local/mysql56m1/mysql.sock -DMYSQL_USER=mysql -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci

time make
time make install

chown -R mysql /home/data/
chgrp -R mysql /home/data/
chown -R mysql /usr/local/mysql

chown -R mysql /usr/local/mysql56m1
chgrp -R mysql /usr/local/mysql

chgrp -R mysql /usr/local/mysql56m1/
mkdir -p /home/data/mysql56m1/binlog/
chown -R mysql.mysql /home/data/mysql56m1/binlog/
mkdir -p /home/data/mysql5610/binlog/
chown -R mysql.mysql /home/data/

cd /usr/local/mysql56m1
time scripts/mysql_install_db –user=mysql –basedir=/usr/local/mysql56m1 –datadir=/home/data/mysql56m1/data –defaults-file=/usr/local/mysql56m1/my.cnf

cp support-files/mysql.server /etc/init.d/mysql56m1
chmod 700 /etc/init.d/mysql56m1
echo “export PATH=$PATH:/usr/local/mysql56m1/bin”>>/etc/profile
source /etc/profile
chkconfig –add mysql56m1

service mysql56m1 start

3, 准备备库

tar -xvf mysql-5.6.12.tar.gz
cd mysql-5.6.12

time cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql56s1 -DMYSQL_DATADIR=/home/data/mysql56s1/data -DWITH_INNOBASE_STORAGE_ENGINE=1 -DMYSQL_UNIX_ADDR==/usr/local/mysql56s1/mysql.sock -DMYSQL_USER=mysql -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci

time make
time make install

chown -R mysql /home/data/
chgrp -R mysql /home/data/
chown -R mysql /usr/local/mysql

chown -R mysql /usr/local/mysql56s1
chgrp -R mysql /usr/local/mysql

chgrp -R mysql /usr/local/mysql56s1/
mkdir -p /home/data/mysql56s1/binlog/
chown -R mysql.mysql /home/data/mysql56s1/binlog/
mkdir -p /home/data/mysql5610/binlog/
chown -R mysql.mysql /home/data/

cd /usr/local/mysql56s1
time scripts/mysql_install_db –user=mysql –basedir=/usr/local/mysql56s1 –datadir=/home/data/mysql56s1/data –defaults-file=/usr/local/mysql56s1/my.cnf

cp support-files/mysql.server /etc/init.d/mysql56s1
chmod 700 /etc/init.d/mysql56s1
echo “export PATH=$PATH:/usr/local/mysql56s1/bin”>>/etc/profile
source /etc/profile
chkconfig –add mysql56s1
service mysql56s1 start

4, 搭建主从环境
在一台服务器,主3317端口,从3327端口。
主库备库上,添加复制帐号:
GRANT REPLICATION SLAVE ON . TO ‘repl’@’10.254.%’ IDENTIFIED BY ‘mycatms’;

在备库上3327端口上设置复制:
stop slave;
reset slave;
change master to master_user=’repl’, master_password=’mycatms’, master_host=’127.0.0.1’,master_port=3317, master_log_file=’mysql-bin.000003’,master_log_pos=840;
start slave;
show slave status\G;

主库:3317端口

从库:3327端口

数据同步测试:

5, 数据同步测试:
配置schema.xml:
然后修改mycat的schema.xml:
balance为1:让全部的readHost及备用的writeHost参与select的负载均衡。
switchType为2:基于MySQL主从同步的状态决定是否切换。
heartbeat:主从切换的心跳语句必须为show slave status。

5.0 数据录入:
mysql> explain create table company(id int not null primary key,name varchar(100));
+———–+———————————————————————+
| DATA_NODE | SQL |
+———–+———————————————————————+
| dn1 | create table company(id int not null primary key,name varchar(100)) |
| dn2 | create table company(id int not null primary key,name varchar(100)) |
| dn3 | create table company(id int not null primary key,name varchar(100)) |
+———–+———————————————————————+
3 rows in set (0.00 sec)

mysql> create table company(id int not null primary key,name varchar(100));
Query OK, 0 rows affected (0.01 sec)

mysql> explain insert into company(id,name) values(1,’alibaba’);
+———–+————————————————–+
| DATA_NODE | SQL |
+———–+————————————————–+
| dn1 | insert into company(id,name) values(1,’alibaba’) |
| dn2 | insert into company(id,name) values(1,’alibaba’) |
| dn3 | insert into company(id,name) values(1,’alibaba’) |
+———–+————————————————–+
3 rows in set (0.10 sec)

mysql> insert into company(id,name) values(1,’alibaba’);
Query OK, 1 row affected (0.00 sec)

mysql>

5.1主库验证:
[root@wgq_idc_squid_1_11 logs]# /usr/local/mysql56m1/bin/mysql -uroot -p -P3317 –socket=/usr/local/mysql56m1/mysql.sock -e “select @@port;select * from db1.company”;
Enter password:
+——–+
| @@port |
+——–+
| 3317 |
+——–+
+—-+———+
| id | name |
+—-+———+
| 1 | alibaba |
+—-+———+
[root@wgq_idc_squid_1_11 logs]#

5.2从库验证:
[root@wgq_idc_squid_1_11 logs]# /usr/local/mysql56s1/bin/mysql -uroot -p -P3327 –socket=/usr/local/mysql56s1/mysql.sock -e “select @@port;select * from db1.company”;
Enter password:
+——–+
| @@port |
+——–+
| 3327 |
+——–+
+—-+———+
| id | name |
+—-+———+
| 1 | alibaba |
+—-+———+
[root@wgq_idc_squid_1_11 logs]#

6,读写分离模式
6.1然后修改mycat的schema.xml:
balance为1:让全部的readHost及备用的writeHost参与select的负载均衡。
switchType为2:基于MySQL主从同步的状态决定是否切换。
heartbeat:主从切换的心跳语句必须为show slave status。

有配置读节点: