1.MySQL的主从复制和MySQL的读写分离两者有着紧密的联系,首先要部署主从复制,只有主从复制完成了,才能在此基础之上进行数据的读写分离
2.MySQL支持三类主从复制类型:
基于语句的复制,在主服务器上的执行的语句SQL语句,在从服务器上执行同样的语句。MySQL默认采用基于语句的复制,效率比较高
基于行的复制,把改变的内容复制过去,而不是把命令在从服务器上执行一遍
混合类型的复制,默认采用基于语句的复制,一旦发现基于语句无法精确复制时,就会采用基于行的复制
1.读写分离就是用户基于第三方服务器,访问数据库时,写在主服务器上,在从服务器上读
基本原理是让主数据库处理事务性查询,而让从数据库处理select查询
数据库主从复制被用来把事务查询导致的变更同步到集群的从数据库
基于中间代理层实现:代理一般位于客户端和服务器之间,代理服务器接到客户端请求通过判断后转发到后端数据库,比如Mycat 、Amoeba
yum install ntp -y
vim /etc/ntp.conf
server 127.127.88.0 //本地是时钟源//
fudge 127.127.88.0 stratum 8 //设置时间层级为8//
关闭防火墙,开启服务:
systemctl start ntpd
systemctl stop firewalld
setenforce 0
yum install ntp ntpdate -y
systemctl start ntpd
systemctl stop firewalld
setenforce 0
/usr/sbin/ntpdate 192.168.88.131 //同步主服务器时间
yum install -y ncurses-devel autoconf cmake make gcc gcc-c++ libaio-devel bison
准备mysql-5.6.26.tar.gz包
具体安装过程参考LAMP架构
vim /etc/my.cnf
log_bin=master-bin //主服务器日志文件//
log-slave-updates=true //从服务器更新二进制日志//
server_id = 11 //指定ID号
service mysqld restart
mysql -uroot -p
GRANT REPLICATION SLAVE ON *.* TO 'myslave'@'192.168.88.%' IDENTIFIED BY '123456';
FLUSH PRIVILEGES;
show master status;
+-------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+-------------------+----------+--------------+------------------+-------------------+
| master-bin.000002 | 411 | | | |
+-------------------+----------+--------------+------------------+-------------------+
vim /etc/my.cnf
relay-log=relay-log-bin //主服务器上同步日志文件记录到本地//
relay-log-index=slave-relay-bin.index //定义relay-log的位置和名称//
server_id = 22 //另一台23
service mysqld restart
mysql -uroot -p
change master to master_host='192.168.88.131',master_user='myslave',master_password='123456',master_log_file='master-bin.000002',master_log_pos=411;
start slave;
show slave status\G;
systemctl stop firewalld.service
setenforce 0
amoeba-mysql-binary-2.2.0.tar.gz
jdk-6u14-linux-x64.bin
cp jdk-6u14-linux-x64.bin /usr/local/
cd /usr/local/
chmod +x jdk-6u14-linux-x64.bin
./jdk-6u14-linux-x64.bin
mv jdk1.6.0_14/ /usr/local/jdk1.6
vim /etc/profile
export JAVA_HOME=/usr/local/jdk1.6
export CLASSPATH=$CLASSPATH:$JAVA_HOME/lib:$JAVA_HOME/jre/lib
export PATH=$JAVA_HOME/lib:$JAVA_HOME/jre/bin/:$PATH:$HOME/bin
export AMOEBA_HOME=/usr/local/amoeba
export PATH=$PATH:$AMOEBA_HOME/bin
source /etc/profile
mkdir /usr/local/amoeba
tar zxvf amoeba-mysql-binary-2.2.0.tar.gz -C /usr/local/amoeba/
chmod -R 755 /usr/local/amoeba/
grant all on *.* to test@'192.168.88.%' identified by '123.com';
cd /usr/local/amoeba/conf
vim amoeba.xml
##### ---30/32行:---
amoeba
123456
##### ---117行去掉注释---
master
master
slaves
vim dbServers.xml
##### ---26-29去掉注释添加用户名密码---
test
123.com
##### ---45行往后添加主从服务器名和地址---
192.168.88.131
192.168.88.133
192.168.88.135
##### ---66行---
slave1,slave2
/usr/local/amoeba/bin/amoeba start&
systemctl stop firewalld.service
setenforce 0
yum install mysql -y
mysql -u amoeba -p 123456 -h 192.168.88.170 -P8066
mysql [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| bbb |
| mysql |
| performance_schema |
| school |
| test |
| xxx |
+--------------------+
7 rows in set (0.00 sec)
mysql [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| ccc |
| mysql |
| performance_schema |
| school |
| test |
| xxx |
+--------------------+
7 rows in set (0.00 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| aaa |
| bbb |
| mysql |
| performance_schema |
| school |
| test |
| xxx |
+--------------------+
7 rows in set (0.00 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| bbb |
| mysql |
| performance_schema |
| school |
| test |
| xxx |
+--------------------+
7 rows in set (0.00 sec)