Mysql

关注公众号 jb51net

关闭
首页 > 数据库 > Mysql > MySQL主从同步配置

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

作者:心静自然凉800

前MySQL主从同步是生产环境中最常用的高可用方案之一,本文基于实际生产经验,整理出一套标准化的 MySQL 主从同步操作流程,具有一定的参考价值,感兴趣的可以了解一下

前言:

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 主从复制的完整搭建流程。核心要点可以总结为三句话:

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

您可能感兴趣的文章:
阅读全文