# mysql8.0主从复制
# 条件是要开启binlog
# mysql8.0默认是开启binlog 从库可以选择开或者不开binlog(建议开启)
查询是否开启binlog
mysql> select @@log_bin;
+-----------+
| @@log_bin |
+-----------+
| 1 |
+-----------+
# 结果为1 就是已开启binlog
主库和从库需要设置server_id 从库需要大于主库的id
主库:
[root@master ~]# grep 'server_id' /etc/my.cnf
server_id=1
从库
[root@slave ~]# grep 'server_id' /etc/my.cnf
server_id=2
# 1. 主库中创建复制用户
mysql> CREATE USER 'copy'@'192.168.200.%' IDENTIFIED BY 'Root@123456';
mysql> GRANT REPLICATION SLAVE ON *.* TO 'copy'@'192.168.200.%';
# 1.2 主库查看当前的binlog信息
mysql> show master status;
+---------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+---------------+----------+--------------+------------------+-------------------+
| binlog.000005 | 157 | | | |
+---------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
# 2.从库添加主库信息
# 模板:
mysql>help CHANGE MASTER TO;
# CHANGE MASTER TO
# MASTER_HOST='source2.example.com',
# MASTER_USER='replication',
# MASTER_PASSWORD='password',
# MASTER_PORT=3306,
# MASTER_LOG_FILE='source2-bin.001',
# MASTER_LOG_POS=4,
# MASTER_CONNECT_RETRY=10;
mysql> CHANGE MASTER TO
-> MASTER_HOST='192.168.200.80',
-> MASTER_USER='copy',
-> MASTER_PASSWORD='Root@123456',
-> MASTER_PORT=3306,
-> MASTER_LOG_FILE='binlog.000005',
-> MASTER_LOG_POS=157,
-> MASTER_CONNECT_RETRY=10;
# 启动从库:
mysql> start slave;
Query OK, 0 rows affected, 2 warnings (0.00 sec)
# 查看主从状态:
mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for source to send event
Master_Host: 192.168.200.80
Master_User: copy
Master_Port: 3306
Connect_Retry: 10
Master_Log_File: binlog.000005
Read_Master_Log_Pos: 157
Relay_Log_File: slave-relay-bin.000002
Relay_Log_Pos: 323
Relay_Master_Log_File: binlog.000005
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: 533
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: 1
Master_UUID: 5fdb77aa-ea46-11ed-9938-000c29e736da
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)
# 下面两个状态为yes即代表已开启复制
# Slave_IO_Running: Yes
# Slave_SQL_Running: Yes
Mysql8.0主从复制构建
最新推荐文章于 2024-06-30 16:06:05 发布
文章详细介绍了如何在MySQL8.0中设置主从复制,包括检查binlog状态,设置不同的server_id,创建复制用户,获取主库的binlog信息,并在从库上配置主库详情以启动复制。最后,验证了主从复制的状态,确保Slave_IO_Running和Slave_SQL_Running均为Yes。
摘要由CSDN通过智能技术生成