Centos下MySQL主从同步配置

说明:因为MySQL不一样版本之间的(二进制日志)binlog格式可能会不同,html

所以最好的搭配组合是Master的MySQL版本和Slave的版本相同或者更低,
Master的版本确定不能高于Slave版本。(版本向下兼容)java

1、环境mysql

        主机: master操做系统:centos 7 64位sql

                   IP:192.168.119.253数据库

                   MySQL版本:5.5.50-MariaDBcentos

         从机:  slave操做系统:centos 7 64位安全

               IP:192.168.119.252服务器

                   MySQL版本:5.5.50-MariaDB网络

2、建立数据库socket

分别登陆master机和slave机的mysql:mysql –u root –p

建立数据库:create database repl;

3、master机和slave机的相关配置

一、修改master机器中mysql配置文件my.cnf,该文件在/etc目录下

在[mysqld]配置段添加以下字段

server-id=1
log-bin=mysql-bin
log-slave-updates=1
binlog-do-db=repl  #须要同步的数据库,若是没有本行表示同步全部的数据库
binlog-ignore-db=mysql  #被忽略的数据

在master机上为slave机添加一同步账号

 MariaDB[(none)]>grant replication slave on *.* to 'repl'@'192.168.119.252' identified by '123456';

 MariaDB[(none)]>flush  privileges;

 

重启master机的mysql服务:service mysqld restart

用show master status 命令看日志状况

MariaDB[(none)]>show master status;

经过该命令得到File和Position,在slave中有用 。注:基准这里的“mysql-bin.000001”和“245”,在下面 “(3)设置Slave复制”的配置中会用到

二、修改slave机中mysql配置文件

(1)修改slave机器中mysql配置文件my.cnf,该文件在/etc目录下

一样在[mysqld]字段下添加以下内容

server-id=2
log-bin= mysql-bin
relay-log= mysql-relay-bin
read-only=1
log-slave-updates=1
replicate-do-db=repl #要同步的数据库,不写本行表示同步全部数据库

而后重启slave机的mysql:service mysqld restart

(2)在slave机上验证对主机链接

 mysql -h192.168.119.253 -urepl -p123456

MariaDB[(none)]>show grants for repl@192.168.119.252;

(3)设置Slave复制

CHANGE MASTER TO
MASTER_HOST='192.168.119.253',
MASTER_USER='repl',
MASTER_PASSWORD='123456',
MASTER_PORT=3306,
MASTER_LOG_FILE='mysql-bin.000001',
MASTER_LOG_POS=245,
MASTER_CONNECT_RETRY=10;

(4)启动Slave

运行SHOW SLAVE STATUS查看输出结果:

主要查看Slave_IO_Running和Slave_SQL_Running 两列是否都为YES

 

4、测试主从服务器是否能同步

在主服务器上面新建一个表,必须在repl数据下

mysql> use repl

Database changed

mysql> create table test(id int,name char(10));

Query OK, 0 rows affected (0.00 sec)

mysql> insert into test values(1,'zaq');

Query OK, 1 row affected (0.00 sec)

mysql> insert into test values(1,'xsw');

Query OK, 1 row affected (0.00 sec)

mysql> select * from test;

+------+------+

| id    | name |

+-------+------+

|    1    | zaq   |

|    1    | xsw |

+-------+------+

2 rows in set (0.00 sec)

在从服务器查看是否同步过来

mysql> use repl;

Database changed

mysql> select * from test;

+------+------+

| id    | name |

+------+------+

|     1 | zaq   |

|     1 | xsw |

+------+------+

2 rows in set (0.00 sec)

说明已经配置成功。

 

6、扩展——MySQL主从复制几个重要的启动选项

(1) log-slave-updates
log-slave-updates这个参数用来配置从服务器的更新是否写入二进制日志,这个选项默认是不打开的,可是,若是这个从服务器B是服务器A的从服务器,同时还做为服务器C的主服务器,那么就须要开发这个选项,这样它的从服务器C才能得到它的二进制日志进行同步操做

(2) master-connect-retry

master-connect-retry这个参数是用来设置在和主服务器链接丢失的时候,重试的时间间隔,默认是60秒

(3) read-only

read-only是用来限制普通用户对从数据库的更新操做,以确保从数据库的安全性,不过若是是超级用户依然能够对从数据库进行更新操做

(4) slave-skip-errors

在复制过程当中,因为各类的缘由,从服务器可能会遇到执行BINLOG中的SQL出错的状况,在默认状况下,服务器会中止复制进程,再也不进行同步,等到用户自行来处理。

Slave-skip-errors的做用就是用来定义复制过程当中从服务器能够自动跳过的错误号,当复制过程当中遇到定义的错误号,就能够自动跳过,直接执行后面的SQL语句。

--slave-skip-errors=[err1,err2,…….|ALL]

但必须注意的是,启动这个参数,若是处理不当,极可能形成主从数据库的数据不一样步,在应用中须要根据实际状况,若是对数据完整性要求不是很严格,那么这个选项确实能够减轻维护的成本

7、问题排查

1.重启发现mariaDB没法启动能够经过查看日志来肯定问题

 2.错误在主机中执行了

CHANGE MASTER TO
MASTER_HOST='192.168.119.253',
MASTER_USER='repl',
MASTER_PASSWORD='123456',
MASTER_PORT=3306,
MASTER_LOG_FILE='mysql-bin.000001',
MASTER_LOG_POS=714,
MASTER_CONNECT_RETRY=10;

要清除slave,那么主机mariaDB中执行 reset slave all; 既能够

 

8.SHOW SLAVE STATUS输出的各列的含义详解

MASTER_LOG_FILE and MASTER_LOG_POS are the coordinates at which the slave I/O thread should begin reading from the master the next time the thread starts. 
RELAY_LOG_FILE and RELAY_LOG_POS are the coordinates at which the slave SQL thread should begin reading from the relay log the next time the thread starts. 
If you specify either of MASTER_LOG_FILE or MASTER_LOG_POS, you cannot specify RELAY_LOG_FILE or RELAY_LOG_POS. 
If you specify either of MASTER_LOG_FILE or MASTER_LOG_POS, you also cannot specify MASTER_AUTO_POSITION = 1 (described later in this section). 
If neither of MASTER_LOG_FILE or MASTER_LOG_POS is specified, the slave uses the last coordinates of the slave SQL thread before CHANGE MASTER TO was issued. 
This ensures that there is no discontinuity in replication, even if the slave SQL thread was late compared to the slave I/O thread, 
when you merely want to change, say, the password to use.
翻译:
MASTER_LOG_FILEMASTER_LOG_POS 下次slave线程启动从主机中读取数据的位置
RELAY_LOG_FILERELAY_LOG_POS 下次SQL线程启动从relay log中读取数据的位置
若是指定了MASTER_LOG_FILE 和MASTER_LOG_POS,那么就不能在指定 RELAY_LOG_FILERELAY_LOG_POS ,同时也不能指定MASTER_AUTO_POSITION=1
若是MASTER_LOG_FILE 和MASTER_LOG_POS都不指定, 从机将采起执行 CHANGE MASTER TO以前的最后一次的位置


MASTER_BIND is for use on replication slaves having multiple network interfaces, and determines which of the slave's network interfaces is chosen for connecting to the master.
翻译:
MASTER_BIN 针对有多个网络接口的经过它来决定使用那个网络接口MASTER_CONNECT_RETRY specifies how many seconds to wait between connect retries. The default is 60. 翻译:
MASTER_CONNECT_RETRY 指定多久重连,默认是60秒

MASTER_DELAY specifies how many seconds behind the master the slave must lag. An event received from the master is not executed until at least interval seconds later than its execution
on the master. The default is 0. An error occurs if interval is not a nonnegative integer in the range from 0 to 231−1
翻译:
MASTER_DELAY 指定相对于主机延迟多久 MASTER_USER and MASTER_PASSWORD are the user name and password of the account to use for connecting to the master
翻译:
MASTER_USER 和 MASTER_PASSWORD 连接主机的帐号密码 MASTER_HOST and MASTER_PORT are the host name (or IP address) of the master host and its TCP/IP port
   Note   Replication cannot use Unix socket files. You must be able to connect to the master MySQL server using TCP/IP.
翻译:
MASTER_HOST 和 MASTER_PORT 主机的计算机名或IP地址,以及端口号
  注意:主从复制必须使用TCP/IP,不能使用socke
MASTER_RETRY_COUNT limits the number of reconnection attempts and updates the value of the Master_Retry_Count column in the output of SHOW SLAVE STATUS. 翻译:
  MASTER_RETRY_COUNT 限制重连的次数

RELAY_LOG_FILE can use either an absolute or relative path, and uses the same base name as MASTER_LOG_FILE. (Bug #12190)
翻译:
  RELAY_LOG_FILE 可使用相对或绝对的路径,或使用 MASTER_LOG_FILE相同的基础的文件名 IGNORE_SERVER_IDS takes a comma-separated list of 0 or more server IDs.
翻译:
  IGNORE_SERVER_IDS 使用都好分割的server IDS

  

 

参考:http://www.cnblogs.com/meetrice/p/5311839.html

How to Setup MariaDB (Master-Slave) Replication in CentOS/RHEL 7 and Debian 8

相关文章
相关标签/搜索