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。