mysql主从复制集群搭建

7 篇文章 0 订阅
7 篇文章 0 订阅

mysql主从复制集群搭建(基于日志点的复制)

安装mysql

【注意】防火墙开启的情况下默认的mysql的3306端口会被阻止,所以需要我们手动设置3306端口在防火墙列表中的列外。

mysql配置步骤

  • 在主master端建立复制用户(此用户是slave端用来读取master端binarylog日志所使用)
  • 备份master端的数据,并在slave端恢复。
  • 使用change master命令配置复制。

配置示例

  • 集群有三个节点,节点A、节点B、节点C
    其中A、B互为主从,C为从节点,B为C的主节点;

节点A配置my.cnf文件

这里写图片描述

配置文件中增加的内容如下:

#table name as lowercase
  lower_case_table_names=1
  max_allowed_packet = 200M

  #master conf
  server-id=154
  log-bin=mysql-bin
  log-bin-index=mysql-bin.index
  binlog-do-db = softcentric
  binlog-ignore-db = mysql
  binlog-ignore-db=information_schema
  binlog-ignore-db=performance_schema
  log-slave-updates
  sync_binlog = 1
  auto_increment_offset = 1
  auto_increment_increment = 2
  replicate-do-db = softcentric
  replicate-ignore-db = mysql,information_schema,performance_schema

节点B配置my.cnf文件

lower_case_table_names=1
max_allowed_packet = 200M
#slave conf
server-id=157
log-bin=mysql-bin
log-bin-index=mysql-bin.index
binlog-do-db = softcentric
binlog-ignore-db = mysql
binlog-ignore-db=information_schema
binlog-ignore-db=performance_schema
replicate-do-db = softcentric
replicate-ignore-db = mysql,information_schema,performance_schema
log-slave-updates
sync_binlog = 1
auto_increment_offset = 2
auto_increment_increment = 2

节点C配置my.cnf文件

lower_case_table_names=1
max_allowed_packet = 200M
#slave conf
server-id=158
log-bin=mysql-bin
log-bin-index=mysql-bin.index
relay-log=slave-relay-bin
relay-log-index=slave-relay-bin.index
binlog-do-db = softcentric
binlog-ignore-db = mysql
binlog-ignore-db=information_schema
binlog-ignore-db=performance_schema

节点A上进入mysql 命令行:

  • 第一步:创建用于复制的用户
mysql> create user 'repl_user'@'192.168.31.15%' identified by '123456';
  • 赋予用户复制的权限
# 在节点A的mysql上授权,让节点B(157)主机使用的repl_user用户有复制的权限

mysql> grant replication slave,replication client on *.* to 'repl_user'@'192.168.31.157';
mysql> flush privileges;
mysql> show master status\G;
*************************** 1. row ***************************
             File: mysql-bin.000003
         Position: 154
     Binlog_Do_DB: softcentric
 Binlog_Ignore_DB: mysql,information_schema,performance_schema
Executed_Gtid_Set: 
1 row in set (0.00 sec)

ERROR: 
No query specified

在节点B(157)上指定master通过设置参数,其中master_log_filemaster_log_pos两个参数来自上一步的输出中的(FilePosition)的值。

mysql> change master to master_host='192.168.31.154',master_port=3306,master_user='repl_user',master_password='123456',master_log_file='mysql-bin.000003',master_log_pos=154;
  • 节点B启动slave并查看状态:
mysql>slave start;
mysql> show master status\G;
*************************** 1. row ***************************
             File: mysql-bin.000003
         Position: 154
     Binlog_Do_DB: softcentric
 Binlog_Ignore_DB: mysql,information_schema,performance_schema
Executed_Gtid_Set: 
1 row in set (0.00 sec)

ERROR: 
No query specified

mysql> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.31.154
                  Master_User: repl_user
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000003
          Read_Master_Log_Pos: 154
               Relay_Log_File: localhost-relay-bin.000002
                Relay_Log_Pos: 320
        Relay_Master_Log_File: mysql-bin.000003
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: softcentric
          Replicate_Ignore_DB: mysql,information_schema,performance_schema

在master A 上导入数据库表,在节点B(slave)上查看同步的结果

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| softcentric        |
| sys                |
+--------------------+
5 rows in set (0.02 sec)

#这里多了一个softcentric数据库

mysql> use softcentric;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> show tables;
+------------------------+
| Tables_in_softcentric  |
+------------------------+
| act_evt_log            |
| act_ge_bytearray       |
| act_ge_property        |
| act_hi_actinst         |
| act_hi_attachment      |
| act_hi_comment         |
| act_hi_detail          |
| act_hi_identitylink    |
| act_hi_procinst        |
| act_hi_taskinst        |
| act_hi_varinst         |
| act_id_group           |

上面我们看到,数据库中的表已经创建,数据已经复制完成。

在slave节点C上(节点B 157作为节点C的master)设置master的参数:

mysql> change master to master_host='192.168.31.157',master_port=3306,master_user='repl_user',master_password='123456',master_log_file='mysql-bin.000003',master_log_pos=154;

查看同步的结果:

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

mysql> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.31.157
                  Master_User: repl_user
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000003
          Read_Master_Log_Pos: 154
               Relay_Log_File: slave-relay-bin.000002
                Relay_Log_Pos: 320
        Relay_Master_Log_File: mysql-bin.000003
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
.....

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| softcentric        |
| sys                |
+--------------------+
5 rows in set (0.07 sec)

mysql> use softcentric;show tables;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
+------------------------+
| Tables_in_softcentric  |
+------------------------+
| act_evt_log            |
| act_ge_bytearray       |
| act_ge_property        |
| act_hi_actinst         |
| act_hi_attachment      |
| act_hi_comment         |
| act_hi_detail          |

参考内容
mycat+mysql集群:实现读写分离,分库分表
高可用mysql 书籍

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MySQL主从集群搭建需要以下步骤: 1. 安装MySQL:首先,在所有服务器上安装MySQL数据库软件。可以选择使用源代码编译安装或者使用操作系统自带的软件包管理器进行安装。 2. 配置主服务器:在主服务器上编辑MySQL配置文件(my.cnf),确保启用二进制日志(binlog)功能。在配置文件中添加以下参数: ``` server_id = <主服务器ID> log_bin = <二进制日志文件路径> ``` 重启MySQL服务使配置生效。 3. 备份和导出数据:在主服务器上备份现有数据,并将其导出到备份文件中。 4. 配置从服务器:在从服务器上编辑MySQL配置文件,设置与主服务器相同的MySQL版本,并添加以下参数: ``` server_id = <从服务器ID> relay_log = <中继日志文件路径> read_only = 1 ``` 重启MySQL服务使配置生效。 5. 同步数据:将备份文件复制到从服务器上,并恢复数据库。然后,使用主服务器上的二进制日志文件(binlog)将从服务器与主服务器同步。可以使用工具如mysqldump和mysqlbinlog来完成此操作。 6. 配置主从复制:在主服务器上创建一个用于复制的用户,并为该用户授予适当的权限。然后,在从服务器上配置主从复制,指定主服务器的IP地址、复制用户的凭据等信息。 7. 启动主从复制:在从服务器上启动复制过程,将从服务器连接到主服务器。可以使用CHANGE MASTER TO语句来启动复制。 8. 测试主从复制:验证主从复制是否正常工作。可以通过在主服务器上进行数据更改并观察从服务器上是否同步来测试。 以上是MySQL主从集群搭建的基本步骤,根据实际情况可能还需要进行其他配置和调整。建议在搭建集群之前仔细阅读相关文档,并确保了解所有操作的影响和风险。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值