mysql基于mysql-proxy和amoeba的读写分离
以及tcpdump+wireshark工具验证
【实验部署准备】
1,首先对后端节点部署主从关系。
2,然后前端MySQL-proxy对后端进行读写分离。
3.后端各节点都要授权
4.
三个节点保持时间一致
[root@node3 ~]# crontab -e #####时间一致性
*/3 * * * * /usr/sbin/ntpdate 172.16.0.1 &> /dev/null
###############################对后端节点做主从复制#################################################
############注意对于mysql的安装这里不再介绍,直接进入配置。
###########################修改主节点配置文件########################################################
[root@director2 ~ ]#vim /etc/mysql/my.cn [mysqld] 42 thread_concurrency= 8 43 innodb_file_per_table = on 44 datadir = /mydata ###############库目录 45 skip_name_resolve = on ###############不反解主机名 59 log-bin=mysql-bin ###############启用二进制日志 67 server-id = 1 ###############唯一ID [root@director2 ~ ]# chkconfig --list mysqld #####保证mysqld的开启自启动 mysqld 0:off 1:off 2:on 3:on 4:on 5:on 6:off
############################授权一个有复制权限的用户给从节点######################################
mysql>grant replication client,replication slave on *.* to 'cpuser'@'172.16.249.141' identified by "magedu"; Query OK, 0 rows affected (0.08 sec) mysql> flush privileges; Query OK, 0 rows affected (0.01 sec)
###########################从节点准备##############################################################
[root@node2 ~ ]# groupadd -g 306 mysql [root@node2 ~ ]# useradd -g 306 -u 306 mysql [root@node2 ~ ]# mkdir /mydata [root@node2 ~ ]# cd /mydata/ [root@node2 / ]# chown -R mysql:mysql /mydata/ [root@node2 / ]# chmod -R a+w /mydata/
###########################把mysql文件传输给从节点#################################################
[root@node2 mysql ]#mkdir /etc/mysql [root@node2 mysql ]# scripts/mysql_install_db --user=mysql--datadir=/mydata ###初始化 [root@director2 ~ ]# scp /etc/mysql/my.cnf 172.16.249.141:/etc/mysql/ ###把主节点的配置文件拷贝过来 [root@node2 / ]# vim /etc/mysql/my.cnf ####编辑从节点的配置文件 59 #log-bin=mysql-bin ####从节点不启用二禁止日志 67 server-id = 20 ##server-id保持唯一 68 relay-log = relay-bin ####从节点开启中继日志 69 read-only =on ####只读模式开启(注意:这个只能限制普通用户)
方法二:此处也可以直接施加读锁。在mysql> lock tables testdb read;
mysql> show variables like 'relay%'; #####中继日志开启了 +-----------------------+---------------------+ | Variable_name | Value | +-----------------------+---------------------+ |relay_log | relay-bin | |relay_log_index | | |relay_log_info_file | relay-log.info | |relay_log_purge | ON | |relay_log_recovery | OFF | |relay_log_space_limit | 0 | +-----------------------+---------------------+ mysql> show variables like "read%"; #############只读模式开启 +----------------------+-----------------------+ |Variable_name | Value | +----------------------+-----------------------+ |read_buffer_size | 1048576 | |read_only | ON | |read_rnd_buffer_size | 4194304 | +----------------------+-----------------------+
#############################至此主从各单个节点配置结束#####################################
#############################配置从连接主节点################################################
#####主节点查看二进制文件的存储位置
mysql> show master status; +----------------------+----------------+--------------+------------------------------+ | File | Position | Binlog_Do_DB |Binlog_Ignore_DB | +----------------------+----------------+--------------+------------------------------+ | mysql-bin.000003 | 501 | | | +----------------------+----------------+--------------+------------------------------+ 1 row in set (0.04 sec) #######################建立主从连接###################################################### mysql> change master to master_host='172.16.11.3',master_user='cpuser',master_password='magedu',master_log_file='mysql-bin.000003',master_log_pos=501; mysql> show slave status\G; #####查看当前从节点状态 ***************************1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 172.16.249.141 Master_User: cpuser Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000003 Read_Master_Log_Pos: 501 Relay_Log_File: relay-bin.000002 Relay_Log_Pos: 529 Relay_Master_Log_File: mysql-bin.000003 Slave_IO_Running: Yes ###IO线程启用 Slave_SQL_Running: Yes ###SQL线程已经启用
#########################测试结果#########################################
######主节点创建库
mysql> create database testdb; Query OK, 1 row affected (0.03 sec) mysql> use testdb; Database changed 从节点已经同步了:
#################################创建一个表###############################################
mysql> create table students(id tinyint unsigned not null auto_increment unique key); Query OK, 0 rows affected (0.25 sec)
#######################主从配置测试完毕#####################################
#####################开始配置mysql-proxy####################################
##但是要注意,因为这个文件是不包含读写分离脚本的。因此,我们还需要下载一个mysql-proxy把里面的读写分离脚本复制过来就好了.
[root@node2 ~ ]# yuminstall mysql-proxy -y [root@node2 lua ]#tar xf mysql-proxy-0.8.3-linux-glibc2.3-x86-64bit.tar.gz [root@node2mysql-proxy ]# pwd /root/mysql-proxy-0.8.3-linux-glibc2.3-x86-64bit/share/doc/mysql-proxy [root@node2mysql-proxy ]# cp rw-splitting.lua /usr/lib64/mysql-proxy/lua/ [root@node2mysql-proxy ]# cd /usr/lib64/mysql-proxy/lua/ [root@node2 lua ]# chown -R root:mysql-proxy * ## ####修改文件属主属组 [root@node2 lua ]# chmod a+x rw-splitting.lua ###给脚本执行权限 [root@node2 ~ ]# vim /etc/mysql-proxy.cnf ###修改配置文件
1 [mysql-proxy] 2 daemon = true 3 pid-file = /var/run/mysql-proxy.pid 4 log-file = /var/log/mysql-proxy.log 5 log-level = debug 6 max-open-files = 1024 7 plugins = admin,proxy 8 user = mysql-proxy 9 # 10 #Proxy Configuration 11 proxy-address =0.0.0.0:3306 ####用于连接后端msyql的账号和端口 12 proxy-backend-addresses = 172.16.11.3:3306 ####后端写节点 13 proxy-read-only-backend-addresses =172.16.249.141:3306 ####后端读节点,多个用逗号分隔 14 proxy-lua-script =/usr/lib64/mysql-proxy/lua/rw-splitting.lua ####读写分离用到的脚本 15 #proxy-skip-profiling = true 16 # 17 # Admin Configuration 18 admin-address =0.0.0.0:4041 ####管理端口和地址 19 admin-lua-script =/usr/lib64/mysql-proxy/lua/admin.lua ####管理端口用到的脚本 20 admin-username = admin ####管理的账号 21 admin-password = admin ####管理的密码
#############################启动mysql-proxy#######################################
[root@node2 lua ]#netstat -tunlp tcp 0 0 0.0.0.0:4041 0.0.0.0:* LISTEN 2019/mysql-proxy tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 2019/mysql-proxy
注意:如果先要前端的代里可以连入后端还要求各后端都给予授权。
######master:
mysql> grant all on *.* to 'admin'@'%' identified by 'admin'; Query OK, 0 rows affected (0.11 sec) mysql> flush privileges; Query OK, 0 rows affected (0.01 sec) ######slave: mysql> grant all on *.* to 'admin'@'%' identified by 'admin'; Query OK, 0 rows affected (0.01 sec) mysql> flush privileges; Query OK, 0 rows affected (0.00 sec)
##############################这时连入测试#####################################
[root@node2 ~ ]# mysql -uadmin -padmin -h172.16.249.106 --port=3306
##########测试写
#########主节点已经有了
##########测试读
[root@node2 ~ ]#mysql -uadmin -padmin -h172.16.249.106 -e 'show databases;' [root@node2 ~ ]#mysql -uadmin -padmin -h172.16.249.106 --port=4041
######UP就说明读写已经全部启用了##################################################
################################或者我们可以抓包验证###########################
[root@node3 ~]# tcpdump -i eth0 -w ~/mysql.pkg port 3306