


Detailed explanation of the sample code sharing of master-slave replication of mysql5.6 under centos7
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!

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

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



When loading CentOS-7.0-1406, there are many optional versions. For ordinary users, they don’t know which one to choose. Here is a brief introduction: (1) CentOS-xxxx-LiveCD.ios and CentOS-xxxx- What is the difference between bin-DVD.iso? The former only has 700M, and the latter has 3.8G. The difference is not only in size, but the more essential difference is that CentOS-xxxx-LiveCD.ios can only be loaded into the memory and run, and cannot be installed. Only CentOS-xxx-bin-DVD1.iso can be installed on the hard disk. (2) CentOS-xxx-bin-DVD1.iso, Ce

Open the centos7 page and appear: welcome to emergency mode! afterloggingin, type "journalctl -xb" to viewsystemlogs, "systemctlreboot" toreboot, "systemctldefault" to tryagaintobootintodefaultmode. giverootpasswordformaintenance(??Control-D???): Solution: execute r

There is a lot of garbage in the tmp directory in the centos7 system. If you want to clear the garbage, how should you do it? Let’s take a look at the detailed tutorial below. To view the list of files in the tmp file directory, execute the command cdtmp/ to switch to the current file directory of tmp, and execute the ll command to view the list of files in the current directory. As shown below. Use the rm command to delete files. It should be noted that the rm command deletes files from the system forever. Therefore, it is recommended that when using the rm command, it is best to give a prompt before deleting the file. Use the command rm-i file name, wait for the user to confirm deletion (y) or skip deletion (n), and the system will perform corresponding operations. As shown below.

Set password rules for security reasons Set the number of days after which passwords expire. User must change password within days. This setting only affects created users, not existing users. If setting to an existing user, run the command "chage -M (days) (user)". PASS_MAX_DAYS60#Password expiration time PASS_MIN_DAYS3#Initial password change time PASS_MIN_LEN8#Minimum password length PASS_WARN_AGE7#Password expiration prompt time Repeat password restriction use [root@linuxprobe~]#vi/etc/pam.d/system-auth#nearline15:

1.UncaughtError:Calltoundefinedfunctionmb_strlen(); When the above error occurs, it means that we have not installed the mbstring extension; 2. Enter the PHP installation directory cd/temp001/php-7.1.0/ext/mbstring 3. Start phpize(/usr/local/bin /phpize or /usr/local/php7-abel001/bin/phpize) command to install php extension 4../configure--with-php-config=/usr/local/php7-abel

Centos7 does not have a mysql database. The default database is mariadb (a branch of mysql). You can install the mysql database manually by following the steps below. 1. Download the rpm installation file wgethttp://repo.mysql.com/mysql-community-release-el7.rpm 2. Execute rpm to install rpm-ivhmysql-community-release-el7.rpm. After the dependency resolution is completed, the following options appear: dependenciesresolved =================================

1. The compressed folder is a zip file [root@cgls]#zip-rmydata.zipmydata2. Unzip mydata.zip into the mydatabak directory [root@cgls]#unzipmydata.zip-dmydatabak3.mydata01 folder and mydata02.txt are compressed into mydata.zip[root@cgls]#zipmydata.zipmydata01mydata02.txt4. Decompress the mydata.zip file directly [root@cgls]#unzipmydata.zip5. View myd

When the default shortcut keys conflict with the keys of the software you are using or you need to use the shortcut keys according to your own habits, you have to modify and set the default shortcut keys. How to modify the default shortcut keys of CentOS7? Let’s take a look at the detailed tutorial below. 1. Start the Centos7 system in the virtual machine and enter the desktop. 2. Click Applications->System Tools->Settings in the upper left corner. 3. Enter the setting interface and click the device. 4. Select Keyboard and click any item on the right. And press the shortcut key to be set on the keyboard to change its shortcut key (note that some cannot be changed!) 5. After the change, as shown below, finally click Settings, so that the shortcut key setting modification is completed.
