mysql5.6.13通用二进制格式安装并使用amoeba实现对mysql5.6数据库读写分离
proxy 192.168.8.39
master 192.168.8.40
slave 192.168.8.20
一、安装mysql-5.6.13服务器
安装包:
mysql-5.6.13-linux-glibc2.5-x86_64.tar.gz
添加mysql数据库用户
# groupadd -r mysql
# useradd -g mysql -r -s /sbin/nologin mysql
# tar xf mysql-5.6.13-linux-glibc2.5-x86_64.tar.gz -C /usr/local/
# cd /usr/local
# ln -sv mysql-5.6.13-linux-glibc2.5-x86_64 mysql
# cd mysql
改变mysql安装文件的数组和属主
# chown -R root.mysql ./*
创建数据库和二进制文件目录并赋权
# mkdir /data/mydata -pv
# chown -R mysql.mysql /data/mydata
主服务器
# mkdir /data/binlogs
# chown -R mysql.mysql /data/binlogs
从服务器
# mkdir /data/relaylogs
# chown -R mysql.mysql /data/relaylogs
执行安装
# scripts/mysql_install_db --datadir=/data/mydata --user=mysql
将服务脚本拷贝到启动项
# cp support-files/mysql.server /etc/rc.d/init.d/mysqld
# chkconfig --add mysqld
验证
# chkconfig --list mysqld
将mysql5.5的配置文件拷贝到该主机中
scp /etc/my.cnf 192.168.8.40:/etc/
编辑my.cnf文件
# vim /etc/my.cnf
加入
innodb_file_per_table = ON
datadir = /data/mydata
log-bin=/data/binlogs/master-bin
binlog_format=row
server-id = 200
将mysql命令加入到环境变量中
# vim /etc/profile.d/mysql.sh
export PATH=/usr/local/mysql/bin:$PATH
修改主机名
192.168.8.40 master.oadb.com master
192.168.8.20 slave.oadb.com slave
# hostname master.oadb.com
# scp /etc/hosts 192.168.8.20:/etc/
二、简单主从模式配置步骤(必须要mysql5.6)
1、配置主从节点的服务配置文件
1.1、配置master节点:
[mysqld]
binlog-format=ROW
log-bin=master-bin
log-slave-updates=true
gtid-mode=on
enforce-gtid-consistency=true
master-info-repository=TABLE
relay-log-info-repository=TABLE
sync-master-info=1
slave-parallel-workers=4
binlog-checksum=CRC32
master-verify-checksum=1
slave-sql-verify-checksum=1
binlog-rows-query-log_events=1
server-id=100
report-port=3306
port=3306
log-bin=/data/binlogs/master-bin
datadir=/mydata/data
socket=/tmp/mysql.sock
report-host=master.oadb.com
1.2、配置slave节点:
[mysqld]
binlog-format=ROW
log-slave-updates=true
gtid-mode=on
enforce-gtid-consistency=true
master-info-repository=TABLE
relay-log-info-repository=TABLE
sync-master-info=1
slave-parallel-workers=4
binlog-checksum=CRC32
master-verify-checksum=1
slave-sql-verify-checksum=1
binlog-rows-query-log_events=1
server-id=11
report-port=3306
port=3306
log-bin=/data/binlogs/master-bin
relay-log=/data/relaylogs/relay-bin
datadir=/data/mydata
socket=/tmp/mysql.sock
report-host=slave.oadb.com
2、创建复制用户(主服务器执行)
mysql> GRANT REPLICATION SLAVE,REPLICATION CLIENT ON *.* TO 'repluser'@'192.168.%.%' IDENTIFIED BY 'replpass';
mysql> FLUSH PRIVILEGES;
========================================================================================
示例说明:
mysql> GRANT REPLICATION SLAVE ON *.* TO repluser@172.16.100.7 IDENTIFIED BY 'replpass';
172.16.100.7是从节点服务器;如果想一次性授权更多的节点,可以自行根据需要修改;
3、为备节点提供初始数据集
锁定主表,备份主节点上的数据,将其还原至从节点;如果没有启用GTID,在备份时需要在master上使用show master status命令查看二进制日志文件名称及事件位置,以便后面启动slave节点时使用。
4、启动从节点的复制线程
如果启用了GTID功能,则使用如下命令:
mysql> CHANGE MASTER TO MASTER_HOST='192.168.8.40',MASTER_USER='repluser',MASTER_PASSWORD='replpass',MASTER_AUTO_POSITION=1;
mysql> SHOW SLAVE STATUS\G
报错:Last_IO_Error: Fatal error: The slave I/O thread stops because master and slave have equal MySQL server ids; these ids must be different for replication to work (or the --replicate-same-server-id option must be used on slave but this does not always make
sense; please check the manual before using it).
意思是说主服务器和从服务器的server-id重复
通过查看my.cnf文件发现 server-id 没有重复
在数据库上查看参数,确实有重复,重启主服务器,通过参数查看没有问题,再次show slave status正常
mysql> show global variables like '%server%';
没启用GTID,需要使用如下命令:
slave> CHANGE MASTER TO MASTER_HOST='172.16.100.6',
-> MASTER_USER='repluser',
-> MASTER_PASSWORD='replpass',
-> MASTER_LOG_FILE='master-bin.000003',
-> MASTER_LOG_POS=1174;
5.验证:通过在主服务上创建库、表查看从服务器是否能够获取响应数据
三、安装amaoeba代理服务器
1.安装jdk1.6.0.31(软件包jdk-6u31-linux-x64-rpm.bin)
# cp jdk-6u31-linux-x64-rpm.bin /usr/local/
# cd /usr/local
# chmod +x jdk-6u31-linux-x64-rpm.bin
# ./jdk-6u31-linux-x64-rpm.bin
加入到环境变量
# vi /etc/profile.d/java.sh
加入
export JAVA_HOME=/usr/java/latest
export PATH=$JAVA_HOME/bin:$PATH
验证jdk安装是否成功
# java -version
2.安装amoeba(软件包amoeba-mysql-binary-2.2.0.tar.gz)
# mkdir /usr/local/amoeba-mysql-2.2.0
# tar xf amoeba-mysql-binary-2.2.0.tar.gz -C /usr/local/amoeba-mysql-2.2.0
创建链接
# ln -sv amoeba-mysql-2.2.0 amoeba
主服务上添加专门的amoeba root用户
mysql> GRANT ALL ON *.* TO 'root'@'192.168.%.%' IDENTIFIED BY 'root.pass';
mysql> FLUSH PRIVILEGES;
从服务器可以看到用户可以被读取到
mysql> show grants for 'root'@'192.168.%.%';
+------------------------------------------------------------------------------------------------------------------------+
| Grants for root@192.168.%.% |
+------------------------------------------------------------------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'root'@'192.168.%.%' IDENTIFIED BY PASSWORD '*79F802E6455F5621E94D5157B3A377363E660552' |
+------------------------------------------------------------------------------------------------------------------------+
添加到环境变量中
# vim /etc/profile.d/amoeba.sh
export AMOEBA_HOME=/usr/local/amoeba
export PATH=$AMOEBA_HOME/bin:$PATH
3.配置amoeba
amoeba.xml
监听端口
<property name="port">3306</property>
绑定地址
<property name="ipAddress">0.0.0.0</property>
<property name="authenticator">
<bean class="com.meidusa.amoeba.mysql.server.MysqlClientAuthenticator">
<property name="user">root</property>
<!--代理服务器管理密码-->
<property name="password">amoebapass</property>
<property name="filter">
<bean class="com.meidusa.amoeba.server.IPAccessController">
<property name="ipFile">${amoeba.home}/conf/access_list.conf</property>
</bean>
</property>
</bean>
</property>
dbServers.xml
<property name="password">root.pass</property>
4.启动amoeba服务(192.168.8.39)
# amoeba start
通过代理服务器进行操作
# mysql -uroot -pamoebapass -h192.168.8.39
通过抓包观察
master服务器(8.40)
# tcpdump -i eth0 -s0 -nn -A tcp dst port 3306 and dst host master
slave服务器(8.20)
# tcpdump -i eth0 -s0 -nn -A tcp dst port 3306 and dst host slave
通过简单配置可以实现将主服务器和从服务器合并作为一个读取数据库池
slave的轮询次数多33%
dbServers.xml
amoeba.xml