mysql: master-slave replication

Transfer the DDL and DML of the master database to the replication server (slave server) through the binary log, and then re-execute the log on the slave server.
Yes, the data of the slave database and the master database are kept in sync
. 1. If there is a problem with the master database, it can be quickly Switch to the slave library service
2. Execute queries on the slave library to reduce the access pressure of the main library
3. Perform backups in the slave library to avoid affecting the main library service during backup The
MySQL implementation is asynchronous replication, if data with high real-time requirements still needs to be copied from the main library The master library obtains the
replication principle:
when the master library submits a transaction, it will record the data changes in the binary file binlog as time events.
The master library pushes the events in the binlog to the [relay log] relay log of the slave library, and the slave library uses the relay log according to the relay log. For data change operations
, 3 threads complete the master-slave replication operation. The binlogdump thread runs in the main library. The i/o thread runs in the slave library. The sql thread runs in the slave library. The
i/o thread establishes a connection with the main library, and the main library creates a binlogdump thread to read. The event is sent to the io thread, the io thread obtains the event data and updates it to the relay log,
reads the update database event in the relay log from the library sql thread and applies it.
Replication method:
show variables like '%binlog_format%'
1. Based on the statement Replication:
The SQL statement executed on the master server executes the same statement on the slave server.
MySQL uses statement-based replication by default, which is more efficient. Row-based copying is automatically selected when exact copying is not possible.
2. Row-based replication:
3. File replication
============================================
1. Install mysql
===== =====================================
MySQL Primary Server: 192.168.150.135:3306
MySQL Backup Server: 192.168 .150.136:3306
===========================================
2. Added backup
User Both primary and secondary servers execute the following commands;
mysql>create user repl;
mysql>grant replication slave,replication client on *.* to repl@'192.168.150.%' identified by '123456'; 
===== =====================================
3. Configure
the main server:
modify or add the following configuration
#server_id to The unique identifier of the database service, generally the last number of the server Ip
server_id=135
log_bin=/Users/samson/log/mysql/mysql-bin.log 
log_error=/Users/samson/log/mysql/error.logView

log
mysql> SHOW MASTER STATUS;
-------------------------------------------- ---
Backup server:
Modify or add the following configuration (configuration file my.cnf)
Modify or add the following configuration
#135 server_id
server_id=136
log_bin=/home/mysql/log/mysql-bin.log   
relay_log=/ var/lib/mysql/mysql-replay-bin 
log_slave_updates=135 
read_only=135 
--------------------------------- -------------- After modifying the configuration, both the active and standby servers need to be restarted: #service mysqld restart  ===================== ===================== 4. Start replication, log in to the backup server mysql; tell the standby database how to connect to the main database and place its log file mysql>change master to master_host='192.168 .150.135', master_user='repl',











master_log_file='mysql-bin.000001',
master_log_pos=0; 

start replication
mysql>start slave; 

view slave status information
mysql>show slave status\G; 
see the following information to start replication successfully:
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
== ========================================
5. Test
any operation that changes the main server database, All will be synchronized to the slave server
===========================================

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326182230&siteId=291194637