mysql主从复制读写分离-Altas

mysql主从复制读写分离

本文读写分离使用的软件是Altas,altas是奇虎360公司开发的开源数据库代理软件。它是基于mysql-proxy开发而成的

它集中地响应应用的请求,依据用户事先设置的规则,将SQL请求发送到特定的数据库上执行。基于此可以实现负载均衡、读写分离、高可用性等需求。

mysql读写分离原理:

数据库层在高并发的情况下,i/o会产生瓶颈。而实际上用户读的请求要远远大于写的请求。

使用代理服务作为数据库前端,将不同的请求根据规则分配到不同的后端数据上面去,比如将写的请求分配给master数据库,将读的请求分配给slave数据库。master和slave可以是一个或多个做成负载均衡。master数据库再通过主从复制同步数据给slave.

了解mysql主从复制原理:http://lesliecheung.blog.51cto.com/12622169/1958255



环境介绍:

HostName OS IP 作用
master centos6.5 192.168.100.150 担任mysql主服务器
salve centos6.5 192.168.100.151 担任mysql从服务器
Altas centos6.5 192.168.100.152 担任mysql代理
ftp centos6.5 192.168.100.100 担任ftp为主从提供yum源,软件支持(可以使用公网yum源代替此主机)

1:主从安装mysql:

    [[email protected] ~]# yum -y install mysql-server
    
    [[email protected] ~]# yum -y install msyql-server

2:修改主从的配置文件,以支持bin_log日志记录

[[email protected] ~]# vi /etc/my.cnf 
7  log-bin=mysql-bin      ##支持bin-log日志记录,bin-log日志文件名以mysql-bin开头
8  server-id=150         ##服务的唯一标识符号,默认是1,这里方便记忆,我使用了ip最后一段

[[email protected] ~]# vi /etc/my.cnf 
7  server-id=151
[[email protected] ~]# /etc/init.d/mysqld start   ##重启服务
[[email protected] ~]# /etc/init.d/mysqld start

3:在主数据库上面授权给从复制的权限:

登陆服务器授权

[[email protected] ~]# mysqladmin -uroot password 123123
[[email protected] ~]# mysql -uroot -p123123
mysql> grant replication slave on *.* to ‘slave‘@"192.168.100.%" identified by ‘123123‘;
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;    ##刷新权限
Query OK, 0 rows affected (0.00 sec)
mysql>

查看主服务的bin-log日志文件信息:

需要记录file 和position两栏中内容:以查到的为准。

mysql> show master status;
+------------------+----------+--------------+------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000003 |      476 |              |                  |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)

4:在从服务器上修改自己的master的数据库

登入数据库

[[email protected] ~]# mysqladmin -uroot password 123123
[[email protected] ~]# mysql -uroot -p123123

设置从服务器读取master bin-log的相关信息

mysql> change master to 
    -> master_host=‘192.168.100.150‘,    ##master的ip
    -> master_user=‘slave‘,              ##授权允许复制的用户名
    -> master_password=‘123123‘,         ##授权允许复制密码
    -> master_log_file=‘mysql-bin.000003‘,   ##bin-log文件名,上一步在master上查到的信息
    -> master_log_pos=476;     ##偏移量,在master上查到的信息
Query OK, 0 rows affected (0.07 sec)

启动slave

mysql> start slave;
Query OK, 0 rows affected (0.00 sec)

插卡slave状态:

            ##查到的状态这两个为yes,下面没有error错误就正常
            Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
mysql> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.100.150
                  Master_User: slave
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000003
          Read_Master_Log_Pos: 706
               Relay_Log_File: mysqld-relay-bin.000002
                Relay_Log_Pos: 481
        Relay_Master_Log_File: mysql-bin.000003
             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: 706
              Relay_Log_Space: 637
              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: 
1 row in set (0.00 sec)
ERROR: 
No query specified
mysql>

5:测试:

在主数据库上新建库,查看库

mysql> 
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| test               |
+--------------------+
3 rows in set (0.00 sec)
mysql> create database test_databases;
Query OK, 1 row affected (0.00 sec)
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| test               |
| test_databases     |
+--------------------+
4 rows in set (0.00 sec)

在从数据库上查看库:

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| test               |
| test_databases     |
+--------------------+
4 rows in set (0.00 sec)

在master端授权:

mysql> grant all on *.* to [email protected]"192.168.100.%" identified by ‘123123‘;
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql>

在Atlas服务器下载安装软件

[[email protected] ~]# wget -O ./altas  https://github.com/Qihoo360/Atlas/releases/download/sharding-1.0.1/Atlas-sharding_1.0.1-el6.x86_64.rpm
[[email protected] ~]# ls
altas  anaconda-ks.cfg  install.log  install.log.syslog
[[email protected] ~]# file altas 
altas: RPM v3.0 bin i386/x86_64 Atlas-sharding_1.0.1-el6
[[email protected] ~]# rpm -ivh altas 
Preparing...                ########################################### [100%]
   1:Atlas                  ########################################### [100%]

修改配置文件:

[[email protected] ~]# vim /usr/local/mysql-proxy/conf/test.cnf

需要更改的地方:

proxy-backend-addresses = 192.168.100.150:3306
proxy-read-only-backend-addresses = 192.168.100.151:3306
pwds = root:++gAN07C/Q0=   ##这里用/usr/local/mysql-proxy/bin/encrypt 加上数据库授权的密码,生成密文密码填写在这里

生成密文密码

[[email protected] bin]# pwd
/usr/local/mysql-proxy/bin
[[email protected] bin]# ./encrypt 123123
++gAN07C/Q0=
daemon = true
sql-log = REALTIME
charset = utf8

全部的配置项,以及注释

#管理接口的密码
admin-password = pwd

#Atlas后端连接的MySQL主库的IP和端口,可设置多项,用逗号分隔
proxy-backend-addresses = 192.168.100.150:3306

#Atlas后端连接的MySQL从库的IP和端口,@后面的数字代表权重,用来作负载均衡,若省略则默认为1,可设置多项,用逗号分隔
#proxy-read-only-backend-addresses = 127.0.0.1:[email protected]
proxy-read-only-backend-addresses = 192.168.100.151:3306

#用户名与其对应的加密过的MySQL密码,密码使用PREFIX/bin目录下的加密程序encrypt加密,下行的user1和user2为示例,将其替换为你的MySQL的用户名和加密密码!
pwds = root:++gAN07C/Q0=

#设置Atlas的运行方式,设为true时为守护进程方式,设为false时为前台方式,一般开发调试时设为false,线上运行时设为true,true后面不能有空格。
daemon = true

keepalive = true

#工作线程数,对Atlas的性能有很大影响,可根据情况适当设置
event-threads = 8

#日志级别,分为message、warning、critical、error、debug五个级别

#带#号的为非必需的配置项目

#管理接口的用户名
admin-username = user

#管理接口的密码
admin-password = pwd

#Atlas后端连接的MySQL主库的IP和端口,可设置多项,用逗号分隔
proxy-backend-addresses = 192.168.100.150:3306

#Atlas后端连接的MySQL从库的IP和端口,@后面的数字代表权重,用来作负载均衡,若省略则默认为1,可设置多项,用逗号分隔
#proxy-read-only-backend-addresses = 127.0.0.1:[email protected]
proxy-read-only-backend-addresses = 192.168.100.151:3306

#用户名与其对应的加密过的MySQL密码,密码使用PREFIX/bin目录下的加密程序encrypt加密,下行的user1和user2为示例,将其替换为你的MySQL的用户名和加密密码!
pwds = root:++gAN07C/Q0=

#设置Atlas的运行方式,设为true时为守护进程方式,设为false时为前台方式,一般开发调试时设为false,线上运行时设为true,true后面不能有空格。
daemon = true

keepalive = true

#工作线程数,对Atlas的性能有很大影响,可根据情况适当设置
event-threads = 8

#日志级别,分为message、warning、critical、error、debug五个级别
log-level = message

#日志存放的路径
log-path = /usr/local/mysql-proxy/log

#SQL日志的开关,可设置为OFF、ON、REALTIME,OFF代表不记录SQL日志,ON代表记录SQL日志,REALTIME代表记录SQL日志且实时写入磁盘,默认为OFF
sql-log = REALTIME

#慢日志输出设置。当设置了该参数时,则日志只输出执行时间超过sql-log-slow(单位:ms)的日志记录。不设置该参数则输出全部日志。
#sql-log-slow = 10

#实例名称,用于同一台机器上多个Atlas实例间的区分
#instance = test

#Atlas监听的工作接口IP和端口
proxy-address = 0.0.0.0:1234

#Atlas监听的管理接口IP和端口
admin-address = 0.0.0.0:2345

#分表设置,此例中person为库名,mt为表名,id为分表字段,3为子表数量,可设置多项,以逗号分隔,若不分表则不需要设置该项
#tables = person.mt.id.3

#默认字符集,设置该项后客户端不再需要执行SET NAMES语句
charset = utf8

#允许连接Atlas的客户端的IP,可以是精确IP,也可以是IP段,以逗号分隔,若不设置该项则允许所有IP连接,否则只允许列表中的IP连接
#client-ips = 127.0.0.1, 192.168.1

#Atlas前面挂接的LVS的物理网卡的IP(注意不是虚IP),若有LVS且设置了client-ips则此项必须设置,否则可以不设置
#lvs-ips = 192.168.1.1

启动关闭代理服务:

[[email protected] bin]# ls
encrypt  mysql-binlog-dump  mysql-myisam-dump  mysql-proxy  mysql-proxyd  VERSION
[[email protected] bin]# ./mysql-proxyd test start
OK: MySQL-Proxy of test is started
[[email protected] bin]# ./mysql-proxyd test stop
OK: MySQL-Proxy of test is stopped
[[email protected] bin]# ./mysql-proxyd test start
OK: MySQL-Proxy of test is started
[[email protected] bin]# ./mysql-proxyd test restart
OK: MySQL-Proxy of test is stopped
OK: MySQL-Proxy of test is started

查看进程:

[[email protected] ~]# ps aux |grep mysql-proxy
root      1266  0.0  0.2  67156  1452 ?        S    19:24   0:00 /usr/local/mysql-proxy/bin/mysql-proxy --defaults-file=/usr/local/mysql-proxy/conf/test.cnf
root      1267  0.0  0.6 161460  3352 ?        Sl   19:24   0:01 /usr/local/mysql-proxy/bin/mysql-proxy --defaults-file=/usr/local/mysql-proxy/conf/test.cnf
root     16756  0.0  0.1 103248   876 pts/0    S+   20:55   0:00 grep mysql-proxy

安装mysql,只安装客户端。

[[email protected] ~]# yum -y install mysql

[[email protected] ~]# mysql -uroot -p123123 -h 192.168.100.152 -P1234

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| test               |
+--------------------+
3 rows in set (0.00 sec)
mysql> quit
Bye

查看日志信息:

[[email protected] ~]# tail -f /usr/local/mysql-proxy/log/test.log

2017-08-22 19:24:32: (message) proxy listening on port 0.0.0.0:1234

2017-08-22 19:24:32: (message) added read/write backend: 192.168.100.150:3306

2017-08-22 19:24:32: (message) added read-only backend: 192.168.100.151:3306

2017-08-22 19:24:32: (message) chassis-event-thread.c:235: starting 8 threads

2017-08-22 19:24:34: (message) chassis-unix-daemon.c:138: [angel] we try to keep PID=1267 alive

2017-08-22 19:24:34: (message) mysql-proxy 0.8.2 started - instance: test

2017-08-22 19:24:34: (message) proxy listening on port 0.0.0.0:1234

2017-08-22 19:24:34: (message) added read/write backend: 192.168.100.150:3306

2017-08-22 19:24:34: (message) added read-only backend: 192.168.100.151:3306

2017-08-22 19:24:34: (message) chassis-event-thread.c:235: starting 8 threads

##可以看到读的操作都交给slave数据库了,master可以读写操作,一般是只写。

登录到管理端口:(可以对后端的mysql数据库进行管理)

[[email protected] ~]# mysql -uuser -ppwd -h192.168.100.152 -P2345

mysql> select * from help;    ##查看管理帮助
+---------------------------------------+---------------------------------------------------------+
| command                               | description                                             |
+---------------------------------------+---------------------------------------------------------+
| SELECT * FROM help                    | shows this help                                         |
| SELECT * FROM backends                | lists the backends and their state                      |
| SET OFFLINE $backend_id               | offline backend server, $backend_id is backend_ndx‘s id |
| SET ONLINE $backend_id                | online backend server, ...                              |
| ADD MASTER $backend                   | example: "add master 127.0.0.1:3306", ...               |
| ADD SLAVE $backend                    | example: "add slave 127.0.0.1:3306", ...                |
| ADD GMASTER $group_id $backend        | example: "add gmaster 1 127.0.0.1:3306", ...            |
| ADD GSLAVE $group_id $backend         | example: "add gslave 1 127.0.0.1:3306", ...             |
| REMOVE BACKEND $backend_id            | example: "remove backend 1", ...                        |
| REMOVE GBACKEND $group_id $backend_id | example: "remove gbackend 1 1", ...                     |
| SELECT * FROM clients                 | lists the clients                                       |
| ADD CLIENT $client                    | example: "add client 192.168.1.2", ...                  |
| REMOVE CLIENT $client                 | example: "remove client 192.168.1.2", ...               |
| SELECT * FROM pwds                    | lists the pwds                                          |
| ADD PWD $pwd                          | example: "add pwd user:raw_password", ...               |
| ADD ENPWD $pwd                        | example: "add enpwd user:encrypted_password", ...       |
| REMOVE PWD $pwd                       | example: "remove pwd user", ...                         |
| SAVE CONFIG                           | save the backends to config file                        |
| SELECT VERSION                        | display the version of Atlas                            |
+---------------------------------------+---------------------------------------------------------+
19 rows in set (0.00 sec)
mysql> select * from backends;    ##查看后端mysql状态,工作类型
+----------+----------------------+-------+------+-------------+
| group_id | address              | state | type | backend_ndx |
+----------+----------------------+-------+------+-------------+
|       -1 | 192.168.100.150:3306 | up    | rw   |           1 |
|       -1 | 192.168.100.151:3306 | up    | ro   |           2 |
+----------+----------------------+-------+------+-------------+
2 rows in set (0.00 sec)
时间: 2024-07-31 08:30:20

mysql主从复制读写分离-Altas的相关文章

【实战】Amoeba 代理 MySQL 主从复制 + 读写分离 【提供源码包】

目录简介: 1· Amoeba 的介绍2· MySQL 主从复制原理3· MySQL 读写分离原理4· 实战案例5· 总结归纳 Amoeba 的介绍 1)Amoeba 是什么: 1·Amoeba 的中文名是:变形虫.它是一个以MySQL为底层数据存储,并对应用提供MySQL协议接口的proxy.它集中地响应应用的请求,依据用户事先设置的规则,将SQL请求发送到特定的数据库上执行.基于此可以实现负载均衡.读写分离.高可用性等需求. 2·Amoeba相当于一个SQL请求的路由器,目的是为负载均衡.读

mysql主从复制读写分离之——proxysql应用

一.说明ProxySQL是一个开源的MySQL代理服务器,这意味着它充当MySQL服务器和访问其数据库的应用程序之间的中介.ProxySQL可以通过在多个数据库服务器池之间分配流量来提高性能,并且如果一个或多个数据库服务器发生故障,还可以通过自动故障切换到备用数据库来提高可用性. 系统环境:master1:ubuntu16.04 mysql5.6 192.168.1.10 3307 master2:ubuntu16.04 mysql5.6 192.168.1.20 3307slave1: ubu

mysql主从复制-读写分离-原理

Mysql主从复制和读写分离 在实际的生产环境中,如果对mysql数据库的读和写都在一台数据库服务器中操作,无论是在安全性.高可用性,还是高并发等各个方面都是不能满足实际需求的.因此,一般通过主从复制的方式来同步数据,再通过读写分离来提升数据库的并发负载能力. Mysql主从复制和读写分离 l 主从复制: Mysql的主从复制和mysql的读写分离两者有紧密的联系,首先要部署主从复制,只有主从复制完成了,才能再此基础上进行数据的读写分离. Mysql支持的复制类型: 1. 基于语句的复制:在主服

利用amoeba实现mysql主从复制读写分离

一般大型网站为了缓解大量的并发访问,会在web端实现负载均衡,但是这是远远不够的.到了数据存储层.数据访问层,如果还是传统的架构,或者只是依靠一台服务器,大量的数据库连接操作,会导致数据库面临崩溃的危险.进而造成数据丢失,后果不堪设想.所以我们会考虑如何减少数据库的连接,一方面进行代码的优化,采用优秀的数据缓存技术如memcached,如果资金丰厚的话,必然会想到假设服务器群,来分担主数据库的压力.今天我们就利用MySQL主从配置,实现读写分离,分散数据库的压力.这种方式,很多网站都有应用,今天

数据库---mysql主从复制读写分离

http://m.open-open.com/m/lib/view/1413274853450.html 原理及架构分析 部署前准备 下载好源码包存放位置要与脚本中对应 mysql-5.5.22.tar.gz,cmake-2.8.6.tar.gz,amoeba-mysql-binary-2.2.0.tar.gz,jdk-6u14-linux-x64.bin selinux和iptables不做设置,关闭 系统光盘镜像为本地yum源,配置好yum文件 环境介绍: 主服务器(master):192.

mysql主从复制+读写分离 菜鸟入门

MYsql主从复制 1.mysql主从复制原理: Master将数据变化记录到二进制日志中[binary log] Slave将master的二进制日志[binary log]拷贝到自己的中继日志[relay log]中. Slave将中继入日志[relay log]事件在做一次,将数据变化,反应到自身数据库. 简述:mysql主从复制其实就是完全备份,和二进制日志备份还原的过程.二进制日志的还原基本上是实时进行的.注意不是完全实时,而是异步实时,主从直接的执行有延迟.如果master压力过大,

mysql主从复制读写分离

一.环境 1.主服务器操作系统:Mac OS MySQL版本:5.1.6 2.从服务器操作系统:Centos 6.5 MySQL版本:5.1.6 二.实战 2.1MySQL主从复制,读写分离示意图 MySQL 复制的工作方式很简单,一台服务器作为主机,一台或多台服务器作为从机.主机会把数据库的变化记录到日志.一旦这些变化被记录到日志,就会立刻(或者以设定的时间间隔)被送到从机. 2.2 主服务器IP:172.16.151.1 从服务器IP:172.16.151.130 在两台服务器分别安装好My

mysql 主从复制读写分离

目标:实现主从复制,读写分离 环境:mysql-proxy:192.168.1.21      version:5.0.77mysql-master:192.168.1.24     version:5.0.95mysql-slave:192.168.1.7       version:5.0.95 一.主从配置过程:登陆mysql-master:授权给从数据库服务器192.168.1.7mysql> GRANT REPLICATION SLAVE ON *.* to 'rep1'@'192.1

mysql主从复制-读写分离

mysql5.6版本: mysql的主从复制环境构建故障恢复 基于GTID的mysql中从复制 使用atlas实现读写分离 文章下载包地址: 链接:https://pan.baidu.com/s/1ht1y9HnqcfrDdWuPHUq_gg 提取码:cxql 原文地址:https://www.cnblogs.com/myself-technology-summary/p/10859878.html