Database courses (01) -MySQL master-slave replication and data backup

1.MySQL database

MySQL use as one of the most widely used database in the world, free is one of the reasons . But it can not be ignored is that the function itself is indeed very powerful. With the development of technology in the actual production environment, not by a single MySQL database server to meet the actual demand. At this point the database cluster on a good solution to this problem.

MySQL uses a distributed cluster, can build a high concurrency, load balancing cluster server. Prior to this we have to ensure that each server in the MySQL data synchronization. We can synchronize data can easily be completed by MySQL internal configuration, there are master replication and master-master replication from (Master Slave) .

1.1 Master-slave replication

In MySQL clustered environment, it can be divided from the master node and the node can be achieved by copying data from the primary backup, failover, MySQL Cluster, high availability, separate read and write and the like.

MySQL master is copied from MySQL itself comes with a feature that does not require additional third-party software can be achieved, it is not a copy function to copy files to achieve, but with the master binlog log file inside the SQL commands implemented from copy, and then I can be understood as a master-side implementation of SQL commands, it will perform again at the same Salve end, so as to achieve the effect of master-slave replication

Here Insert Picture Description
Principle is as follows:

  1. Library is generated from two threads, one I / O thread, a thread SQL;
  2. I / O request binlog thread to the main library, and writes the obtained log binlog relay log (relay log) file;
  3. Primary library will generate a log dump thread, from a library used to binlog i / o threads pass;
  4. SQL thread, reads the relay log log file, and parsed into specific operation to achieve consistent master-slave operation, and consistent final data;

2. The master-slave replication environment configuration

First, prepare two servers:

server ip
The primary database server 192.168.162.132
From the database server 192.168.162.133

2.1 Installing MySQL

In "192.168.162.132" and "192.168.162.133" to install MySQL, not go into details here, reference is made to the article:

The installation is complete, you can visit:
Here Insert Picture Description

MySQL 2.2 master server configuration

1. Configure my.cnf

vi /etc/my.cnf  

Add the following:

server_id=132  ###服务器id
log-bin=mysql-bin   ###开启日志文件

Here Insert Picture Description
2. Restart the mysql service

service mysqld restart
mysql -u root -p

3. Verify that the configuration has been successfully
able to query server_id instructions correspond in the configuration file has been configured successfully:

show variables like '%server_id%';

Here Insert Picture Description
4. able to see the files synchronized, and the number of rows have configured successfully

show master status;

Here Insert Picture Description

2.3 MySQL server configuration from

1. Configure my.cnf

vi /etc/my.cnf

Configuration reads as follows:

server_id=133  ###从服务器server_id
log-bin=mysql-bin  ###日志文件同步方式
binlog_do_db=test   ###同步数据库

Here Insert Picture Description

2. Restart the mysql service

service mysqld restart
mysql -u root -p

3. Verify that the configuration has been successful

Server_id instructions correspond to query the configuration file has been configured successfully

show variables like '%server_id%';

Here Insert Picture Description

From 2.4 Copy Test

1. The synchronization server configuration from the primary server

First check the status of the primary server:

show master status;

Here Insert Picture Description
Configure the master server from the server:

CHANGE MASTER TO master_host = '192.168.162.132',
master_user = 'root',
master_password = '123456',
master_log_file = 'mysql-bin.000001',
master_log_pos = 1148;

Start Sync:

start slave;

4. query synchronization status

SHOW SLAVE STATUS

Waiting display the master node:
Here Insert Picture Description

2. The primary server creates a test database, create tables and insert data:

create database test;

use test;

CREATE TABLE `t_user` (
	`uuid` VARCHAR ( 200 ) NOT NULL,
	`name` VARCHAR ( 50 ) DEFAULT NULL,
	`age` INT ( 11 ) DEFAULT NULL,
	`sex` VARCHAR ( 10 ) DEFAULT NULL,
PRIMARY KEY ( `uuid` ) 
);

INSERT INTO `test`.`t_user`(`uuid`, `name`, `age`, `sex`) VALUES ('0000-0000-0000-0001', 'zhangsan', 18, '0');

INSERT INTO `test`.`t_user`(`uuid`, `name`, `age`, `sex`) VALUES ('0000-0000-0000-0002', 'lisi', 20, '0');

Here Insert Picture Description
3. Verify

You can see already coming in from the primary server 192.168.162.132 192.168.162.133 synchronize content from the server:
Here Insert Picture Description
Now I add a master server in the data:

INSERT INTO `test`.`t_user`(`uuid`, `name`, `age`, `sex`) VALUES ('0000-0000-0000-0003', 'wangwu', 30, '0');

132 can immediately see from the data passed from the synchronization server 133.
Here Insert Picture Description
Here Insert Picture Description

2.4 Considerations

1. UUID conflict :

Check the status of replication from the server

SHOW SLAVE STATUS

If the log query error:

Fatal error: The slave I/O thread stops because
master and slave have equal MySQL server UUIDs; these UUIDs must be
different for replication to work.

This is because when the server UUID generated clones cross repeat

Solution (delete auto.cnf file):

cd /var/lib/mysql
rm -rf auto.cnf

Then you can restart the server:

service mysqld restart

2. Stop Sync :
Also stop command to synchronize from the server:

stop slave;

to sum up

Here Insert Picture Description

Released 2618 original articles · won praise 4883 · Views 390,000 +

Guess you like

Origin blog.csdn.net/qq_20042935/article/details/103627350