当前位置: 首页 > news >正文

mysql 主从搭建步骤

主库:

 开启log-bin参数,log-bin 参数修改需要重启服务器



--You can change the server_id value dynamically by issuing a statement like this:

SET GLOBAL server_id = 2;



--to enable binary logging using a log file name prefix of mysql-bin, and configure a server ID of 1, use these lines:

[mysqld]
log-bin=mysql-bin
server-id=1

--After making the changes, restart the server.

Note

The following options have an impact on this procedure:

  • For the greatest possible durability and consistency in a replication setup using InnoDB with transactions, you should use innodb_flush_log_at_trx_commit=1 and sync_binlog=1 in the source's my.cnf file.

  • Ensure that the skip_networking system variable is not enabled on your source. If networking has been disabled, the replica cannot communicate with the source and replication fails.

主库:


---创建账号
mysql> CREATE USER 'repl'@'%.example.com' IDENTIFIED BY 'password';
mysql> GRANT REPLICATION SLAVE ON *.* TO 'repl'@'%.example.com';


----flush all tables and block write statements by executing the FLUSH TABLES WITH READ LOCK statement:


mysql> FLUSH TABLES WITH READ LOCK;
mysql> SHOW MASTER STATUS\G
*************************** 1. row ***************************
             File: mysql-bin.000003
         Position: 73
     Binlog_Do_DB: test
 Binlog_Ignore_DB: manual, mysql
Executed_Gtid_Set: 3E11FA47-71CA-11E1-9E33-C80AA9429562:1-5
1 row in set (0.00 sec)


备份数据,把数据恢复到从库

主库操作

  • On the source, released the read lock:

    mysql> UNLOCK TABLES;

从库:

Setting the Replica Configuration 

You can change the server_id value dynamically by issuing a statement like this:

SET GLOBAL server_id = 21;

If you are shutting down the replica server, you can edit the [mysqld] section of the configuration file to specify a unique server ID. For example:

[mysqld]
server-id=21

从库:

mysql> CHANGE MASTER TO
    ->     MASTER_HOST='source_host_name',
    ->     MASTER_USER='replication_user_name',
    ->     MASTER_PASSWORD='replication_password',
    ->     MASTER_LOG_FILE='recorded_log_file_name',
    ->     MASTER_LOG_POS=recorded_log_position;
  1. 开始同步和检查状态:

    mysql> START SLAVE;
    mysql> STOP SLAVE;
    mysql> SHOW SLAVE STATUS\G

参考:

 MySQL :: MySQL 5.7 Reference Manual :: 16.1.2 Setting Up Binary Log File Position Based Replication

相关文章:

  • C语言函数(一)
  • 文法 2025/3/3
  • uniapp中uploadFile的用法
  • DEYOLO和YOLO-MS
  • LLM之Agent(十五)| 使用Langchain实现模型上下文协议(MCP)
  • XSS 攻击(详细)
  • 2025 年福建省职业院校技能大赛 中职组 物联网应用与服务(样题)
  • 从JVM到分布式锁:高并发架构设计的六把密钥
  • JavaScript中的NaN、undefined和null 的区别
  • 【NLP 面经 2】
  • 策略模式与元数据映射模式融合 JSR 380 验证规范实现枚举范围校验
  • Linux环境基础开发工具——(1)yum
  • ROS Image and Compressed
  • 本地飞牛NAS快速部署WordPress个人网站并一键上线公网远程访问
  • 异步框架使用loguru和contextvars实现日志按Id输出
  • 第十四章:JSON和CSV格式详解及Python操作
  • 一个判断A股交易状态的python脚本
  • 二、概念篇
  • 线程概念与控制(下)
  • SQL Server从安装到入门一文掌握应用能力。
  • 有没有教做化学药品的网站/seo网站排名全选
  • 沈阳网红餐厅/南宁seo推广服务
  • 咋做211校徽加网站/广州优化疫情防控措施
  • 自学设计的网站/百度云官网登录首页
  • 17网一起做网店网站/seo培训费用
  • 甘肃省路桥建设集团网站/郑州seo哪家专业