侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

MySQL5.6主从复制(mysql数据同步配置)

数据库  /  管理员 发布于 6年前   190

规划

master 192.168.56.2

slave  192.168.56.5

1、在主库上,设置一个复制使用的账户rep1,并授予REPLICATION SLAVE权限。

mysql> grant replication slave on *.* to 'rep1'@'192.168.56.2' identified by '123456';Query OK, 0 rows affected (0.01 sec)

2、修改主数据库服务器的配置文件my.cnf,开启BINLOG,并设置server-id的值。这两个参数的修改需要重新启动数据库服务才可以生效。

 vi /etc/my.cnf

 [mysqld]

 log-bin=/home/mysql/log/mysql-bin.log

 server-id=1

[root@rhel6 ~]# service mysql restartShutting down MySQL..                                      [  OK  ]Starting MySQL.                                            [  OK  ]

3、在主库上,设置读锁定有效,这个操作是为了确保没有数据库操作,以便获得一个一致性的快照。

mysql> flush tables with read lock;Query OK, 0 rows affected (0.02 sec)

4、得到主库上当前的二进制日志名和偏移量值。这个操作的目的是为了从数据库启动以后,从这个点开始进行数据的恢复。

mysql> show master status;+-----------------+----------+--------------+------------------+-------------------+| File            | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |+-----------------+----------+--------------+------------------+-------------------+| mysqlbin.000021 |      120 |              |                  |                   |+-----------------+----------+--------------+------------------+-------------------+1 row in set (0.03 sec)

5、现在主数据库服务器已经停止了更新操作,需要生成主数据库的备份,备份的方式有很多种,可以直接在操作系统下复制全部的数据文件到从数据库服务器上,也可以通过mysqldump导出数据或者使用ibbackup工具进行数据库的备份。如果主数据库的服务可以停止,那么直接复制数据文件应该是最快的生成快照的方法:

[root@rhel6 lib]# tar -zcvf mysql.tar.gz mysqlmysql/tar: mysql/mysql.sock: socket ignoredmysql/mysqlbin.000019mysql/test1/mysql/test1/db.optmysql/test1/pack.MYDmysql/test1/myisam2.frmmysql/test1/emp.ibdmysql/test1/mer_myisam.MRGmysql/test1/emp.frmmysql/test1/pri_t.TRG....传到从数据库[root@rhel6 lib]# scp mysql.tar.gz [email protected]:/[email protected]'s password: mysql.tar.gz                                                                                                                                                                    100% 1402KB   1.4MB/s   00:00

6、主数据库备份完毕后可恢复写操作,剩下的只需要在从库上执行

mysql> unlock tables;Query OK, 0 rows affected (0.02 sec)

7、将主数据库的一致性备份恢复到从数据库上。如果是使用.tar.gz打包的文件包,只需要解开到相应的目录即可。

tar -zxvf mysql.tar.gz

8、修改从数据库的配置文件my.cnf,增加server-id参数。注意server-id的值必须是唯一的,不能和主数据库的配置相同,如果有多个从数据库服务器,每个从数据库服务器必须有自己唯一的server-id值。

 vi my.cnf

 [mysqld]

 server-id=2

9、在从库上,使用--skip-slave-start选项启动数据库,这样不会立即启动从数据库服务上的复制进程,方便我们对从数据库的服务进行进一步的配置(可选)

 mysqld_safe --skip-slave-start &

 或者修改my.cnf,添加skip-slave-start参数,service mysql start

10、对从数据库服务器做相应设置,指定复制使用的用户,主数据库服务器的IP、端口以及开始执行复制的日志文件和位置等

mysql> change master to     ->  master_host='192.168.56.2',    ->  master_user='rep1',    ->  master_port=3306,    ->  master_password='123456',    ->  master_log_file='mysqlbin.000021',    ->  master_log_pos=120;Query OK, 0 rows affected, 2 warnings (0.08 sec)

11、在从库上,启动slave线程

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

12、从库上查看进程

mysql> show processlist \G;*************************** 1. row ***************************     Id: 1   User: root   Host: localhost     db: zxCommand: Query   Time: 0  State: init   Info: show processlist*************************** 2. row ***************************     Id: 4   User: system user   Host:      db: NULLCommand: Connect   Time: 1484  State: Waiting for master to send event   Info: NULL*************************** 3. row ***************************     Id: 5   User: system user   Host:      db: NULLCommand: Connect   Time: 739  State: Slave has read all relay log; waiting for the slave I/O thread to update it   Info: NULL3 rows in set (0.00 sec)mysql> show slave status \G*************************** 1. row ***************************               Slave_IO_State: Waiting for master to send event                  Master_Host: 192.168.56.2                  Master_User: rep1                  Master_Port: 3306                Connect_Retry: 60              Master_Log_File: mysqlbin.000021          Read_Master_Log_Pos: 652               Relay_Log_File: rhel6-relay-bin.000002                Relay_Log_Pos: 814        Relay_Master_Log_File: mysqlbin.000021             Slave_IO_Running: Yes            Slave_SQL_Running: Yes              Replicate_Do_DB:           Replicate_Ignore_DB:            Replicate_Do_Table:        Replicate_Ignore_Table:       Replicate_Wild_Do_Table:   Replicate_Wild_Ignore_Table:                    Last_Errno: 0                   Last_Error:                  Skip_Counter: 0          Exec_Master_Log_Pos: 652              Relay_Log_Space: 987              Until_Condition: None               Until_Log_File:                 Until_Log_Pos: 0           Master_SSL_Allowed: No           Master_SSL_CA_File:            Master_SSL_CA_Path:               Master_SSL_Cert:             Master_SSL_Cipher:                Master_SSL_Key:         Seconds_Behind_Master: 0Master_SSL_Verify_Server_Cert: No                Last_IO_Errno: 0                Last_IO_Error:                Last_SQL_Errno: 0               Last_SQL_Error:   Replicate_Ignore_Server_Ids:              Master_Server_Id: 1                  Master_UUID: 3743271b-aa6d-11e6-aa2e-080027e5f5dd             Master_Info_File: /mysqldata/master.info                    SQL_Delay: 0          SQL_Remaining_Delay: NULL      Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it           Master_Retry_Count: 86400                  Master_Bind:       Last_IO_Error_Timestamp:      Last_SQL_Error_Timestamp:                Master_SSL_Crl:            Master_SSL_Crlpath:            Retrieved_Gtid_Set:             Executed_Gtid_Set:                 Auto_Position: 01 row in set (0.00 sec)

Slave_IO_Running和Slave_SQL_Running全部为YES才算搭建成功。如果出错查看报错原因。我搭建时遇到的两个错误:

(1)Last_IO_Error: Fatal error: The slave I/O thread stops because master and slave have equal MySQL server UUIDs; these UUIDs must be different for replication to work.

原因:搭建时把主库整个目录复制过来的,主库和从库的UUID一致了,修改从库的UUID即可。

方法:修改$datadir/auto.cnf,按照16进制格式随便改一下,重启mysql即可。

(2)Last_IO_Error: Got fatal error 1236 from master when reading data from binary log: 'Could not find first log file name in binary log index file'

原因:配置change master to时把master_log_file配置错误

方法:change master to master_log_file='mysqlbin.000021';start slave;即可。

13、测试复制的正确性

主库mysql> create database zx;Query OK, 1 row affected (0.05 sec)mysql> use zx;Database changedmysql> create table t(id int);Query OK, 0 rows affected (0.09 sec)mysql> insert into t values(1),(2),(3);Query OK, 3 rows affected (0.01 sec)Records: 3  Duplicates: 0  Warnings: 0从库mysql> show databases;+--------------------+| Database           |+--------------------+| information_schema || data               || mysql              || performance_schema || test               || test1              || zx                 |+--------------------+7 rows in set (0.00 sec)mysql> use zxReading table information for completion of table and column namesYou can turn off this feature to get a quicker startup with -ADatabase changedmysql> select * from t;+------+| id   |+------+|    1 ||    2 ||    3 |+------+3 rows in set (0.00 sec)


  • 上一条:
    详解Mysql命令大全(推荐)
    下一条:
    mysql group by having 实例代码
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • 分库分表的目的、优缺点及具体实现方式介绍(0个评论)
    • DevDB - 在 VS 代码中直接访问数据库(0个评论)
    • 在ubuntu系统中实现mysql数据存储目录迁移流程步骤(0个评论)
    • 在mysql中使用存储过程批量新增测试数据流程步骤(0个评论)
    • php+mysql数据库批量根据条件快速更新、连表更新sql实现(0个评论)
    • 近期文章
    • 在go+gin中使用"github.com/skip2/go-qrcode"实现url转二维码功能(0个评论)
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(0个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 欧盟关于强迫劳动的规定的官方举报渠道及官方举报网站(0个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf文件功能(0个评论)
    • Laravel从Accel获得5700万美元A轮融资(0个评论)
    • 在go + gin中gorm实现指定搜索/区间搜索分页列表功能接口实例(0个评论)
    • 在go语言中实现IP/CIDR的ip和netmask互转及IP段形式互转及ip是否存在IP/CIDR(0个评论)
    • PHP 8.4 Alpha 1现已发布!(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2017-06
    • 2017-08
    • 2017-09
    • 2017-10
    • 2017-11
    • 2018-01
    • 2018-05
    • 2018-10
    • 2018-11
    • 2020-02
    • 2020-03
    • 2020-04
    • 2020-05
    • 2020-06
    • 2020-07
    • 2020-08
    • 2020-09
    • 2021-02
    • 2021-04
    • 2021-07
    • 2021-08
    • 2021-11
    • 2021-12
    • 2022-02
    • 2022-03
    • 2022-05
    • 2022-06
    • 2022-07
    • 2022-08
    • 2022-09
    • 2022-10
    • 2022-11
    • 2022-12
    • 2023-01
    • 2023-03
    • 2023-04
    • 2023-05
    • 2023-07
    • 2023-08
    • 2023-10
    • 2023-11
    • 2023-12
    • 2024-01
    • 2024-03
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客