Copy and tuning MySQL

I. Introduction

MySQL comes with replication scheme, benefits include:

data backup.
Load balancing.
Distributed data.

Concepts:

Host (master): the database is replicated.
Slave (slave): Copy the database host data.

Copy steps:
(1) Master Record details of the changes, log into binary (binary log)..
(2). Master sends a synchronization message to the slave.
(3). Slave after receiving the message, the master copy of the relay to the local binary log log (relay log).
(4). Slave reproducing relay log messages to change the data in the database.

Place the piece of classic pictures to illustrate this process: Copy Schematic

II. Implementing Replication

Copy implement the following steps:

1. Set main MySQL database server-id and the binary log

MySQL configuration files are typically stored in /etc/my.cnf

# 在[mysqld]下面添加配置选项
[mysqld]
server-id=1
log-bin=mysql-bin.log

server-id is a unique identifier in the database the entire database cluster, must be unique.
Restart MySQL.
注:如果MySQL配置文件中已经配置过此文件,则可以跳过此步。

2. Copy the new account

In the main library inside the new main library account used to copy data from a database, and grant permission to copy.

mysql> GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO user_name@'host' IDENTIFIED BY 'password';

3. Set the main MySQL database server-id

And configuration as the second step, where there are two points to note:

  1. If not from the library as the main library from another library, then you do not need to configure binary log.
  2. In many cases you do not need to copy the entire database to copy the main library (especially mysql configuration information repository). You can configure replicate_do_db to specify a replicated database

4. library initialization data from the main library

If the amount of data is not large, the tool may be used to export mysqldump master data repository, then imported into the repository.

mysqldump --single-transaction --triggers --master-data databasename > data.sql

If the situation should be under a large amount of data to be exported using Xtrabackup database, it does not describe here.
There may be students ask, why not use the binary log to initialize it?

  1. If we run the main library relatively long period of time, and is not suitable for use to copy the data from the database according to the binary log, use the binary log to initialize from the library would be more time-consuming and performance.
  2. More often, the binary log configuration items in the main library is not open, therefore there is no binary log before the operation.

5. Copy the open

Execute the following command from the library

mysql> CHANGE MASTER TO MASTER_HOST='host',
-> MASTER_USER='user',
-> MASTER_PASSWORD='password', -> MASTER_LOG_FILE='mysql-bin.000001', -> MASTER_LOG_POS=0;

Note that the last two commands: MASTER_LOG_FILE and MASTER_LOG_POS, which indicates the start of a binary file from the library to read from the offset from there, these two parameters can be found in our imported SQL inside.

Import Note

Open copy

start slave;

This time to complete the copy, update data or a new data in the main library can query from the library to the result.
Copy View
On the main library you can also check the status of replication thread.
Thread State

III. Copy the log format

MySQL replication log format, there are three main ways according to different inventory data put the following three:

Replication Feature advantage Shortcoming
row Copy row format based on modifications of the information data record for each row. If you modify a SQL data 2w line, it will log format line 2w Ensure strong data consistency, and because the record is the result of execution, execution from the database restore will be faster Many number of log records, transfer between master and slave needs more time.
statement Log-based replication format segment, which is the record SQL record changes, rather than rows of change. The minimum amount of logging. For some function of the output inconclusive results, the implementation is likely to be a problem again from the library, such as uuid, from the library main library when the data needs to be performed according to the log restore again SQL, time is relatively slow.
mixed Mixing the above two log format log records, as to when to use which is determined by the way the log MySQL itself. You can balance the advantages and disadvantages of the above two log format.  

mysql5.7 statement before the default format.
Setting mode, you can set in the configuration file (preferred):

binlog_format=ROW

Or temporary global variable (the current connection is valid mysql):

查看日志格式
mysql > show variables like 'binlog_format';

设置日志格式
mysql > set binlog_format='row';

由于两个主从服务器一般都会放在同一个机房里面,两者之间同步的速度会会比较快,为保证强一致性,应该首选行的日志格式记录(row),保证传输素速度可以选择混合方式(mixed)。
而行的日志格式有下面三种记录方式:

记录方式 特点
minimal 只记录被修改列的数据
full 记录被修改的行的全部列的数据
noblob 特点同上,只是如果没有修改blob和text类型的列的情况下,不会记录这些列的数据(也就是大数据列)

mysql默认是full,最好修改成minimal。

binlog_row_image=minimal

四. 主从复制延迟

由于主库和从库之间不在同一个主机上,数据同步之间不可以避免地具有延迟,解决的方法有添加缓存,业务层的跳转等待,如果非得从数据库层面去减缓延迟问题,可以从复制时候的三大步骤(主库产生日志,主从传输日志,从库还原日志内容)入手:
1.主库写入到日志的速度
控制主库的事务大小,分割大事务为多个小事务。
如插入20w的数据,改成插入多次5000行(可以利用分页的思路)

2.二进制日志在主从之间传输时间 主从之间尽量在同一个机房或地域。
日志格式改用MIXED,且设置行的日志格式未minimal,原理详见上面的日志格式介绍。

3.减少从库还原日志的时间
在MySQL5.7版本后可以利用逻辑时钟方式分配SQL多线程。
设置逻辑时钟:slave_parallel_type=‘logical_clock’;
设置复制线程个数:slave_parallel_workers=4;

五. 需要注意的地方

    1. 重启MySQL最好切换未MySQL用户再进行操作,不然文件启动后会有权限问题。
    2. MySQL to build a good environment after the set configuration in the log-bin option, so that if a database needs to be copied from the library, you do not need to restart the database, interrupt the business.
    3. You need to open firewall port corresponding mysql main library.
    4. Because of the way from the main library database synchronization, monitoring the main library of information transmitted, rather than polling, so if a fault occurs the communication, re-connection if the primary library does not change the operating data, synchronization data is not from the library, so inserting null data may be synchronized by way of a transaction.

Guess you like

Origin www.cnblogs.com/niuben/p/10961979.html
Recommended