Master-slave replication experiment:
Step 1: Operation on the main server
1. Modify the main server master:
[root@localhost ~]# vim /etc/my.cnf server_id = 1 //[Required] Unique server ID, default is 1 log-bin=mysql-bin //[must] enable binary logging
2. Restart the main database
[root@localhost ~]# systemctl restart mysqld
3. Create an account on the master server and authorize the slave:
mysql> grant replication slave on *.* to 'zhangsan'@'%' identified by '123456'; Query OK, 0 rows affected (0.00 sec) mysql> flush privileges; Query OK, 0 rows affected (0.00 sec)
4. Log in to the mysql of the master server and query the status of the master
mysql> show master status; +------------------+----------+--------------+------------------+-------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +------------------+----------+--------------+------------------+-------------------+ | mysql-bin.000001 | 401 | | | | +------------------+----------+--------------+------------------+-------------------+ 1 row in set (0.00 sec)
Note: Do not operate the master server MYSQL after this step to prevent the state value of the master server from changing
Step 2: Operation from the server
1. Modify the slave server:
[root@localhost ~]# vim /etc/my.cnf server_id = 2 log-bin=mysql-bin
2. Restart the slave database
[root@localhost ~]# systemctl restart mysqld
3. Configure the slave server Slave:
mysql> change master to master_host='192.168.35.131',master_user='zhangsan',master_password='123456', master_log_file='mysql-bin.000001',master_log_pos=401; Query OK, 0 rows affected, 2 warnings (0.02 sec) mysql> flush privileges; Query OK, 0 rows affected (0.00 sec)
4. Start the replication function from the server and check the status of the replication function from the server
mysql> start slave; Query OK, 0 rows affected (0.13 sec) mysql> show slave status \G; *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.35.131 //Master server address Master_User: zhangsan //Authorized account name, try to avoid using root Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000001 Read_Master_Log_Pos: 401 //#The position of reading the binary log synchronously, greater than or equal to Exec_Master_Log_Pos Relay_Log_File: localhost-relay-bin.000002 Relay_Log_Pos: 283 Relay_Master_Log_File: mysql-bin.000001 Slave_IO_Running: Yes //This state must be YES Slave_SQL_Running: Yes //This state must be 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: 401 Relay_Log_Space: 460 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: 31f8646b-484e-11e8-b503-000c298f5b19 Master_Info_File: /data/mysqldb/master.info SQL_Delay: 0 SQL_Remaining_Delay: NULL Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it 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 1 row in set (0.00 sec) ERROR: No query specified
Note: The Slave_IO and Slave_SQL processes must be running normally, that is, the YES state, otherwise it is an error state (for example: one of the NO is an error).
Step 3: Detect master-slave configuration
1. Create a library data in the master database and check whether there is any data in the slave library
In the main library:
mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | test | +--------------------+ 4 rows in set (0.00 sec) mysql> create database data; Query OK, 1 row affected (0.00 sec) mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | data | | mysql | | performance_schema | | test | +--------------------+ 5 rows in set (0.00 sec)
2. View from the library:
mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | data | | mysql | | performance_schema | | test | +--------------------+ 5 rows in set (0.00 sec)
It can be seen that there is a data database in the slave database, indicating that the master-slave replication configuration is completed, but it should be noted that operations such as update and delete cannot be performed from the slave table, only queries can be used, so be careful.
Summary: After completing the above operation process, the master-slave server configuration is completed.