Mysql实战之主从复制的读写分离
author:JevonWei
版权声明:原创作品
ProxySQL构建主从复制的读写分离
ProxySQL官网及下载地址 http://www.proxysql.com/
架构角色
mysql-slave2 172.16.252.92
mysql-slave1 172.16.252.82
ProxySQL 172.16.253.105
mysql-master 172.16.252.30
准备阶段:
各节点服务端都需提前同步时间及安装mariadb数据库
[root@mysql-master ~]# ntpdate 172.16.0.1
[root@mysql-slave1 ~]# yum -y install mariadb-server
关闭防火墙
[root@mysql-master ~]# iptables -F
下载ProxySQL程序包
http://www.proxysql.com/
mysql-master
[root@mysql-master ~]# vim /etc/my.cnf.d/server.cnf
[mysqld]
server_id=1
log_bin = master-log
skip_name_resolve = ON
innodb_file_per_table = ON \\每表使用单独的表文件存储
[root@mysql-master ~]# systemctl start mariadb
[root@mysql-master ~]# mysql
创建用户并授权连接主节点复制数据的权限
MariaDB [(none)]> GRANT REPLICATION SLAVE,REPLICATION CLIENT ON *.* TO 'repluser'@'172.16.252.%' IDENTIFIED BY 'replpass';
MariaDB [(none)]> FLUSH PRIVILEGES;
显示master节点的状态,记录当前节点的位置
MariaDB [(none)]> SHOW MASTER STATUS;
+-------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+-------------------+----------+--------------+------------------+
| master-log.000004 | 498 | | |
+-------------------+----------+--------------+------------------+
创建连接ProxySQL代理的用户和密码,并同步到各节点主机
MariaDB [(none)]> GRANT ALL ON *.* TO 'proxysql'@'172.16.%.%' IDENTIFIED BY 'proxypass';
mysql-slave1
[root@mysql-slave1 ~]# vim /etc/my.cnf.d/server.cnf
[mysqld]
server_id = 2
relay-log = relay-log
skip_name_resolve = ON
innodb_file_per_table = ON
read_only = ON \\普通用户在slave节点上只有只读权限
[root@mysql-slave1 ~]# systemctl start mariadb
[root@mysql-slave1 ~]# mysql
从master的当前节点开始同步复制数据
MariaDB [(none)]> CHANGE MASTER TO MASTER_HOST='172.16.252.30',MASTER_USER='repluser',MASTER_PASSWORD='replpass',MASTER_LOG_FILE='master-log.000004',MASTER_LOG_POS=498;
开启复制进程
MariaDB [(none)]> START SLAVE;
显示slave节点的状态信息
MariaDB [(none)]> SHOW SLAVE STATUS\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 172.16.252.30
Master_User: repluser
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: master-log.000004
Read_Master_Log_Pos: 498
Relay_Log_File: relay-log.000002
Relay_Log_Pos: 530
Relay_Master_Log_File: master-log.000004
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 498
Relay_Log_Space: 818
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 1
mysql-slave2
[root@mysql-slave1 ~]# vim /etc/my.cnf.d/server.cnf
[mysqld]
server_id = 3
relay-log = relay-log
skip_name_resolve = ON
innodb_file_per_table = ON
read_only = ON
[root@mysql-slave2 ~]# systemctl start mariadb
[root@mysql-slave2 ~]# mysql
从master的当前节点开始同步复制数据
MariaDB [(none)]> CHANGE MASTER TO MASTER_HOST='172.16.252.30',MASTER_USER='repluser',MASTER_PASSWORD='replpass',MASTER_LOG_FILE='master-log.000004',MASTER_LOG_POS=498;
开启复制进程
MariaDB [(none)]> START SLAVE;
显示slave节点的状态信息
MariaDB [(none)]> SHOW SLAVE STATUS\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 172.16.252.30
Master_User: repluser
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: master-log.000004
Read_Master_Log_Pos: 498
Relay_Log_File: relay-log.000002
Relay_Log_Pos: 530
Relay_Master_Log_File: master-log.000004
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 498
Relay_Log_Space: 818
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 1
mysql-master
创建连接ProxySQL代理的用户和密码,并同步到各节点主机
MariaDB [(none)]> GRANT ALL ON *.* TO 'proxysql'@'172.16.%.%' IDENTIFIED BY 'proxypass';
ProxySQL
下载ProxySQL程序包到本地并安装
[root@ProxySQL ~]# ls proxysql-1.4.2-1-centos7.x86_64.rpm
proxysql-1.4.2-1-centos7.x86_64.rpm
[root@ProxySQL ~]# yum -y install ./proxysql-1.4.2-1-centos7.x86_64.rpm
[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
配置Proxy
[root@ProxySQL ~]# vim /etc/proxysql.cnf
datadir="/var/lib/proxysql"
admin_variables=
{
admin_credentials="admin:admin"
mysql_ifaces="127.0.0.1:6032;/tmp/proxysql_admin.sock" \\若本机mysql服务没有启用,则使用/tmp/proxysql_admin.sock套接字文件连接管理通信
}
mysql_variables=
{
threads=4 \\开启的线程数
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" \\若本机mysql服务没有启用,则使用/tmp/proxysql.sock套接字文件连接通信
default_schema="hidb"
stacksize=1048576
server_version="5.5.30"
connect_timeout_server=3000
}
mysql_servers =
(
{
address = "172.16.253.105"
port = 3306
hostgroup = 0
status = "ONLINE"
weight = 1
compression = 0
},
{
address = "172.16.252.82"
port = 3306
hostgroup = 1
status = "ONLINE"
weight = 1
compression = 0
},
{
address = "172.16.252.92"
port = 3306
hostgroup = 1
status = "ONLINE"
weight = 1
compression = 0
}
)
mysql_users:
(
{
username = "proxysql" # no default , required
password = "proxypass" # default: ''
default_hostgroup = 0 # default: 0
default_schema=“hidb”
active = 1 # default: 1
}
)
mysql_replication_hostgroups=
(
{
writer_hostgroup=0
reader_hostgroup=1
comment="repl cluster 1"
}
)
[root@ProxySQL ~]# service proxysql start
[root@ProxySQL ~]# ss -ntl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:3306 *:*
LISTEN 0 128 *:3306 *:*
LISTEN 0 128 *:3306 *:*
LISTEN 0 128 *:3306 *:*
测试
登录本机数据库,因为本机并没有安装mysql,故使用-S参数指定配置文件中指定的/tmp/proxysql.sock套接字文件通信,使用定义连接ProxySQL代理的账号密码登录
[root@ProxySQL ~]# mysql -S /tmp/proxysql.sock -uproxysql -pproxypass
Your MySQL connection id is 212
Server version: 5.5.30 (ProxySQL) \\显示mysql服务器的版本号
MariaDB [(none)]> CREATE DATABASE hidb;
MariaDB [(none)]> USE hidb;
MariaDB [hidb]> CREATE TABLE mytb(id INT,name CHAR(50));
MariaDB [hidb]> INSERT INTO mytb VALUES (1,'tom');
MariaDB [hidb]> SELECT * FROM mytb;
+------+------+
| id | name |
+------+------+
| 1 | tom |
+------+------+
CLAVE服务端数据实现了同步,即ProxySQL实现了代理数据库的效果,并达到了数据分离的效果
mysql-slave1
MariaDB [(none)]> SELECT * FROM hidb.mytb;
+------+------+
| id | name |
+------+------+
| 1 | tom |
+------+------+
Mysql实战之主从复制的读写分离的更多相关文章
- 基于Mysql-Proxy实现Mysql的主从复制以及读写分离(上)
基于Mysql-Proxy实现Mysql的主从复制以及读写分离(上) 上周BOSS给分配任务让实现一下Mysql数据库的主从复制以及读写分离,然后花了一盏茶的功夫进行了调研,发现主从复制数据库进行一番 ...
- 重新学习Mysql数据13:Mysql主从复制,读写分离,分表分库策略与实践
一.MySQL扩展具体的实现方式 随着业务规模的不断扩大,需要选择合适的方案去应对数据规模的增长,以应对逐渐增长的访问压力和数据量. 关于数据库的扩展主要包括:业务拆分.主从复制.读写分离.数据库分库 ...
- Mysql主从复制,读写分离(mysql-proxy),双主结构完整构建过程
下面介绍MySQL主从复制,读写分离,双主结构完整构建过程,不涉及过多理论,只有实验和配置的过程. Mysql主从复制(转载请注明出处,博文地址:) 原理是master将改变记录到二进制日志(bina ...
- linux上使用amoeba实现MySql集群,以及读写分离,主从复制
一.由于是MySql集群,所以就不可能只有一个MySql,需要多个MySql,具体安装步骤,可以参考http://www.cnblogs.com/ywzq/p/4882140.html这个地址进行安装 ...
- Mysql主从复制,读写分离
一个简单完整的 Mysql 主从复制,读写分离的示意图. 1. 首先搭建 Mysql 主从架构,实现 将 mater 数据自动复制到 slave MySQL 复制的工作方式很简单,一台服务器作为主机, ...
- Mysql 主从复制,读写分离设置
一个简单完整的 Mysql 主从复制,读写分离的示意图. 1. 首先搭建 Mysql 主从架构,实现 将 mater 数据自动复制到 slave MySQL 复制的工作方式很简单,一台服务器作为主机, ...
- mysql主从复制以及读写分离
之前我们已经对LNMP平台的Nginx做过了负载均衡以及高可用的部署,今天我们就通过一些技术来提升数据的高可用以及数据库性能的提升. 一.mysql主从复制 首先我们先来看一下主从复制能够解决什么问题 ...
- JAVAEE——宜立方商城13:Mycat数据库分片、主从复制、读写分离、100%Linux中成功安装Mysql的方法
1 海量数据的存储问题 如今随着互联网的发展,数据的量级也是撑指数的增长,从GB到TB到PB.对数据的各种操作也是愈加的困难,传统的关系性数据库已经无法满足快速查询与插入数据的需求.这个时候NoSQL ...
- 基于Mysql-Proxy实现Mysql的主从复制以及读写分离(下)
基于Mysql-Proxy实现Mysql的主从复制以及读写分离(下) 昨天谈到了Mysql实现主从复制,但由于时间原因并未讲有关读写分离的实现,之所以有读写分离,是为了使数据库拥有双机热备功能,至于双 ...
随机推荐
- [论文理解]MetaAnchor: Learning to Detect Objects with Customized Anchors
MetaAnchor: Learning to Detect Objects with Customized Anchors Intro 本文我其实看了几遍也没看懂,看了meta以为是一个很高大上的东 ...
- Python pep8代码规范
title: Python pep8代码规范 tags: Python --- 介绍(Introduction) 官方文档:PEP 8 -- Style Guide for Python Code 很 ...
- x5webview 微信H5支付
mWebView.setWebViewClient(new WebViewClient() { // @Override // public boolean shouldOverrideUrlLoad ...
- 详解----memcache服务端与客户端
Memcache是danga.com的一个项目,用这个缓存项目来构建自己大负载的网站,来分担数据库的压力. 它可以应对任意多个连接,使用非阻塞的网络IO.由于它的工作机制是在内存中开辟一块空间,然后建 ...
- Java - 网络
要事为先,你如果想要在这个行业发展下去的话,实际上三角形的三个点在支撑着你发展,一个是技术.一个是管理(不是说管理别人,是管理你自己的时间,管理你自己的精力).还有一个就是沟通,注重这三点均衡的发展. ...
- ASP.NET 自定义路由 RouteBase
适用场景:当前项目有一个接口:http://xxx.com/a.aspx,现在我们在不需要a.aspx这个文件的,直接处理这个地址的请求,我们可以做很多的事情,比如,直接返回一个静态的JSON文件内容 ...
- jquery 省市区联动插件
使用方式: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF ...
- PHP array_multisort()函数超详细理解
项目中用到这个函数了 ,起初对这个函数一直是懵逼状态,文档都看的朦朦胧胧的 网上无意间看到这篇文章 ,写的超级详细,收藏了 . 当然要先放原地址:https://www.cnblogs.com/WuN ...
- C#小知识点积累
1.sealed 修饰符 概念: C#提出了一个密封类(sealed class)的概念,帮助开发人员来解决这一问题. 密封类在声明中使用sealed 修饰符,这样就可以防止该类被其它类继承.如果试图 ...
- Python虚拟机类机制之自定义class(四)
用户自定义class 在本章中,我们将研究对用户自定义class的剖析,在demo1.py中,我们将研究单个class的实现,所以在这里并没有关于继承及多态的讨论.然而在demo1.py中,我们看到了 ...