当前位置: 代码网 > it编程>数据库>Mysql > MySQL主从同步配置(一主一从)的实现

MySQL主从同步配置(一主一从)的实现

2026年07月17日 Mysql 我要评论
前言:mysql 主从同步是生产环境中最常用的高可用方案之一,也是运维工程师必备的基础技能。然而在实际搭建过程中,备份时机、binlog 位置、用户创建顺序等细节问题常常导致复制链路失败。本文基于实际

前言:

mysql 主从同步是生产环境中最常用的高可用方案之一,也是运维工程师必备的基础技能。然而在实际搭建过程中,备份时机、binlog 位置、用户创建顺序等细节问题常常导致复制链路失败。

本文基于实际生产经验,整理出一套标准化的 mysql 主从同步操作流程(sop),每一步都经过验证,确保“按步骤做就能成功”。

一、环境准备

主机ip地址
mysql1192.168.88.10
mysql2192.168.88.11

二、主服务器配置

#1.启用binlog并为binlog起名:
[root@mysql1 ~]# sed -i '$a log_bin=mysql-bin' /etc/my.cnf.d/mysql-server.cnf
#2.检查binlog日志是否开启:
[root@mysql1 ~]# mysql -e "show variables like 'log_bin'"
+---------------+-------+
| variable_name | value |
+---------------+-------+
| log_bin       | on    |
+---------------+-------+
#on为已开启binlog,off则是没有开启
#3.配置server_id,全局唯一标识,不允许重复:
[root@mysql1 ~]# sed -i '$a server_id=10' /etc/my.cnf.d/mysql-server.cnf
#4.重启mysqld服务
[root@mysql1 ~]# systemctl restart mysqld
#5.创建用于主从同步的用户:
[root@mysql1 ~]# mysql -e "create user replicauser1@'192.168.88.11' identified by'123456';"
#注意:mysql的用户完整标识是 用户名@登录主机,用户名相同、host不同 = 两个完全独立账号。
#6.给主从同步的用户授权:
[root@mysql1 ~]# mysql -e "grant replication slave on *.* to replicauser1@'192.168.88.11'"
#*.*代表所有库和所有表
#7.检查主服务器是否配置ssl证书:
[root@mysql1 ~]# mysql -e "show variables like '%ssl'"
+---------------+-------+
| variable_name | value |
+---------------+-------+
| have_openssl  | yes   |
| have_ssl      | yes   |
+---------------+-------+
#yes表示配置了ssl证书
8.对数据全量备份:
[root@mysql1 ~]# mysqldump  -a > all.sql
#-a表示备份全部数据
9.将备份的数据发给192.168.88.11:
[root@mysql1 ~]# scp all.sql root@192.168.88.11:/root
10.查看主服务器信息:
[root@mysql1 ~]# mysql -e "show  master status;"
+------------------+----------+--------------+------------------+-------------------+
| file             | position | binlog_do_db | binlog_ignore_db | executed_gtid_set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 |      729 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
#file:当前正在写入的binlog文件名(从库配置source_log_file要填这个名字)
#position:binlog当前写入位置偏移量(从库source_log_pos填这个)

三、从服务器配置

1.配置server_id,全局唯一标识,不允许重复:
[root@mysql2 ~]# sed -i '$a server_id=11' /etc/my.cnf.d/mysql-server.cnf
2.重启mysqld服务:
[root@mysql2 ~]# systemctl restart mysqld
3.导入备份过来的数据:
[root@mysql2 ~]# mysql < all.sql
4.验证数据是否导入成功:
[root@mysql2 ~]# mysql -e "select user,host from mysql.user;"
+------------------+---------------+
| user             | host          |
+------------------+---------------+
| replicauser1     | 192.168.88.11 |
| mysql.infoschema | localhost     |
| mysql.session    | localhost     |
| mysql.sys        | localhost     |
| root             | localhost     |
+------------------+---------------+
#replicauser1@192.168.88.11是我们刚才创建的用户,出现代表恢复成功了
5.进入mysql配置主服务器信息:
[root@mysql2 ~]# mysql
mysql> change replication source to          #修改主从同步主库信息
       source_host="192.168.88.10",          #主库地址
       source_port=3306,                     #主库端口
       source_user="repilicauser1",          #用于主从同步的用户(故意写错用户)
       source_password="123456",             #用于主从同步用户的密码
       source_ssl=1,                         #主服务器没开启则不需要加
       source_log_file="mysql-bin.000001",   #主服务器的binlog文件名
       source_log_pos=729;                   #主服务器的段偏移量
query ok, 0 rows affected, 2 warnings (0.44 sec)
6.启动io/sql线程:
mysql> start replica;
7.验证主从同步是否设置成功:
mysql> show replica status\g
*************************** 1. row ***************************
             replica_io_state: connecting to source       #io线程一直尝试连接主库
                  source_host: 192.168.88.10              #主库ip地址
                  source_user: repilicauser1              #连接主库的用户名
                  source_port: 3306                       #主库mysql的端口号
                connect_retry: 60                         #连接失败再次连接的间隔
              source_log_file: mysql-bin.000001           #主库binlog日志文件名
          read_source_log_pos: 729                        #io线程读到主库的段偏移量
               relay_log_file: mysql2-relay-bin.000001    #从库中继日志文件名
                relay_log_pos: 4                          #中继日志回放位置
        relay_source_log_file: mysql-bin.000001           #中继日志对应主库原始的binlog文件
           replica_io_running: connecting                 #正在尝试连接
          replica_sql_running: yes                        #sql回放线程运行状态
8.下拉查看错误信息:
replica_io_running: connecting
replica_sql_running: yes
#io线程正在连接
#sql线程状态正常
last_io_errno: 1045
# io线程最近一次错误编号:1045
last_io_error: error connecting to source 'repilicauser1@192.168.88.10:3306'. 
this was attempt 1/86400, with a delay of 60 seconds between attempts.
message: access denied for user 'repilicauser1'@'mysql2' (using password: yes)
#错误信息可以直接发给ai,由ai解读
last_sql_errno: 0
#回放线程无错误
last_sql_error:
#回放线程无错误信息
9.停止io/sql线程
mysql> stop replica;
10.重置从服务器信息:
mysql> reset replica all;
11.重新配置主服务器信息:
[root@mysql2 ~]# mysql
mysql> change replication source to          #修改主从同步主库信息
       source_host="192.168.88.10",          #主库地址
       source_port=3306,                     #主库端口
       source_user="replicauser1",           #用于主从同步的用户(写正确的用户名)
       source_password="123456",             #用于主从同步用户的密码
       source_ssl=1,                         #主服务器没开启则不需要加
       source_log_file="mysql-bin.000001",   #主服务器的binlog文件名
       source_log_pos=729;                   #主服务器的段偏移量
query ok, 0 rows affected, 2 warnings (0.44 sec)
12.启动io/sql线程:
mysql> start replica;
13.验证主从同步是否设置成功:
mysql> show replica status\g
*************************** 1. row ***************************
     replica_io_state: waiting for source to send event
#等待主库推送新数据,表示连接主库成功
14.下拉查看信息:
replica_io_running: yes
replica_sql_running: yes
#一切正常
last_io_errno: 0
last_io_error: 
last_sql_errno: 0
last_sql_error:
#一切正常

四、验证主从同步效果

1.mysql1创建库表,插入数据
[root@mysql1 ~]# mysql -e "create database hello;"
[root@mysql1 ~]# mysql -e "create table hello.hi(id int,name char(10));"
[root@mysql1 ~]# mysql -e "insert into hello.hi values (1,zhangsan)"
[root@mysql1 ~]# mysql -e "insert into hello.hi values (1,'zhangsan')"

2.mysql2查看数据,验证结果
[root@mysql2 ~]# mysql -e "show databases;"
+--------------------+
| database           |
+--------------------+
| hello              |
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
[root@mysql2 ~]# mysql -d hello -e "show tables;"
+-----------------+
| tables_in_hello |
+-----------------+
| hi              |
+-----------------+
[root@mysql2 ~]# mysql -e "select * from hello.hi"
+------+----------+
| id   | name     |
+------+----------+
|    1 | zhangsan |
+------+----------+

结语:

以上就是 mysql 主从复制的完整搭建流程。核心要点可以总结为三句话:

  • 先开 binlog + 建用户 → 备份数据(备份文件必须包含 binlog 位置和复制用户)
  • 从库先配 server_id → 恢复数据 → 启动复制
  • 最后必须用 show replica status 验证双 yes,才算真正成功

到此这篇关于mysql主从同步配置(一主一从)的实现的文章就介绍到这了,更多相关mysql主从同步配置内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2026  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com