1. 实验环境
SystemIPHost
CentOS 7.4.1708192.168.100.101master
CentOS 7.4.1708192.168.100.102slave
2. 安装MySQL8
3. 配置主从复制
要想将主节点配置为使用基于二进制日志的复制,必须确保启用了二进制日志记录,并建立唯一的服务器ID
[root@master ~]# 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
[root@slave ~]# cat /etc/my.cnf[mysqld]datadir=/var/lib/mysqlsocket=/var/lib/mysql/mysql.socklog-error=/var/log/mysqld.logpid-file=/var/run/mysqld/mysqld.pidserver-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> UNLOCK TABLES;
# 这条命令是确定当前二进制日志文件的名称和位置。mysql> show master status;+---------------+----------+--------------+------------------+-------------------+| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |+---------------+----------+--------------+------------------+-------------------+| binlog.000002 | 866 | | | |+---------------+----------+--------------+------------------+-------------------+1 rowinset(0.00 sec)
6. 配置从节点
mysql> CHANGE MASTER TOMASTER_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: Waitingformaster 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: 0Master_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 hasreadall relaylog; waitingformore 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: 01 rowinset(0.00 sec)
7. 测试主从复制
# 主节点mysql> CREATE DATABASE REPL;Query OK, 1 row affected (0.04 sec)