


This article mainly introduces the detailed explanation of master-slave replication in mysql5.6 under centos7. The editor thinks it is quite good, so I will share it with you now and give it as a reference. Let’s follow the editor and take a look.
1. Introduction to mysql master-slave replication
Mysql’s master-slave replication is not on the database disk The file is copied directly, but copied to the local server to be synchronized through the logical binlog log, and then the local thread reads the sql statement in the log and re-applies it to the mysql database.
Mysql database supports replication in different business scenarios such as one-way, two-way, chain cascade, and ring. One server acts as the main server master and receives updates from users, while one or more other servers act as The slave server receives the log content from the master server's binlog file, parses the sql, and updates it to the slave server.
One master and one slave (A -> B, A is the master, B is the slave)
One master and multiple slaves (A -> B, A -> C, A is the slave Master, B and C are slaves)
Double-master bidirectional synchronization (A -> B, B -> A, A and B are both masters and backup each other)
Linear cascade (A -> B -> C , A and B are masters and slaves, and C is slave)
Ring cascade (A -> B -> C -> A, A, B and C are both masters, and each node can write data)
2. Solution to realize mysql master-slave read and write separation
1. Realize reading and writing separation through programs (judgment statementskeywords to connect the master-slave database)
2. Realize reading and writing separation through open source software (mysql-proxy, amoeba, stable The performance and function are average, not recommended for production use)
3. Independent development of DAL layer software
3. Introduction to the principle of mysql master-slave replication
Mysql master-slave replication is an asynchronous replication process, which copies a master library to a slave library. The entire process between master and slave is completed by three threads. The sql thread and I/O thread are on the slave side, and the other I/O thread is on the master side.
Copying Principle and Process
1. Execute the start slave command on the slave, turn on the master-slave replication switch, and start master-slave replication.
2. The I/O thread of the slave requests the master through the authorized replication user on the master and requests the specified location of the specified binlog log.
3. After the master receives the request from the slave's I/O thread, its own I/O thread responsible for copying will read the log information after the specified position of the specified binlog log in batches based on the slave's request information. Then it is returned to the slave's I/O thread. In addition to the binlog log, the returned information also includes the master's new binlog file name and the next specified update position in the new binlog.
4. The slave obtains the binlog log content sent from the I/O thread on the master. After the log file and the location point, the binlog content will be written to the end of the slave's own relay log (relay log) file in turn. And record the new binlog file name and location into the master-info file, so that the next time the new binlog log is read from the master, the master can be told to read from the new location of the new binlog.
5. The slave's sql thread will detect the newly added log content of the I/O thread in the local relay log in real time, parse the content in the relay log file into sql statements in a timely manner, and parse the sql statements in the order of their positions. Execute these sql statements. Relay-log.info records the file name and location of the current application relay log.
4. Mysql master-slave replication operation
I have mysql single-machine multiple instances, 3306, 3308, 3309
The master library is 3306, the slave library is 3308,3309
(1), on the master library
1, set the server-id value and open it Binlog function
> vi /etc/my.cnf
[mysqld] #用于同步的每台机器server-id都不能相同 server-id = 10 log-bin = /data/mysql56/data/mysql-bin
2. Restart the main library
> service mysqld restart
3. Log in to the main library and check the server-id
> mysql -uroot -p > show variables like 'server_id';
4. Create a database on the main library for copying from the library Account
> grant replication slave on *.* to "rep"@"%" identified by "123456"; > flush privileges; > select user,host from mysql.user; > show grants for rep@"%";
5. Read-only lock table of main database database (do not close the current window)
> flush table with read lock;
View main database status
> show master status;
6 , Back up all data files in the main library
> mysqldump -uroot -p -A -B | gzip > /data/mysql_bak.$(date +%F).sql.gz
7. After backing up the main library data, unlock
> unlock tables;
8. Migrate the data exported from the main library to the slave library
( 2) On the slave library
1, set the server-id value and turn off the binlog function
①There are two situations where binlog needs to be turned on
② To synchronize B in the middle of A->B->C in cascade, you need to enable binlog
③When doing database backup in the slave database, you must have full backup and binlog log to be a complete backup.
> vi /mysql-instance/3308/my.cnf [mysqld] server-id = 11 relay-log = /mysql-instance/3308/relay-bin relay-log-info-file = /mysql-instance/3308/relay-log.info
2. Restart the slave library
> /mysql-instance/3308/mysql restart
3. Log in to the slave library to check the parameters
> mysql -uroot -p -S /mysql-instance/3308/mysql.sock > show variables like 'log_bin'; > show variables like 'server_id';
4. Restore the data exported by mysqldump from the main library to the slave library
> gzip -d /data/mysql_bak.2017-01-15.sql.gz
Restore the master database data to the slave database
> mysql -uroot -p -S /mysql-instance/3308/mysql.sock < /data/mysql_bak.2017-01-15.sql
5. Log in to the slave database and configure the replication parameters
CHANGE MASTER TO MASTER_HOST='127.0.0.1', MASTER_PORT=3306, MASTER_USER='rep', MASTER_PASSWORD='123456', MASTER_LOG_FILE='mysql-bin.000001', MASTER_LOG_POS=396;
Note that the above MASTER_LOG_FILE and MASTER_LOG_POS are used in the master database using show master status; View information.
View the master.info file
> cat /mysql-instance/3308/data/master.info
6. Start the slave library synchronization switch and test the master-slave replication situation
> mysql -uroot -p -S /mysql-instance/3308/mysql.sock -e "start slave;" > mysql -uroot -p -S /mysql-instance/3308/mysql.sock -e "show slave status\G;" > mysql -uroot -p -S /mysql-instance/3308/mysql.sock -e "show slave status\G" | egrep "IO_Running|SQL_Running|_Behind_Master"
7. Test the master-slave replication
> mysql -uroot -p -e "create database wohehe;" > mysql -uroot -p -S /mysql-instance/3308/mysql.sock -e "show databases;"
五、mysql主从复制线程状态说明及用途
1、主库线程的同步状态
> show processlist\G; *************************** 1. row *************************** Id: 5 User: rep Host: localhost:47605 db: NULL Command: Binlog Dump Time: 4728 State: Master has sent all binlog to slave; waiting for binlog to be updated Info: NULL
说明主库线程已从binlog读取更新,发送到了从库,线程处理空闲状态,等待binlog的事件更新。
2、从库线程的同频状态
> show processlist\G; *************************** 2. row *************************** Id: 6 User: system user Host: db: NULL Command: Connect Time: 5305 State: Slave has read all relay log; waiting for the slave I/O thread to update it Info: NULL
说明从库已读取所有中继日志,等待从库I/O线程的更新。
六、主从复制故障
如果我在从库上创建了一个库,然后去主库创建同名的库,那么这就会冲突了。
> show slave status; Slave_IO_Running: Yes Slave_SQL_Running: No Seconds_Behind_Master: NULL Last_Error: Error 'Can't create database 'xxxxx'; database exists' on query. Default database: 'xxxxx'. Query: 'create database xxxxx'
对于该冲突解决方法
方法一
> stop slave; #将同步指针移动下一个,如果多次不同步,可重复操作 > set global sql_slave_skip_counter = 1; > start slave;
方法二
> vi /mysql-instance/3308/my.cnf #把可以忽略的错误号事先在配置文件中配置 slave-skip-errors = 1002,1007,1032
The above is the detailed content of Detailed explanation of the sample code sharing of master-slave replication of mysql5.6 under centos7. For more information, please follow other related articles on the PHP Chinese website!

MySQL's position in databases and programming is very important. It is an open source relational database management system that is widely used in various application scenarios. 1) MySQL provides efficient data storage, organization and retrieval functions, supporting Web, mobile and enterprise-level systems. 2) It uses a client-server architecture, supports multiple storage engines and index optimization. 3) Basic usages include creating tables and inserting data, and advanced usages involve multi-table JOINs and complex queries. 4) Frequently asked questions such as SQL syntax errors and performance issues can be debugged through the EXPLAIN command and slow query log. 5) Performance optimization methods include rational use of indexes, optimized query and use of caches. Best practices include using transactions and PreparedStatemen

MySQL is suitable for small and large enterprises. 1) Small businesses can use MySQL for basic data management, such as storing customer information. 2) Large enterprises can use MySQL to process massive data and complex business logic to optimize query performance and transaction processing.

InnoDB effectively prevents phantom reading through Next-KeyLocking mechanism. 1) Next-KeyLocking combines row lock and gap lock to lock records and their gaps to prevent new records from being inserted. 2) In practical applications, by optimizing query and adjusting isolation levels, lock competition can be reduced and concurrency performance can be improved.

MySQL is not a programming language, but its query language SQL has the characteristics of a programming language: 1. SQL supports conditional judgment, loops and variable operations; 2. Through stored procedures, triggers and functions, users can perform complex logical operations in the database.

MySQL is an open source relational database management system, mainly used to store and retrieve data quickly and reliably. Its working principle includes client requests, query resolution, execution of queries and return results. Examples of usage include creating tables, inserting and querying data, and advanced features such as JOIN operations. Common errors involve SQL syntax, data types, and permissions, and optimization suggestions include the use of indexes, optimized queries, and partitioning of tables.

MySQL is an open source relational database management system suitable for data storage, management, query and security. 1. It supports a variety of operating systems and is widely used in Web applications and other fields. 2. Through the client-server architecture and different storage engines, MySQL processes data efficiently. 3. Basic usage includes creating databases and tables, inserting, querying and updating data. 4. Advanced usage involves complex queries and stored procedures. 5. Common errors can be debugged through the EXPLAIN statement. 6. Performance optimization includes the rational use of indexes and optimized query statements.

MySQL is chosen for its performance, reliability, ease of use, and community support. 1.MySQL provides efficient data storage and retrieval functions, supporting multiple data types and advanced query operations. 2. Adopt client-server architecture and multiple storage engines to support transaction and query optimization. 3. Easy to use, supports a variety of operating systems and programming languages. 4. Have strong community support and provide rich resources and solutions.

InnoDB's lock mechanisms include shared locks, exclusive locks, intention locks, record locks, gap locks and next key locks. 1. Shared lock allows transactions to read data without preventing other transactions from reading. 2. Exclusive lock prevents other transactions from reading and modifying data. 3. Intention lock optimizes lock efficiency. 4. Record lock lock index record. 5. Gap lock locks index recording gap. 6. The next key lock is a combination of record lock and gap lock to ensure data consistency.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

WebStorm Mac version
Useful JavaScript development tools