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                |
+--------------------+

 基本的配置完成,實現基本的同步功能!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM