MySQL Master-Slave synchronization

Source: Internet
Author: User

MySQL master-slave synchronization is currently using a relatively broad database architecture, technology is relatively mature, configuration is not complex, especially for the load of large sites, master-slave synchronization can effectively alleviate the database read and write pressure.

MySQL master-slave synchronization mechanism

MySQL master-slave synchronization is implemented on the basis of MySQL master-slave replication (Master-slave Replication), by setting the Binlog on master MySQL (leaving it open), Slave MySQL through a i/ The o thread reads binlog from master MySQL and then transmits it to slave MySQL's trunk log, then slave mysql's SQL thread to read the relay log from the log and then apply it to the slave MySQL database. This realizes the master-slave data synchronization function.


the role of MySQL master-slave synchronization

1, as a backup mechanism, equivalent to hot backup
2, can be used to do read and write separation, balanced database load

MySQL master-slave synchronization steps

First, ready to operate
1, master-slave database version consistent, recommended version 5.5 or more
2, master-slave database data consistent

Second, the master database master modification

1. Modify the MySQL configuration:

[Plain]View Plaincopy
    1. # log file name
    2. Log-bin = Mysql-bin
    3. # Primary database-side ID number
    4. Server-id = 1

2. Restart MySQL to create an account for synchronization:

[Plain]View Plaincopy
    1. # Create slave account slave_account, password 123456
    2. Mysql>grant replication Slave on * * to ' slave_account ' @ ' percent ' identified by ' 123456 ';
    3. # Update Database permissions
    4. Mysql>flush privileges;

3. Query the status of master

[Plain]View Plaincopy
    1. Mysql> Show master status;
    2. +------------------+----------+--------------+------------------+
    3. | File | Position | binlog_do_db | binlog_ignore_db |
    4. +------------------+----------+--------------+------------------+
    5. |      mysql-bin.000009 |              196 |                  | |
    6. +------------------+----------+--------------+------------------+
    7. 1 row in Set

Note: Do not operate the primary database until this step is complete, preventing changes in the primary database state value

Third, from the database slave modification

1. Modify the MySQL configuration:

[Plain]View Plaincopy
    1. # from the database-side ID number
    2. Server-id =2

2. Perform synchronous commands

[Plain]View Plaincopy
    1. # Execute Sync command, set primary database IP, synchronize account password, sync location
    2. Mysql>change Master to master_host= ' 192.168.1.2 ', master_user= ' slave_account ', master_password= ' 123456 ', master_ Log_file= ' mysql-bin.000009 ', master_log_pos=196;
    3. # Enable sync function
    4. Mysql>start slave;

3. Check the status from the database:

[Plain]View Plaincopy
  1. Mysql> show Slave status\g;
  2. 1. Row ***************************
  3. Slave_io_state:waiting for Master to send event
  4. master_host:192.168.1.2
  5. Master_user:slave_account
  6. master_port:3306
  7. Connect_retry:60
  8. master_log_file:mysql-bin.000009
  9. read_master_log_pos:196
  10. relay_log_file:vicky-relay-bin.000002
  11. relay_log_pos:253
  12. relay_master_log_file:mysql-bin.000009
  13. Slave_io_running:yes
  14. Slave_sql_running:yes
  15. replicate_do_db:
  16. replicate_ignore_db:
  17. ...

Note: The slave_io_running and slave_sql_running processes must be running normally, that is, the Yes state, otherwise the synchronization fails.

Here, the master-slave database setup work has been completed, you can create a new database and tables, insert and modify data, test the success

Iv. other relevant parameters that may be used

1, Master side:

[Plain]View Plaincopy
  1. # What databases are not synchronized
  2. binlog-ignore-db = MySQL
  3. BINLOG-IGNORE-DB = Test
  4. Binlog-ignore-db = Information_schema
  5. # only which databases are synchronized, other than those that are not synchronized
  6. Binlog-do-db = Game
  7. # Log retention Time
  8. Expire_logs_days = 10
  9. # controls the write frequency of the binlog. How many transactions per execution are written once
  10. # This parameter consumes a lot of performance, but can reduce the loss caused by MySQL crash
  11. Sync_binlog = 5
  12. # log format, recommended mixed
  13. # Statement Saving SQL statements
  14. # row save affects record data
  15. # Mixed in front of two combinations
  16. Binlog_format = Mixed

2, Slave end:

[Plain]View Plaincopy
    1. # Stop master-Slave synchronization
    2. mysql> stop Slave;
    3. # Reconnect time-out when the connection is disconnected
    4. mysql> change Master to master_connect_retry=50;
    5. # Turn on master-slave synchronization
    6. mysql> start slave;

The above connection time-out settings, similar way can be used to set the primary database IP, synchronization account password, synchronization location

Expand reading:

MySQL log format Binlog_format

Transferred from: http://blog.csdn.net/mycwq/article/details/17136001

MySQL Master-Slave synchronization

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.