From Mysql master copy (reset Edition)

  Mysql MySQL Replication is carrying a function may be implemented to copy data from a database server (master) to one or more database servers (Slave), belonging to the default asynchronous replication, without maintaining a long connection. Libraries can copy all or several of the library by configuration, or a portion of the library table.
(1) The working principle
  simply means that the server will change the master database writes binary log (binary log), slave server to synchronize the binary log events (binary log events) to its relay log (relay log), then slave the relay server redo log event data manipulation.
  Note: the server is opened by a slave I / O thread pull data. More pressure on the single master single slave on a slave server case, but greater pressure on the single master multi slave master server case.
(2) The role of
  1. failover
  2. backup service, failure to execute SQL statements generated recovery, limited backup
  3. A high-performance, multiple Slave may, separating read and write
(3) Common Scheme
  1. a more from the master, corresponding to a plurality of master slave, I / O pressure is concentrated on the master.
  2.MSS, S is the middle of the slave relay, in order to alleviate the master server I / O in the middle of the pressure of a slave relay.
  3.MM, each other by the double main, mainly among the
  4.MMM, basically useless
  5 from a multi-master, which is to save costs, will automatically integrate a plurality of master data, but the data will be modified very much, from the server high pressure.

(4) the main relevant parameters mysql profile encounter when copying from

server-id = ID number 1 of the database, the value must be a positive integer value between 1 to 2 ^ 32-1, and unique. master and slave must be present.
log-bin = mysql-bin to enable binary log file and specify, can be a relative path (based datadir), or an absolute path. master must exist, slave can exist.
relay_log = mysql-relay-bin logs to enable and specify the relay, may be a relative path (DATADIR based), or an absolute path. slave parameters.
log-slave-updates = 1 replication events will write their own binary log. slave parameters.
read_only = 1 read-only databases, slave parameters, typically used for separate read and write.
sync-binlog = 1 log file is written to disk in real time binary synchronous
binlog-format = [STATEMENT | ROW | MIXED] based on three values represent the SQL statement replication (STATEMENT), row-based replication (ROW), mixed-mode replication ( MIXED). Detailed view: https://www.cnblogs.com/langtianya/p/5504774.html 

 

If the following two parameters have not specified, the default copy all slave database
binlog-do-db = data synchronization binary database name, Master parameter
binary database name binlog-ignore-db = mysql not synchronized, Master parameters

 

explicit_defaults_for_timestamp = true This is to cancel TIMESTAMP character types (time stamp) are automatically updated with the update operation (must be written before the creation timestamp character type configuration file, or need to modify the parameters of the column), the default is off, useless character to TIMESTAMP type not set. explicit_defaults_for_timestamp parameters Detailed view: https://www.cnblogs.com/JiangLe/p/6956865.html

 

slave-skip-errors have four possible values, off, all, ddl_exist_errors and error codes ([list of error codes]) , default OFF
slave-skip-errors = 1062,1053 Error Skip specified error code, continued perform a copy
slave-skip-errors = all errors skip all
slave-skip-errors = ddl_exist_errors this is a series error Code: 1007,1008,1050,1051,1054,1060,1061,1068,1094,1146
Description:
  1007: database already exists, create a database failure
  1008: The database does not exist, delete the database failed
  1050: data tables already exist, create a data table failed
  1050: data table does not exist, delete the data table failed
  1054: field does not exist, or program files have now database conflict
  1060: duplicate field, resulting in not insert
  1061: duplicate key name
  1068: more than one primary key defined
  1094: location thread ID
  1146: data tables are missing, please restore the database

(4) The deployment MS (a master-slave)

Experimental environment: youxi1 192.168.5.101 CentOS7.5 Mysql5.7.16 Mysql port 3306 to do Master
     youxi2 192.168.5.102 port 3306 CentOS7.5 Mysql5.7.16 Mysql do Slave

1) operation on the master server

  Creating a user required for replication

mysql> grant replication slave on *.* to [email protected] identified by '12345678';
Query OK, 0 rows affected, 1 warning (0.01 sec)
mysql> flush privileges;  //刷新权限表

  Creating a database for replication

mysql> create database test_db;
Query OK, 1 row affected (0.00 sec)

  Modify the configuration files, operating at [mysqld] module. Then restart mysqld

[youxi1 the root @ ~] # Vim /etc/my.cnf 
Server-ID = // add or modify. 1 
log-bin bin-Master-MySQL = 
the binlog-do-DB = TEST_DB 
Slave-// All Skip-personal errors = I recommend skipping wrong, so wrong slave will not run these statements [root @ youxi1 ~] # systemctl restart mysqld

  View master status, record the current log file name and the current node.

mysql> show master status \ G // This is to check the status of master 
*************************** 1. row ***** ********************** 
             file: MySQL-bin-master.000001 // current log file 
         Position: 154 // current node 
     Binlog_Do_DB: test_db // sync database 
 Binlog_Ignore_DB: // database is not synchronized 
Executed_Gtid_Set: 
1 Row in the SET (0.00 sec) 

MySQL> Show binlog Events \ G // this is the view binary log information 
**************** *************************** 1. Row *********** 
   log_name: MySQL-bin-master.000001 // log file name 
        Pos: 4 // starting node 
 event_type: Format_desc // event type 
  Server_id: 1 // execution server identifies 
End_log_pos: 123 // end node  
       Info: server ver: 5.7.16-log , Binlog ver: 4 / / information, more of a SQL statement executed
**************** *********** 2. row ***************************
   Log_name: mysql-bin-master.000001
        Pos: 123
 Event_type: Previous_gtids
  Server_id: 1
End_log_pos: 154
       Info: 
2 rows in set (0.00 sec)

Description: Show binlog Events [the IN 'log_name'] [the FROM pos] [LIMIT [offset,] row_count]
    the IN 'log_name' specified binlog file name you want to query (that is, do not specify the first binlog file)
    the FROM pos Specify which pos search from the starting point (that is, do not specify a start counting from the first pos point of the entire file)
    LIMIT [offset,] offset (not specified is 0)
    row_count total number of queries (do not specify that all lines)

  Note: If the firewall is turned on, remember to add the port number mysql.

[root@youxi1 ~]# firewall-cmd --permanent --zone=public --add-port=3306/tcp
success
[root@youxi1 ~]# firewall-cmd --reload 
success
[root@youxi1 ~]# firewall-cmd --zone=public --list-ports 
3306/tcp

 2) operation on the slave server

  master and slave must ensure that the same version of the database, copy the database exactly the same as before the binary logging operations.

  Establish exactly the same with the master database needs to be replicated, can be derived from the master and then imported into the slave, you can create manually.

mysql> create database test_db;
Query OK, 1 row affected (0.01 sec)

  Modify the configuration files, operating at [mysqld] module. Then restart mysqld.

[root @ youxi2 ~] # Vim /etc/my.cnf 
Server-ID = 2 // If there are no additional requirements (e.g. MSS, MM, separate read and write, the MHA, etc.), so that it 
[root @ youxi2 ~ ] # systemctl restart mysqld

  Specify the configuration information of the master

mysql> stop slave; // If this is the first configuration slave, may not be required 
Query the OK, 0 rows affected, warning. 1 (0.00 sec) 

MySQL> Change Master to MASTER_HOST = '192.168.5.101', the IP address of the // master 
    -> master_port = 3306, // port number, default 3306 
    -> MASTER_USER = 'the repl', // user name 
    -> master_password = '12345678', // password 
    -> master_log_file = 'mysql-bin -master.000001', // read binary log files from which 
    -> master_log_pos = 154; // pos point from which to start reading binary log files 
Query the OK, 0 rows affected, 2 Represents warnings (0.06 sec) 
// If there is no default master_log_file and master_log_pos from the start point of the first binary 0pos log file 
MySQL> Slave start; 
Query the OK, 0 rows affected (0.02 sec)

  Note: If you reconfigure the master after the error message, then the IP address of the master, the port number, user name, password, no change can not be reconfigured, you can simply specify master_log_file and master_log_pos.

  View slave status

mysql> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.5.101
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin-master.000001
          Read_Master_Log_Pos: 154
               Relay_Log_File: youxi2-relay-bin.000003
                Relay_Log_Pos: 327
        Relay_Master_Log_File: mysql-bin-master.000001
             Slave_IO_Running: Yes  //这两行都是Yes时就是配置正确了
            Slave_SQL_Running: Yes

3) Testing

  Create a table in the master, and add the data

mysql> use test_db;
Database changed
mysql> create table user_id(id int,name varchar(20));
Query OK, 0 rows affected (0.05 sec)

mysql> insert into user_id values(1,'zhangsan');
Query OK, 1 row affected (0.02 sec)

  View on slave

mysql> select * from test_db.user_id;
+------+----------+
| id   | name     |
+------+----------+
|    1 | zhangsan |
+------+----------+
1 row in set (0.00 sec)

(5) Deployment MM

  MM is actually based on MS to do a reverse master-slave. I then made a down above MS

1) operation on youxi2 (formerly Salve)

  Create a replication user

mysql> grant replication slave on *.* to [email protected] identified by '12345678';
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> flush privileges;      
Query OK, 0 rows affected (0.00 sec)

  Modify the configuration files, operating at [mysqld] module. Then restart mysqld.

[youxi2 the root @ ~] # Vim /etc/my.cnf 
log-bin bin-Slave-MySQL = // add or modify 
the binlog-do-DB = TEST_DB 
[youxi2 the root @ ~] # systemctl the restart mysqld

  View master status

mysql> show master status\G
*************************** 1. row ***************************
             File: mysql-bin-slave.000001
         Position: 154
     Binlog_Do_DB: test_db
 Binlog_Ignore_DB: 
Executed_Gtid_Set: 
1 row in set (0.00 sec)

  Note: If the firewall is turned on, remember to add the port number mysql.

[root@youxi2 ~]# firewall-cmd --permanent --zone=public --add-port=3306/tcp
success
[root@youxi2 ~]# firewall-cmd --reload 
success
[root@youxi2 ~]# firewall-cmd --zone=public --list-ports 
3306/tcp

2) operate on youxi1 (original master)

  Specifies the master configuration information

mysql> stop slave;  //如果第一次配置slave,可以不需要
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> change master to master_host='192.168.5.102',master_user='repl',master_password='12345678',master_log_file='mysql-bin-slave.000001',master_log_pos=154;
Query OK, 0 rows affected, 2 warnings (0.02 sec)

mysql> start slave;
Query OK, 0 rows affected (0.01 sec)

  View slave status

mysql> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.5.102
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin-slave.000001
          Read_Master_Log_Pos: 154
               Relay_Log_File: youxi1-relay-bin.000002
                Relay_Log_Pos: 326
        Relay_Master_Log_File: mysql-bin-slave.000001
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes

3) Testing

  Inserting data on youxi2

mysql> use test_db;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> insert into user_id values(2,'lisi');
Query OK, 1 row affected (0.01 sec)

  View on youxi1

mysql> select * from test_db.user_id;
+------+----------+
| id   | name     |
+------+----------+
|    1 | zhangsan |
|    2 | lisi     |
+------+----------+
2 rows in set (0.01 sec)

(6). MSS deployment

Experimental environment: youxi1 192.168.5.101 CentOS7.5 Mysql5.7.16 Mysql port 3306 to do Master
     youxi2 192.168.5.102 port 3306 CentOS7.5 Mysql5.7.16 Mysql do Slave relay
     youxi3 192.168.5.103 CentOS7.5 Mysql5.7.16 Mysql do Slave port 3306

1) operation on the master server

  Creating a user required for replication

mysql> grant replication slave on *.* to [email protected] identified by '12345678';
Query OK, 0 rows affected, 1 warning (0.01 sec)
mysql> flush privileges;  //刷新权限表

  Creating a database for replication

mysql> create database test_db;
Query OK, 1 row affected (0.00 sec)

  Modify the configuration files, operating at [mysqld] module. Then restart mysqld

[root@youxi1 ~]# vim /etc/my.cnf
server-id=1
log-bin=mysql-bin-master
binlog-do-db=test_db
sync-binlog=1
binlog-format=row
[root@youxi1 ~]# systemctl restart mysqld

  View master status, record the current log file name and the current node

mysql> show master status\G
*************************** 1. row ***************************
             File: mysql-bin-master.000001
         Position: 154
     Binlog_Do_DB: test_db
 Binlog_Ignore_DB: 
Executed_Gtid_Set: 
1 row in set (0.00 sec)

mysql> show binlog events\G
*************************** 1. row ***************************
   Log_name: mysql-bin-master.000001
        Pos: 4
 Event_type: Format_desc
  Server_id: 1
End_log_pos: 123
       Info: Server ver: 5.7.16-log, Binlog ver: 4
*************************** 2. row ***************************
   Log_name: mysql-bin-master.000001
        Pos: 123
 Event_type: Previous_gtids
  Server_id: 1
End_log_pos: 154
       Info: 
2 rows in set (0.01 sec)

  Note: If the firewall is turned on, remember to add the port number mysql.

[root@youxi1 ~]# firewall-cmd --permanent --zone=public --add-port=3306/tcp
success
[root@youxi1 ~]# firewall-cmd --reload 
success
[root@youxi1 ~]# firewall-cmd --zone=public --list-ports 
3306/tcp

2) operation on the slave relay server

  slave relay server from a server, the primary server is the master server a slave server, which it itself is from, and the main.

  Creating a user for replication

mysql> grant replication slave on *.* to [email protected] identified by '12345678';
Query OK, 0 rows affected, 1 warning (0.01 sec)

mysql> flush privileges;  //刷新权限表
Query OK, 0 rows affected (0.01 sec)

  Create the same server with the master database, can be derived from the master and then imported into the slave, you can create manually.

mysql> create database test_db;
Query OK, 1 row affected (0.00 sec)

  Modify the configuration file, because it is a slave relay server itself does not produce data, the binary event log files are pulled from the master server, and there is no database synchronized or not specified.

[root @ youxi2 ~] # vim /etc/my.cnf 
Server-the above mentioned id = 2 
log-bin = MySQL-bin-slave1 // open the binary log file 
log-slave-updates = 1 // copy the events are written to the binary log document 
the binlog the format-Row = 
[youxi2 the root @ ~] # systemctl the restart mysqld

  Specify the configuration information of the master

mysql> stop slave; // If this is the first configuration slave, may not be required 
Query the OK, 0 rows affected, warning. 1 (0.00 sec) 

MySQL> Change Master to MASTER_HOST = '192.168.5.101', the IP address of the // master 
    -> master_user = 'repl', // username 
    -> master_password = '12345678', // password 
    -> master_log_file = 'mysql-bin -master.000001', // read binary log file from which 
    -> master_log_pos = 154; // pos point from which to start reading binary log files 
Query the OK, 0 rows affected, 2 Represents warnings (0.06 sec) 
// If there is no default 0pos master_log_pos master_log_file and starting from the first point of a binary log files 
mysql> Slave Start; 
Query the OK, 0 rows affected (0.02 sec)

  View master status, record the current log file name and the current node

mysql> show master status\G
*************************** 1. row ***************************
             File: mysql-bin-slave1.000001
         Position: 322
     Binlog_Do_DB: 
 Binlog_Ignore_DB: 
Executed_Gtid_Set: 
1 row in set (0.00 sec)

  View slave status

mysql> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.5.101
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin-master.000001
          Read_Master_Log_Pos: 154
               Relay_Log_File: youxi2-relay-bin.000002
                Relay_Log_Pos: 327
        Relay_Master_Log_File: mysql-bin-master.000001
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes

  Note: If the firewall is turned on, remember to add the port number mysql.

[root@youxi2 ~]# firewall-cmd --permanent --zone=public --add-port=3306/tcp
success
[root@youxi2 ~]# firewall-cmd --reload 
success
[root@youxi2 ~]# firewall-cmd --zone=public --list-ports 
3306/tcp

3) Configure slave server

  Create and master the same database, can be derived from the master and then imported into the slave, you can create manually.

mysql> create database test_db;
Query OK, 1 row affected (0.00 sec)

  Modify the configuration files, operating at [mysqld] module. Then restart mysqld.

[root@youxi3 ~]# vim /etc/my.cnf
server-id = 3
binlog-format=row
[root@youxi3 ~]# systemctl restart mysqld

  Specify the configuration information of the master

mysql> stop slave; // If this is the first configuration slave, may not be required 
Query the OK, 0 rows affected, warning. 1 (0.00 sec) 
 
MySQL> Change Master to MASTER_HOST = '192.168.5.102', the IP address of the // master 
    -> master_user = 'repl', // username 
    -> master_password = '12345678', // password 
    -> master_log_file = 'mysql-bin -slave1.000001', // read binary log file from which 
    -> master_log_pos = 322; // pos point from which to start reading the binary log files 
Query the OK, 0 rows affected, 2 Represents warnings (0.06 sec) 
// If there is no master_log_file and master_log_pos default 0pos points starting from the first binary log file 
mysql> Slave Start; 
Query the OK, 0 rows affected (0.02 sec)

  View slave status

mysql> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.5.102
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin-slave1.000001
          Read_Master_Log_Pos: 322
               Relay_Log_File: youxi3-relay-bin.000005
                Relay_Log_Pos: 327
        Relay_Master_Log_File: mysql-bin-slave1.000001
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes

4) Test

  Create a table on the master server, and add the data

mysql>  use test_db;
Database changed
mysql> create table user_id(id int,name varchar(20));
Query OK, 0 rows affected (0.03 sec)

mysql> insert into user_id values(1,'zhangsan');
Query OK, 1 row affected (0.05 sec)

  View on the slave relay server

mysql> select * from test_db.user_id;
+------+----------+
| id   | name     |
+------+----------+
|    1 | zhangsan |
+------+----------+
1 row in set (0.00 sec)

  View on the slave server

mysql> select * from test_db.user_id;
+------+----------+
| id   | name     |
+------+----------+
|    1 | zhangsan |
+------+----------+
1 row in set (0.00 sec)

5) If you do not want to slave relay server data synchronization, but without affecting the slave server how to do it

  First modify the slave relay server table is the black hole engine engine

mysql> set sql_log_bin = off; // Close binary log 
Query the OK, 0 rows affected (0.00 sec) 

MySQL> Table test_db.user_id ALTER ENGINE = blackhole; 
Query the OK,. 1 Row affected (0.03 sec) 
Records: Duplicates. 1: 0 Warnings : 0 

MySQL> = ON SET SQL_LOG_BIN; // open binary log 
Query OK, 0 rows affected (0.00 sec)

  Then view the status of slave slave server

mysql> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.5.102
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin-slave1.000001
          Read_Master_Log_Pos: 776
               Relay_Log_File: youxi3-relay-bin.000006
                Relay_Log_Pos: 327
        Relay_Master_Log_File: mysql-bin-slave1.000001
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes

  And then insert the test data on the master server

mysql> insert into user_id values(2,'lisi');
Query OK, 1 row affected (0.02 sec)

  View slave relay server

mysql> select * from test_db.user_id;
Empty set (0.00 sec)

  View slave server data

mysql> select * from test_db.user_id;
+------+----------+
| id   | name     |
+------+----------+
|    1 | zhangsan |
|    2 | lisi     |
+------+----------+
2 rows in set (0.00 sec)

. (7) extensions: mysql error 1593

  Call the shots from the copy, because I was cloned server, resulting in the same mysql uuid, so there are a 1593 error.

  When there is an error 1593, using SHOW VARIABLES LIKE '% server_%' ; see two mysql id and uuid. If the same server_id, enter /etc/my.cnf modify the value of the server_id. If the same server_uuid, using the SELECT uuid (); random obtaining a uuid, and then enters $ datadir / auto.cnf modify the value of uuid. Eventually you need to restart mysqld.

Guess you like

Origin www.cnblogs.com/diantong/p/11037423.html