Mysql-8 配置主从复制(基于二进制日志)

2019/04/15 11:30
阅读数 45

[toc]

<br><br>

1. 实验环境

| System | IP | Host | Desc | |-|-|-| | CentOS 7.4.1708 |192.168.100.101 |master|mysql-8.0.12-1.el7.x86_64.rpm-bundle.tar| | CentOS 7.4.1708 |192.168.100.102 |slave|mysql-8.0.12-1.el7.x86_64.rpm-bundle.tar|

<br><br>

2. 安装MySQL8

使用官方整合包安装Mysql8

<br><br>

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

<br>

[root@slave ~]# 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:拒绝连接到主节点

<br><br>

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)

<br><br>

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 row in set (0.00 sec)

<br><br>

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)

<br><br>

7. 测试主从复制

# 主节点
mysql> CREATE DATABASE REPL;
Query OK, 1 row affected (0.04 sec)

<br>

# 从节点
mysql> SHOW DATABASES;
+--------------------+
| Database           |
+--------------------+
| REPL               |
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.01 sec)
展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部