jane27 发表于 2018-10-8 10:39:52

搭建互为主从的MySQL服务器

  node1的IP地址:192.168.1.254
  node2的IP地址:192.168.1.253
  操作系统为RHEL7.3,MySQL版本为5.7.18
  1、修改node1的MySQL配置文件
  #注意server_id字段一定不能一样,由于前面的配置server_id已经配置,且其值唯一,所以此处不再进行配置
  修改node1的mysql配置文件
  # 服务器的ID,必须唯一,一般设置自己的IP
  server_id=254
  # 复制过滤:不需要备份的数据库(MySQL库一般不同步)
  binlog-ignore-db=mysql
  # 开启二进制日志功能,名字可以随便取,最好有含义(比如项目名)
  log-bin=lamp-mysql-bin
  # 为每个 session 分配的内存,在事务过程中用来存储二进制日志的缓存
  binlog_cache_size=1M
  # 主从复制的格式(mixed,statement,row,默认格式是 statement)
  binlog_format=mixed
  # 二进制日志自动删除/过期的天数。默认值为 0,表示不自动删除。
  expire_logs_days=7
  ## 跳过主从复制中遇到的所有错误或指定类型的错误,避免 slave 端复制中断。
  ## 如:1062 错误是指一些主键重复,1032 错误是因为主从数据库数据不一致
  slave_skip_errors=1062
  # 作为从服务器时的中继日志
  relay_log=lamp-mysql-relay-bin
  # log_slave_updates 表示 slave 将复制事件写进自己的二进制日志
  log_slave_updates=1
  # 主键自增规则,避免主从同步ID重复的问题
  auto_increment_increment=2# 自增因子(根据MySQL服务器数量修改,本例中是2台MySQL服务器)
  auto_increment_offset=1   # 自增偏移(从1开始)
  2、修改node2的MySQL配置文件
  # 复制过滤:不需要备份的数据库(MySQL库一般不同步)
  binlog-ignore-db=mysql
  # 开启二进制日志功能,名字可以随便取,最好有含义(比如项目名)
  log-bin=lamp-mysql-bin
  # 为每个 session 分配的内存,在事务过程中用来存储二进制日志的缓存
  binlog_cache_size=1M
  # 主从复制的格式(mixed,statement,row,默认格式是 statement)
  binlog_format=mixed
  # 二进制日志自动删除/过期的天数。默认值为 0,表示不自动删除。
  expire_logs_days=7
  ## 跳过主从复制中遇到的所有错误或指定类型的错误,避免 slave 端复制中断。
  ## 如:1062 错误是指一些主键重复,1032 错误是因为主从数据库数据不一致
  slave_skip_errors=1062
  # 作为从服务器时的中继日志
  relay_log=lamp-mysql-relay-bin
  # log_slave_updates 表示 slave 将复制事件写进自己的二进制日志
  log_slave_updates=1
  # 主键自增规则,避免主从同步ID重复的问题
  auto_increment_increment=2# 自增因子(根据MySQL服务器数量修改,本例中是2台MySQL服务器)
  auto_increment_offset=2   # 自增偏移(从2开始)
  3、分别在node1和node2上重启MySQL服务
  在node1上重启MySQL服务
  # systemctl restart mysqld
  在node2上重启MySQL服务
  # systemctl restart mysqld
  4、在node1上进入MySQL数据库,且配置允许从node2同步的账号
  # mysql -u root -p
  Enter password:
  Welcome to the MySQL monitor.Commands end with ; or \g.

  Your MySQL connection>  Server version: 5.7.18-log Source distribution
  Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.
  Oracle is a registered trademark of Oracle Corporation and/or its
  affiliates. Other names may be trademarks of their respective
  owners.
  Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

  mysql> grant replication slave, replication client on *.* to 'repl'@'192.168.1.253'>  mysql> flush privileges;
  Query OK, 0 rows affected (0.06 sec)
  5、在node1上查看并记录binlog文件的position和File的值,配置从机时会用到
  mysql> show master status;
  +-----------------------+----------+--------------+------------------+-------------------+
  | File                  | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
  +-----------------------+----------+--------------+------------------+-------------------+
  | lamp-mysql-bin.000001 |      631 |            | mysql            |                   |
  +-----------------------+----------+--------------+------------------+-------------------+
  1 row in set (0.00 sec)
  mysql>
  6、在node2上将node1作为自己的主服务器,并且开启slave状态。
  mysql> change master to master_host='192.168.1.254',master_user='repl', master_password='repl@123', master_port=3306, master_log_file='lamp-mysql-bin.000001', master_log_pos=631, master_connect_retry=30;
  mysql> start slave;
  7、在node2上查看自己的slave状态。
  mysql> show slave status\G;
  Slave_IO_Running: Yes
  Slave_SQL_Running: Yes
  以上两个参数要全部是Yes状态
  8、在node2上创建允许从node1同步的账号
  # mysql -u root -p
  Enter password:
  Welcome to the MySQL monitor.Commands end with ; or \g.

  Your MySQL connection>  Server version: 5.7.18-log Source distribution
  Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.
  Oracle is a registered trademark of Oracle Corporation and/or its
  affiliates. Other names may be trademarks of their respective
  owners.
  Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

  mysql> grant replication slave, replication client on *.* to 'repl'@'192.168.1.254'>  mysql> flush privileges;
  9、在node2上查看并记录binlog文件的position和File的值。
  mysql> show master status;
  +-----------------------+----------+--------------+------------------+-------------------+
  | File                  | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
  +-----------------------+----------+--------------+------------------+-------------------+
  | lamp-mysql-bin.000001 |      474 |            | mysql            |                   |
  +-----------------------+----------+--------------+------------------+-------------------+
  10、在node1上将node2作为自己的主服务器,并且开启slave状态。
  mysql> change master to master_host='192.168.1.253',master_user='repl', master_password='repl@123', master_port=3306, master_log_file='lamp-mysql-bin.000001', master_log_pos=474, master_connect_retry=30;
  mysql> start slave;
  11、在node1上查看自己的slave状态。
  mysql> show slave status\G;
  Slave_IO_Running: Yes
  Slave_SQL_Running: Yes
  以上两个参数要全部是Yes状态
  12、在node1或node2任意一台数据库中创建数据库,会自动同步到另外一台数据库
  mysql> create database netser;
  mysql> show databases;
  +--------------------+
  | Database         |
  +--------------------+
  | information_schema |
  | mysql            |
  | netser             |
  | performance_schema |
  | sys                |
  +--------------------+
  5 rows in set (0.09 sec)
  在另外一个节点,查看数据库,会看到完全一致的数据信息
  mysql> show databases;
  +--------------------+
  | Database         |
  +--------------------+
  | information_schema |
  | mysql            |
  | netser             |
  | performance_schema |
  | sys                |
  +--------------------+
  5 rows in set (0.06 sec)

页: [1]
查看完整版本: 搭建互为主从的MySQL服务器