Mysql的主从同步

一主一从

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

master节点操作

bash 复制代码
配置文件增加两行参数
[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节点操作

bash 复制代码
增加该配置参数
[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 event
                  Master_Host: 192.168.10.200
                  Master_User: repluser
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql200.000001
          Read_Master_Log_Pos: 157
               Relay_Log_File: mysql201-relay-bin.000002
                Relay_Log_Pos: 325
        Relay_Master_Log_File: mysql200.000001
             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: 157
              Relay_Log_Space: 538
              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: 0
Master_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: 200
                  Master_UUID: 5e9b61a1-36c5-11f0-b2d4-000c294906b2
             Master_Info_File: mysql.slave_master_info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Replica has read all relay log; waiting for more updates
           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: 0
         Replicate_Rewrite_DB: 
                 Channel_Name: 
           Master_TLS_Version: 
       Master_public_key_path: 
        Get_master_public_key: 0
            Network_Namespace: 
1 row in set, 1 warning (0.00 sec)

ERROR: 
No query specified

一主多从

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

master节点操作

bash 复制代码
将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 event
                  Master_Host: 192.168.10.200
                  Master_User: repluser
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql200.000001
          Read_Master_Log_Pos: 157
               Relay_Log_File: mysql202-relay-bin.000003
                Relay_Log_Pos: 325
        Relay_Master_Log_File: mysql200.000001
             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: 157
              Relay_Log_Space: 718
              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: 0
Master_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: 200
                  Master_UUID: 5e9b61a1-36c5-11f0-b2d4-000c294906b2
             Master_Info_File: mysql.slave_master_info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Replica has read all relay log; waiting for more updates
           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: 0
         Replicate_Rewrite_DB: 
                 Channel_Name: 
           Master_TLS_Version: 
       Master_public_key_path: 
        Get_master_public_key: 0
            Network_Namespace: 
1 row in set, 1 warning (0.00 sec)

ERROR: 
No query specified

验证效果

bash 复制代码
使用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   |
+--------+
| 测试   |
+--------+

一主二从效果成功
相关推荐
洛克大航海2 小时前
解锁 PySpark SQL 的强大功能:有关 App Store 数据的端到端教程
linux·数据库·sql·pyspark sql
XueminXu3 小时前
ClickHouse数据库的表引擎
数据库·clickhouse·log·表引擎·mergetree·special·integrations
冒泡的肥皂4 小时前
MVCC初学demo(二
数据库·后端·mysql
代码程序猿RIP4 小时前
【Redis 】Redis 详解以及安装教程
数据库·etcd
小生凡一4 小时前
redis 大key、热key优化技巧|空间存储优化|调优技巧(一)
数据库·redis·缓存
oe10194 小时前
好文与笔记分享 A Survey of Context Engineering for Large Language Models(上)
数据库·笔记·语言模型·agent·上下文工程
小马哥编程4 小时前
【软考架构】案例分析-对比MySQL查询缓存与Memcached
java·数据库·mysql·缓存·架构·memcached
一 乐4 小时前
高校后勤报修系统|物业管理|基于SprinBoot+vue的高校后勤报修系统(源码+数据库+文档)
java·前端·javascript·数据库·vue.js·毕设
折翼的恶魔4 小时前
SQL190 0级用户高难度试卷的平均用时和平均得分
java·数据库
煎蛋学姐5 小时前
SSM基于框架在线电影评论投票系统3gr0f(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面。
数据库·系统开发·ssm 框架·在线电影评论投票系统