Mysql master-slave configuration for CentOS and Ubantu

1. Introduction to MySql master-slave replication

Master-slave replication (also known as AB replication) allows data from one MySQL database server (master server) to be replicated to one or more MySQL database servers (slave servers). Replication is asynchronous and slaves do not need to be permanently connected to receive updates from the master. Depending on the configuration, you can copy all databases in a database, selected databases or even selected tables.

MySQL中复制的优点包括:

  1. Scale-out solution - distributes load among multiple slaves to improve performance. In this environment, all writes and updates must occur on the master server. However, reads can occur on one or more slave devices. This model can improve write performance (since the master is dedicated to updates) while significantly improving read speed for a growing number of slaves.
  2. Data Security − Because data is replicated to the slaves and the replication process can be paused by the slaves, it is possible to run backup services on the slaves without corrupting the corresponding master data.
  3. Analysis − Real-time data can be created on the master server, while information analysis can be performed on the slave server without affecting the performance of the master server.
  4. Remote data distribution − You can use replication to create local copies of data for remote sites without permanent access to the master server.

Replication 的原理:当主库写入文件的时候,从库读取写入文件的SQL,到从库再次执行一次

The premise is that the database server with the role of the master server must enable the binary log . Since the (binary log) bin-log format may be different between different versions of MySQL, it is recommended that the MySQL version of the Master (primary) and the version of the Slave (slave) be as close as possible. possibly the same or lower. MySQL master-slave is based on binlog, and binlog needs to be enabled on the host to be master-slave.

主从过程步骤

  1. Any modification on the main server will be saved in the binary log Binary log through its own I/O thread (I/O thread).
  2. Also start an I/O thread from the server, connect to the master server to request to read the binary log through the configured user name and password, and then write the read binary log to a local Realy log (relay log) in.
  3. Open a SQL thread from the server at the same time to check the Realy log (this file is also binary) regularly. If there is an update, immediately execute the updated content on the local database. Each slave receives a copy of the entire contents of the master's binary log.
  4. The slave server device is responsible for deciding which statements in the binary log should be executed. Unless otherwise specified, all events in the master-slave binary log are executed on the slave. If desired, you can configure a slave server to only process events for some specific databases or tables.

insert image description here

MySQL主从作用

  1. Real-time disaster recovery for failover
  2. Separation of reading and writing, providing query services
  3. Backup to avoid business impact

insert image description here

2. Master-slave replication operation

环境准备

Role operating system IP address MySQL version
Host (Master) CentOS 120.118.55.220 mysql 5.7.26
Slave People 130.211.18.183 mysql 5.7.26

Note: Centos is mysqld, and Ubuntu is mysql service

2.1, Master host operation

1、Master节点配置 /etc/my.cnf (master节点执行)

Notice:

  1. The master node and the slave node must be configured with a unique id, other configurations are optional
  2. centos is vim /etc/my.cnf
  3. ubantu is vim /etc/mysql/mysql.conf.d/mysqld.cnf
> vim /etc/my.cnf
# 找到[mysqld]添加下面内容
[mysqld]
## 同一局域网内注意要server-id一定要唯一
server-id=100  
## 开启二进制日志功能,可以随便取(关键)
log-bin=mysql-bin
## 复制过滤:过滤掉不需要的数据库,如过滤掉mysql数据库
binlog-ignore-db=mysql
## 为每个session 分配的内存,在事务过程中用来存储二进制日志的缓存
binlog_cache_size=1M
## 主从复制的格式(mixed,statement,row,默认格式是statement)
binlog_format=mixed

# 重启mysql服务,一般配置文件写完后,都需要重启该服务才生效
systemctl restart mysqld

2、重启mysql服务,使新配置生效

>  systemctl restart mysqld

3、mysql查看是否开启二进制日志文件
The default is OFF and not enabled, only after configuring the log_bin of my.cnf, it is ON

mysql> show variables like 'log_%';

insert image description here

4、在master服务器授权slave服务器的同步权限(master节点执行)

mysql > mysql -uroot -p密码
# 授予slave服务器可以同步master服务
mysql > grant replication slave, replication client on *.* to 'root'@'slave服务的ip' identified by '从机访问主机的密码';
mysql > flush privileges;
# 查看MySQL现在有哪些用户及对应的IP权限(可以不执行,只是一个查看)
mysql > select user,host from mysql.user;


#如:
mysql> grant replication slave, replication client on *.* to 'root'@'130.211.18.183' identified by '3456789';
mysql > flush privileges;
# 'root'     :就是从机(Slave)要访问主机(Master)的用户名;
# '130.211.18.183':就是从机(Slave)的IP地址;
# '3456789'     :就是从机(Slave)访问主机(Master)的密码;是你自己新设置的密码,不是主机的密码。

insert image description here

若出现 ERROR 1819 (HY000): Your password does not satisfy the current policy requirements

  1. The database default password rules must carry uppercase and lowercase letters, special symbols, and the character length is greater than 8, otherwise an error will be reported.
  2. Therefore, when setting a relatively simple password, you need to modify the set global validate_password_policy and _length parameter values ​​first.
mysql> set global validate_password_policy=0;
Query OK, 0 rows affected (0.00 sec)
mysql> set global validate_password_length=1;
Query OK, 0 rows affected (0.00 sec)

5、查询master服务的binlog文件名和位置(master节点执行)

mysql > show master status;

Log file name: mysql-bin.000003

Copied location: 154

2.2, Slave slave machine operation

1、Slave节点配置 vim /etc/mysql/mysql.conf.d/mysqld.cnf

>  vim /etc/mysql/mysql.conf.d/mysqld.cnf
[mysqld]
## 设置server_id,注意要唯一
server-id=102
## 开启二进制日志功能,以备Slave作为其它Slave的Master时使用
log-bin=mysql-slave-bin
## relay_log配置中继日志
relay_log=edu-mysql-relay-bin
##复制过滤:不需要备份的数据库,不输出(mysql库一般不同步)
binlog-ignore-db=mysql
## 如果需要同步函数或者存储过程
log_bin_trust_function_creators=true
## 为每个session 分配的内存,在事务过程中用来存储二进制日志的缓存
binlog_cache_size=1M
## 主从复制的格式(mixed,statement,row,默认格式是statement)
binlog_format=mixed
## 跳过主从复制中遇到的所有错误或指定类型的错误,避免slave端复制中断。
## 如:1062错误是指一些主键重复,1032错误是因为主从数据库数据不一致
slave_skip_errors=1062

# 重启mysql服务,一般配置文件写完后,都需要重启该服务才生效
systemctl restart mysql

2、重启mysql服务,使新配置生效

>  systemctl restart mysqld

3、slave进行关联master节点

1. Enter the slave node:

mysql > mysql -uroot -p密码

2. Start binding

mysql> change master to master_host='master服务器ip', master_user='root', master_password='master密码', master_port=3306, master_log_file='master二进制日志文件名',master_log_pos=master二进制日志文件Position;

# 如
mysql> change master to master_host='120.118.55.220', master_user='root', master_password='3456789', master_port=3306, master_log_file='mysql-bin.000003',master_log_pos=2897;
master_host IP address of the master node (Master)
master_user Username authorized in the master node database: root
master_port The port corresponding to MySQL: 3306
master_password The password of the slave node authorized in the master node database
master_log_file The file behind the corresponding File when showing master status in the master node
master_log_pos The number after the corresponding Position when showing master status in the master node

4、在slave节点上查看主从同步状态

1. Start master-slave replication

mysql> start slave;
Query OK, 0 rows affected (0.00 sec)

2. Check the master-slave synchronization status of the slave

mysql> show slave status\G;
# \G是换行查看,不换行查看的话,即没有格式化内容,就比较凌乱。

3. When both Slave_IO_Running and Slave_SQL_Running are Yes, it means that the master and slave can be synchronized normally

insert image description here

Other commands (slave node execution)

# 停止复制
mysql> stop slave;

5、主从复制测试

1. View the database existing in the master node (Master):

insert image description here

2. View the database existing in the slave node (Slave):
insert image description here

3. We create the Student database in the master node (Master):

mysql > create database Student charset utf8;
mysql> show databases;

insert image description here

4. We check whether there is a Student database in the slave node (Slave):

insert image description here

5. The test is completed. At this time, the data you created in the Student of the main library will also be seen in the slave library!

小总结

  1. Create databases and tables under the master, or modify and add new records, and delete records will be synchronized (executed by the master node)
  2. Click to view slave node information (slave node execution)
  3. During the master-slave replication operation, do not create a database or related operations based on it, and then delete it immediately. This will cause the pos of the master-slave replication to change, causing the replication to fail. If such a problem occurs, see the troubleshooting below.
  4. Note that the data added by the slave is based on the slave. The master will not update the data inserted by the slave. When the master adds data with an id of 2, the slave will not update the data, but after adding other new data, the slave will The machine updates the data automatically.
  5. For example, the master and slave only have data with id equal to 1, and the slave adds id equal to 2, name=zhangsan, and the master will not change, while the master adds id=2, name=lisi at this time, and the slave will not change anything Variety. But the master adds id=3, name=xiaoming, and the slave copies accordingly, and the table also has id=3, name=xiaoming

2.3. Troubleshooting related problems of master-slave replication

1、主从复制Connecting问题

insert image description here

After enabling start slavethe master-slave replication process, if SlaveIORunning is always Connecting, it means that the master-slave replication is always connected. This situation is generally caused by the following reasons, and we can rule it out according to the Last_IO_Error prompt.

  1. Network failure
    Check ip, port
  2. The password is wrong
    Check whether the user created for synchronization and the user password are correct
  3. pos is wrong,
    check the Master's Position

2、MYSQL镜像服务器因错误停止的恢复 —Slave_SQL_Running: No

# 在从机先stop slave,然后执行了一下提示的语句
mysql > stop slave;
mysql > set global sql_slave_skip_counter=1;
mysql > start slave; 
mysql > show slave status\G ;

3、从MYSQL服务器Slave_IO_Running: No的解决

  • Master node execution, get log files and post
mysql > show master status;
  • slave node to rebind
mysql > stop slave;mysql > CHANGE MASTER TO MASTER_LOG_FILE='mysql-bin.000008', MASTER_LOG_POS=519086591; mysql > start slave;

The reason for this type of problem is generally that when master-slave replication is based on creating a table, and then immediately deleting and operating the data table or table, the pos position of the master node changes!

Guess you like

Origin blog.csdn.net/qq_44231797/article/details/126136396
Recommended