Implementation ideas and steps for MySQL master-slave construction (multiple masters and one slave)

Implementation ideas and steps for MySQL master-slave construction (multiple masters and one slave)

background:

Since the company's projects seem to be unable to withstand the pressure of concurrency recently, optimization is imminent. Since the current system is a single database system, we have tried our best to optimize where possible, but the database bottleneck still severely limits the project's concurrent capabilities. So we considered adding a database to increase the project's concurrency capabilities.

Ideas:

1: Create a centralized library: mainly to store historical data. Use as a query.

2: Create multiple business libraries: to meet the high concurrency capabilities of the project.

Demo environment:

1: VM ware virtual machine - centOS 7

centOS-1: 192.168.194.3 main 100-------business library

centOS-2: 192.168.194.4 main 200-------business library

centOS-3: 192.168.194.5 from 300-------equivalent to the centralized library

2: MySQL 5.7

step

1: Main library 100

Set up my.cnf.

[mysqld]
lower_case_table_names = 1 # Table names are case insensitive server-id = 100
log_bin = mysql-bin #Start binlog recording binlog_format = MIXED 
#Every time a transaction is committed, MySQL will flush the binlog. This is the safest setting but it has the greatest performance loss.
#In this case, the system may lose the data of one transaction only if the host operating system where the database is located is damaged or the power is suddenly lost. #However, although binlog is sequential IO, if sync_binlog=1 is set, multiple transactions are submitted at the same time.
#It also greatly affects MySQL and IO performance. Set as needed.
sync_binlog = 1 
# Number of days after which binary logs are automatically deleted/expired. The default value is 0, which means no automatic deletion.
expire_logs_days = 7
#binlog_cache_size = 128m
#max_binlog_cache_size = 512m
#max_binlog_size = 256M
# Need to synchronize the database binlog-do-db = dev
# No synchronization library is required binlog-ignore-db = mysql
binlog_ignore_db = information_schema
binlog_ignore_db = performance_schema
binlog_ignore_db = sys
datadir=/var/lib/mysql

Set up slave users

#mysql -uroot -p 
# password: xxxxxx
# mysql> GRANT REPLICATION SLAVE ON *.* TO 'slave'@'%' IDENTIFIED BY 'root1234';
# mysql> flush privileges;
# quit;
# systemctl restart mysqld;
# show master status \G
#*************************** 1. row ***************************
             File:mysql-bin.000001
         Position: 886
     Binlog_Do_DB: dev
 Binlog_Ignore_DB: mysql,information_schema,performance_schema,sys
Executed_Gtid_Set: 
1 row in set (0.00 sec)

2: Main library 200

Set up my.cnf.

[mysqld]
lower_case_table_names = 1 # Table names are case insensitive server-id = 200
log_bin = mysql-bin #Start binlog recording binlog_format = MIXED 
#Every time a transaction is committed, MySQL will flush the binlog. This is the safest setting but it has the greatest performance loss.
#In this case, the system may lose the data of one transaction only if the host operating system where the database is located is damaged or the power is suddenly lost. #However, although binlog is sequential IO, if sync_binlog=1 is set, multiple transactions are submitted at the same time.
#It also greatly affects MySQL and IO performance. Set as needed.
sync_binlog = 1 
# Number of days after which binary logs are automatically deleted/expired. The default value is 0, which means no automatic deletion.
expire_logs_days = 7
#binlog_cache_size = 128m
#max_binlog_cache_size = 512m
#max_binlog_size = 256M
# Need to synchronize the database binlog-do-db = dev
# No synchronization library is required binlog-ignore-db = mysql
binlog_ignore_db = information_schema
binlog_ignore_db = performance_schema
binlog_ignore_db = sys
datadir=/var/lib/mysql

Set up slave users

#mysql -uroot -p 
# password: xxxxxx
# mysql> GRANT REPLICATION SLAVE ON *.* TO 'slave'@'%' IDENTIFIED BY 'root1234';
# mysql> flush privileges;
# quit;
# systemctl restart mysqld;
# show master status \G
#*************************** 1. row ***************************
             File:mysql-bin.000001
         Position: 154
     Binlog_Do_DB: dev
 Binlog_Ignore_DB: mysql,information_schema,performance_schema,sys
Executed_Gtid_Set: 
1 row in set (0.00 sec)

3: From library 300

Set up my.cnf.

[mysqld]
lower_case_table_names = 1 # Table names are case insensitive server-id = 300
master_info_repository = table
relay_log_info_repository = table 
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock  
symbolic-links=0 
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid

Set the master database information

#mysql -uroot -p 
# password: xxxxxx
# Set the master database information# mysql> CHANGE MASTER TO  
# ->MASTER_HOST='192.168.194.3',
# ->MASTER_PORT=3306, MASTER_USER='slave', 
# ->MASTER_PASSWORD='root1234',
# ->MASTER_LOG_FILE='mysql-bin.000001', MASTER_LOG_POS=886 for channel '100';
# mysql> CHANGE MASTER TO  
# ->MASTER_HOST='192.168.194.4',
# ->MASTER_PORT=3306, MASTER_USER='slave', 
# ->MASTER_PASSWORD='root1234',
# ->MASTER_LOG_FILE='mysql-bin.000001', MASTER_LOG_POS=154 for channel '200';
# Set the master database information# mysql> flush privileges;
# start slave;
# mysql>show slave status \G

Identification synchronization success

Slave_IO_Running: Yes
Slave_SQL_Running: Yes

test.

Random main database: create a table and insert a piece of data.

CREATE TABLE `t_user` (
  `id` varchar(32) NOT NULL COMMENT 'Primary key ID',
  `name` varchar(32) CHARACTER SET utf8mb4 NULL COMMENT 'User name',
  `code` varchar(32) CHARACTER SET utf8mb4 NULL COMMENT 'User code',
  `phone_number` varchar(300) CHARACTER SET utf8mb4 NULL COMMENT 'Phone number',
  `create_date` datetime NULL COMMENT 'Creation time',
  `update_date` datetime NULL COMMENT 'Modification time',
  PRIMARY KEY (`id`)
) COMMENT = 'User information table';
INSERT INTO t_user (`id`, `name`, `code`, `phone_number`, `create_date`, `update_date`) 
VALUES ('userId_4', '张三', '123456789', '123456789632', '2020-04-27 22:05:00', '2020-04-27 22:05:00');

Query from library

SELECT * FROM t_user;
------+--------+----------+--------------+---------------------+-------------+
| id | name | code | phone_number | create_date | update_date |
+-------+--------+----------+--------------+---------------------+----------+
| userId_4 | Zhang San|123456789|123456789632|2020-04-27 22:05:00|2020-04-27 22:05:00

Summarize

This concludes this article about the implementation ideas and steps of MySQL master-slave construction (multiple masters and one slave). For more relevant MySQL master-slave construction content, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • MySQL multi-master and one-slave data backup method tutorial
  • Centos7 Mysql 5.6 multi-master and one-slave solution and detailed configuration
  • MySQL8.0.18 configuration of multiple masters and one slave

<<:  Design Theory: Hierarchy in Design

>>:  How to add color mask to background image in CSS3

Recommend

Use of select, distinct, and limit in MySQL

Table of contents 1. Introduction 2. select 2.1 Q...

A little-known JS problem: [] == ![] is true, but {} == !{} is false

console.log( [] == ![] ) // true console.log( {} ...

VMware12 installs Ubuntu19.04 desktop version (installation tutorial)

1. Experimental description In the virtual machin...

Docker-compose tutorial installation and quick start

Table of contents 1. Introduction to Compose 2. C...

About the role of meta in HTML (collected and sorted from the Internet)

W3Cschool explains it this way The <meta> el...

Add ico mirror code to html (favicon.ico is placed in the root directory)

Code: Copy code The code is as follows: <!DOCTY...

Analysis of the process of building a LAN server based on http.server

I don’t know if you have ever encountered such a ...

MySQL database introduction: detailed explanation of database backup operation

Table of contents 1. Single database backup 2. Co...

Dynamically add tables in HTML_PowerNode Java Academy

Without further ado, I will post the code for you...

Analysis of MySQL data backup and recovery implementation methods

This article uses examples to describe how to bac...

How to quickly install tensorflow environment in Docker

Quickly install the tensorflow environment in Doc...

XHTML tags have a closing tag

<br />Original link: http://www.dudo.org/art...

A collection of common uses of HTML meta tags

What is a mata tag The <meta> element provi...