mysql> show tables;
+-----------------+
| Tables_in_study |
+-----------------+
| class |
| course |
| part |
| score |
| student |
| tb31 |
| tb32 |
| teacher |
| user_info |
+-----------------+
9 rows in set (0.00 sec)
数据已经可以正常同步;注意主从同步只需要第一次手动启动;之后都随mysql服务自动启动;主从同步架构只方便了数据的同步,此时如果没有 第三方工具介入想做到读写分离就需要在程序中去做,难免出错;而出错了,就���要手动同步数据;这里通过proxysql来做读写分离;
三、proxysql之读写分离
以上已经完成了主从复制配置;然而这只是一个基本配置,加上一个proxysql实现mysql读写分离,proxysql类似haproxy七层代理路由功能且支持MySQL 协议的的数据库代理;是dba开发给dba使用的;用户请求发向proxysql,如果是写请求发往主节点;读请求发下从节点组中;以此实现读写分离;一定程序上减轻了主数据库的io压力;
下载安装proxysql
目前最新版本是1.4.7-1(由于最新版本有问题)
我们这里下载使用1.3.6-1的基于CentOS7的rpm包;下载到本地并yum安装
[root@proxysql ~]# yum install proxysql-1.3.6-1-centos7.x86_64.rpm -y
[root@proxysql ~]# rpm -ql proxysql
/etc/init.d/proxysql
/etc/proxysql.cnf #主配置文件
/usr/bin/proxysql
/usr/share/proxysql/tools/proxysql_galera_checker.sh
/usr/share/proxysql/tools/proxysql_galera_writer.pl
配置如下:
在配置proxysql之前需要在主节点配置授权账号以作proxysql对主从节点操作;另外proxysql上的mysql客户端工具需要和主从节点上的保持一致;
在主节点master上授权登录账号:
mysql > GRANT ALL ON *.* TO 'myadmin'@'172.16.3.%' identified by 'mypass';
proxysql.cnf配置
[root@proxysql ~]# egrep -v '(^$|^#)' /etc/proxysql.cnf
datadir="/var/lib/proxysql"
admin_variables=
{
admin_credentials="admin:admin" #proxysql自己的管理用户名密码
mysql_ifaces="127.0.0.1:6032;/tmp/proxysql_admin.sock"
}
mysql_variables=
{
threads=4 #线程数,建议和cpu核心数一致
max_connections=2048 #最大连接
default_query_delay=0
default_query_timeout=36000000
have_compress=true
poll_timeout=2000
interfaces="0.0.0.0:3306;/tmp/proxysql.sock" #对外接口
default_schema="information_schema"
stacksize=1048576
server_version="5.5.30"
connect_timeout_server=3000
monitor_username="monitor"
monitor_password="monitor"
monitor_history=600000
monitor_connect_interval=60000
monitor_ping_interval=10000
monitor_read_only_interval=1500
monitor_read_only_timeout=500
ping_interval_server_msec=120000
ping_timeout_server=500
commands_stats=true
sessions_sort=true
connect_retries_on_failure=10
}
#####主从节点的配置
mysql_servers =
(
{
address = "172.16.3.175" # no default, required . If port is 0 , address is interpred as a Unix Socket Domain
port = 3306 # no default, required . If port is 0 , address is interpred as a Unix Socket Domain
hostgroup = 1 # 设置组号
status = "ONLINE" # default: ONLINE
weight = 1 # default: 1
compression = 0 # default: 0
max_connections = 200 ###定义最大的连接
},
{
address = "172.16.3.235" # no default, required . If port is 0 , address is interpred as a Unix Socket Domain
port = 3306 # no default, required . If port is 0 , address is interpred as a Unix Socket Domain
hostgroup = 2 # no default, required
status = "ONLINE" # default: ONLINE
weight = 1 # default: 1
compression = 0 # default: 0
max_connections=1000
},
{
address = "172.16.3.241" # no default, required . If port is 0 , address is interpred as a Unix Socket Domain
port = 3306 # no default, required . If port is 0 , address is interpred as a Unix Socket Domain
hostgroup = 2 # no default, required
status = "ONLINE" # default: ONLINE
weight = 1 # default: 1
compression = 0 # default: 0
max_connections=1000
}
)
mysql_users:
(
{
username = "myadmin" # no default , required
password = "mypass" # default: ''
default_hostgroup = 1 # default: 0
max_connections=1000
default_schema="test"
active = 1 #是否激活
}
)
mysql_query_rules:
(
)
scheduler=
(
)
mysql_replication_hostgroups=
(
{
writer_hostgroup=1 #定义写组号1
reader_hostgroup=2 #定义读组号2
comment="test repl 1" #注释内容
}
)
启动proxysql服务
[root@proxysql ~]# service proxysql start
测试proxysql