MySQL server master-slave replication synchronization-master-slave replication to synchronize data, an essential operation for enterprises! I will copy from the master, how about you?

1. The principle of MySQL master-slave replication

1.1. Replication types supported by MySQL

  • Statement-based replication (default)
    statement executed on the master server, and the same statement executed on the slave server
  • Line-based replication Copy
    the changed content to the slave server
  • Mixed types of replication
    Once it is found that statement-based replication cannot be accurately replicated, row-based replication will be used

1.2. The working process of copying

       ① Before the completion of each transaction to update the data, the Master records these changes in the binary log. After the binary log is written, the master notifies the storage engine to submit the transaction
        ②Slave copies the master's Binary log to its relay log. First of all, Slave starts a worker thread-I/O thread, I/O thread opens a normal connection on Master, and then starts Binlog dump process. Binlog dump process reads events from the binary log of the Master. If it has kept up with the Master, he will sleep and wait for the Master to generate new events. The I/O thread writes these events to the relay log.
        ③SQL slave thread (SQL slave thread) handles the last step of the process. The SQL thread reads events from the relay log, replays the events and updates the data in the slave to make it consistent with the data in the master. As long as the thread is consistent with the I/O thread, the relay log is usually located in the OS cache, so the overhead of the relay log is small.
The replication process has a very important limitation, that is, the replication is serialized on the Slave, which means that the parallel update operation on the Master cannot be operated in parallel on the Slave.

Insert picture description here

2. Reasons for master-slave replication and transformation methods

2.1. Reason

■In the corporate website, when there is only one back-end MySQL database, there will be the following problems

  • Single point of failure, service unavailable
  • Unable to handle a large number of concurrent data requests
  • Data loss-catastrophe
    Insert picture description here

2.2. Modification method

■Renovation method

  • Increase the MySQL database server to back up data to form a master/backup
  • Ensure that the primary and secondary MySQL database server data are the same
  • The main server is down, the backup server continues to work, and the data is guaranteed

Insert picture description here

2.3, more advanced solutions

■More advanced solutions

  • Synchronize data through master-slave replication, and then use read-write separation to improve the concurrent load capacity of the database

Insert picture description here

3. Experimental process

3.1. Experimental environment

  • server configuration

  IP address Host Operating system
20.0.0.25 master centos-7.6-x86_64
20.0.0.24 Slave1 centos-7.6-x86_64
20.0.0.23 Slave2 centos-7.6-x86_64

  • Three MySQL servers turn off the firewall and turn off the core protection

3.2. Establish a time synchronization environment

  • Install and configure the NTP time synchronization server (20.0.0.25) on the main server
[root@localhost~]# yum -y install ntp    ###如果不是最小化安装,就不用yum安装ntp

[root@localhost~]# vi /etc/ntp.conf  
###删除下面四行###
server 0.centos.pool.ntp.org iburst
server 1.centos.pool.ntp.org iburst
server 2.centos.pool.ntp.org iburst
server 3.centos.pool.ntp.org iburst
###添加这两行###
server 127.127.1.0
fudge 127.127.1.0 stratum 8
[root@localhost~]# systemctl restart ntpd
[root@localhost~]# systemctl enable ntpd
[root@localhost ~]# service ntpd restart
[root@localhost ~]# systemctl restart ntpd
[root@localhost ~]# systemctl enable ntpd
  • Configure NTP synchronization on slave server 1 (20.0.0.24)
[root@localhost ~]# yum -y install ntpdate
[root@localhost ~]# ntpdate 20.0.0.25
[root@localhost ~]# crontab -e
*/2 * * * * /usr/sbin/ntpdate 20.0.0.24 >> /var/log/ntpdate.log
[root@localhost ~]# touch /var/log/ntpdate.log 
[root@localhost ~]# systemctl restart crond
[root@localhost ~]# systemctl enable crond
[root@localhost ~]# tail -f /var/log/ntpdate.log  ###动态查看更新日志文件
  • Configure NTP synchronization on slave 2 (20.0.0.23)
[root@localhost ~]# yum -y install ntpdate
[root@localhost ~]# ntpdate 20.0.0.25
[root@localhost ~]# crontab -e
*/2 * * * * /usr/sbin/ntpdate 20.0.0.23 >> /var/log/ntpdate.log
[root@localhost ~]# touch /var/log/ntpdate.log 
[root@localhost ~]# systemctl restart crond
[root@localhost ~]# systemctl enable crond
[root@localhost ~]# tail -f /var/log/ntpdate.log  ###动态查看更新日志文件

3.3. Install MySQL service on the master server, slave server 1, and slave server 2

[root@localhost~]# yum -y install \
ncurses \
ncurses-devel \
bison \
cmake \
gcc \
gcc-c++
[root@localhost~]# useradd -s /sbin/nologin  mysql
[root@localhost~]# cd /opt
[root@localhost opt]# tar xf mysql-boost-5.7.20.tar.gz
[root@localhost opt]# cd /opt/mysql-5.7.20/
[root@localhost mysql-5.7.20]# 
cmake \
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock \
-DSYSCONFDIR=/etc \
-DSYSTEMD_PID_DIR=/usr/local/mysql \
-DDEFAULT_CHARSET=utf8  \
-DDEFAULT_COLLATION=utf8_general_ci \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_ARCHIVE_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DWITH_PERFSCHEMA_STORAGE_ENGINE=1 \
-DMYSQL_DATADIR=/usr/local/mysql/data \
-DWITH_BOOST=boost \
-DWITH_SYSTEMD=1
[root@localhost mysql-5.7.20]# make -j4
[root@localhost mysql-5.7.20]# make install

[root@localhost mysql-5.7.20]# chown -R mysql:mysql /usr/local/mysql/  
 '//数据库目录进行权限调整'

[root@localhost mysql-5.7.20]# vi /etc/my.cnf     
 '//建立调整配置文件'
[client]
port = 3306
default-character-set=utf8
socket = /usr/local/mysql/mysql.sock

[mysql]
port = 3306
default-character-set=utf8
socket = /usr/local/mysql/mysql.sock

[mysqld]
user = mysql
basedir = /usr/local/mysql
datadir = /usr/local/mysql/data
port = 3306
character_set_server=utf8
pid-file = /usr/local/mysql/mysqld.pid
socket = /usr/local/mysql/mysql.sock
server-id = 1

sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_AUTO_VALUE_ON_ZERO,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,PIPES_AS_CONCAT,ANSI_QUOTES

[root@localhost mysql-5.7.20]# chown mysql:mysql /etc/my.cnf
[root@localhost mysql-5.7.20]# echo 'PATH=/usr/local/mysql/bin:/usr/local/mysql/lib:$PATH' >> /etc/profile
[root@localhost mysql-5.7.20]# echo 'export PATH' >> /etc/profile  
     '//设置环境变量'
[root@localhost mysql-5.7.20]# source /etc/profile    
'//生效'

[root@localhost mysql-5.7.20]# cd /usr/local/mysql/

[root@localhost mysql]# bin/mysqld \
--initialize-insecure \
--user=mysql \
--basedir=/usr/local/mysql \
--datadir=/usr/local/mysql/data

[root@localhost mysql]# cp usr/lib/systemd/system/mysqld.service /usr/lib/systemd/system/
[root@localhost mysql]# systemctl enable mysqld
[root@localhost mysql]# systemctl start mysqld
[root@localhost mysql]# systemctl status mysqld
[root@localhost mysql]# netstat -anpt | grep 3306
[root@localhost mysql]# mysqladmin -u root -p password
'//设置密码'
[root@localhost ~]# mysql -u root -p   ## 验证是否可以登录数据库
Enter password: 
mysql>

3.4, the main server configuration

[root@localhost ~]# vi /etc/my.cnf ###在原来server-id=1的地方修改成11后面新增log_bin=master-bin log-slave-updates=true###
server-id = 11         ## 修改为 11,下面的从服务器也会改,3个server-id 不可相同
log_bin = master-bin   ##设置二进制日志名
log-slave-updates = true   ## 从服务器更新二进制日志

[root@localhost ~]# systemctl restart mysqld ###重启数据库

登录Master数据库给从服务器授权
[root@localhost ~]# mysql -uroot -p
## 让20.0.0.0端的从服务器拥有复制权限,可以使用myslave身份12345密码复制所有的库和表
mysql> grant replication slave on *.* to 'myslave'@'20.0.0.%' identified by '12345';
mysql> flush privileges;   ## 刷新
mysql> show master status;  
+-------------------+----------+--------------+------------------+-------------------+
| File              | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+-------------------+----------+--------------+------------------+-------------------+
| master-bin.000001 |      599 |              |                  |                   |
+-------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

3.5. Configuration from Server 1

[root@localhost ~]# vi /etc/my.cnf  ###在原来server-id=1的地方修改成22 后面新增relay-log=relay-log-bin relay-log-index=slave-relay-bin.index###
server-id = 22     ## 修改为22,另一台从服务器的id设为33,三个id不可相同
relay-log = relay-log-bin          ## 设置二进制日志名
relay-log-index = slave-relay-bin.index      ## 服务器更新二进制日志
[root@localhost ~]# systemctl restart mysqld  ###重启数据库
登录slave数据库配置同步 ###注意下这边的master_log_file='master-bin.000001',master_log_pos=601;要和Master数据库信息不一致,不一致的话要更改先stop slave;然后更改同步信息
[root@localhost ~]# mysql -uroot -p12345
mysql> change master to master_host='20.0.0.25',master_user='myslave',master_password='12345',master_log_file='master-bin.000001',master_log_pos=599;
mysql> start slave;
mysql> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 20.0.0.25
                  Master_User: myslave
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: master-bin.000001
          Read_Master_Log_Pos: 599
               Relay_Log_File: relay-log-bin.000002
                Relay_Log_Pos: 321
        Relay_Master_Log_File: master-bin.000001
             Slave_IO_Running: Yes   ## 查看IO是正常的,这里显示 Yes 就可以了
            Slave_SQL_Running: Yes   ## 查看SQL是正常的,这里显示 Yes 就可以了
            ............................................
            1 row in set (0.00 sec)

3.6, configuration from server 2

[root@localhost ~]# vi /etc/my.cnf
server-id = 33
relay-log = relay-log-bin
relay-log-index = slave-relay-bin.index
[root@localhost ~]# systemctl restart mysqld
[root@localhost ~]# mysql -uroot -p12345
mysql> change master to master_host='20.0.0.25',master_user='myslave',master_password='12345',master_log_file='master-bin.000001',master_log_pos=599;

mysql> start slave;
mysql> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 20.0.0.25
                  Master_User: myslave
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: master-bin.000001
          Read_Master_Log_Pos: 599
               Relay_Log_File: relay-log-bin.000002
                Relay_Log_Pos: 321
        Relay_Master_Log_File: master-bin.000001
             Slave_IO_Running: Yes   ## 这里显示 Yes 就可以了
            Slave_SQL_Running: Yes   ## 这里显示 Yes 就可以了
            ............................................
            1 row in set (0.00 sec)

3.7. Verification: MySQL master-slave server

Main server

mysql> create database School;  ## 创建 School库
Query OK, 1 row affected (0.00 sec)

mysql> show databases;   ## 查看主服务器有 School库
+--------------------+
| Database           |
+--------------------+
| information_schema |
| School             |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.00 sec)

Slave server 1

mysql> show databases;   ## 查看从服务器 1 有 School库
+--------------------+
| Database           |
+--------------------+
| information_schema |
| School             |   ## 同步成功!
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.00 sec)

Slave 2

mysql> show databases;   ## 查看从服务器 2 有 School库
+--------------------+
| Database           |
+--------------------+
| information_schema |
| School             |   ## 同步成功!
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.00 sec)

3.8, small experimental problems

The master-slave copy is not successful, and the displayed parameter is Slave_IO_Running: Connecting .
Solution:

## 当我们输入这句代码完之后
mysql> change master to master_host='20.0.0.25',master_user='myslave',master_password='12345',master_log_file='master-bin.000001',master_log_pos=599;
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)
mysql> show slave status\G
*************************** 1. row ***************************
            Slave_IO_Running: Connecting  ## 这里并没有改成 Yes
            Slave_SQL_Running: Yes

1. It may be that the value of master_log_pos=599 is entered incorrectly, resulting in no synchronization.
2. It may also be because the IP of the main server is entered incorrectly, or the password is incorrect.

  • At this time we need to stop slave
mysql> stop slave;  ## 先关闭
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> change master to master_host='20.0.0.25',master_user='myslave',master_password='12345',master_log_file='master-bin.000001',master_log_pos=599;  ## 然后再给一次复制权限,把里面的参数改成正确的参数
mysql> start slave;  ## 在开启
mysql> show slave status\G
*************************** 1. row ***************************
            Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
## 再查看就已经设置成功了!
## 排障完成
  • The experiment ends here! We finally verified that master-slave replication is possible.
        1. Do hot backup of data. As a backup database, after the primary database server fails, you can switch to the secondary database to continue working to avoid data loss.
        2. Expansion of the architecture. The business volume is getting bigger and bigger, and the I/O access frequency is too high for a single machine to meet. At this time, multi-bank storage is used to reduce the frequency of disk I/O access and improve the I/O performance of a single machine.
        3. Separation of reading and writing enables the database to support greater concurrency. It is especially important in reports. Due to the very slow sql statement of some reports, the table is locked and the front desk service is affected. If the front desk uses the master and the report uses the slave, then the report SQL will not cause the front desk to lock, ensuring the front desk speed.

In the next blog, I will introduce the read-write separation of master-slave replication! Welcome to!

Guess you like

Origin blog.csdn.net/m0_46563938/article/details/108576868