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

Mysql的主从同步

一主一从

IP地址主机名
master节点192.168.10.200mysql200
slave节点192.168.10.201mysql201

master节点操作

配置文件增加两行参数
[root@mysql200 ~]# tail -n 2 /etc/my.cnf.d/mysql-server.cnf 
log-bin=/mylog/mysql200
server-id=200
[root@mysql200 ~]# systemctl restart mysqld

slave节点操作

增加该配置参数
[root@mysql201 ~]# tail -n 1 /etc/my.cnf.d/mysql-server.cnf 
server-id=201
mysql> change master to master_host='192.168.10.200',master_user='repluser',master_password='123456',master_log_file='mysql200.000001',master_log_pos=157;
Query OK, 0 rows affected, 8 warnings (0.01 sec)mysql> start slave;
Query OK, 0 rows affected, 1 warning (0.01 sec)mysql> show slave status\G;
*************************** 1. row ***************************Slave_IO_State: Waiting for source to send eventMaster_Host: 192.168.10.200Master_User: repluserMaster_Port: 3306Connect_Retry: 60Master_Log_File: mysql200.000001Read_Master_Log_Pos: 157Relay_Log_File: mysql201-relay-bin.000002Relay_Log_Pos: 325Relay_Master_Log_File: mysql200.000001Slave_IO_Running: YesSlave_SQL_Running: YesReplicate_Do_DB: Replicate_Ignore_DB: Replicate_Do_Table: Replicate_Ignore_Table: Replicate_Wild_Do_Table: Replicate_Wild_Ignore_Table: Last_Errno: 0Last_Error: Skip_Counter: 0Exec_Master_Log_Pos: 157Relay_Log_Space: 538Until_Condition: NoneUntil_Log_File: Until_Log_Pos: 0Master_SSL_Allowed: NoMaster_SSL_CA_File: Master_SSL_CA_Path: Master_SSL_Cert: Master_SSL_Cipher: Master_SSL_Key: Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: NoLast_IO_Errno: 0Last_IO_Error: Last_SQL_Errno: 0Last_SQL_Error: Replicate_Ignore_Server_Ids: Master_Server_Id: 200Master_UUID: 5e9b61a1-36c5-11f0-b2d4-000c294906b2Master_Info_File: mysql.slave_master_infoSQL_Delay: 0SQL_Remaining_Delay: NULLSlave_SQL_Running_State: Replica has read all relay log; waiting for more updatesMaster_Retry_Count: 86400Master_Bind: Last_IO_Error_Timestamp: Last_SQL_Error_Timestamp: Master_SSL_Crl: Master_SSL_Crlpath: Retrieved_Gtid_Set: Executed_Gtid_Set: Auto_Position: 0Replicate_Rewrite_DB: Channel_Name: Master_TLS_Version: Master_public_key_path: Get_master_public_key: 0Network_Namespace: 
1 row in set, 1 warning (0.00 sec)ERROR: 
No query specified

一主多从

IP地址主机名
master节点192.168.10.200mysql200
slave节点192.168.10.201mysql201
slave2节点192.168.10.202mysql202

master节点操作
 

将master节点数据导出
[root@mysql200 ~]# mysqldump  -A >all.sql
[root@mysql200 ~]# scp all.sql root@192.168.10.202:/root
[root@mysql202 ~]# mysql <all.sql
[root@mysql202 ~]# tail -n 1 /etc/my.cnf.d/mysql-server.cnf 
server-id=202
[root@mysql202 ~]# systemctl restart mysqld
[root@mysql202 ~]# mysql
mysql> start slave;change master to master_host='192.168.10.200',master_user='repluser',master_password='123456',master_log_file='mysql200.000001',master_log_pos=157;
mysql> start slave;
mysql> show slave status\G;
*************************** 1. row ***************************Slave_IO_State: Waiting for source to send eventMaster_Host: 192.168.10.200Master_User: repluserMaster_Port: 3306Connect_Retry: 60Master_Log_File: mysql200.000001Read_Master_Log_Pos: 157Relay_Log_File: mysql202-relay-bin.000003Relay_Log_Pos: 325Relay_Master_Log_File: mysql200.000001Slave_IO_Running: YesSlave_SQL_Running: YesReplicate_Do_DB: Replicate_Ignore_DB: Replicate_Do_Table: Replicate_Ignore_Table: Replicate_Wild_Do_Table: Replicate_Wild_Ignore_Table: Last_Errno: 0Last_Error: Skip_Counter: 0Exec_Master_Log_Pos: 157Relay_Log_Space: 718Until_Condition: NoneUntil_Log_File: Until_Log_Pos: 0Master_SSL_Allowed: NoMaster_SSL_CA_File: Master_SSL_CA_Path: Master_SSL_Cert: Master_SSL_Cipher: Master_SSL_Key: Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: NoLast_IO_Errno: 0Last_IO_Error: Last_SQL_Errno: 0Last_SQL_Error: Replicate_Ignore_Server_Ids: Master_Server_Id: 200Master_UUID: 5e9b61a1-36c5-11f0-b2d4-000c294906b2Master_Info_File: mysql.slave_master_infoSQL_Delay: 0SQL_Remaining_Delay: NULLSlave_SQL_Running_State: Replica has read all relay log; waiting for more updatesMaster_Retry_Count: 86400Master_Bind: Last_IO_Error_Timestamp: Last_SQL_Error_Timestamp: Master_SSL_Crl: Master_SSL_Crlpath: Retrieved_Gtid_Set: Executed_Gtid_Set: Auto_Position: 0Replicate_Rewrite_DB: Channel_Name: Master_TLS_Version: Master_public_key_path: Get_master_public_key: 0Network_Namespace: 
1 row in set, 1 warning (0.00 sec)ERROR: 
No query specified

验证效果
 

使用150主机当客户端访问
[root@mysql150 ~]# mysql  -urepluser -p123456  -h192.168.10.200
mysql> create database gamedb1;
Query OK, 1 row affected (0.00 sec)mysql> create table gamedb1.test (name char(10));
Query OK, 0 rows affected (0.02 sec)mysql> insert into gamedb1.test values ('测试');
Query OK, 1 row affected (0.01 sec)[root@mysql200 ~]# mysql -urepluser -p123456 -e "select * from gamedb1.test"
mysql: [Warning] Using a password on the command line interface can be insecure.
+--------+
| name   |
+--------+
| 测试   |
+--------+[root@mysql201 ~]# mysql -urepluser -p123456 -e "select * from gamedb1.test"
mysql: [Warning] Using a password on the command line interface can be insecure.
+--------+
| name   |
+--------+
| 测试   |
+--------+[root@mysql202 ~]# mysql -urepluser -p123456 -e "select * from gamedb1.test"
mysql: [Warning] Using a password on the command line interface can be insecure.
+--------+
| name   |
+--------+
| 测试   |
+--------+一主二从效果成功

http://www.xdnf.cn/news/582697.html

相关文章:

  • VR溺水安全:为生命筑牢数字化防线
  • 常见算法题目1 - 给定一个整数数组和一个目标值,找出数组中两个数之和等于目标值的数组下标组合
  • MySQL的相关操作
  • RTC技术
  • 第六部分:阶段项目 5:构建 NestJS RESTful API 服务器
  • STM32+rt-thread使用MQTT协议连接腾讯物联网平台
  • 旧物回收小程序:让闲置焕发光彩,为生活增添价值
  • spring boot启动报错:2002 - Can‘t connect to server on ‘192.168.10.212‘ (10061)
  • 响应式架构下的调试挑战:WebDebugX 如何帮助前端稳住场面?
  • 优化 CRM 架构,解锁企业竞争力密码
  • 解决:VMware 虚拟机 Ubuntu 系统共享文件夹无法访问问题
  • 将 Docker 镜像推送到 GitLab Container Registry 的完整步骤
  • C++初阶-list的使用1
  • JAVA8怎么使用9的List.of
  • 数据结构与算法-线性表-双向链表(Double Linked List)
  • Excalidraw云端协作实战:如何用智能绘图打破地理限制?深度解析来了!
  • Chrome 缓存文件路径
  • 50天50个小项目 (Vue3 + Tailwindcss V4) ✨ | Blurry Loading (毛玻璃加载)
  • 二叉数的统一迭代法
  • 程序代码篇---Pytorch实现LATM+APF轨迹预测
  • 杰发科技AC7801——PWM获取固定脉冲个数
  • OpenAI 推出 Codex —— ChatGPT 内的“软件工程智能体”
  • 2025年- H42-Lc150 --146. LRU缓存(哈希表,双链表)需二刷--Java版
  • 先更新数据库,再删除缓存的cache aside策略
  • 6.DevOps体系之Jenkins
  • 深入掌握Node.js HTTP模块:从开始到放弃
  • JS实现直接下载PDF文件
  • 动手学深度学习12.6. 多GPU的简洁实现-笔记练习(PyTorch)
  • OpenCV图像平移示例
  • Linux笔记---信号(下)