Principles of mysql master-slave replication, configuration method and online addition of multiple slave Databases

Source: Internet
Author: User
Database master-slave replication principle: as the name suggests, the master-slave replication of a database is to copy data from the master database to the slave database. The whole replication between the master and slave requires three threads to complete, two of them are on the slave side and one is on the master side. The binlog function must be enabled on the master, because the full operation day of the master data needs to be obtained from the database.

Database master-slave replication principle: as the name suggests, the master-slave replication of a database is to copy data from the master database to the slave database. The whole replication between the master and slave requires three threads to complete, two of them are on the slave side and one is on the master side. The binlog function must be enabled on the master, because the full operation day of the master data needs to be obtained from the database.

Principles of master-slave database replication:

As the name suggests, the master-slave replication of a database is replicated from the master database to the slave database. The whole replication between the master database and the slave requires three threads, two of which are on the slave database and one on the master database.

The binlog function must be enabled on the master, because the database needs to obtain the complete operation log of the master data, and then perform various operations in the execution log in its own order.

Main steps:

1. Connect the slave io thread to the master and request the operation log content after the specified location of the specified log file;

2. After the master receives the slave io thread request, it returns the content at the specified location of the specified log read in the request to the slave io thread, including the location and name of the binlog;

3. After receiving information, the slave io thread writes the received log content to the end of the relay log file on the slave end, the binlog file name and location are recorded in the master-info file, so that the next request is sent to the master;

4. The slave SQL thread will detect some new log Content in the relay log and parse it into executable query statements to be executed on the slave side, so as to achieve data consistency between the master and slave.



Preparation Method:

1. Edit the master configuration file/etc/my. cnf and add the following content to [mysqld:

Log-bin = mysql-bin // enable the mysql operation log function

Binlog_format = mixed // master-slave replication mode. The default value of the hybrid mode (MBR) is

Binlog_cache_size = 4 M // you can specify the binlog cache size.

Max_binlog_size = 300 M // you can specify the maximum size of the binlog file.

2. Authorize the slave database on the master to create a VPC account for the replication user

Mysql> grantreplication slave on *. * TO 'repl' @ '% 'identified BY 'repl ';

Mysql> flush privileges;

3. Back up the master database

When backing up master data, you must lock the master table:

Mysql> flush tables with read lock;

Query OK, 0 rows affected (0.00 sec)

Mysql> reset master; // refresh the database

Query OK, 0 rows affected (0.00 sec)

If you do not exit the terminal (otherwise the lock table will become invalid), enable a terminal to directly package and compress data files or use mysqldump to export data:

# Mysqldump-uroot-h 127.0.0.1 -- opt-r xxxxxx>/data/mysql/backup/XXXXXX. SQL

# Cd/data/mysql/backup/

4. Back up the database to the slave Database

# Tar-czvf XXXXXX.tar.gz XXXXXX. SQL

# Scp XXXXXX.tar.gz root@10.7.70.42:/data/mysql/backup

You can import the database to another slave machine in the same way. After the import is complete, run the following UNLOCK command on the command terminal:

Mysql> UNLOCKTABLES;

Mysql> show processlist;

Mysql> show master status \ G; // view the bin-log and id of the master server

5. Set the slave host

Edit the/etc/my. cnf configuration file of the slave database. The preceding configuration parameters are the same as those of the master database and are adjusted according to the hardware memory:

# Binlog_format = mixed

# Required unique id between 1 and 2 ^ 32-1

# Defaults to 1 if master-host is not set

# But will not function as a master if omitted

Server-id = 2 // slave id. Do not repeat it.

Read_only = 1

Binlog_format = mixed

Binlog_cache_size = 4 M // you can specify the binlog cache size.

Max_binlog_size = 300 M // you can specify the maximum size of the binlog file.

Create a new database on the slave host and import the data of the master database:

Mysql> create database XXX;

Mysql> useXXX;

Mysql> set names utf8;

Mysql> source/XXXXXX/XXX. SQL


Set the master of the slave database:

Mysql> change master to MASTER_HOST = 'xx. XX. XX. xx', // ip address of the MASTER database

-> MASTER_USER = 'repl ',

-> MASTER_PASSWORD = 'repl ',

-> MASTER_LOG_FILE = 'mysql-bin.000001 ', // set this parameter based on the binlog and id of the master server.

-> MASTER_LOG_POS = 107 ;//


After setting, enable the slave service:

Mysql> slave start;

Query OK, 0 rows affected (0.00 sec)

Mysql> show slave status \ G; // displays the slave status.


If:If both Slave_IO_Running and Slave_ SQL _Running are Yes, the configuration is successful.



Add multiple slave databases online:


Principle: If the slave database is directly added to the master database, the mysql service will be stopped to lock the table, thus affecting the normal workflow, therefore, online addition of slave databases can achieve database replication by backing up old slave databases. The change master can be used again to add the slave database online.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.