---
title: "Linux Mysql 5.6 主从（Master/Slave）同步配置"
author: "feicklpp"
date: "2018-08-21T16:30:16Z"
tags: []
categories: ["数据库"]
slug: "linux-mysql-5-6-master-slave"
draft: false
lastmod: "2018-08-22T10:58:37Z"
id: 2429066
url: "https://feicklpp.iteye.com/blog/2429066"
---

**安装环境：**

操作系统：CentOS 6.7 64位

数据库：mysql 5.6

主库（master）IP：192.168.1.91

从库（slave）IP：192.168.1.92

 

主从数据库的同步主要通过bin-log 日志进行读取赋值，所有操作sql都会在bin-log中展示，所以主从库赋值也主要是通过日志方式来访问。

 

**Master配置：**

在Linux下mysql配置文件/etc/my.cnf，添加一下配置：

 

```
log-bin=mysql-bin
server-id=2
binlog-ignore-db=information_schema
binlog-ignore-db=cluster
binlog-ignore-db=mysql
binlog-do-db=test
```

 

 

server-id：数据库唯一标识，可任意设置

binlog-ignore-db：同步时忽略的数据库

binlog-do-db：需要同步的数据库

 

完整配置如下：

![](http://dl2.iteye.com/upload/attachment/0130/5409/ad96caf8-38a6-3f96-830d-0935a8298ec8.png)  
 

&lt;!--\[if !supportLists]--&gt;1.       &lt;!--\[endif]--&gt;重启mysql：service mysql restart

&lt;!--\[if !supportLists]--&gt;2.       &lt;!--\[endif]--&gt;进入mysql：mysql –u root –p

&lt;!--\[if !supportLists]--&gt;3.       &lt;!--\[endif]--&gt;显示主库信息：mysql&gt;show master status;

![](http://dl2.iteye.com/upload/attachment/0130/5411/015c108c-2d2a-367f-bba9-174d0bc69c37.png)  
 

 

 

**Slave配置：**

从库同样修改mysql配置文件 /etc/my.cnf，主要配置如下：

```
log-bin=mysql-bin
server-id=3
binlog-ignore-db=information_schema
binlog-ignore-db=cluster
binlog-ignore-db=mysql
replicate-do-db=test
replicate-ignore-db=mysql
log-slave-updates
slave-skip-errors=all
slave-net-timeout=60
```

 

完整配置如下：

![](http://dl2.iteye.com/upload/attachment/0130/5413/9d7baa38-ab93-3570-96da-dbf2e9d632e1.png)  
 

 

注：Mysql5.6 之前版本不能在配置文件中配置master相关配置信息：

master-host=192.168.1.91 #Master的主机IP

master-user=root

master-password=mysql password #Master的MySQL密码

 

修改my.cnf文件后进入mysql

mysql&gt; stop slave;  #关闭Slave

mysql&gt; change master to master\_host='192.168.1.91',master\_user='root',master\_password='123456',master\_log\_file='mysql-bin.000001', master\_log\_pos=5;

mysql&gt; start slave;  #开启Slave

 

查看slave：

mysql&gt; show slave status \\G;

mysql&gt; show slave status \\G;

\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\** 1. row \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\**

               Slave\_IO\_State: Waiting for master to send event

                  Master\_Host: 192.168.1.91

                  Master\_User: root

                  Master\_Port: 3306

                Connect\_Retry: 60

              Master\_Log\_File: mysql-bin.000002

          Read\_Master\_Log\_Pos: 120

               Relay\_Log\_File: localhost-relay-bin.000006

                Relay\_Log\_Pos: 283

        Relay\_Master\_Log\_File: mysql-bin.000002

             Slave\_IO\_Running: Yes

            Slave\_SQL\_Running: Yes

              Replicate\_Do\_DB: ufind\_db,test

          Replicate\_Ignore\_DB: mysql

           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: 120

              Relay\_Log\_Space: 623

              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: 2

                  Master\_UUID: a2e674e1-a536-11e8-92da-000c29d0ad20

             Master\_Info\_File: /data/mysql/master.info

                    SQL\_Delay: 0

          SQL\_Remaining\_Delay: NULL

      Slave\_SQL\_Running\_State: Slave has read all relay log; waiting for the slave I/O thread to update it

           Master\_Retry\_Count: 86400

                  Master\_Bind:

      Last\_IO\_Error\_Timestamp:

     Last\_SQL\_Error\_Timestamp:

               Master\_SSL\_Crl:

           Master\_SSL\_Crlpath:

           Retrieved\_Gtid\_Set:

            Executed\_Gtid\_Set:

                Auto\_Position: 0

1 row in set (0.00 sec)

 

注：在该处可能会有如下错误：

Got fatal error 1236 from master when reading data from binary log: 'Could not find first log file name in binary log index file'

解决方法：

mysql&gt; stop slave;

mysql&gt; reset slave;

mysql&gt; start slave;

 

在主库中配置文件中需要添加binlog-do-db 需同步的数据库的配置信息，同样在从库中也要添加需要赋值的replicate-do-db 的数据库的配置信息。

 

**访问配置：**

访问通过360的Atlas代理方式访问两个数据库，代码GitHub下载，连接地址：https://github.com/Qihoo360/Atlas

下载Atlas-XX.el6.x86\_64.rpm 

linux下安装

rpm –i Atlas-XX.el6.x86\_64.rpm 

修改mysql-proxy/conf/test.cnf文件内容，修改后内容如下

```
[mysql-proxy]

 

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

 

#管理接口的用户名

admin-username = root

 

#管理接口的密码

admin-password = 123456

 

#Atlas后端连接的MySQL主库的IP和端口，可设置多项，用逗号分隔

proxy-backend-addresses = 192.168.1.91:3306

 

#Atlas后端连接的MySQL从库的IP和端口，@后面的数字代表权重，用来作负载均衡，若省略则默认为1，可设置多项，用逗号分隔

proxy-read-only-backend-addresses = 192.168.1.92:3306@1

 

#用户名与其对应的加密过的MySQL密码，密码使用PREFIX/bin目录下的加密程序encrypt加密，下行的user1和user2为示例，将其替换为你的MySQL的用户名和加密密码！

pwds = root:/iZxz+0GRoA=

 

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

daemon = false

 

#设置Atlas的运行方式，设为true时Atlas会启动两个进程，一个为monitor，一个为worker，monitor在worker意外退出后会自动将其重启，设为false时只有worker，没有monitor，一般开发调试时设为false，线上运行时设为true,true后面不能有空格。

keepalive = false

 

#工作线程数，对Atlas的性能有很大影响，可根据情况适当设置

event-threads = 4

 

#日志级别，分为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
```

 

 

启动

mysql-proxy/bin/mysql-proxyd test start

 

启动后正常访问mysql方式访问，访问地址192.168.1.91 端口1234

可查看日志mysql-proxy/log/sql\_test.log

 

读数据库是访问的是192.168.1.92，写数据库时访问的是192.168.1.91