MySQL主从复制搭建流程分步实现
目录
- 主从复制
- 主从搭建
- 配置主从复制的命令
- 测试
- 总结
主从复制
主从也叫做(AB复制),允许一个服务器从一个服务器数据库(主服务器)的数据复制到一个或者多个mysql数据库服务器。
主从复制的优点有很多,包括横向扩展解决方案,数据安全性,数据分析,远程数据分发。
主从搭建
作为主服务器的服务器开启二进制日志(binlophpg);
下面我们来搭建主从复制的,采用3个节点来尝试;
节点角色 | 主机名 | Ip地址 | server-id |
---|---|---|---|
master | master | 192.168.246.132 | 1 |
slave | salve | 192.168.246.134 | 2 |
slave2 | salve2 | 192.168.246.136 | 3 |
首先需要在etc/hosts中添加IP地址和主机名之间的映射关系,启动节点的数据库,然后可以查看他们的server-id
show variables like 'server_id'; # 创建主从复制的账号 create user 'rep1'@'192.168.246.%' identified by '123456'; grant replication slave on * .* to 'rep1'@'192.168.246.%'; flush privileges;
创建管理账号
create user 'myadmin'@'192.168.246.%' identified by '123456'; grant all privileges on *.* to 'myadmin'@'192.168.246.%'; flush privileges;
在所有的节点上都启用GTID
set @@GLOBAL.ENFORCE_GTID_CONSISTENCY = WARN; set global ENFORCE_GTID_CONSISTENCY = ON; set global GTID_MODE = OFF_PERMISSIVE; set global GTID_MODE = ON_PERMISSIVE; set global GTID_MODE = ON;
GTID全称为Global Transaction Identifier, 即全局事务标识符。用于在binlog中标识唯一的事务,当MySQL Server在写binlog的时候,会写一个特殊的binlog Event,指定下一个事务的GTID。
配置主从复制的命令
注意,需要在所有的从节点都配置
change master to master_host='192.168.246.132',master_user='rep1',master_password='123456',master_auto_position=1;
在所有的从节点中开启主从复制
start salve;
然后在从节点上查看主从复制的状态,出现下面的就算成功了。
show slave status \G;
show slave status \G;
*************************** 1. row *************************** Slave_IO_State: Waiting for source to send event Master_Host: 192.168.246.132 Master_User: rep1 Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-binlog.000008 Read_Master_Log_P开发者_SQLiteos: 157 Relay_Log_File: salve-relay-bin.000002 Relay_Log_Pos: 379 Relay_Master_Log_File: mysql-binlog.000008 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: 589 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_Idjavascripts: Master_Server_Id: 1 Master_UUID: 71341b64-4450-11ed-90f4-000c29f2a7f4 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_Sewww.devze.comt: Auto_Position: 1 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.01 sec)ERROR:
No query specified
测试
我们现在主节点上创建一张表并且插入一些数据,来查看从节点是否有同步数据;
create database demo2; use demo2; create table test1 (tid int); insert into test1 values(123);
这个时候我们去从节点上进行查询,这个时候我们可以看到数据已经同步过来了。
我们还可以到我们设置的data目录下面查看binlog,
再到数据库里面查看,就可以查看具体的binlog信息了。
mysql> show binlog events in 'mysql-binlog.000008';
+---------------------+-----+----------------+-----------+-------------+-------------------------------------------------------------------+| Log_name | Pos | Event_type | Server_id | End_log_pos | Info |+---------------------+-----+----------------+-----------+-------------+-------------------------------------------------------------------+| mysql-binlog.000008 | 4 | Format_desc | 2 | 126 | Server ver: 8.0.30, Binlog ver: 4 || mysql-binlog.000008 | 126 | Previous_gtids | 2 | 157 | || mysql-binlog.000008 | 157 | Gtid | 1 | 241 | SET @@SESSION.GTID_NEXT= '71341b64-4450-11ed-90f4-000c29f2a7f4:1' || mysql-binlog.000008 | 241 | Query | 1 | 352 | create database demo2 /* xid=51 */ || mysql-binlog.000008 | 352 | Gtid | 1 | 436 | SET @@SESSION.GTID_NEXT= '71341b64-4450-11ed-90f4-000c29f2a7f4:2' || mysql-binlog.000008 | 436 | Query | 1 | 554 | use `demo2`; create table test1 (tid int) /* xid=64 */ || mysql-binlog.000008 | 554 | Gtid | 1 | 640 | SET @@SESSION.GTID_NEXT= '71341b64-4450-11ed-90f4-000c29f2a7f4:3' || mysql-binlog.000008 | 640 | Query | 1 | 711 | BEGIN || mysql-binlog.000008 | 711 | Table_map | 1 | 763 | table_id: 94 (demo2.test1) || mysql-binlog.000008 | 763 | Write_rows | 1 | 803 | table_id: 94 flags: STMT_END_F || mysql-binlog.000008 | 803 | Xid | 1 | 834 | COMMIT /* xid=66 */ |+---------------------+-----+----------------+-----------+-------------+-------------------------------------------------------------------+11 rows in set (0.00 sec)
总结
上述就是搭建主从复制的搭建过程,相信大家跟着教程一步步搭建起来应该也可以成功
到此这篇关于MySQL主从复制搭建流程分步实现的文章就介绍到这了,更多相关MySQL主从复制内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!
精彩评论