MySQL主从集群搭建

在本文中,使用单机上多个实例来搭建一个主从集群。

基于的MySQL版本是5.7.27。

单机多实例的具体安装过程可参考文档单机安装MySQL多实例

将要搭建的集群实例信息如下:

角色端口
主库3306
从库13307
从库23308

1.创建复制账号

在主库上,创建复制用的账号,并赋予相应权限:

mysql> CREATE USER 'rep'@'%' IDENTIFIED BY 'replication*001';
mysql> GRANT  REPLICATION SLAVE ON *.* TO 'rep'@'%';

查看用户:

mysql> select host, user from mysql.user;
+-----------+---------------+
| host      | user          |
+-----------+---------------+
| %         | rep           |
| localhost | mysql.session |
| localhost | mysql.sys     |
| localhost | root          |
+-----------+---------------+
4 rows in set (0.00 sec)

mysql> show grants for 'rep'@'%';
+---------------------------------------------+
| Grants for %                            |
+---------------------------------------------+
| GRANT REPLICATION SLAVE ON *.* TO 'rep'@'%' |
+---------------------------------------------+
1 row in set (0.00 sec)

2.设置从库指向主库

首先,清空从库1,从库2上的binlog日志位点信息:

reset master

注意,reset master 会清空日志文件及GTID信息,在生产环境谨慎操作!

这里是为了简便快速搭建集群才这样操作的。

接着,设置复制关系:

CHANGE MASTER TO MASTER_HOST='127.0.0.1', MASTER_PORT=3306, MASTER_USER='rep', MASTER_PASSWORD='replication*001', MASTER_AUTO_POSITION=1;

指定主从复制参数:主库的地址和端口,复制账号和密码,以及GTID参数。

启动复制:

start slave;

3.查看复制状态

在从库上,查看复制状态

mysql> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 127.0.0.1
                  Master_User: rep
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000001
          Read_Master_Log_Pos: 853
               Relay_Log_File: relay-bin.000003
                Relay_Log_Pos: 454
        Relay_Master_Log_File: mysql-bin.000001
             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: 853
              Relay_Log_Space: 1567
              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: 43cf1658-41a4-11ea-9f90-080027974966
             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: 43cf1658-41a4-11ea-9f90-080027974966:1-3
            Executed_Gtid_Set: 43cf1658-41a4-11ea-9f90-080027974966:1-3
                Auto_Position: 1
         Replicate_Rewrite_DB: 
                 Channel_Name: 
           Master_TLS_Version: 
1 row in set (0.00 sec)

在主库上查看有哪些从库:

mysql> show slave hosts;
+-----------+------+------+-----------+--------------------------------------+
| Server_id | Host | Port | Master_id | Slave_UUID                           |
+-----------+------+------+-----------+--------------------------------------+
|         3 |      | 3308 |         1 | 2bbf28c5-41a5-11ea-bf30-080027974966 |
|         2 |      | 3307 |         1 | 254f18a1-41a5-11ea-be32-080027974966 |
+-----------+------+------+-----------+--------------------------------------+
2 rows in set (0.00 sec)

可以看到,主库有两个从库:从库1,从库2。

相关推荐