MySQL Master-Slave synchronization

Source: Internet
Author: User
Tags error handling mysql in mysql query unique id

Mysql's built -in replication capabilities are the foundation for building large, high-performance applications. The distribution of MySQL data across multiple systems is done by copying data from one of the MySQL hosts to the other host (slaves) and re-executing it again. One server acts as the primary server during replication, while one or more other servers act as slave servers. The Primary server writes the update to the binary log file and maintains an index of the file to track the log loop . These logs can record updates that are sent to the slave server. When a primary server is connected from the server, it notifies the primary server where the last successful update was read from the server in the log. Receive any updates from the server from then on, and then block and wait for the primary server to notify the new updates.

Note: When you make a copy, all updates to the tables in the replication must be made on the primary server, and the operations from the slave will not be updated to master.

Environment:

172.24.0.130 3306 Master

172.24.0.130 3307 Slave

Single-machine multi-instance build MySQL:

MySQL Master-slave replication

Just say its master-slave copy, the steps are as follows:

1, the master-slave server for the following actions:
1.1.version Consistent
1.2.Initialize the table and start MySQL in the background
1.3, change the root password

2. Modify Master server Master:
#vi/etc/my.cnf
[Mysqld]
Log-bin=mysql-bin//[must] enable binary logging
server-id=222//[must be] server unique ID, default is 1, usually take IP last paragraph

3. Modify the slave from the server:
#vi/etc/my.cnf
[Mysqld]
Log-bin=mysql-bin//[not required] enable binary logging
server-id=226//[must be] server unique ID, default is 1, usually take IP last paragraph

4. Restart MySQL for two servers
/etc/init.d/mysql restart

5. Establish an account on the primary server and authorize slave:
#/usr/local/mysql/bin/mysql-uroot-pmttang
Mysql>GRANT REPLICATION SLAVE on * * to ' mysync ' @ '% ' identified by ' q123456 ';Generally do not use the root account, indicating that all clients may be connected, as long as the account, the password is correct, this can be replaced by specific client IP, such as 192.168.145.226, enhance security.

6, log on to the master server MySQL, query the status of master
Mysql>show Master status;
+------------------+----------+--------------+------------------+
| File | Position | binlog_do_db | binlog_ignore_db |
+------------------+----------+--------------+------------------+
|mysql-bin.000004 | 308 || |
+------------------+----------+--------------+------------------+
1 row in Set (0.00 sec)
Note: Do not operate the master server MySQL again after performing this step to prevent the change of the primary server state value

7. Configure the slave from the server:
Mysql>change Master to master_host= ' 192.168.145.222 ', master_user= ' Mysync ', master_password= ' q123456 ',
master_log_file= ' mysql-bin.000004 ', master_log_pos=308;Be careful not to disconnect, there are no single quotes around 308 digits.

Mysql>start slave; To start the Copy from Server feature

8. Check the status of the replication function from the server:

Mysql> Show Slave Status\g

1. Row ***************************

Slave_io_state:waiting for Master to send event
master_host:192.168.2.222//Primary server address
Master_user:mysync//Authorization account name, try to avoid using root
master_port:3306//Database port, some versions do not have this line
Connect_retry:60
master_log_file:mysql-bin.000004
read_master_log_pos:600//#同步读取二进制日志的位置, greater than or equal to Exec_master_log_pos
relay_log_file:ddte-relay-bin.000003
relay_log_pos:251
relay_master_log_file:mysql-bin.000004
Slave_io_running:yes//This status must be Yes
Slave_sql_running:yes//This status must be Yes
......
Note: The slave_io and slave_sql processes must function normally, that is, the Yes state, otherwise it is an error state (e.g., one of the No is an error).

9, the master-slave server test:

master server MySQL, build the database, and create a table in this library to insert a piece of data:

mysql> CREATE DATABASE hi_db;
Query OK, 1 row Affected (0.00 sec)

MySQL Query from server:

mysql> show databases;

+--------------------+
| Database |
+--------------------+
| Information_schema |
|       hi_db | I ' M here, you see?
| MySQL |
| Test |

+--------------------+
4 rows in Set (0.00 sec)

Error handling:

01, mysql> start slave;
ERROR 1872 (HY000): Slave failed to initialize relay log info structure from the repository

Solution: Reset slave All clears all replication information from the new Setup master-slave copy

Change Master to master_host= ' 127.0.0.1 ', master_user= ' Mysync ', master_password= ' 123456 ', master_log_file= ' Mysql-bin.000003 ', master_log_pos=1300;

02, master-Slave replication error

Last_error:error ' Can ' t create database ' Mvpbang '; Database exists ' on query. Default database: ' Mvpbang '. Query: ' Create Database Mvpbang '

Solution:

Stop slave;

Set global sql_slave_skip_counter = 1; #跳过events个数

Start slave;

Learn from:

http://369369.blog.51cto.com/319630/790921/

http://blog.csdn.net/hguisu/article/details/7325124/

MySQL Master-Slave synchronization

Related Article

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.