Mysql essay - Mysql5.7 master-slave replication

foreword

I didn't intend to write this article, but the project needs to engage in Mysql master-slave, so I made a note on a whim. It's been a long time since I wrote an article. I hope it can be helpful to you. If you like it, you can give it a good review.

master-slave principle

I don’t know if you know about Mysql master-slave. There will be a certain performance bottleneck in the database under high concurrency. In most cases, 80% of the performance problems of a system are read. In this case, we can use Mysql master-slave to share the pressure of reading. .

The meaning of master-slave is: all write operations are on the master library, and read operations are on one or more slave libraries. Of course, the master library can also undertake read requests, and the data of the slave library is copied from the master library. Mysql automatically With the function of master-slave replication. The principle is as shown in the figure below
insert image description here

Master-slave replication steps:

  1. Open the binary-log log file of the Master, and mysql will write all DDL, DML, and TCL into the BinaryLog log file
  2. Master will generate a log dump thread to transfer binlog to the i/o thread of the slave library
  3. Request the binlog of the main library from the i/o thread of the library, and write the obtained binlog log to the relay log (relaylog)
  4. The sql thread of the slave library will read the logs in the relaylog file and parse them into specific operations. Through the consistent operation of the master and slave, the final data consistency will be achieved.

Based on such a principle, our project can realize the separation of reading and writing. as follows:

insert image description here
Mysql can automatically implement master-slave replication, so the write operation initiated by our project will go to the master library, and the read operation will go to the slave library. What we are talking about today is mainly how to realize the master-slave replication function of Mysql.

Install mysql master-slave

first step

A Mysql has been installed on my machine, the version is 5.7. On this basis, we will demonstrate the installation and configuration of Mysql master and slave (the same is true for Linux). First find the installation directory of Mysql, mine is installed in C:\Program Files\MySQL, copy a copy, as follows:
insert image description here

  • C:\Program Files\MySQL\MySQL Server 5.7: As my main msyql, which is master, the port is 3306
  • C:\Program Files\MySQL\MySQL-slave: I copied it as a slave from Mysql

Step two:

Then find the data directory of Mysql, which is in C:\ProgramData\MySQL by default, and make a copy, as follows:

insert image description here

  • C:\ProgramData\MySQL\MySQL Server 5.7 : is the data directory of my master
  • C:\ProgramData\MySQL\MySQL-slave: is the data directory of my slave slave library

third step

Then enter the data directory of the master master mysql to modify my.ini and enable the bin-log synchronization function of the master. Mine is C:\ProgramData\MySQL\MySQL Server 5.7\my.ini

[mysqld]
#lower_case_table_names=1 #忽略大小写
#服务器id ,不可重复
server-id=1
#打开log-bin日志
log-bin=mysql-bin

#要给从机同步的库,如果是多个库,可以写多个命令
binlog-do-db=itsource-student
#binlog-do-db=xxxx
#不给从机同步的库(多个写多行)
binlog-ignore-db=mysql
binlog-ignore-db=information_schema
binlog-ignore-db=performance_schema
binlog-ignore-db=sys
#自动清理 7 天前的log文件,可根据需要修改
expire_logs_days=7
  • Note that by default, there is a server-id=1 in my.ini, so the configuration of the master can be ignored
  • log-bin=mysql-bin : Enable the log-bin writing function of the master
  • binlog-do-db : the name of the database to be synchronized

the fourth step

The main Mysql configuration has been modified, just restart it directly, and then modify the slave configuration C:\ProgramData\MySQL\MySQL-slave\my.ini, the content is as follows

[mysqld]

#lower_case_table_names = 1
#不可以与主数据库的ID相同
server-id = 2
#跳过出现的错误,继续执行复制
slave_skip_errors = 1146,2003 
#slave_skip_errors = 2003
port=3307

# Path to installation directory. All paths are usually resolved relative to this.
basedir="C:/Program Files/MySQL/MySQL-slave/"

# Path to the database root
datadir=C:/ProgramData/MySQL/MySQL-slave/Data

Just change the configuration from Mysql, one more thing: log-bin can also be enabled from Mysql.

  • server-id = 2 : Note that the default server-id = 1 must be changed, otherwise it will be repeated with the master
  • port=3307 : Change the server port under mysqld, otherwise there will be a port conflict
  • basedir: The installation directory should be changed to the slave installation directory
  • datadir: Change the data directory to the data directory of the slave

the fifth step

In addition, delete the auto.cnf file from the data directory\data directory of the slave library, because there is a UUID in the file, we are using the copy master library, and the UUID will be repeated, which will cause the slave to fail to start.

step six

Now let's start two Mysqls in turn. The main mysql can be restarted directly. I also need to install a service from mysql. CMD enters the bin directory under the installation directory of the slave library: C:\Program Files\MySQL\MySQL-slave\bin , and then execute the following command to install the slave Serve:

C:\Program Files\MySQL\MySQL-slave\bin>mysqld.exe --install MysqlSlave --defaults-file="C:\ProgramData\MySQL\MySQL-slave\my.ini"
  • MysqlSlave: is the service name
  • –defaults-file: is the address of the configuration file from

insert image description here
If it prompts that there is no permission, use the management to run cmd, and then start Mysql from the task manager, as follows:

insert image description here

Realize Mysql master-slave automatic synchronization

first step

Use a tool to connect to 2 Mysqls, and the slave Mysql is the master Mysql copied, so the password is the same

insert image description here
The slave mysql needs to synchronize data from the master mysql, so first you need to create an account for data replication in the master mysql

GRANT REPLICATION SLAVE ON *.* TO 'slaveuser'@'%' IDENTIFIED BY '密码';

FLUSH PRIVILEGES;

Then you can view the status of the main Mysql through the show master status; command
insert image description here

  • mysql-bin.000002 : Master's log-bin log file
  • Position : the position of data synchronization
  • Binlog-Do_DB : The database we specify to synchronize data
  • Binlog-Ignore_DB: This is to ignore out-of-sync tables

second step

This shows that the master is ready. Next, configure the Slave database. You must execute the following command on the Slave to establish a master-slave synchronization relationship.

# 先停掉slave
stop slave ; 
# 建立主从关系
change master to master_host='127.0.0.1' , master_user='slaveuser',master_password='密码',master_log_file='master-bin.000002',master_log_pos=1876;
#启动同步,如有报错执行 reset slave;
start slave;			
#显示从的状态			
show slave status
#确保 IO 和 SQL 线程都是 Yes,代表同步正常。
Slave_IO_Running: Yes				#负责与主机的io通信
Slave_SQL_Running: Yes    #负责自己的slave mysql进程
  • master_log_file : log-bin file name, same as displayed by master query status
  • master_log_pos : The position of the log, as shown by the master query status
  • master_host: the IP of the master database
  • master_user: The account created in the master database before, specially used for data synchronization

Note: The values ​​of master_log_file and master_log_pos should be consistent with those queried by the Master, and you should fill in your own values ​​instead of directly copying the contents of the article.

If Slave_IO_Running = no, you can check the data at these points:

  • The server-id of the master Mysql and the slave Mysql are duplicated
  • The UUID values ​​in the two data directories \data\auto.cnf are duplicated
  • When configuring slave synchronization, it is caused by the fact that the slave does not have permission to access the master; the permission can be reassigned to the account
  • If it doesn't work, you have to check your operation steps in detail, it may be caused by a wrong step

The last step is to test. Because my synchronization is itsource-student, only the master mysql is required to modify any data in the database table, and the data in the slave mysql will change accordingly.

The article is over, I hope it will be helpful to you, please keep repeating if you like it. . .

Guess you like

Origin blog.csdn.net/u014494148/article/details/126881544