搭建互为主从的MySQL服务器

发布时间:2020-07-24 07:56:19 作者:高鹏举
来源:网络 阅读:282

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服务
[root@node1 ~]# systemctl restart mysqld
在node2上重启MySQL服务
[root@node2 ~]# systemctl restart mysqld

4、在node1上进入MySQL数据库,且配置允许从node2同步的账号
[root@node1 ~]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
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' identified by 'repl@123';
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同步的账号
[root@node2 ~]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
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' identified by 'repl@123';
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. centos7 mysql互为主从+keepalived
  2. Mysql 主从搭建

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

搭建互为主从的mysql服务器 互为主从 mysql服务器

上一篇:Android布局—Layout_weight

下一篇:Linux 下搭建 WordPress 个人站点

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》