目录
- 1. 实验环境
- 2. 安装MySQL8
- 3. 配置主从复制
- 4. 配置复制用户
- 5. 数据的同步
- 6. 配置从节点
- 7. 测试主从复制
1. 实验环境
System | IP | Host |
---|---|---|
CentOS 7.4.1708 | 192.168.100.101 | master |
CentOS 7.4.1708 | 192.168.100.102 | slave |
2. 安装MySQL8
3. 配置主从复制
要想将主节点配置为使用基于二进制日志的复制,必须确保启用了二进制日志记录,并建立唯一的服务器ID
[[email protected] ~]# cat /etc/my.cnf
[mysqld]
datadir=/var/lib/mysql # 这是数据存放的路径
socket=/var/lib/mysql/mysql.sock # 这是监听的套接字
log-error=/var/log/mysqld.log # 日志输出的路径
pid-file=/var/run/mysqld/mysqld.pid # pid存放的路径
log-bin=on # 开启二进制日志
server-id=1 # 服务器id为1
[[email protected] ~]# cat /etc/my.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
server-id=2
主节点server-id为0:拒绝来自从节点的任何连接
从节点server-id为0:拒绝连接到主节点
4. 配置复制用户
创建一个只能复制的用户。
mysql> CREATE USER 'repl'@'192.168.100.101' IDENTIFIED BY 'MyNewPass4!';
Query OK, 0 rows affected (0.02 sec)
mysql> GRANT REPLICATION SLAVE ON *.* TO 'repl'@'192.168.100.101';
Query OK, 0 rows affected (0.06 sec)
mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.01 sec)
5. 数据的同步
在配置之前,要注意的是,主节点现在的数据与从节点是不同步的,这可能导致复制失败,所以你可能需要将主节点的数据手动导入到从节点。在此你必须保证数据不被写入,所以你需要阻止写入。
# 这条命令会阻止对所有表的写入操作,但当前客户端断开,则会释放锁定
mysql> FLUSH TABLES WITH READ LOCK;
# 这条命令是确定当前二进制日志文件的名称和位置。
mysql> show master status;
+---------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+---------------+----------+--------------+------------------+-------------------+
| binlog.000002 | 866 | | | |
+---------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
6. 配置从节点
mysql> CHANGE MASTER TO
MASTER_HOST='192.168.100.101', # 主节点地址
MASTER_USER='repl', # 主节点用户
MASTER_PASSWORD='MyNewPass4!', # 主节点密码
MASTER_LOG_FILE='binlog.000002', # 二进制日志文件
MASTER_LOG_POS=866; # 二进制日志位置
Query OK, 0 rows affected, 2 warnings (0.05 sec)
mysql> START SLAVE;
Query OK, 0 rows affected (0.04 sec)
mysql> SHOW SLAVE STATUS\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.100.101
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: binlog.000002
Read_Master_Log_Pos: 866
Relay_Log_File: slave-relay-bin.000003
Relay_Log_Pos: 319
Relay_Master_Log_File: binlog.000002
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: 866
Relay_Log_Space: 691
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: aa8d6cc4-5f26-11e9-b7d7-000c29999aa1
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:
Executed_Gtid_Set:
Auto_Position: 0
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
Master_public_key_path:
Get_master_public_key: 0
1 row in set (0.00 sec)
7. 测试主从复制
# 主节点
mysql> CREATE DATABASE REPL;
Query OK, 1 row affected (0.04 sec)
# 从节点
mysql> SHOW DATABASES;
+--------------------+
| Database |
+--------------------+
| REPL |
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.01 sec)
原文地址:https://www.cnblogs.com/liuhedong/p/10707124.html
时间: 2024-10-11 23:17:12