mysql-- master (master-slave synchronization) Copy from

Content Highlights:

First, why should master-slave replication?

Two, Mysql replication master from type:

Third, the master copy of the work process:

Fourth, case presentations

First, why should master-slave replication?

In the corporate Web site, if the backend as long as a Mysql server, it will cause, such as a single point of failure, unable to handle a large number of concurrent data requests and data loss and other issues.

Therefore, it reflects the importance of the Mysql master-slave replication:

  • Mysql a master server, a plurality of backup data from the service master and slave;

  • Mysql standby database server to ensure data is the same;

  • The primary server goes down, the backup server can still continue to work, the data is guaranteed.

Two, Mysql replication master from type:

(1) Based on the statement of the copy (the default):

Statements executed on the primary server, execute the same statement from the server.


(2) row-based replication:

Copy the contents of the change from the server.


(3) mixed types of replication:

Once found based on statements can not be precisely replicated, it will take based replication lines.


Third, the master copy of the work process:

image.png


Preparing the environment:

  • Two virtual machines, build a good Mysql services;

  • A server call the shots, the other one to do from the server;

  • Experimental results: statements that are executed on the primary server, execute the same statement from the server will be.

Master server (master) IP address: 192.168.220.141
From the server (slave) IP address: 192.168.220.140

Step 1: Set time synchronization

1, the primary server:


(1) Installation time server:

yum install ntp -y


(2) modify the configuration file:


vim /etc/ntp.conf
server 127.127.220.0 // clock source is local
fudge 127.127.220.0 stratum 8 // set time to level 8


(3) to restart the service, and turn off the firewall, etc.


systemctl start ntpd // open the Time Synchronization Service
systemctl stop firewalld // turn off the firewall
setenforce 0

2, from the server:


(1) Installation time server:

yum install ntp ntpdate -y


(2) open service, turn off the firewall:


systemctl start ntpd
systemctl stop firewalld
setenforce 0


(3)进行时间同步:

/usr/sbin/ntpdate 192.168.220.141

image.png

第二步:主服务器配置

1、修改配置文件:


vim /etc/my.cnf
server-id  = 11              //指定id号,服务器的唯一标识
log-bin=master-bin           //主服务器日志文件
log-slave-updates=true       //从服务器更新二进制日志

2、重启服务:

systemctl restart mysqld.service

3、进入数据库:


mysql -uroot -p      //进入数据库
GRANT REPLICATION SLAVE ON *.* TO 'myslave'@'192.168.220.%' IDENTIFIED BY '123456';
FLUSH PRIVILEGES;                //刷新MySQL的系统权限相关表
show master status;             //查看主服务器状态

image.png

第三步:从服务器配置

1、修改配置文件:


vim /etc/my.cnf
server-id  = 22             //指定id号,服务器的唯一标识
relay-log=relay-log-bin        //从主服务器上同步日志文件记录到本地
relay-log-index=slave-relay-bin.index        //定义relay-log的位置和名称


2、重启mysql服务:

systemctl restart mysqld.service


3、进入数据库:


mysql -uroot -p
change master to master_host='192.168.220.141',master_user='myslave',master_password='123456',master_log_file='master-bin.000001',master_log_pos=604;
start slave;         
show slave status \ G; // check the status of

image.png

Step four: Test the master-slave synchronization

1. Create a database named school on the first primary server:

create database school;

image.png

2, viewing from the database server about whether there are school library:

image.png


From the master to complete the configuration synchronization.




Guess you like

Origin blog.51cto.com/14475876/2457293