Master-slave synchronization data MySql

1.1. Synchronization Introduction

Mysql master-slave synchronization is an asynchronous replication process, replication from one to the other Master Slave. The copy operation will be accomplished primarily achieved between the Master and Slave by the three threads, which two threads (threads and the Sql IO threads) Slave side, another thread (Thread IO) at the Master side. 

   To achieve master-slave synchronization of MySQL, you must first open the Master end of BinaryLog (mysql-bin) function, can not be achieved otherwise. Because the whole process is actually a copy Slave from the Master side to obtain the log and then himself completely in the implementation of the order who log recorded in various operations. Open may increase the MySQL Binary Log "log- by using" -log-bin "parameter options in starting the MySQL Server or mysqld parameters ([mysqld] parameters after identification section) in the configuration file my.cnf bin "parameter entry.

1.2. Mysql from the master server configuration

Primary Server IP: 192.168.1.60

From the service IP: 192.168.1.61

Linux:centOS

Mysql Version: 5.1.58

The following configuration are configured on this service

A primary database operation

Check the main library server configuration file my.cnf, according mysql installation package, different versions of the relevant configuration file path and the path will be substantially different, but in most cases is the same. The file in the etc / directory. Mysql service is also provided when configured to use different profiles for different servers, to use them, just eleven my.cnf file to replace the above with any of them. NOTE: Replace the file name must be my.cnf.

cnf usually have four profiles can be viewed by order # ll /usr/share/mysql/*.cnf;

my-small.cnf memory is less than or equal to 64M, provide very little database services;

my-medium.cnf memory 32M - but also with other services between and 64M, for example, Web;

my-large.cnf 512M memory is the main provider of database services;

my-huge.cnf memory is 1G to 2G, the main provider of database services;

my-innodb-heavy-4G.cnf memory is 4G, mainly to provide a greater load on the database service (usually servers use this);

Configuration files can be selected according to different server configuration cnf.

 

1, the configuration file my.cnf

Use the default etc / my.cnf profile configuration, to configure the master server you need to add the following items in the main configuration file (when they are not added to ensure that the file exists, because as above to select several different configuration file, the contents of the configuration will be different, if there is no corresponding entries need to be added).

With vi etc / my.cnf file open, file edit, add modifications [mysqld] below:

The above mentioned id = 1-Server   # This is the database ID, which is unique, the main library defaults to 1, this ID increment from other libraries, ID value can not be repeated, otherwise it will sync error;

log-bin = mysql-bin   binary log file, this is required, or can not synchronize data;

binlog-do-db = testcreate   database # need to be synchronized, if the need to synchronize a plurality of databases;

Continue to add this.

binlog-do-db = testcreate1

binlog-do-db = testcreate2

binlog-ignore-db = mysql  does not require synchronization of the database;

So far the main server configuration my.cnf configuration is complete, save and exit the file.

Now restart the server to Mysql: #servicemysqld restart If that fails, then the proof profile error, need to re-check.

Recommendation: best for their backup before my.cnf modified, so as not to modify the configuration file can not fail in time to restart the server.

2. After the configuration database need to establish an account for synchronization

Main library needs to provide an account from the library to make for onwards connection and synchronize with the command mysql server

mysql>grantreplication slave on *.* to ‘testcreate’@’192.168.1.61’ identified by ‘123456’;

Description: 'testcreate': synchronization database;

      '192.168.1.61': the address of the synchronization database;

        '123456': database password synchronization, the configuration needs to be set from the library.

3, showing the main library information

mysql>showmaster status;

Executing the command displays:

 

See above information indicates that the main library is successfully established.

4, the backup database (if the server is also for the new library, this operation is not required)

Way to back up the database There are many: If you can copy the database in the same LAN, you can also import data directly with the tool.

Copy the database with the method of packaging:

# Cd / data to the next storage path Mysql database

# tarcvfz testcreate.tar.gz db/

Then copy the unpacked from the database

# cd/data

# scp192.168.1.61:/data/testcreate.tar.gz

#tarxvfz testcreate.tar.gz

 

Data replication is completed;

Second, from the library configuration

The basic configuration from the configuration server to the main library almost, selected from the library server: 192.168.1.61

1. my.cnf configuration file from the library service

# Vietc / my.cnf be modified as follows:

ID = 2-Server  # 2 to herein as the main repository ID is 1;

log-bin = mysql-bin   required for data synchronization;

master-host = 192.168.1.60  main library the IP;

master-user = test  for synchronization account;

master-password = 123456  sync account password is provided when the primary database;

master-port = 3306  port number for database synchronization.

2. Restart from the library server

#service mysqld restart

If that fails then the configuration file in question, you need to re-check the configuration.

3. Adjust the copied permissions (if copied from the main library database) database

Main library database permissions are copied to root privileges, you need to modify the permissions to mysql

#chown–R mysql:mysql /var/lib/mysql

If the library is a new library this step is not required

4. Show status from the server

Be mysql server:

mysql> showslave status \ G; FIG show:

 

If slave_io_running and slave_sql_running are yes then the configuration is successful from the server.

If you encounter on position-related errors, you need the following information to manually set the File / Postion of

mysql>Changemaster to master_host = ‘192.168.1.60’

master_user= ‘test’

master_password=’123456’

File information master_log_file = 'mysql-bin.000001', the main library

master_log_pos = '106'; Position information of the master repository

Third, from the master server tests

The primary server and the database from the server to establish the same: testCreate table: test, field ID, name

Add the data to the master server

 

In this case the server from the database is displayed as 192.168.1.61 in testcreate

 

It represents the master-slave server synchronization is successful.

Fourth, Frequently Asked Questions

When configured as a server, you can not modify the operation from the library, from the library only read permissions, if you modify the database from the library, will result in synchronization failure: failure is found, with showslave status \ G; view the status information from the server

If slave_IO_Running occurs: is NO, it means from the database connection failure

Last_Error: will show the error log information

 

This problem occurs mainly things rolled back problems, solutions are:

1、

mysql> slave stop; stop service from 

mysql>set GLOBAL SQL_SLAVE_SKIP_COUNTER=1;

mysql> slave start; start from the service

2, manual reset from the server

mysql>Changemaster to

master_host= ‘192.168.1.60’

master_user= ‘test’

master_password=’123456’

master_log_file = 'mysql-bin.000001'; master database file information

master_log_pos = '106'; Position information of the master repository

This configuration is completed from the master Mysql service

In the main server, based mainly write data, the data suggested InnoDB engine, in terms of reading by reading Mysql in the MyISAM engine efficiency is very good.

MyIsAM engine supports full-text indexing, but does not support transactions, InnoDB but just the opposite!

Guess you like

Origin www.cnblogs.com/HKROnline-SyncNavigator/p/10972479.html