mysql主从之配置验证

Posted zyxnhr

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mysql主从之配置验证相关的知识,希望对你有一定的参考价值。

实验环境:

master  192.168.132.121 主库

slave     192.168.132.122 从库

mysql主从复制的配置

1.1 mysql主库给从库复制的权限

mysql> grant replication slave on *.* to replication@192.168.132.122 identified by 1234567;   #主库需要给从库复制的权限,一般从都有自己的ip,一台从库放开一个ip
ERROR 2006 (HY000): MySQL server has gone away
No connection. Trying to reconnect...
Connection id:    6
Current database: *** NONE ***
Query OK, 0 rows affected, 1 warning (0.01 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql> reset master;
Query OK, 0 rows affected (0.01 sec)
mysql> show master logs;
+-------------------+-----------+
| Log_name | File_size |
+-------------------+-----------+
| master-bin.000001 | 154 |
+-------------------+-----------+
1 row in set (0.00 sec)

mysql> show binlog events in master-bin.000001;
+-------------------+-----+----------------+-----------+-------------+---------------------------------------+
| Log_name | Pos | Event_type | Server_id | End_log_pos | Info |
+-------------------+-----+----------------+-----------+-------------+---------------------------------------+
| master-bin.000001 | 4 | Format_desc | 1 | 123 | Server ver: 5.7.26-log, Binlog ver: 4 |
| master-bin.000001 | 123 | Previous_gtids | 1 | 154 | |
+-------------------+-----+----------------+-----------+-------------+---------------------------------------+

1.2 从库使用命令去同步主库的数据

从库使用命令去同步主库。主库ip、主库端口、用户名、密码、binlog文件名、索引

mysql> change master to master_host=192.168.132.121,master_port=3306,master_user=replication,master_password=1234567,master_log_file=master-bin.000001,master_log_pos=154;
Query OK, 0 rows affected, 2 warnings (0.02 sec)

mysql> start slave;         #开启salve
Query OK, 0 rows affected (0.00 sec)
mysql> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.132.121
                  Master_User: replication
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: master-bin.000001
          Read_Master_Log_Pos: 154
               Relay_Log_File: relay-log.000003
                Relay_Log_Pos: 321
        Relay_Master_Log_File: master-bin.000001
             Slave_IO_Running: Yes         #IO进程
            Slave_SQL_Running: Yes         #SQL进程
              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: 154
              Relay_Log_Space: 689
              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: 77278e78-9da8-11e9-bc6c-000c2991dd19
             Master_Info_File: /data/mysql/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: 
1 row in set (0.01 sec)

show processlist;   #查看主从同步的进程

主端:
mysql> show processlist; +----+-------------+-----------------------+------+-------------+------+---------------------------------------------------------------+------------------+ | Id | User | Host | db | Command | Time | State | Info | +----+-------------+-----------------------+------+-------------+------+---------------------------------------------------------------+------------------+ | 6 | root | localhost | NULL | Query | 0 | starting | show processlist | | 9 | replication | 192.168.132.122:51736 | NULL | Binlog Dump | 306 | Master has sent all binlog to slave; waiting for more updates | NULL | +----+-------------+-----------------------+------+-------------+------+---------------------------------------------------------------+------------------+ 从端: mysql> show processlist; +----+-------------+-----------+------+---------+------+--------------------------------------------------------+------------------+ | Id | User | Host | db | Command | Time | State | Info | +----+-------------+-----------+------+---------+------+--------------------------------------------------------+------------------+ | 4 | root | localhost | NULL | Query | 0 | starting | show processlist | | 10 | system user | | NULL | Connect | 314 | Waiting for master to send event | NULL | | 11 | system user | | NULL | Connect | 314 | Slave has read all relay log; waiting for more updates | NULL | +----+-------------+-----------+------+---------+------+--------------------------------------------------------+------------------+

二 验证

创建库、创建表、增删改查验证同步是否正常,查看binlog日志

2.1 创建库

主端建库
mysql> create database darren;
Query OK, 1 row affected (0.00 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| darren             |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.00 sec)

mysql> show master logs;
+-------------------+-----------+
| Log_name          | File_size |
+-------------------+-----------+
| master-bin.000001 |       319 |
+-------------------+-----------+
1 row in set (0.00 sec)

mysql> show binlog events in master-bin.000001;
+-------------------+-----+----------------+-----------+-------------+---------------------------------------+
| Log_name          | Pos | Event_type     | Server_id | End_log_pos | Info                                  |
+-------------------+-----+----------------+-----------+-------------+---------------------------------------+
| master-bin.000001 |   4 | Format_desc    |         1 |         123 | Server ver: 5.7.26-log, Binlog ver: 4 |
| master-bin.000001 | 123 | Previous_gtids |         1 |         154 |                                       |
| master-bin.000001 | 154 | Anonymous_Gtid |         1 |         219 | SET @@SESSION.GTID_NEXT= ANONYMOUS  |
| master-bin.000001 | 219 | Query          |         1 |         319 | create database darren                |
+-------------------+-----+----------------+-----------+-------------+---------------------------------------+
从端查看
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| darren             |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.00 sec)

mysql> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.132.121
                  Master_User: replication
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: master-bin.000001     
          Read_Master_Log_Pos: 319               #读到的偏移量,和master查看的偏移相同,数据已经读完,并同步
               Relay_Log_File: relay-log.000003
                Relay_Log_Pos: 486
        Relay_Master_Log_File: master-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: 319
              Relay_Log_Space: 854
              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: 77278e78-9da8-11e9-bc6c-000c2991dd19
             Master_Info_File: /data/mysql/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: 
1 row in set (0.00 sec)

2.2 其他命令,增删改查验证

主端查看表为空
mysql> use darren;
Database changed
mysql> show tables;
Empty set (0.00 sec)
从端也一样
mysql> use darren;
Database changed
mysql> show tables;
Empty set (0.00 sec)
主端建表
mysql> create table test (id int);
Query OK, 0 rows affected (0.01 sec)

mysql> show tables;
+------------------+
| Tables_in_darren |
+------------------+
| test             |
+------------------+
1 row in set (0.00 sec)
从端查看
mysql> show tables;
+------------------+
| Tables_in_darren |
+------------------+
| test             |
+------------------+
主端插入数据:
mysql> insert into test values (1);
Query OK, 1 row affected (0.00 sec)

mysql> select * from test;
+------+
| id   |
+------+
|    1 |
+------+
从端:
mysql> select * from test;
+------+
| id   |
+------+
|    1 |
+------+
继续插入数据:
mysql> insert into test values (2);
Query OK, 1 row affected (0.00 sec)

mysql> insert into test values (2);
Query OK, 1 row affected (0.00 sec)

mysql> insert into test values (3);
Query OK, 1 row affected (0.00 sec)
mysql> show master status;
+-------------------+----------+--------------+------------------+-------------------+
| File              | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+-------------------+----------+--------------+------------------+-------------------+
| master-bin.000001 |     1524 |              |                  |                   |
+-------------------+----------+--------------+------------------+-------------------+
从端
mysql> select * from test;
+------+
| id   |
+------+
|    1 |
|    2 |
|    2 |
|    3 |
+------+
4 rows in set (0.00 sec)

mysql> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.132.121
                  Master_User: replication
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: master-bin.000001
          Read_Master_Log_Pos: 1524
               Relay_Log_File: relay-log.000003
                Relay_Log_Pos: 1691
        Relay_Master_Log_File: master-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: 1524
              Relay_Log_Space: 2059
              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: 77278e78-9da8-11e9-bc6c-000c2991dd19
             Master_Info_File: /data/mysql/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: 
主端删除表
mysql> delete from test;
Query OK, 4 rows affected (0.01 sec)
mysql> select * from test;
Empty set (0.00 sec)
从端:
mysql> select * from test;
Empty set (0.00 sec)
主端删除库
mysql> drop database darren;
Query OK, 1 row affected (0.01 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
从端:
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+

 基本的配置完成,实现基本的同步功能!

以上是关于mysql主从之配置验证的主要内容,如果未能解决你的问题,请参考以下文章

MySQL主从复制及读写分离实际部署与验证

Linux—Centos7.4之搭建Mysql数据库主从同步读写分离

zabbix监控Mysql主从状态

MYSQL | 企业整合解决方案之mysql集群搭建-主从配置

Mysql主从扩展之问题及解决方法

MySQL传统主从配置