系统城装机大师 - 固镇县祥瑞电脑科技销售部宣传站!

当前位置:首页 > 数据库 > Mysql > 详细页面

MySQL 8.X 主从配置教程

时间:2020-08-19来源:www.pcxitongcheng.com作者:电脑系统城

Desc#

✏️ 本文以基于GTID的复制示例

GTID(Global Transaction ID)是对于一个已提交事务的唯一编号,并且是一个全局(主从复制)唯一的编号。

它的官方定义如下:

GTID = source_id :transaction_id

7E11FA47-31CA-19E1-9E56-C43AA21293967:29

什么是sever_uuid,和Server-id 区别?

核心特性: 全局唯一,具备幂等性

GTID核心参数

重要参数:

gtid-mode=on --启用gtid类型,否则就是普通的复制架构

enforce-gtid-consistency=true --强制GTID的一致性

log-slave-updates=1 --slave更新是否记入日志

MySQL 8.x版本安装参考

 

Master端配置#

✏️ 1.编辑配置文件


 
Copy
# vim /etc/my.cnf [mysqld] datadir=/data/mysql socket=/var/lib/mysql/mysql.sock log-error=/var/log/mysqld.log pid-file=/var/run/mysqld/mysqld.pid port=3306 server-id=100 gtid-mode=on #启用gtid类型,否则就是普通的复制架构 log-slave-updates #slave更新是否记入日志 enforce-gtid-consistency #强制GTID的一致性 log_bin=/data/mysql/mybinlog max_connections=1000

✏️ 2.创建主从复制专用账户


 
Copy
# mysql -uroot -pNewPass#123 mysql> create user 'slave'@'192.168.3.%' identified with mysql_native_password by 'Mslave#q818'; Query OK, 0 rows affected (2.23 sec) mysql> grant replication slave on *.* to 'slave'@'192.168.3.%'; Query OK, 0 rows affected (0.01 sec)

✏️ ​3.查看master的日志及其偏移量


 
Copy
mysql> show master status; +-----------------+----------+--------------+------------------+------------------------------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +-----------------+----------+--------------+------------------+------------------------------------------+ |mybinlog.000003 | 195 | | | 11cdb65b-e125-11ea-904c-fa39b0d35500:1-6 | +-----------------+----------+--------------+------------------+------------------------------------------+ 1 row in set (0.00 sec)

 

Slave端配置#

✏️ 1.编辑配置文件


 
Copy
# vim /etc/my.cnf [mysqld] datadir=/data/mysql socket=/var/lib/mysql/mysql.sock log-error=/var/log/mysqld.log pid-file=/var/run/mysqld/mysqld.pid port=3306 server-id=106 gtid-mode=on log-slave-updates enforce-gtid-consistency skip-slave-start relay_log=/data/mysql/relay.log max_connections=1000 read-only=1

✏️ 2.进入mysql进行配置连接


 
Copy
mysql> change master to master_host='192.168.3.95', \ master_user='slave', \ master_password='Mslave#q818', \ master_port=3306, \ master_auto_position=1; mysql> start slave;

✏️ 3.查看slave状态


 
Copy
mysql> show slave status\G; *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.3.95 Master_User: slave Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mybinlog.000003 Read_Master_Log_Pos: 195 Relay_Log_File: relay.000003 Relay_Log_Pos: 407 Relay_Master_Log_File: mybinlog.000003 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: 195 Relay_Log_Space: 2379 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: 100 Master_UUID: 11cdb65b-e125-11ea-904c-fa39b0d35500 Master_Info_File: mysql.slave_master_info SQL_Delay: 0 SQL_Remaining_Delay: NULL Slave_SQL_Running_State: Slave 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: 11cdb65b-e125-11ea-904c-fa39b0d35500:1-6 Executed_Gtid_Set: 11cdb65b-e125-11ea-904c-fa39b0d35500:1-6, 8ee47a46-e12a-11ea-a237-fa55fa771000:1 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 (0.00 sec) ERROR: No query specified

 

分享到:

相关信息

  • MySQL的核心查询语句详解

    一、单表查询 1、排序 2、聚合函数 3、分组 4、limit 二、SQL约束 1、主键约束 2、非空约束 3、唯一约束 4、外键约束 5、默认值 三、多表查询 1、内连接 1)隐式内连接: 2)显式内连接: 2、外连接 1)左外连接 2)右外连接 四...

    2023-10-30

  • Mysql中如何删除表重复数据

    Mysql删除表重复数据 表里存在唯一主键 没有主键时删除重复数据 Mysql删除表中重复数据并保留一条 准备一张表 用的是mysql8 大家自行更改 创建表并添加四条相同的数据...

    2023-10-30

系统教程栏目

栏目热门教程

人气教程排行

站长推荐

热门系统下载